Module: Writexlsx::Utility::RichText

Included in:
Chart, Chart::Axis, Chart::Caption, Chart::Chartline, Chart::Series, Chart::Table
Defined in:
lib/write_xlsx/utility/rich_text.rb

Instance Method Summary collapse

Instance Method Details

#convert_font_args(params) ⇒ Object

Convert user defined font values into private hash values.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/write_xlsx/utility/rich_text.rb', line 23

def convert_font_args(params)
  return unless params

  font = params_to_font(params)

  # Convert font size units.
  font[:_size] *= 100 if font[:_size] && font[:_size] != 0

  # Convert rotation into 60,000ths of a degree.
  font[:_rotation] = 60_000 * font[:_rotation].to_i if ptrue?(font[:_rotation])

  font
end

#get_font_latin_attributes(font) ⇒ Object

Get the font latin attributes from a font hash.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/write_xlsx/utility/rich_text.rb', line 73

def get_font_latin_attributes(font)
  return [] unless font
  return [] unless font.respond_to?(:[])

  attributes = []
  attributes << ['typeface', font[:_name]]            if ptrue?(font[:_name])
  attributes << ['pitchFamily', font[:_pitch_family]] if font[:_pitch_family]
  attributes << ['charset', font[:_charset]]          if font[:_charset]

  attributes
end

#get_font_style_attributes(font) ⇒ Object

Get the font style attributes from a font hash.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/write_xlsx/utility/rich_text.rb', line 55

def get_font_style_attributes(font)
  return [] unless font
  return [] unless font.respond_to?(:[])

  attributes = []
  attributes << ['sz', font[:_size]]      if ptrue?(font[:_size])
  attributes << ['b',  font[:_bold]]      if font[:_bold]
  attributes << ['i',  font[:_italic]]    if font[:_italic]
  attributes << %w[u sng]                 if font[:_underline]

  # Turn off baseline when testing fonts that don't have it.
  attributes << ['baseline', font[:_baseline]] if font[:_baseline] != -1
  attributes
end

#params_to_font(params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/write_xlsx/utility/rich_text.rb', line 37

def params_to_font(params)
  {
    _name:         params[:name],
    _color:        params[:color],
    _size:         params[:size],
    _bold:         params[:bold],
    _italic:       params[:italic],
    _underline:    params[:underline],
    _pitch_family: params[:pitch_family],
    _charset:      params[:charset],
    _baseline:     params[:baseline] || 0,
    _rotation:     params[:rotation]
  }
end

#underline_attributes(underline) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/write_xlsx/utility/rich_text.rb', line 7

def underline_attributes(underline)
  case underline
  when 2
    [%w[val double]]
  when 33
    [%w[val singleAccounting]]
  when 34
    [%w[val doubleAccounting]]
  else
    []    # Default to single underline.
  end
end

#write_a_body_pr(rot, is_y_axis = nil) ⇒ Object

Write the <a:bodyPr> element.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/write_xlsx/utility/rich_text.rb', line 104

def write_a_body_pr(rot, is_y_axis = nil) # :nodoc:
  rot = -5400000 if !rot && ptrue?(is_y_axis)
  attributes = []
  if rot
    if rot == 16_200_000
      # 270 deg/stacked angle.
      attributes << ['rot',  0]
      attributes << %w[vert wordArtVert]
    elsif rot == 16_260_000
      # 271 deg/stacked angle.
      attributes << ['rot',  0]
      attributes << %w[vert eaVert]
    else
      attributes << ['rot',  rot]
      attributes << %w[vert horz]
    end
  end

  @writer.empty_tag('a:bodyPr', attributes)
end

#write_a_def_rpr(font = nil) ⇒ Object

Write the <a:defRPr> element.



154
155
156
157
158
159
160
# File 'lib/write_xlsx/utility/rich_text.rb', line 154

def write_a_def_rpr(font = nil) # :nodoc:
  write_def_rpr_r_pr_common(
    font,
    get_font_style_attributes(font),
    'a:defRPr'
  )
end

#write_a_end_para_rprObject

Write the <a:endParaRPr> element.



179
180
181
# File 'lib/write_xlsx/utility/rich_text.rb', line 179

def write_a_end_para_rpr # :nodoc:
  @writer.empty_tag('a:endParaRPr', [%w[lang en-US]])
end

#write_a_lst_styleObject

Write the <a:lstStyle> element.



128
129
130
# File 'lib/write_xlsx/utility/rich_text.rb', line 128

def write_a_lst_style # :nodoc:
  @writer.empty_tag('a:lstStyle')
end

#write_a_p_formula(font = nil) ⇒ Object

Write the <a:p> element for formula titles.



135
136
137
138
139
140
141
142
# File 'lib/write_xlsx/utility/rich_text.rb', line 135

def write_a_p_formula(font = nil) # :nodoc:
  @writer.tag_elements('a:p') do
    # Write the a:pPr element.
    write_a_p_pr_formula(font)
    # Write the a:endParaRPr element.
    write_a_end_para_rpr
  end
end

#write_a_p_pr_formula(font) ⇒ Object

Write the <a:pPr> element for formula titles.



147
148
149
# File 'lib/write_xlsx/utility/rich_text.rb', line 147

def write_a_p_pr_formula(font) # :nodoc:
  @writer.tag_elements('a:pPr') { write_a_def_rpr(font) }
end

#write_def_rpr_r_pr_common(font, style_attributes, tag) ⇒ Object

:nodoc:



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/write_xlsx/utility/rich_text.rb', line 162

def write_def_rpr_r_pr_common(font, style_attributes, tag)  # :nodoc:
  latin_attributes = get_font_latin_attributes(font)
  has_color = ptrue?(font) && ptrue?(font[:_color])

  if !latin_attributes.empty? || has_color
    @writer.tag_elements(tag, style_attributes) do
      write_a_solid_fill(color: font[:_color]) if has_color
      write_a_latin(latin_attributes) unless latin_attributes.empty?
    end
  else
    @writer.empty_tag(tag, style_attributes)
  end
end

#write_tx_pr(font, is_y_axis = nil) ⇒ Object

Write the <c:txPr> element.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/write_xlsx/utility/rich_text.rb', line 88

def write_tx_pr(font, is_y_axis = nil) # :nodoc:
  rotation = nil
  rotation = font[:_rotation] if font && font.respond_to?(:[]) && font[:_rotation]
  @writer.tag_elements('c:txPr') do
    # Write the a:bodyPr element.
    write_a_body_pr(rotation, is_y_axis)
    # Write the a:lstStyle element.
    write_a_lst_style
    # Write the a:p element.
    write_a_p_formula(font)
  end
end