Class: Magick::RVG::Utility::TextStrategy

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

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ TextStrategy

Returns a new instance of TextStrategy.



69
70
71
72
# File 'lib/rvg/misc.rb', line 69

def initialize(context)
  @ctx = context
  @ctx.shadow.affine = @ctx.text_attrs.affine
end

Instance Method Details

#enquote(text) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rvg/misc.rb', line 74

def enquote(text)
  return text if text.length > 2 && /\A(?:"[^\"]+"|'[^\']+'|\{[^\}]+\})\z/.match(text)

  if !text['\'']
    text = '\'' + text + '\''
    return text
  elsif !text['"']
    text = '"' + text + '"'
    return text
  elsif !(text['{'] || text['}'])
    text = '{' + text + '}'
    return text
  end

  # escape existing braces, surround with braces
  text.gsub!(/[}]/) { |b| '\\' + b }
  '{' + text + '}'
end

#glyph_metrics(glyph_orientation, glyph) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rvg/misc.rb', line 93

def glyph_metrics(glyph_orientation, glyph)
  gm = @ctx.shadow.get_type_metrics('a' + glyph + 'a')
  gm2 = @ctx.shadow.get_type_metrics('aa')
  h = (gm.ascent - gm.descent + 0.5).to_i
  w = gm.width - gm2.width
  if glyph_orientation.zero? || glyph_orientation == 180
    [w, h]
  else
    [h, w]
  end
end

#render_glyph(glyph_orientation, x, y, glyph) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rvg/misc.rb', line 149

def render_glyph(glyph_orientation, x, y, glyph)
  if glyph_orientation.zero?
    @ctx.gc.text(x, y, enquote(glyph))
  else
    @ctx.gc.push
    @ctx.gc.translate(x, y)
    @ctx.gc.rotate(glyph_orientation)
    @ctx.gc.translate(-x, -y)
    @ctx.gc.text(x, y, enquote(glyph))
    @ctx.gc.pop
  end
end

#shift_baseline(glyph_orientation, glyph) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rvg/misc.rb', line 126

def shift_baseline(glyph_orientation, glyph)
  glyph_dimensions = @ctx.shadow.get_type_metrics(glyph)
  x = if glyph_orientation.zero? || glyph_orientation == 180
        glyph_dimensions.width
      else
        glyph_dimensions.ascent - glyph_dimensions.descent
      end
  case @ctx.text_attrs.baseline_shift
  when :baseline
    x = 0
  when :sub

  when :super
    x = -x
  when /[-+]?(\d+)%/
    m = Regexp.last_match(1) == '-' ? -1.0 : 1.0
    x = (m * x * Regexp.last_match(1).to_f / 100.0)
  else
    x = -@ctx.text_attrs.baseline_shift
  end
  x
end

#text_rel_coords(text) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rvg/misc.rb', line 105

def text_rel_coords(text)
  y_rel_coords = []
  x_rel_coords = []
  first_word = true
  words = text.split(::Magick::RVG::WORD_SEP)
  words.each do |word|
    unless first_word
      wx, wy = get_word_spacing
      x_rel_coords << wx
      y_rel_coords << wy
    end
    first_word = false
    word.chars.each do |glyph|
      wx, wy = get_letter_spacing(glyph)
      x_rel_coords << wx
      y_rel_coords << wy
    end
  end
  [x_rel_coords, y_rel_coords]
end