Class: Prawn::SVG::Elements::TextComponent
- Inherits:
-
DirectRenderBase
- Object
- Base
- DirectRenderBase
- Prawn::SVG::Elements::TextComponent
- Defined in:
- lib/prawn/svg/elements/text_component.rb
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.
-
#font ⇒ Object
readonly
Returns the value of attribute font.
-
#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.
- #lay_out(prawn) ⇒ Object
- #letter_spacing_pixels ⇒ Object
- #parse ⇒ Object
- #render_component(prawn, renderer, cursor, translate_x = nil) ⇒ 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::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.
7 8 9 10 11 12 13 14 |
# File 'lib/prawn/svg/elements/text_component.rb', line 7 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 |
#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 |
#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 |
#lay_out(prawn) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/prawn/svg/elements/text_component.rb', line 44 def lay_out(prawn) @children.each do |child| prawn.save_font do prawn.font(font.name, style: font.subfamily) if font 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
111 112 113 114 115 116 117 |
# File 'lib/prawn/svg/elements/text_component.rb', line 111 def letter_spacing_pixels if computed_properties.letter_spacing == 'normal' nil else x_pixels(computed_properties.letter_spacing) end end |
#parse ⇒ Object
16 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 |
# File 'lib/prawn/svg/elements/text_component.rb', line 16 def parse raise SkipElementError, '<text> elements are not supported in clip paths' if 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 = select_font @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) else warnings << "Unknown tag '#{child.name}' inside text tag; ignoring" [] end end end end |
#render_component(prawn, renderer, cursor, translate_x = nil) ⇒ Object
63 64 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 63 def render_component(prawn, renderer, cursor, translate_x = nil) 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 if computed_properties.dominant_baseline == 'middle' height = FontMetrics.x_height_in_points(prawn, size || prawn.font_size) y_offset = -height / 2.0 end prawn.save_font do prawn.font(font.name, style: font.subfamily) if font 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) prawn.restore_graphics_state else raise end end end end renderer.render_calls(prawn, base_calls) end |