-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_type_metrics.rb.html
172 lines (146 loc) · 4.74 KB
/
get_type_metrics.rb.html
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
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
<!DOCTYPE public PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta name="generator" content="ex2html.rb" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/popup.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>
hljs.configure({ cssSelector: "pre" });
hljs.highlightAll();
</script>
<title>RMagick example: get_type_metrics.rb</title>
</head>
<body>
<h1>get_type_metrics.rb</h1>
<div class="bodybox">
<div class="bodyfloat">
<pre class="language-ruby">
# frozen_string_literal: true
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
end
ORIGIN_X = 110
ORIGIN_Y = 230
GLYPH = 'g'
FONT = RUBY_PLATFORM.include?('mingw') ? '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 |options|
options.pointsize = 124
options.stroke = 'none'
options.fill = 'black'
options.font_family = FONT
options.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(FONT)
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')
</pre>
</div>
</div>
<div id="close"><a href="javascript:window.close();">Close window</a></div>
</body>
</html>