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 = 64;
25 static int skip_unused = 1;
27 void swf_SetLoadFontParameters(int _scale, int _skip_unused)
29 loadfont_scale = _scale;
30 skip_unused = _skip_unused;
35 #include <freetype/freetype.h>
36 #include <freetype/ftglyph.h>
37 #include <freetype/ftsizes.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;
111 if(FT_Init_FreeType(&ftlibrary)) {
112 fprintf(stderr, "Couldn't init freetype library!\n");
116 error = FT_New_Face(ftlibrary, filename, 0, &face);
117 FT_Set_Pixel_Sizes (face, 16, 16);
120 fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
123 if(face->num_glyphs <= 0) {
124 fprintf(stderr, "File %s contains %d glyphs\n", face->num_glyphs);
128 font = malloc(sizeof(SWFFONT));
129 memset(font, 0, sizeof(SWFFONT));
132 font->layout = malloc(sizeof(SWFLAYOUT));
133 memset(font->layout, 0, sizeof(SWFLAYOUT));
134 font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
135 font->style = ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
136 |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
137 font->encoding = FONT_ENCODING_UNICODE;
138 font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
139 memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
141 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
142 memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
143 if(FT_HAS_GLYPH_NAMES(face)) {
144 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
145 memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
148 font->layout->ascent = face->ascender*20/FT_SUBPIXELS; //face->bbox.xMin;
149 font->layout->descent = abs(face->descender)*20/FT_SUBPIXELS; //face->bbox.xMax;
150 font->layout->leading = abs(face->bbox.yMin - face->bbox.yMax); //-face->bbox.xMin*20/FT_SUBPIXELS;
151 font->layout->kerningcount = 0;
153 name = FT_Get_Postscript_Name(face);
155 font->name = (U8*)strdup(name);
157 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
159 for(t=0;t<65536;t++) {
160 int index = FT_Get_Char_Index(face, t);
161 if(index>=0 && index<face->num_glyphs) {
162 if(font->glyph2ascii[index]<0)
163 font->glyph2ascii[index] = t;
167 // Map Glyphs to Unicode, version 2 (much nicer):
168 // (The third way would be the AGL algorithm, as proposed
169 // by Werner Lemberg on freetype@freetype.org)
171 charcode = FT_Get_First_Char(face, &gindex);
174 if(gindex >= 0 && gindex<face->num_glyphs) {
175 if(!font->glyph2ascii[gindex]) {
176 font->glyph2ascii[gindex] = charcode;
177 if(charcode + 1 > font->maxascii) {
178 font->maxascii = charcode + 1;
182 charcode = FT_Get_Next_Char(face, charcode, &gindex);
185 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
187 for(t=0;t<font->maxascii;t++) {
188 int g = FT_Get_Char_Index(face, t);
189 if(!g || g>=face->num_glyphs)
191 font->ascii2glyph[t] = g;
193 if(!font->glyph2ascii[g]) {
194 font->glyph2ascii[g] = t;
201 glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
203 for(t=0; t < face->num_glyphs; t++) {
212 if(FT_HAS_GLYPH_NAMES(face)) {
213 error = FT_Get_Glyph_Name(face, t, name, 127);
214 if(!error && name[0] && !strstr(name, "notdef")) {
215 font->glyphnames[font->numchars] = strdup(name);
219 if(!font->glyph2ascii[t] && !hasname && skip_unused) {
222 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP);
224 fprintf(stderr, "Couldn't load glyph %d, error:%d\n", t, error);
227 error = FT_Get_Glyph(face->glyph, &glyph);
229 fprintf(stderr, "Couldn't get glyph %d, error:%d\n", t, error);
233 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
234 bbox.yMin = -bbox.yMin;
235 bbox.yMax = -bbox.yMax;
236 if(bbox.xMax < bbox.xMin) {
238 bbox.xMax ^= bbox.xMin;
239 bbox.xMin ^= bbox.xMax;
240 bbox.xMax ^= bbox.xMin;
242 if(bbox.yMax < bbox.yMin) {
244 bbox.yMax ^= bbox.yMin;
245 bbox.yMin ^= bbox.yMax;
246 bbox.yMax ^= bbox.yMin;
249 swf_Shape01DrawerInit(&draw, 0);
251 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
252 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
256 fprintf(stderr, "Couldn't decompose glyph %d\n", t);
265 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
267 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
270 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
273 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
275 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
276 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
277 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
278 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
282 FT_Done_Glyph(glyph);
283 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
284 glyph2glyph[t] = font->numchars;
287 /* notice: if skip_unused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
288 have more memory allocated than just font->numchars, but only the first font->numchars
291 for(t=0;t<font->maxascii;t++) {
292 if(font->ascii2glyph[t]>=0) {
293 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
299 FT_Done_FreeType(ftlibrary);ftlibrary=0;
303 #else //HAVE_FREETYPE
305 SWFFONT* swf_LoadTrueTypeFont(char*filename)
307 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
317 static int t1lib_initialized = 0;
319 static int counter = 0;
321 SWFFONT* swf_LoadT1Font(char*filename)
325 float angle,underline;
326 char*fontname,*fullname,*familyname;
335 if(!t1lib_initialized) {
337 if ((T1_InitLib(NO_LOGFILE)==NULL)){
338 fprintf(stderr, "Initialization of t1lib failed\n");
341 t1lib_initialized = 1;
343 nr = T1_AddFont(filename);
346 charnames = T1_GetAllCharNames(nr);
348 fprintf(stderr, "No Charnames record- not a Type1 Font?\n");
352 angle = T1_GetItalicAngle(nr);
353 fontname = T1_GetFontName(nr);
354 fullname = T1_GetFullName(nr);
355 familyname = T1_GetFamilyName(nr);
356 underline = T1_GetUnderlinePosition(nr);
357 bbox = T1_GetFontBBox(nr);
359 font = (SWFFONT*)malloc(sizeof(SWFFONT));
360 memset(font, 0, sizeof(SWFFONT));
364 font->name = (U8*)strdup(fontname);
367 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
368 memset(font->layout, 0, sizeof(SWFLAYOUT));
371 charname = charnames;
375 if(*charname) encoding[num] = strdup(*charname);
376 else encoding[num] = strdup(".notdef");
381 encoding[t] = strdup(".notdef");
383 //T1_ReencodeFont(nr, encoding);
385 font->maxascii = num;
386 font->numchars = num;
388 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
390 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
391 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
392 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
393 memset(font->glyph2ascii, 0, num*sizeof(U16));
394 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
395 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
396 font->layout->ascent = (U16)(underline - bbox.lly);
397 font->layout->descent = (U16)(bbox.ury - underline);
398 font->layout->leading = (U16)(font->layout->ascent -
399 font->layout->descent -
400 (bbox.lly - bbox.ury));
401 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
402 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
403 font->layout->kerningcount = 0;
404 font->layout->kerning = 0;
405 font->glyphnames = malloc(num*sizeof(char*));
406 memset(font->glyphnames, 0, num*sizeof(char*));
410 charname = charnames;
411 for(c=0;c<font->numchars;c++) {
414 T1_OUTLINE * outline;
418 outline = T1_GetCharOutline(nr, c, 100.0, 0);
419 firstx = outline->dest.x/0xffff;
425 font->glyphnames[c] = strdup(*charname);
428 font->ascii2glyph[c] = c;
429 font->glyph2ascii[c] = c;
431 swf_Shape01DrawerInit(&draw, 0);
434 pos.x += (outline->dest.x/(float)0xffff);
435 pos.y += (outline->dest.y/(float)0xffff);
437 if(outline->type == T1_PATHTYPE_MOVE) {
438 draw.moveTo(&draw,&pos);
439 } else if(outline->type == T1_PATHTYPE_LINE) {
440 draw.lineTo(&draw,&pos);
441 } else if(outline->type == T1_PATHTYPE_BEZIER) {
442 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
444 b.x = o2->B.x/(float)0xffff+last.x;
445 b.y = o2->B.y/(float)0xffff+last.y;
446 c.x = o2->C.x/(float)0xffff+last.x;
447 c.y = o2->C.y/(float)0xffff+last.y;
448 draw_cubicTo(&draw,&b,&c,&pos);
450 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
453 outline = outline->link;
454 printf("(%f,%f) ", pos.x, pos.y);
460 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
461 bbox = swf_ShapeDrawerGetBBox(&draw);
464 font->layout->bounds[c] = bbox;
465 font->glyph[c].advance = bbox.xmax;
466 if(!font->glyph[c].advance) {
467 font->glyph[c].advance = firstx;
480 SWFFONT* swf_LoadT1Font(char*filename)
482 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
488 SWFFONT* swf_DummyFont()
490 SWFFONT*font = (SWFFONT*)malloc(sizeof(SWFFONT));
491 memset(font, 0, sizeof(SWFFONT));
495 static int isSWF(const char*filename)
497 FILE*fi = fopen(filename, "rb");
503 memset(a, 0, sizeof(a));
507 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
513 SWFFONT* swf_LoadFont(char*filename)
517 return swf_DummyFont();
518 is_swf = isSWF(filename);
522 return swf_ReadFont(filename);
525 #if defined(HAVE_FREETYPE)
526 return swf_LoadTrueTypeFont(filename);
527 #elif defined(HAVE_T1LIB)
528 return swf_LoadT1Font(filename);
530 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);