Repository navigation
Expand file tree
/
Copy pathFontastic.java
More file actions
531 lines (453 loc) · 13.1 KB
/
Copy pathFontastic.java
File metadata and controls
531 lines (453 loc) · 13.1 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/**
* Fontastic
* A font file writer to create TTF and WOFF (Webfonts).
* http://code.andreaskoller.com/libraries/fontastic
*
* Copyright (C) 2013 Andreas Koller http://andreaskoller.com
*
* Uses:
* doubletype http://sourceforge.net/projects/doubletype/ for TTF creation
* sfntly http://code.google.com/p/sfntly/ for WOFF creation
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* @author Andreas Koller http://andreaskoller.com
* @modified 06/19/2013
* @version 0.4 (4)
*/
package fontastic;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.doubletype.ossa.Engine;
import org.doubletype.ossa.OutOfRangeException;
import org.doubletype.ossa.adapter.EContour;
import org.doubletype.ossa.adapter.EContourPoint;
import org.doubletype.ossa.adapter.EControlPoint;
import org.doubletype.ossa.module.GlyphFile;
import org.doubletype.ossa.module.TypefaceFile;
import processing.core.PApplet;
/**
* Fontastic A font file writer to create TTF and WOFF (Webfonts). http://code.andreaskoller.com/libraries/fontastic
*
*/
public class Fontastic {
//private org.doubletype.ossa.module.TypefaceFile typeface;
private org.doubletype.ossa.Engine m_engine;
private String fontname;
private String TTFfilename;
private List<FGlyph> glyphs;
private int advanceWidth = 512;
public final static String VERSION = "0.4";
private boolean debug = true; // debug toggles println calls
/** Uppercase alphabet 26 characters **/
public final static char alphabet[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z' };
/** Lowercase alphabet 26 characters **/
public final static char alphabetLc[] = { 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z' };
/**
* Return the version of the library.
*
* @return String
*/
public static String version() {
return VERSION;
}
/**
* Constructor
*
* @example Fontastic f = new Fontastic(this, "MyFont");
*
* @param theParent
* Your processing sketch (this).
* @param fontname
* Font name
*
*/
public Fontastic(PApplet myParent, String fontname) {
this.fontname = fontname;
intitialiseFont();
this.glyphs = new ArrayList<FGlyph>();
}
/**
* Returns the font name.
*
* @return String
*/
public String getFontname() {
return fontname;
}
/**
* Creates and initialises a new typeface. Font data is put into sketch
* folder data/fontname.
*/
private void intitialiseFont() {
File data_dir = new File("data/");
if (!data_dir.exists()) {
data_dir.mkdir();
}
File a_dir = new File("data/"+fontname);
if (!a_dir.exists()) {
a_dir.mkdir();
} else {
deleteFolderContents(a_dir, false);
}
m_engine = Engine.getSingletonInstance();
m_engine.buildNewTypeface(fontname, a_dir);
this.setFontFamilyName(fontname);
this.setVersion("CC BY-SA 3.0 http://creativecommons.org/licenses/by-sa/3.0/"); // default
// license
String directoryName = a_dir + File.separator + "bin" + File.separator;
TTFfilename = directoryName + fontname + ".ttf";
}
/**
* Builds the font and writes the .ttf and the .woff file as well as a HTML template for previewing the WOFF.
* If debug is set (default is true) then you'll see the .ttf and .woff file name in the console.
*/
public void buildFont() {
// Create TTF file with doubletype
m_engine.addDefaultGlyphs();
for (FGlyph glyph : glyphs) {
GlyphFile glyphFile = m_engine.addNewGlyph(glyph.getGlyphChar());
glyphFile.setAdvanceWidth(glyph.getAdvanceWidth());
for (FContour contour : glyph.getContours()) {
EContour econtour = new EContour();
econtour.setType(EContour.k_cubic);
for (FPoint point : contour.points) {
EContourPoint e = new EContourPoint(point.x, point.y, true);
if (point.hasControlPoint1()) {
EControlPoint cp1 = new EControlPoint(true,
point.controlPoint1.x, point.controlPoint1.y);
e.setControlPoint1(cp1);
}
if (point.hasControlPoint2()) {
EControlPoint cp2 = new EControlPoint(false,
point.controlPoint2.x, point.controlPoint2.y);
e.setControlPoint2(cp2);
}
econtour.addContourPoint(e);
}
glyphFile.addContour(econtour);
}
glyphFile.saveGlyphFile();
}
m_engine.buildTrueType(false);
if (debug)
System.out.println("TTF file created successfully: " + getTTFfilename());
}
/**
* Deletes all the glyph files created by doubletype in your data/fontname
* folder.
*/
public void cleanup() {
File a_dir = new File("data/"+fontname);
File[] filesToExclude = new File[3];
filesToExclude[0] = new File(getTTFfilename());
deleteFolderContents(a_dir, true, filesToExclude);
if (debug)
System.out
.println("Cleaned up and deleted all glyph files, except font files.");
}
/**
* Sets the author of the font.
*/
public void setAuthor(String author) {
m_engine.setAuthor(author);
}
/**
* Sets the copyright year of the font.
*/
public void setCopyrightYear(String copyrightYear) {
m_engine.setCopyrightYear(copyrightYear);
}
/**
* Sets the version of the font (default is "0.1").
*/
public void setVersion(String version) {
m_engine.getTypeface().getGlyph().getHead().setVersion(version);
}
/**
* Sets the font family name of the font. Also called in the constructor. If
* changed with setFontFamilyName() it won't affect folder the font is
* stored in.
*/
public void setFontFamilyName(String fontFamilyName) {
m_engine.setFontFamilyName(fontFamilyName);
}
/**
* Sets the sub family of the font.
*/
public void setSubFamily(String subFamily) {
m_engine.getTypeface().setSubFamily(subFamily);
}
/**
* Sets the license of the font (default is
* "CC BY-SA 3.0 http://creativecommons.org/licenses/by-sa/3.0/")
*/
public void setTypefaceLicense(String typefaceLicense) {
m_engine.setTypefaceLicense(typefaceLicense);
}
/**
* Sets the baseline of the font.
*/
public void setBaseline(float baseline) {
m_engine.setBaseline(baseline);
}
/**
* Sets the meanline of the font.
*/
public void setMeanline(float meanline) {
m_engine.setMeanline(meanline);
}
/**
* Sets the advanceWidth of the font. Can be changed for every glyph
* individually. Won't affect already created glyphs.
*/
public void setAdvanceWidth(int advanceWidth) {
m_engine.setAdvanceWidth(advanceWidth);
this.advanceWidth = advanceWidth;
}
public void setTopSideBearing(float topSideBearing) {
try {
m_engine.getTypeface().setTopSideBearing(topSideBearing);
} catch (OutOfRangeException e) {
System.out
.println("Error while setting aopSideBearing (must be within range "
+ m_engine.getTypeface().getEm());
e.printStackTrace();
}
}
public void setBottomSideBearing(float bottomSideBearing) {
try {
m_engine.getTypeface().setBottomSideBearing(bottomSideBearing);
} catch (OutOfRangeException e) {
System.out
.println("Error while setting bottomSideBearing (must be within range "
+ m_engine.getTypeface().getEm());
e.printStackTrace();
}
}
public void setAscender(float ascender) {
try {
m_engine.getTypeface().setAscender(ascender);
} catch (OutOfRangeException e) {
System.out
.println("Error while setting ascender (must be within range 0 to "
+ m_engine.getTypeface().getEm() + ")");
e.printStackTrace();
}
}
public void setDescender(float descender) {
try {
m_engine.getTypeface().setDescender(descender);
} catch (OutOfRangeException e) {
System.out
.println("Error while setting descender (must be within range 0 to "
+ m_engine.getTypeface().getEm() + ")");
e.printStackTrace();
}
}
public void setXHeight(float xHeight) {
try {
m_engine.getTypeface().setXHeight(xHeight);
} catch (OutOfRangeException e) {
System.out
.println("Error while setting xHeight (must be within range 0 to "
+ m_engine.getTypeface().getEm()
+ " as well as lower than the ascender "
+ m_engine.getTypeface().getAscender() + ")");
e.printStackTrace();
}
}
/**
* Sets the default metrics for the typeface: setTopSideBearing(170); // 2
* px setAscender(683); // 8 px setXHeight(424); // 5 px setDescender(171);
* // 2 px setBottomSideBearing(0); // 0px
*
*/
public void setDefaultMetrics() {
m_engine.getTypeface().setDefaultMetrics();
}
/**
* Sets debug to true (in debug mode, the library outputs what happens under
* the hood).
*/
public void setDebug() {
setDebug(true);
}
/**
* Sets the value of debug
*
* @param debug
* true or false
*/
public void setDebug(boolean debug) {
this.debug = debug;
}
/**
* Add a glyph
*
* @param c
* Character of the glyph.
*
* @return FGlyph that has been created.
*
*/
public FGlyph addGlyph(char c) {
FGlyph glyph = new FGlyph(c);
glyph.setAdvanceWidth(advanceWidth);
glyphs.add(glyph);
if (debug)
System.out.println("Glyph " + c + " added. Number of glyphs: "
+ glyphs.size());
return glyph;
}
/**
* Add a glyph and its one contour
*
* @param c
* Character of the glyph.
*
* @param FContour
* Shape of the glyph as FContour.
*
* @return The glyph FGlyph that has been created. You can use this to store
* the glyph and add contours afterwards. Alternatively, you can
* call getGlyph(char c) to retrieve it.
*/
public FGlyph addGlyph(char c, FContour contour) {
FGlyph glyph = new FGlyph(c);
glyphs.add(glyph);
if (debug)
System.out.println("Glyph " + c + " added. Number of glyphs: "
+ glyphs.size());
glyph.addContour(contour);
glyph.setAdvanceWidth(advanceWidth);
return glyph;
}
/**
* Add a glyph and its contours
*
* @param c
* Character of the glyph.
*
* @param FContour
* [] Shape of the glyph in an array of FContour.
*
* @return The FGlyph that has been created. You can use this to store the
* glyph and add contours afterwards. Alternatively, you can call
* getGlyph(char c) to retrieve it.
*/
public FGlyph addGlyph(char c, FContour[] contours) {
FGlyph glyph = new FGlyph(c);
glyphs.add(glyph);
if (debug)
System.out.println("Glyph " + c + " added. Number of glyphs: "
+ glyphs.size());
for (FContour contour : contours) {
// if (debug) System.out.println(p.x + " - " + p.y);
glyph.addContour(contour);
}
glyph.setAdvanceWidth(advanceWidth);
return glyph;
}
/**
* Get glyph by character
*
* @param c
* The character of the glyph
*
* @return The glyph
*/
public FGlyph getGlyph(char c) {
FGlyph glyph = null;
for (int i = 0; i < glyphs.size(); i++) {
if (glyphs.get(i).getGlyphChar() == c) {
glyph = glyphs.get(i);
break;
}
}
return glyph;
}
/**
* Engine getter
*
* @return The doubletype Engine used for font creation, so that you can
* access all functions of doubletype in case you need them.
*/
public Engine getEngine() {
return m_engine;
}
/**
* Returns the TypefaceFile
*
* @return The doubletype TypefaceFile used for font creation, so that you
* can access functions of doubletype in case you need them.
*/
public TypefaceFile getTypefaceFile() {
return m_engine.getTypeface();
}
/**
* Returns the .ttf file name
*
* @return The .ttf file name, which is being created when you call build()
*/
public String getTTFfilename() {
return TTFfilename;
}
private static void deleteFolderContents(File folder,
boolean deleteFolderItself) {
File[] files = folder.listFiles();
if (files != null) { // some JVMs return null for empty dirs
for (File f : files) {
if (f.isDirectory()) {
deleteFolderContents(f, true);
f.delete();
} else {
f.delete();
}
}
}
if (deleteFolderItself)
folder.delete();
}
private static void deleteFolderContents(File folder,
boolean deleteFolderItself, File[] exceptions) {
File[] files = folder.listFiles();
if (files != null) { // some JVMs return null for empty dirs
for (File f : files) {
boolean deleteFile = true;
for (File exceptfile : exceptions) {
if (f.equals(exceptfile))
deleteFile = false;
}
if (deleteFile) {
if (f.isDirectory()) {
deleteFolderContents(f, true, exceptions);
f.delete();
} else {
f.delete();
}
}
}
}
if (deleteFolderItself)
folder.delete();
}
}