Class: Prawn::SVG::Elements::TextComponent
- Inherits:
-
DirectRenderBase
- Object
- Base
- DirectRenderBase
- Prawn::SVG::Elements::TextComponent
- Defined in:
- lib/prawn/svg/elements/text_component.rb
Direct Known Subclasses
Constant Summary
Constants inherited from Base
Base::COMMA_WSP_REGEXP, Base::MissingAttributesError, Base::PAINT_TYPES, Base::SVG_NAMESPACE, Base::SkipElementError, Base::SkipElementQuietly
Constants included from Attributes::Stroke
Attributes::Stroke::CAP_STYLE_TRANSLATIONS, Attributes::Stroke::JOIN_STYLE_TRANSLATIONS
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#dx ⇒ Object
readonly
Returns the value of attribute dx.
-
#dy ⇒ Object
readonly
Returns the value of attribute dy.
-
#fallback_fonts ⇒ Object
readonly
Returns the value of attribute fallback_fonts.
-
#font ⇒ Object
readonly
Returns the value of attribute font.
-
#inside_clip_path ⇒ Object
readonly
Returns the value of attribute inside_clip_path.
-
#length_adjust ⇒ Object
readonly
Returns the value of attribute length_adjust.
-
#parent_component ⇒ Object
readonly
Returns the value of attribute parent_component.
-
#rotation ⇒ Object
readonly
Returns the value of attribute rotation.
-
#text_length ⇒ Object
readonly
Returns the value of attribute text_length.
-
#x_values ⇒ Object
readonly
Returns the value of attribute x_values.
-
#y_values ⇒ Object
readonly
Returns the value of attribute y_values.
Attributes inherited from Base
#attributes, #base_calls, #calls, #document, #parent_calls, #properties, #source, #state
Instance Method Summary collapse
- #calculated_width ⇒ Object
- #current_length_adjust_is_scaling? ⇒ Boolean
-
#initialize(document, source, _calls, state, parent_component = nil) ⇒ TextComponent
constructor
A new instance of TextComponent.
-
#kerning_enabled? ⇒ Boolean
Resolves the effective kerning setting from the SVG 1.1 kerning property and the SVG 2 font-kerning property.
- #kerning_pixels ⇒ Object
- #lay_out(prawn) ⇒ Object
- #letter_spacing_pixels ⇒ Object
- #parse ⇒ Object
- #render_component(prawn, renderer, cursor, translate_x = nil, accumulated_baseline_shift = 0) ⇒ Object
- #word_spacing_pixels ⇒ Object
Methods inherited from DirectRenderBase
Methods inherited from Base
#name, #parse_and_apply, #process
Methods included from TransformParser
Methods included from PDFMatrix
#load_matrix, #matrix_for_pdf, #rotation_matrix, #scale_matrix, #translation_matrix
Methods included from Attributes::Space
Methods included from Attributes::Stroke
#parse_stroke_attributes_and_call
Methods included from Attributes::Mask
#parse_mask_attribute_and_call
Methods included from Attributes::ClipPath
#parse_clip_path_attribute_and_call
Methods included from Attributes::Opacity
#parse_opacity_attributes_and_call
Methods included from Attributes::Transform
#parse_transform_attribute_and_call
Constructor Details
#initialize(document, source, _calls, state, parent_component = nil) ⇒ TextComponent
Returns a new instance of TextComponent.
8 9 10 11 12 13 14 15 |
# File 'lib/prawn/svg/elements/text_component.rb', line 8 def initialize(document, source, _calls, state, parent_component = nil) if parent_component.nil? && source.name != 'text' raise SkipElementError, 'attempted to <use> a component inside a text element, this is not supported' end super(document, source, [], state) @parent_component = parent_component end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/prawn/svg/elements/text_component.rb', line 3 def children @children end |
#dx ⇒ Object (readonly)
Returns the value of attribute dx.
4 5 6 |
# File 'lib/prawn/svg/elements/text_component.rb', line 4 def dx @dx end |
#dy ⇒ Object (readonly)
Returns the value of attribute dy.
4 5 6 |
# File 'lib/prawn/svg/elements/text_component.rb', line 4 def dy @dy end |
#fallback_fonts ⇒ Object (readonly)
Returns the value of attribute fallback_fonts.
5 6 7 |
# File 'lib/prawn/svg/elements/text_component.rb', line 5 def fallback_fonts @fallback_fonts end |
#font ⇒ Object (readonly)
Returns the value of attribute font.
5 6 7 |
# File 'lib/prawn/svg/elements/text_component.rb', line 5 def font @font end |
#inside_clip_path ⇒ Object (readonly)
Returns the value of attribute inside_clip_path.
6 7 8 |
# File 'lib/prawn/svg/elements/text_component.rb', line 6 def inside_clip_path @inside_clip_path end |
#length_adjust ⇒ Object (readonly)
Returns the value of attribute length_adjust.
4 5 6 |
# File 'lib/prawn/svg/elements/text_component.rb', line 4 def length_adjust @length_adjust end |
#parent_component ⇒ Object (readonly)
Returns the value of attribute parent_component.
3 4 5 |
# File 'lib/prawn/svg/elements/text_component.rb', line 3 def parent_component @parent_component end |
#rotation ⇒ Object (readonly)
Returns the value of attribute rotation.
4 5 6 |
# File 'lib/prawn/svg/elements/text_component.rb', line 4 def rotation @rotation end |
#text_length ⇒ Object (readonly)
Returns the value of attribute text_length.
4 5 6 |
# File 'lib/prawn/svg/elements/text_component.rb', line 4 def text_length @text_length end |
#x_values ⇒ Object (readonly)
Returns the value of attribute x_values.
4 5 6 |
# File 'lib/prawn/svg/elements/text_component.rb', line 4 def x_values @x_values end |
#y_values ⇒ Object (readonly)
Returns the value of attribute y_values.
4 5 6 |
# File 'lib/prawn/svg/elements/text_component.rb', line 4 def y_values @y_values end |
Instance Method Details
#calculated_width ⇒ Object
97 98 99 |
# File 'lib/prawn/svg/elements/text_component.rb', line 97 def calculated_width children.reduce(0) { |total, child| total + child.calculated_width } end |
#current_length_adjust_is_scaling? ⇒ Boolean
101 102 103 104 105 106 107 108 109 |
# File 'lib/prawn/svg/elements/text_component.rb', line 101 def current_length_adjust_is_scaling? if @text_length @length_adjust == 'spacingAndGlyphs' elsif parent_component parent_component.current_length_adjust_is_scaling? else false end end |
#kerning_enabled? ⇒ Boolean
Resolves the effective kerning setting from the SVG 1.1 kerning property and the SVG 2 font-kerning property. kerning takes precedence when non-default; otherwise font-kerning is used.
114 115 116 117 118 119 120 |
# File 'lib/prawn/svg/elements/text_component.rb', line 114 def kerning_enabled? if computed_properties.kerning == 'auto' computed_properties.font_kerning != 'none' else false end end |
#kerning_pixels ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/prawn/svg/elements/text_component.rb', line 122 def kerning_pixels if computed_properties.kerning == 'auto' nil else x_pixels(computed_properties.kerning) end end |
#lay_out(prawn) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/prawn/svg/elements/text_component.rb', line 47 def lay_out(prawn) with_svg_fonts(prawn) do @children.each do |child| child.lay_out(prawn) end end if @text_length flexible_width, fixed_width = total_flexible_and_fixed_width if flexible_width.positive? target_width = [@text_length - fixed_width, 0].max factor = target_width / flexible_width apply_factor_to_base_width(factor) end end end |
#letter_spacing_pixels ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/prawn/svg/elements/text_component.rb', line 130 def letter_spacing_pixels if computed_properties.letter_spacing == 'normal' nil else x_pixels(computed_properties.letter_spacing) end end |
#parse ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/prawn/svg/elements/text_component.rb', line 17 def parse @inside_clip_path = state.inside_clip_path @x_values = parse_wsp('x').map { |n| x(n) } @y_values = parse_wsp('y').map { |n| y(n) } @dx = parse_wsp('dx').map { |n| x_pixels(n) } @dy = parse_wsp('dy').map { |n| y_pixels(n) } @rotation = parse_wsp('rotate').map(&:to_f) @text_length = normalize_length(attributes['textLength']) @length_adjust = attributes['lengthAdjust'] @font, @fallback_fonts = select_fonts @children = svg_text_children.flat_map do |child| if child.node_type == :text build_text_node(child) else case child.name when 'tspan', 'tref' build_child(child) when 'textPath' build_text_path(child) else warnings << "Unknown tag '#{child.name}' inside text tag; ignoring" [] end end end end |
#render_component(prawn, renderer, cursor, translate_x = nil, accumulated_baseline_shift = 0) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/prawn/svg/elements/text_component.rb', line 65 def render_component(prawn, renderer, cursor, translate_x = nil, accumulated_baseline_shift = 0) raise SkipElementQuietly if computed_properties.display == 'none' add_yield_call do prawn.translate(translate_x, 0) if translate_x size = computed_properties.numeric_font_size y_offset = dominant_baseline_offset(prawn, size || prawn.font_size) || 0 with_svg_fonts(prawn) do total_baseline_shift = accumulated_baseline_shift + baseline_shift_offset(prawn, size) y_offset += total_baseline_shift children.each do |child| case child when Elements::TextNode child.render(prawn, size, cursor, y_offset) when self.class prawn.save_graphics_state child.render_component(prawn, renderer, cursor, nil, total_baseline_shift) prawn.restore_graphics_state else raise end end end end renderer.render_calls(prawn, base_calls) end |
#word_spacing_pixels ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/prawn/svg/elements/text_component.rb', line 138 def word_spacing_pixels if computed_properties.word_spacing == 'normal' nil else x_pixels(computed_properties.word_spacing) end end |