/
SimpleGlyph.as
277 lines (221 loc) · 6.55 KB
/
SimpleGlyph.as
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
/*
'firetype' is an ActionScript 3 library which loads font files and renders characters via the GPU.
Copyright ©2013 Max Knoblich
www.maxdid.it
me@maxdid.it
This file is part of 'firetype' by Max Did It.
'firetype' is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
'firetype' is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with 'firetype'. If not, see <http://www.gnu.org/licenses/>.
*/
package de.maxdidit.hardware.font.data.tables.truetype.glyf.simple
{
import de.maxdidit.hardware.font.data.tables.truetype.glyf.GlyphTableData;
import de.maxdidit.hardware.text.cache.HardwareCharacterCache;
import de.maxdidit.hardware.font.data.tables.truetype.glyf.contours.Contour;
import de.maxdidit.hardware.font.data.tables.truetype.glyf.contours.Vertex;
import de.maxdidit.hardware.font.data.tables.truetype.glyf.Glyph;
import de.maxdidit.hardware.font.HardwareFont;
import de.maxdidit.hardware.font.HardwareGlyph;
import de.maxdidit.hardware.text.components.HardwareGlyphInstance;
import flash.geom.Point;
import flash.utils.ByteArray;
/**
* ...
* @author Max Knoblich
*/
public class SimpleGlyph extends Glyph
{
///////////////////////
// Member Fields
///////////////////////
private var _endPointsOfContours:Vector.<uint>;
private var _instructionLength:uint;
private var _instructions:ByteArray;
CONFIG::debug
{
private var _flagData:Vector.<uint>;
}
private var _flags:Vector.<SimpleGlyphFlags>;
CONFIG::debug
{
private var _xCoordinates:Vector.<int>;
private var _yCoordinates:Vector.<int>;
}
private var _contours:Vector.<Contour>;
///////////////////////
// Constructor
///////////////////////
public function SimpleGlyph()
{
super();
}
///////////////////////
// Member Properties
///////////////////////
// endPointsOfContours
public function get endPointsOfContours():Vector.<uint>
{
return _endPointsOfContours;
}
public function set endPointsOfContours(value:Vector.<uint>):void
{
_endPointsOfContours = value;
}
// instructionLength
public function get instructionLength():uint
{
return _instructionLength;
}
public function set instructionLength(value:uint):void
{
_instructionLength = value;
}
// instructions
public function get instructions():ByteArray
{
return _instructions;
}
public function set instructions(value:ByteArray):void
{
_instructions = value;
}
// flagData
CONFIG::debug
{
public function get flagData():Vector.<uint>
{
return _flagData;
}
public function set flagData(value:Vector.<uint>):void
{
_flagData = value;
}
}
// flags
public function get flags():Vector.<SimpleGlyphFlags>
{
return _flags;
}
public function set flags(value:Vector.<SimpleGlyphFlags>):void
{
_flags = value;
}
// xCoordinates
CONFIG::debug
{
public function get xCoordinates():Vector.<int>
{
return _xCoordinates;
}
public function set xCoordinates(value:Vector.<int>):void
{
_xCoordinates = value;
}
// yCoordinates
public function get yCoordinates():Vector.<int>
{
return _yCoordinates;
}
public function set yCoordinates(value:Vector.<int>):void
{
_yCoordinates = value;
}
}
// contours
public function get contours():Vector.<Contour>
{
return _contours;
}
public function set contours(value:Vector.<Contour>):void
{
_contours = value;
distributeHoles();
}
///////////////////////
// Member Functions
///////////////////////
override public function retrieveGlyphInstances(instances:Vector.<HardwareGlyphInstance>):void
{
var glyphInstance:HardwareGlyphInstance = HardwareGlyphInstance.getHardwareGlyphInstance(null);
glyphInstance.glyph = this;
instances.push(glyphInstance);
}
override public function retrievePaths(vertexDistance:Number, connectedPaths:Vector.<Vector.<Vertex>>, originalPaths:Vector.<Vector.<Vertex>>):void
{
const l:uint = _contours.length;
connectedPaths.length = l;
for (var i:uint = 0; i < l; i++)
{
connectedPaths[i] = new Vector.<Vertex>();
_contours[i].retrievePath(vertexDistance, connectedPaths[i], originalPaths);
}
}
private function distributeHoles():void
{
var potentialHole:Contour;
var potentialContainer:Contour;
var container:Contour;
if (_contours.length == 1)
{
return;
}
_contours.sort(sortByBoundingBox);
var holeDirection:Boolean = !_contours[0].clockWise; // assume that the biggest contour is not a hole.
for (var i:int = _contours.length - 1; i >= 0; i--)
{
potentialHole = _contours[i];
if (potentialHole.clockWise != holeDirection)
{
// this is not a hole
continue;
}
container = null;
for (var j:int = _contours.length - 1; j >= 0; j--)
{
potentialContainer = _contours[j];
if (potentialContainer == potentialHole)
{
continue;
}
if (potentialContainer.clockWise == potentialHole.clockWise)
{
continue;
}
if (!potentialContainer.contains(potentialHole))
{
continue;
}
if (!container)
{
container = potentialContainer;
}
else if(container.boundingBox.width > potentialContainer.boundingBox.width || container.boundingBox.height > potentialContainer.boundingBox.height)
{
container = potentialContainer;
}
}
if (container)
{
_contours.splice(i, 1);
container.addHole(potentialHole);
}
}
for (i = 0; i < _contours.length; i++)
{
_contours[i].sortHoles();
}
}
private function sortByBoundingBox(contourA:Contour, contourB:Contour):Number
{
return (contourB.boundingBox.width * contourB.boundingBox.height) - (contourA.boundingBox.width * contourA.boundingBox.height);
}
}
}