3 Functions for loading external fonts.
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2003, 2004 Matthias Kramm
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 static int loadfont_scale = 1;
25 static int skip_unused = 1;
27 // TODO: should be named "setLoadFontParameters"
28 void swf_SetLoadFontParameters(int _scale, int _skip_unused)
30 loadfont_scale = _scale;
31 skip_unused = _skip_unused;
36 #include <freetype/freetype.h>
37 #include <freetype/ftglyph.h>
38 #include <freetype/ftsnames.h>
39 #include <freetype/ttnameid.h>
40 #include <freetype/ftoutln.h>
42 #define FT_SCALE loadfont_scale
43 #define FT_SUBPIXELS 64
45 static int ft_move_to(FT_Vector* _to, void* user)
47 drawer_t* draw = (drawer_t*)user;
49 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
50 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
51 draw->moveTo(draw, &to);
54 static int ft_line_to(FT_Vector* _to, void* user)
56 drawer_t* draw = (drawer_t*)user;
58 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
59 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
60 draw->lineTo(draw, &to);
63 static int ft_cubic_to(FT_Vector* _c1, FT_Vector* _c2, FT_Vector* _to, void* user)
65 drawer_t* draw = (drawer_t*)user;
67 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
68 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
69 c1.x = _c1->x*FT_SCALE/(float)FT_SUBPIXELS;
70 c1.y = -_c1->y*FT_SCALE/(float)FT_SUBPIXELS;
71 c2.x = _c2->x*FT_SCALE/(float)FT_SUBPIXELS;
72 c2.y = -_c2->y*FT_SCALE/(float)FT_SUBPIXELS;
73 draw_cubicTo(draw, &c1, &c2, &to);
76 static int ft_conic_to(FT_Vector* _c, FT_Vector* _to, void* user)
78 drawer_t* draw = (drawer_t*)user;
80 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
81 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
82 c.x = _c->x*FT_SCALE/(float)FT_SUBPIXELS;
83 c.y = -_c->y*FT_SCALE/(float)FT_SUBPIXELS;
84 draw_conicTo(draw, &c, &to);
87 static FT_Outline_Funcs outline_functions =
96 static FT_Library ftlibrary = 0;
98 SWFFONT* swf_LoadTrueTypeFont(char*filename)
102 const char* name = 0;
110 if(FT_Init_FreeType(&ftlibrary)) {
111 fprintf(stderr, "Couldn't init freetype library!\n");
115 error = FT_New_Face(ftlibrary, filename, 0, &face);
117 fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
120 if(face->num_glyphs <= 0)
123 font = malloc(sizeof(SWFFONT));
124 memset(font, 0, sizeof(SWFFONT));
127 font->layout = malloc(sizeof(SWFLAYOUT));
128 memset(font->layout, 0, sizeof(SWFLAYOUT));
129 font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
130 font->style = ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
131 |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
132 font->encoding = FONT_ENCODING_UNICODE;
133 font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
134 memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
136 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
137 memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
138 if(FT_HAS_GLYPH_NAMES(face)) {
139 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
140 memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
143 font->layout->ascent = face->ascender*20/FT_SUBPIXELS; //face->bbox.xMin;
144 font->layout->descent = abs(face->descender)*20/FT_SUBPIXELS; //face->bbox.xMax;
145 font->layout->leading = abs(face->bbox.yMin - face->bbox.yMax); //-face->bbox.xMin*20/FT_SUBPIXELS;
146 font->layout->kerningcount = 0;
148 name = FT_Get_Postscript_Name(face);
150 font->name = (U8*)strdup(name);
152 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
154 for(t=0;t<65536;t++) {
155 int index = FT_Get_Char_Index(face, t);
156 if(index>=0 && index<face->num_glyphs) {
157 if(font->glyph2ascii[index]<0)
158 font->glyph2ascii[index] = t;
162 // Map Glyphs to Unicode, version 2 (much nicer):
163 // (The third way would be the AGL algorithm, as proposed
164 // by Werner Lemberg on freetype@freetype.org)
166 charcode = FT_Get_First_Char(face, &gindex);
169 if(gindex >= 0 && gindex<face->num_glyphs) {
170 if(!font->glyph2ascii[gindex]) {
171 font->glyph2ascii[gindex] = charcode;
172 if(charcode + 1 > font->maxascii) {
173 font->maxascii = charcode + 1;
177 charcode = FT_Get_Next_Char(face, charcode, &gindex);
180 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
182 for(t=0;t<font->maxascii;t++) {
183 int g = FT_Get_Char_Index(face, t);
184 if(!g || g>=face->num_glyphs)
186 font->ascii2glyph[t] = g;
188 if(!font->glyph2ascii[g]) {
189 font->glyph2ascii[g] = t;
196 glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
198 for(t=0; t < face->num_glyphs; t++) {
207 if(FT_HAS_GLYPH_NAMES(face)) {
208 error = FT_Get_Glyph_Name(face, t, name, 127);
209 if(!error && name[0] && !strstr(name, "notdef")) {
210 font->glyphnames[font->numchars] = strdup(name);
214 if(!font->glyph2ascii[t] && !hasname && skip_unused) {
217 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
219 error = FT_Get_Glyph(face->glyph, &glyph);
222 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
223 bbox.yMin = -bbox.yMin;
224 bbox.yMax = -bbox.yMax;
225 if(bbox.xMax < bbox.xMin) {
227 bbox.xMax ^= bbox.xMin;
228 bbox.xMin ^= bbox.xMax;
229 bbox.xMax ^= bbox.xMin;
231 if(bbox.yMax < bbox.yMin) {
233 bbox.yMax ^= bbox.yMin;
234 bbox.yMin ^= bbox.yMax;
235 bbox.yMax ^= bbox.yMin;
238 swf_Shape01DrawerInit(&draw, 0);
240 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
241 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
248 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
250 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
253 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
256 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
258 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
259 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
260 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
261 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
265 FT_Done_Glyph(glyph);
266 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
267 glyph2glyph[t] = font->numchars;
270 /* notice: if skip_unused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
271 have more memory allocated than just font->numchars, but only the first font->numchars
274 for(t=0;t<font->maxascii;t++) {
275 if(font->ascii2glyph[t]>=0) {
276 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
282 FT_Done_FreeType(ftlibrary);ftlibrary=0;
286 #else //HAVE_FREETYPE
288 SWFFONT* swf_LoadTrueTypeFont(char*filename)
290 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
300 static int t1lib_initialized = 0;
302 SWFFONT* swf_LoadT1Font(char*filename)
306 float angle,underline;
307 char*fontname,*fullname,*familyname;
315 if(!t1lib_initialized) {
317 if ((T1_InitLib(NO_LOGFILE)==NULL)){
318 fprintf(stderr, "Initialization of t1lib failed\n");
321 t1lib_initialized = 1;
323 nr = T1_AddFont(filename);
326 num = T1_SetDefaultEncoding(encoding);
327 for(;num<256;num++) encoding[num] = 0;
329 charnames = T1_GetAllCharNames(nr);
331 angle = T1_GetItalicAngle(nr);
332 fontname = T1_GetFontName(nr);
333 fullname = T1_GetFullName(nr);
334 familyname = T1_GetFamilyName(nr);
335 underline = T1_GetUnderlinePosition(nr);
336 bbox = T1_GetFontBBox(nr);
338 font = (SWFFONT*)malloc(sizeof(SWFFONT));
339 memset(font, 0, sizeof(SWFFONT));
343 font->name = (U8*)strdup(fontname);
346 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
347 memset(font->layout, 0, sizeof(SWFLAYOUT));
350 charname = charnames;
356 font->maxascii = num;
357 font->numchars = num;
359 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
361 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
362 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
363 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
364 memset(font->glyph2ascii, 0, num*sizeof(U16));
365 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
366 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
367 font->layout->ascent = (U16)(underline - bbox.lly);
368 font->layout->descent = (U16)(bbox.ury - underline);
369 font->layout->leading = (U16)(font->layout->ascent -
370 font->layout->descent -
371 (bbox.lly - bbox.ury));
372 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
373 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
374 font->layout->kerningcount = 0;
375 font->layout->kerning = 0;
376 font->glyphnames = malloc(num*sizeof(char*));
377 memset(font->glyphnames, 0, num*sizeof(char*));
381 charname = charnames;
382 for(c=0;c<font->numchars;c++) {
385 T1_OUTLINE * outline;
389 outline = T1_GetCharOutline(nr, c, 100.0, 0);
390 firstx = outline->dest.x/0xffff;
396 font->glyphnames[c] = strdup(*charname);
399 font->ascii2glyph[c] = c;
400 font->glyph2ascii[c] = c;
402 swf_Shape01DrawerInit(&draw, 0);
405 pos.x += (outline->dest.x/(float)0xffff);
406 pos.y += (outline->dest.y/(float)0xffff);
408 if(outline->type == T1_PATHTYPE_MOVE) {
409 draw.moveTo(&draw,&pos);
410 } else if(outline->type == T1_PATHTYPE_LINE) {
411 draw.lineTo(&draw,&pos);
412 } else if(outline->type == T1_PATHTYPE_BEZIER) {
413 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
415 b.x = o2->B.x/(float)0xffff+last.x;
416 b.y = o2->B.y/(float)0xffff+last.y;
417 c.x = o2->C.x/(float)0xffff+last.x;
418 c.y = o2->C.y/(float)0xffff+last.y;
419 draw_cubicTo(&draw,&b,&c,&pos);
421 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
424 outline = outline->link;
429 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
430 bbox = swf_ShapeDrawerGetBBox(&draw);
433 font->layout->bounds[c] = bbox;
434 font->glyph[c].advance = bbox.xmax;
435 if(!font->glyph[c].advance) {
436 font->glyph[c].advance = firstx;
445 SWFFONT* swf_LoadT1Font(char*filename)
447 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
453 static int isSWF(const char*filename)
455 FILE*fi = fopen(filename, "rb");
461 memset(a, 0, sizeof(a));
465 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
471 SWFFONT* swf_LoadFont(char*filename)
473 int is_swf = isSWF(filename);
477 return swf_ReadFont(filename);
479 #if defined(HAVE_FREETYPE)
480 return swf_LoadTrueTypeFont(filename);
481 #elif defined(HAVE_T1LIB)
482 return swf_LoadT1Font(filename);
484 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);