Class: Magick::RVG::Utility::TBTextStrategy

Inherits:
TextStrategy show all
Defined in:
lib/rvg/misc.rb

Overview

class RLTextStrategy

Instance Method Summary collapse

Methods inherited from TextStrategy

#enquote, #glyph_metrics, #initialize, #render_glyph, #shift_baseline, #text_rel_coords

Constructor Details

This class inherits a constructor from Magick::RVG::Utility::TextStrategy

Instance Method Details

#get_letter_spacing(glyph) ⇒ Object



229
230
231
232
# File 'lib/rvg/misc.rb', line 229

def get_letter_spacing(glyph)
  gx, gy = glyph_metrics(@ctx.text_attrs.glyph_orientation_vertical, glyph)
  [gx, gy + @ctx.text_attrs.letter_spacing]
end

#get_word_spacingObject



224
225
226
227
# File 'lib/rvg/misc.rb', line 224

def get_word_spacing
  @word_space ||= glyph_metrics(@ctx.text_attrs.glyph_orientation_vertical, ' ')[1]
  [0, @word_space + @ctx.text_attrs.word_spacing]
end

#render(x, y, text) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/rvg/misc.rb', line 234

def render(x, y, text)
  x_rel_coords, y_rel_coords = text_rel_coords(text)
  dx = x_rel_coords.max
  dy = y_rel_coords.sum

  # We're handling the anchoring.
  @ctx.gc.push
  @ctx.gc.text_anchor(Magick::StartAnchor)
  if @ctx.text_attrs.text_anchor == :end
    y -= dy
  elsif @ctx.text_attrs.text_anchor == :middle
    y -= dy / 2
  end

  # Align the first glyph such that its center
  # is aligned on x and its top is aligned on y.

  case @ctx.text_attrs.glyph_orientation_vertical
  when 0
    x -= x_rel_coords.max / 2
    y += y_rel_coords[0]
  when 90
    x -= x_rel_coords.max / 2
  when 180
    x += x_rel_coords.max / 2
  when 270
    x += x_rel_coords.max / 2
    y += y_rel_coords.shift
    y_rel_coords << 0 # since we used an element we need to add a dummy
  end

  x -= shift_baseline(@ctx.text_attrs.glyph_orientation_vertical, text[0, 1])

  first_word = true
  text.split(::Magick::RVG::WORD_SEP).each do |word|
    unless first_word
      y += y_rel_coords.shift
      x_rel_coords.shift
    end
    first_word = false
    word.chars.each do |glyph|
      case @ctx.text_attrs.glyph_orientation_vertical.to_i
      when 0, 90, 270
        x_shift = (dx - x_rel_coords.shift) / 2
      when 180
        x_shift = -(dx - x_rel_coords.shift) / 2
      end

      render_glyph(@ctx.text_attrs.glyph_orientation_vertical, x + x_shift, y, glyph)
      y += y_rel_coords.shift
    end
  end

  @ctx.gc.pop
  [0, dy]
end