/
get_type_metrics.rb
146 lines (117 loc) · 3.79 KB
/
get_type_metrics.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
#! /usr/local/bin/ruby -w
require 'RMagick'
# Add a method for drawing braces.
module Magick
class Draw
# (w,h) - width & height of rectangle enclosing brace.
# Normally the brace is drawn with its opening to the
# left and its lower point on the origin.
#
# Set w < 0 to draw right-opening brace. Set h < 0 to
# position top point at origin.
#
# The placement & orientation is affected by the
# current user coordinate system.
def brace(w, h)
raise(ArgumentError, "width must be != 0") unless w != 0
raise(ArgumentError, "height must be != 0") unless h != 0
path("M0,0 Q#{w},0 #{w/2.0},#{-h/4.0} T#{w},#{-h/2.0}" +
"Q0,#{-h/2.0} #{w/2.0},#{-(3.0*h/4.0)} T0,#{-h}")
end
end # class Draw
end
Origin_x = 110
Origin_y = 230
Glyph = 'g'
Face = RUBY_PLATFORM =~ /mswin/ ? "Verdana" : "Times"
canvas = Magick::Image.new(410, 320, Magick::HatchFill.new('white', 'lightcyan2'))
# Draw a big lowercase 'g' on the canvas. Leave room on all sides for
# the labels. Use 'undercolor' to set off the glyph.
glyph = Magick::Draw.new
glyph.annotate(canvas, 0, 0, Origin_x, Origin_y, Glyph) do
glyph.pointsize = 124
glyph.stroke = 'none'
glyph.fill = 'black'
glyph.font_family = Face
glyph.undercolor = '#ffff00c0'
end
# Call get_type_metrics. This is what this example's all about.
metrics = glyph.get_type_metrics(canvas, Glyph)
gc = Magick::Draw.new
gc.translate(Origin_x, Origin_y)
# Draw the origin as a big red dot.
gc.stroke('red')
gc.fill('red')
gc.circle(0, 0, 0, 2)
# All our lines will be medium-gray, dashed, and thin.
gc.stroke('gray50')
gc.stroke_dasharray(5,2)
gc.stroke_width(1)
gc.fill('none')
# baseline
gc.line(-10, 0, metrics.width+20, 0)
# a vertical line through the origin
gc.line(0, -metrics.descent-metrics.height-10, 0, -metrics.descent+15)
# descent
gc.line(-10, -metrics.descent, metrics.width+20, -metrics.descent)
# ascent
gc.line(-10, -metrics.ascent, metrics.width+20, -metrics.ascent)
# height
gc.line(-10, -metrics.descent-metrics.height,
metrics.width+10, -metrics.descent-metrics.height)
# width
gc.line(metrics.width, -metrics.descent-metrics.height-10,
metrics.width, -metrics.descent+20)
# max_advance
gc.line(metrics.max_advance, -10, metrics.max_advance, -metrics.descent+20)
gc.draw(canvas)
# Draw the braces and labels. Position the braces by transforming the
# user coordinate system with translate and rotate methods.
gc = Magick::Draw.new
gc.font_family('Face')
gc.pointsize(13)
gc.fill('none')
gc.stroke('black')
gc.stroke_width(1)
gc.translate(Origin_x, Origin_y)
# between origin and descent
gc.push
gc.translate(metrics.width+23, 0)
gc.brace(10, metrics.descent)
gc.pop
# between origin and ascent
gc.push
gc.translate(metrics.width+23, 0)
gc.brace(10, metrics.ascent)
gc.pop
# between origin and height
gc.push
gc.translate(-13, -metrics.descent-metrics.height)
gc.rotate(180)
gc.brace(10, metrics.height)
gc.pop
# between origin and width
gc.push
gc.translate(metrics.width, -metrics.descent-metrics.height-10-3)
gc.rotate(-90)
gc.brace(10, metrics.width)
gc.pop
# between origin and max_advance
gc.push
gc.translate(0, -metrics.descent+15)
gc.rotate(90)
gc.brace(10, metrics.max_advance)
gc.pop
# Add labels
gc.font_weight(Magick::NormalWeight)
gc.font_style(Magick::NormalStyle)
gc.stroke('none')
gc.fill('black')
gc.text(metrics.width+40, -(metrics.ascent/2)+4, 'ascent')
gc.text(metrics.width+40, -(metrics.descent/2)+4, 'descent')
gc.text(-60, -metrics.descent-metrics.height/2+4, 'height')
gc.text((metrics.width/2)-15, -metrics.descent-metrics.height-25, 'width')
gc.text((metrics.max_advance)/2-38, -metrics.descent+35, "max_advance")
gc.draw(canvas)
canvas.border!(1,1,'blue')
canvas.write('get_type_metrics.gif')