Class: Prawn::SVG::Elements::TextNode
- Inherits:
-
Object
- Object
- Prawn::SVG::Elements::TextNode
- Defined in:
- lib/prawn/svg/elements/text_node.rb
Defined Under Namespace
Classes: Chunk
Instance Attribute Summary collapse
-
#chunks ⇒ Object
readonly
Returns the value of attribute chunks.
-
#component ⇒ Object
readonly
Returns the value of attribute component.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #calculate_text_rendering_mode ⇒ Object
- #calculated_width ⇒ Object
-
#initialize(component, text, leading_space, trailing_space) ⇒ TextNode
constructor
A new instance of TextNode.
- #lay_out(prawn) ⇒ Object
- #leading_space? ⇒ Boolean
- #render(prawn, size, cursor, y_offset) ⇒ Object
- #render_underline(prawn, size, cursor, y_offset, width) ⇒ Object
- #total_flexible_and_fixed_width ⇒ Object
- #trailing_space? ⇒ Boolean
Constructor Details
#initialize(component, text, leading_space, trailing_space) ⇒ TextNode
Returns a new instance of TextNode.
8 9 10 11 12 13 |
# File 'lib/prawn/svg/elements/text_node.rb', line 8 def initialize(component, text, leading_space, trailing_space) @component = component @text = text @leading_space = leading_space @trailing_space = trailing_space end |
Instance Attribute Details
#chunks ⇒ Object (readonly)
Returns the value of attribute chunks.
5 6 7 |
# File 'lib/prawn/svg/elements/text_node.rb', line 5 def chunks @chunks end |
#component ⇒ Object (readonly)
Returns the value of attribute component.
5 6 7 |
# File 'lib/prawn/svg/elements/text_node.rb', line 5 def component @component end |
#text ⇒ Object
Returns the value of attribute text.
6 7 8 |
# File 'lib/prawn/svg/elements/text_node.rb', line 6 def text @text end |
Instance Method Details
#calculate_text_rendering_mode ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/prawn/svg/elements/text_node.rb', line 159 def calculate_text_rendering_mode fill = !component.computed_properties.fill.none? # rubocop:disable Style/InverseMethods stroke = !component.computed_properties.stroke.none? # rubocop:disable Style/InverseMethods if fill && stroke :fill_stroke elsif fill :fill elsif stroke :stroke else :invisible end end |
#calculated_width ⇒ Object
23 24 25 26 27 |
# File 'lib/prawn/svg/elements/text_node.rb', line 23 def calculated_width @chunks.reduce(0) do |total, chunk| total + (chunk.fixed_width || chunk.base_width) + chunk.offset end end |
#lay_out(prawn) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 96 97 98 99 100 101 102 103 104 |
# File 'lib/prawn/svg/elements/text_node.rb', line 42 def lay_out(prawn) remaining_text = @text @chunks = [] while remaining_text != '' x = y = dx = dy = rotate = nil remaining = rotation_remaining = false comp = component while comp shifted = comp.x_values.shift x ||= shifted shifted = comp.y_values.shift y ||= shifted shifted = comp.dx.shift dx ||= shifted shifted = comp.dy.shift dy ||= shifted shifted = comp.rotation.length > 1 ? comp.rotation.shift : comp.rotation.first if shifted && rotate.nil? rotate = shifted remaining ||= comp.rotation != [0] end remaining ||= comp.x_values.any? || comp.y_values.any? || comp.dx.any? || comp.dy.any? || (rotate && rotate != 0) rotation_remaining ||= comp.rotation.length > 1 comp = comp.parent_component end rotate = (-rotate if rotate && rotate != 0) text_to_draw = remaining ? remaining_text[0..0] : remaining_text opts = { size: component.computed_properties.numeric_font_size, kerning: true } total_spacing = text_to_draw.length > 1 ? (component.letter_spacing_pixels || 0) * (text_to_draw.length - 1) : 0 base_width = prawn.width_of(text_to_draw, opts) + total_spacing offset = dx ? [0, dx].max : 0 @chunks << Chunk.new(text_to_draw, x, y, dx, dy, rotate, base_width, offset, nil) if remaining remaining_text = remaining_text[1..] else # we can get to this path with rotations still pending # solve this by shifting them out by the number of # characters we've just drawn shift = remaining_text.length - 1 if rotation_remaining && shift.positive? comp = component while comp count = [shift, comp.rotation.length - 1].min comp.rotation.shift(count) if count.positive? comp = comp.parent_component end end break end end end |
#leading_space? ⇒ Boolean
15 16 17 |
# File 'lib/prawn/svg/elements/text_node.rb', line 15 def leading_space? @leading_space end |
#render(prawn, size, cursor, y_offset) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/prawn/svg/elements/text_node.rb', line 106 def render(prawn, size, cursor, y_offset) chunks.each do |chunk| cursor.x = chunk.x if chunk.x cursor.x += chunk.dx if chunk.dx cursor.y = chunk.y if chunk.y cursor.y -= chunk.dy if chunk.dy render_underline(prawn, size, cursor, y_offset, chunk.fixed_width || chunk.base_width) if component.computed_properties.text_decoration == 'underline' opts = { size: size, at: [cursor.x, cursor.y + (y_offset || 0)] } opts[:rotate] = chunk.rotate if chunk.rotate scaling = if chunk.fixed_width && component.current_length_adjust_is_scaling? chunk.fixed_width * 100 / chunk.base_width else 100 end spacing_enabled = chunk.fixed_width && !component.current_length_adjust_is_scaling? && chunk.text.length > 1 # This isn't perfect. It assumes the parent component which started the textLength context # has a character at the end of its text nodes. If it doesn't, the last character in its # children should not take the space. This is possible but would involve a lot more work so # I will park it for now. parent_spacing = spacing_enabled && !component.text_length spacing = if spacing_enabled ((chunk.fixed_width - chunk.base_width) / (chunk.text.length - (parent_spacing ? 0 : 1))) + (component.letter_spacing_pixels || 0) end prawn.horizontal_text_scaling(scaling) do prawn.character_spacing(spacing || component.letter_spacing_pixels || prawn.character_spacing) do prawn.text_rendering_mode(calculate_text_rendering_mode) do prawn.draw_text(chunk.text, **opts) end end end cursor.x += chunk.fixed_width || chunk.base_width # If we're in a textLength context for one of our parents, we'll need to add spacing # to the end of our string. See comment above for why this isn't quite right. cursor.x += spacing if parent_spacing end end |
#render_underline(prawn, size, cursor, y_offset, width) ⇒ Object
153 154 155 156 157 |
# File 'lib/prawn/svg/elements/text_node.rb', line 153 def render_underline(prawn, size, cursor, y_offset, width) offset, thickness = FontMetrics.underline_metrics(prawn, size) prawn.fill_rectangle [cursor.x, cursor.y + (y_offset || 0) + offset + (thickness / 2.0)], width, thickness end |
#total_flexible_and_fixed_width ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/prawn/svg/elements/text_node.rb', line 29 def total_flexible_and_fixed_width flexible = fixed = 0 chunks.each do |chunk| if chunk.fixed_width.nil? flexible += chunk.base_width fixed += chunk.offset else fixed += chunk.offset + chunk.fixed_width end end [flexible, fixed] end |
#trailing_space? ⇒ Boolean
19 20 21 |
# File 'lib/prawn/svg/elements/text_node.rb', line 19 def trailing_space? @trailing_space end |