Class: Prawn::SVG::Elements::TextNode

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/elements/text_node.rb

Defined Under Namespace

Classes: Chunk

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#chunksObject (readonly)

Returns the value of attribute chunks.



5
6
7
# File 'lib/prawn/svg/elements/text_node.rb', line 5

def chunks
  @chunks
end

#componentObject (readonly)

Returns the value of attribute component.



5
6
7
# File 'lib/prawn/svg/elements/text_node.rb', line 5

def component
  @component
end

#textObject

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_modeObject



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/prawn/svg/elements/text_node.rb', line 291

def calculate_text_rendering_mode
  return :fill if component.inside_clip_path

  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_widthObject



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

#combined_kerning_and_letter_spacingObject



280
281
282
283
284
285
286
287
288
289
# File 'lib/prawn/svg/elements/text_node.rb', line 280

def combined_kerning_and_letter_spacing
  kerning = component.kerning_pixels
  letter = component.letter_spacing_pixels

  if kerning && letter
    kerning + letter
  else
    kerning || letter
  end
end

#font_for_glyph(prawn, char, primary_font, fallback_fonts) ⇒ Object



237
238
239
240
241
242
243
244
245
246
# File 'lib/prawn/svg/elements/text_node.rb', line 237

def font_for_glyph(prawn, char, primary_font, fallback_fonts)
  return nil if primary_font.glyph_present?(char)

  fallback_fonts.each do |fb|
    prawn.font(fb)
    return fb if prawn.font.glyph_present?(char)
  end

  nil
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
105
106
107
108
109
110
# 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

    kerning = component.kerning_enabled?
    opts = { size: component.computed_properties.numeric_font_size, kerning: kerning }

    fallback_fonts = component.fallback_fonts
    font_runs = fallback_fonts&.any? ? split_into_font_runs(prawn, text_to_draw, fallback_fonts) : nil

    kerning_spacing = text_to_draw.length > 1 ? (component.kerning_pixels || 0) * (text_to_draw.length - 1) : 0
    letter_spacing = text_to_draw.length > 1 ? (component.letter_spacing_pixels || 0) * (text_to_draw.length - 1) : 0
    word_spacing = (component.word_spacing_pixels || 0) * text_to_draw.count(' ')
    base_width = width_of_text(prawn, text_to_draw, font_runs, opts) + kerning_spacing + letter_spacing + word_spacing

    offset = dx ? [0, dx].max : 0

    @chunks << Chunk.new(text_to_draw, x, y, dx, dy, rotate, base_width, offset, nil, font_runs)

    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

Returns:

  • (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



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/prawn/svg/elements/text_node.rb', line 112

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

    width = chunk.fixed_width || chunk.base_width

    unless component.inside_clip_path
      decoration = component.computed_properties.text_decoration
      unless decoration == 'none'
        render_underline(prawn, size, cursor, y_offset, width) if decoration.include?('underline')
        render_overline(prawn, size, cursor, y_offset, width) if decoration.include?('overline')
        render_line_through(prawn, size, cursor, y_offset, width) if decoration.include?('line-through')
      end
      render_link_annotation(prawn, size, cursor, y_offset, width)
    end

    kerning = component.kerning_enabled?
    opts = { size: size, at: [cursor.x, cursor.y + (y_offset || 0)], kerning: kerning }
    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.kerning_pixels || 0) + (component.letter_spacing_pixels || 0)
      end

    # Inside clip paths, text renders white in a soft mask to define the
    # clipping region. Override any fill color from the SVG element.
    prawn.fill_color('ffffff') if component.inside_clip_path

    combined_spacing = spacing || combined_kerning_and_letter_spacing
    prawn.horizontal_text_scaling(scaling) do
      prawn.character_spacing(combined_spacing || prawn.character_spacing) do
        prawn.word_spacing(component.word_spacing_pixels || prawn.word_spacing) do
          prawn.text_rendering_mode(calculate_text_rendering_mode) do
            render_text_directly(prawn, chunk.text, chunk.font_runs, opts)
          end
        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_line_through(prawn, size, cursor, y_offset, width) ⇒ Object



