/
AthensTextRenderer.class.st
142 lines (102 loc) · 4.92 KB
/
AthensTextRenderer.class.st
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
"
This is a text renderer, which pipelining a text command(s) , produced by AthensTextComposer
into a concrete calls to canvas.
Its using a glyph renderer instance(s) to draw separate font glyphs on a surface in a most suitable/efficient
manner for given Athens surface.
"
Class {
#name : #AthensTextRenderer,
#superclass : #Object,
#instVars : [
'text',
'font',
'canvas',
'glyphRenderer',
'color',
'advance',
'maxAscent',
'currentLine'
],
#category : #'Athens-Text'
}
{ #category : #'as yet unclassified' }
AthensTextRenderer class >> textGoryDetails [
"
Took from: http://www.freetype.org/freetype2/docs/tutorial/step2.html
(Font face metrics)
In this case, you can access the global design metrics as:
units_per_EM
This is the size of the EM square for the font face. It is used by scalable formats to scale design coordinates to device pixels, as described in the last chapter of this section. Its value usually is 2048 (for TrueType) or 1000 (for Type 1), but others are possible too. It is set to 1 for fixed-size formats like FNT/FON/PCF/BDF.
bbox
The global bounding box is defined as the largest rectangle that can enclose all the glyphs in a font face.
ascender
The ascender is the vertical distance from the horizontal baseline to the highest ‘character’ coordinate in a font face. Unfortunately, font formats define the ascender differently. For some, it represents the ascent of all capital latin characters (without accents), for others it is the ascent of the highest accented character, and finally, other formats define it as being equal to bbox.yMax.
descender
The descender is the vertical distance from the horizontal baseline to the lowest ‘character’ coordinate in a font face. Unfortunately, font formats define the descender differently. For some, it represents the descent of all capital latin characters (without accents), for others it is the ascent of the lowest accented character, and finally, other formats define it as being equal to bbox.yMin. This field is negative for values below the baseline.
height
This field is simply used to compute a default line spacing (i.e., the baseline-to-baseline distance) when writing text with this font. Note that it usually is larger than the sum of the ascender and descender taken as absolute values. There is also no guarantee that no glyphs extend above or below subsequent baselines when using this distance.
max_advance_width
This field gives the maximum horizontal cursor advance for all glyphs in the font. It can be used to quickly compute the maximum advance width of a string of text. It doesn't correspond to the maximum glyph image width!
max_advance_height
Same as max_advance_width but for vertical text layout.
underline_position
When displaying or rendering underlined text, this value corresponds to the vertical position, relative to the baseline, of the underline bar's center. It is negative if it is below the baseline.
underline_thickness
When displaying or rendering underlined text, this value corresponds to the vertical thickness of the underline.
"
]
{ #category : #initialization }
AthensTextRenderer >> initialize [
color := Color black.
]
{ #category : #'as yet unclassified' }
AthensTextRenderer >> render: aText lines: lines on: aCanvas [
text := aText.
canvas := aCanvas.
canvas pathTransform restoreAfter: [
lines do: [:line | | origY |
currentLine := line.
maxAscent := line maxAscent.
"set the origin to font's baseline position"
canvas pathTransform translateX: 0 Y: maxAscent.
"reset advance"
advance := 0@0.
line renderOn: self.
"line is rendered, advance vertically by line height"
canvas pathTransform translateX: 0 Y: line height * 1.2 - maxAscent.
] ]
]
{ #category : #'as yet unclassified' }
AthensTextRenderer >> renderBackground: bkObj [
"self halt."
canvas setPaint: bkObj color.
canvas drawShape: ( bkObj start@0 corner: bkObj start + bkObj width @ currentLine heightAndDescent)
]
{ #category : #'as yet unclassified' }
AthensTextRenderer >> renderBackground: aColor width: w [
"self halt."
canvas setPaint: aColor.
canvas drawShape: ( advance x@0 corner: advance x + w +1@ currentLine heightAndDescent)
]
{ #category : #'as yet unclassified' }
AthensTextRenderer >> renderCharactersFrom: start to: stop [
" accumulate advance while rendering spans"
"canvas drawShape: (advance x @ advance y extent: 2 @ 2)."
glyphRenderer advance: advance.
advance := advance + (glyphRenderer renderCharacters: text asString from: start to: stop).
]
{ #category : #'as yet unclassified' }
AthensTextRenderer >> setCurrentColor: aColor [
color = aColor ifTrue: [ ^ self ].
color := aColor.
" canvas setPaint: aColor."
glyphRenderer ifNotNil: [ glyphRenderer setColor: aColor ].
]
{ #category : #'as yet unclassified' }
AthensTextRenderer >> setCurrentFont: aFont [
font := aFont.
glyphRenderer := font glyphRendererOn: canvas surface.
glyphRenderer
advance: advance;
setColor: color.
]