class Axlsx::RichTextRun
The RichTextRun
class creates and self serializing text run.
Constants
- INLINE_STYLES
A list of allowed inline style attributes used for validation
Attributes
The inline bold property for the cell @return [Boolean]
The inline charset property for the cell As far as I can tell, this is pretty much ignored. However, based on the spec it should be one of the following: 0  ANSI_CHARSET 1 DEFAULT_CHARSET 2 SYMBOL_CHARSET 77 MAC_CHARSET 128 SHIFTJIS_CHARSET 129  HANGUL_CHARSET 130  JOHAB_CHARSET 134  GB2312_CHARSET 136  CHINESEBIG5_CHARSET 161  GREEK_CHARSET 162  TURKISH_CHARSET 163  VIETNAMESE_CHARSET 177  HEBREW_CHARSET 178  ARABIC_CHARSET 186  BALTIC_CHARSET 204  RUSSIAN_CHARSET 222  THAI_CHARSET 238  EASTEUROPE_CHARSET 255  OEM_CHARSET @return [String]
The inline color property for the cell @return [Color]
The inline condense property for the cell @return [Boolean]
The inline extend property for the cell @return [Boolean]
The inline family property for the cell @return [Integer] 1 Roman 2 Swiss 3 Modern 4 Script 5 Decorative
The inline font_name
property for the cell @return [String]
The inline italic property for the cell @return [Boolean]
The inline outline property for the cell @return [Boolean]
The inline scheme property for the cell this must be one of [:none, major, minor] @return [Symbol]
The inline shadow property for the cell @return [Boolean]
The inline strike property for the cell @return [Boolean]
The inline sz property for the cell @return [Inteter]
The inline underline property for the cell. It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting, true @return [Boolean] @return [String] @note true is for backwards compatability and is reassigned to :single
The inline vertical alignment property for the cell this must be one of [:baseline, :subscript, :superscript] @return [Symbol]
Public Class Methods
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 16 def initialize(value, options={}) self.value = value parse_options(options) end
Public Instance Methods
Tries to work out the width of the longest line in the run @param [Array] widtharray this array is populated with the widths of each line in the run. @return [Array]
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 163 def autowidth(widtharray) return if value.nil? if styles.cellXfs[style].alignment && styles.cellXfs[style].alignment.wrap_text first = true value.to_s.split(/\r?\n/, -1).each do |line| if first first = false else widtharray << 0 end widtharray[-1] += string_width(line, font_size) end else widtharray[-1] += string_width(value.to_s, font_size) end widtharray end
@see b
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 76 def b=(v) set_run_style :validate_boolean, :b, v; end
@see charset
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 57 def charset=(v) set_run_style :validate_unsigned_int, :charset, v; end
@param [String] v The 8 character representation for an rgb color #FFFFFFFF“
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 130 def color=(v) @color = v.is_a?(Color) ? v : Color.new(:rgb=>v) end
@see condense
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 106 def condense=(v) set_run_style :validate_boolean, :condense, v; end
@see extend
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 112 def extend=(v) set_run_style :validate_boolean, :extend, v; end
@see family
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 68 def family=(v) set_run_style :validate_family, :family, v.to_i end
@see font_name
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 31 def font_name=(v) set_run_style :validate_string, :font_name, v; end
@see i
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 82 def i=(v) set_run_style :validate_boolean, :i, v; end
@see outline
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 94 def outline=(v) set_run_style :validate_boolean, :outline, v; end
@see scheme
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 155 def scheme=(v) RestrictionValidator.validate :cell_scheme, [:none, :major, :minor], v set_run_style nil, :scheme, v end
Utility method for setting inline style attributes
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 182 def set_run_style(validator, attr, value) return unless INLINE_STYLES.include?(attr.to_sym) Axlsx.send(validator, value) unless validator.nil? self.instance_variable_set :"@#{attr.to_s}", value end
@see shadow
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 100 def shadow=(v) set_run_style :validate_boolean, :shadow, v; end
@see strike
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 88 def strike=(v) set_run_style :validate_boolean, :strike, v; end
@see sz
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 138 def sz=(v) set_run_style :validate_unsigned_int, :sz, v; end
Serializes the RichTextRun
@param [String] str @return [String]
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 191 def to_xml_string(str = '') valid = RichTextRun::INLINE_STYLES data = Hash[self.instance_values.map{ |k, v| [k.to_sym, v] }] data = data.select { |key, value| valid.include?(key) && !value.nil? } str << '<r><rPr>' data.keys.each do |key| case key when :font_name str << ('<rFont val="' << font_name << '"/>') when :color str << data[key].to_xml_string else str << ('<' << key.to_s << ' val="' << xml_value(data[key]) << '"/>') end end clean_value = Axlsx::trust_input ? @value.to_s : ::CGI.escapeHTML(Axlsx::sanitize(@value.to_s)) str << ('</rPr><t>' << clean_value << '</t></r>') end
@see u
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 121 def u=(v) v = :single if (v == true || v == 1 || v == :true || v == 'true') set_run_style :validate_cell_u, :u, v end
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 21 def value=(value) @value = value end
@see vertAlign
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 145 def vertAlign=(v) RestrictionValidator.validate :cell_vertAlign, [:baseline, :subscript, :superscript], v set_run_style nil, :vertAlign, v end
Private Instance Methods
we scale the font size if bold style is applied to either the style font or the cell itself. Yes, it is a bit of a hack, but it is much better than using imagemagick and loading metrics for every character.
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 224 def font_size return sz if sz font = styles.fonts[styles.cellXfs[style].fontId] || styles.fonts[0] (font.b || (defined?(@b) && @b)) ? (font.sz * 1.5) : font.sz end
Returns the width of a string according to the current style This is still not perfect…
- scaling is not linear as font sizes increase
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 216 def string_width(string, font_size) font_scale = font_size / 10.0 string.size * font_scale end
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 230 def style cell.style end
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 234 def styles cell.row.worksheet.styles end
Converts the value to the correct XML representation (fixes issues with Numbers)
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 240 def xml_value value if value == true 1 elsif value == false 0 else value end.to_s end