260
261
262
263
264
# File 'lib/prawn/svg/elements/text_node.rb', line 260

def render_line_through(prawn, size, cursor, y_offset, width)
  offset, thickness = FontMetrics.strikethrough_metrics(prawn, size)

  prawn.fill_rectangle [cursor.x, cursor.y + (y_offset || 0) + offset + (thickness / 2.0)], width, thickness
end


266
267
268
269
270
271
272
273
274
# File 'lib/prawn/svg/elements/text_node.rb', line 266

def render_link_annotation(prawn, size, cursor, y_offset, width)
  href = component.state.anchor_href
  return unless href

  text_bottom = cursor.y + (y_offset || 0) - scaled_font_size(prawn, :descender, size)
  font_height = scaled_font_size(prawn, :height, size)

  LinkRenderer.new(href, [cursor.x, text_bottom + font_height, cursor.x + width, text_bottom]).render(prawn)
end

#render_overline(prawn, size, cursor, y_offset, width) ⇒ Object



254
255
256
257
258
# File 'lib/prawn/svg/elements/text_node.rb', line 254

def render_overline(prawn, size, cursor, y_offset, width)
  offset, thickness = FontMetrics.overline_metrics(prawn, size)

  prawn.fill_rectangle [cursor.x, cursor.y + (y_offset || 0) + offset + (thickness / 2.0)], width, thickness
end

#render_text_directly(prawn, text, font_runs, opts) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/prawn/svg/elements/text_node.rb', line 193

def render_text_directly(prawn, text, font_runs, opts)
  if font_runs.nil?
    prawn.draw_text(text, **opts)
  else
    kerning = opts[:kerning]
    x = opts[:at][0]
    font_runs.each do |font_name, run_text|
      run_opts = opts.merge(at: [x, opts[:at][1]])
      if font_name
        prawn.font(font_name) do
          prawn.draw_text(run_text, **run_opts)
          x += prawn.width_of(run_text, size: opts[:size], kerning: kerning)
        end
      else
        prawn.draw_text(run_text, **run_opts)
        x += prawn.width_of(run_text, size: opts[:size], kerning: kerning)
      end
    end
  end
end

#render_underline(prawn, size, cursor, y_offset, width) ⇒ Object



248
249
250
251
252
# File 'lib/prawn/svg/elements/text_node.rb', line 248

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

#scaled_font_size(prawn, method_name, size) ⇒ Object



276
277
278
# File 'lib/prawn/svg/elements/text_node.rb', line 276

def scaled_font_size(prawn, method_name, size)
  (prawn.font.public_send(method_name) / prawn.font_size) * size
end

#split_into_font_runs(prawn, text, fallback_fonts) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/prawn/svg/elements/text_node.rb', line 214

def split_into_font_runs(prawn, text, fallback_fonts)
  primary_font = prawn.font
  runs = []
  current_font = nil
  current_text = +''

  prawn.save_font do
    text.each_char do |char|
      font_for_char = font_for_glyph(prawn, char, primary_font, fallback_fonts)

      if font_for_char != current_font && !current_text.empty?
        runs << [current_font, current_text]
        current_text = +''
      end
      current_font = font_for_char
      current_text << char
    end
  end

  runs << [current_font, current_text] unless current_text.empty?
  runs
end

#total_flexible_and_fixed_widthObject



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

Returns:

  • (Boolean)


19
20
21
# File 'lib/prawn/svg/elements/text_node.rb', line 19

def trailing_space?
  @trailing_space
end

#width_of_text(prawn, text, font_runs, opts) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/prawn/svg/elements/text_node.rb', line 177

def width_of_text(prawn, text, font_runs, opts)
  if font_runs.nil?
    prawn.width_of(text, **opts)
  else
    font_runs.sum(0.0) do |font_name, run_text|
      if font_name
        width = nil
        prawn.font(font_name) { width = prawn.width_of(run_text, **opts) }
        width
      else
        prawn.width_of(run_text, **opts)
      end
    end
  end
end