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) {
121 fprintf(stderr, "File %s contains %d glyphs\n", face->num_glyphs);
125 font = malloc(sizeof(SWFFONT));
126 memset(font, 0, sizeof(SWFFONT));
129 font->layout = malloc(sizeof(SWFLAYOUT));
130 memset(font->layout, 0, sizeof(SWFLAYOUT));
131 font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
132 font->style = ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
133 |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
134 font->encoding = FONT_ENCODING_UNICODE;
135 font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
136 memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
138 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
139 memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
140 if(FT_HAS_GLYPH_NAMES(face)) {
141 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
142 memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
145 font->layout->ascent = face->ascender*20/FT_SUBPIXELS; //face->bbox.xMin;
146 font->layout->descent = abs(face->descender)*20/FT_SUBPIXELS; //face->bbox.xMax;
147 font->layout->leading = abs(face->bbox.yMin - face->bbox.yMax); //-face->bbox.xMin*20/FT_SUBPIXELS;
148 font->layout->kerningcount = 0;
150 name = FT_Get_Postscript_Name(face);
152 font->name = (U8*)strdup(name);
154 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
156 for(t=0;t<65536;t++) {
157 int index = FT_Get_Char_Index(face, t);
158 if(index>=0 && index<face->num_glyphs) {
159 if(font->glyph2ascii[index]<0)
160 font->glyph2ascii[index] = t;
164 // Map Glyphs to Unicode, version 2 (much nicer):
165 // (The third way would be the AGL algorithm, as proposed
166 // by Werner Lemberg on freetype@freetype.org)
168 charcode = FT_Get_First_Char(face, &gindex);
171 if(gindex >= 0 && gindex<face->num_glyphs) {
172 if(!font->glyph2ascii[gindex]) {
173 font->glyph2ascii[gindex] = charcode;
174 if(charcode + 1 > font->maxascii) {
175 font->maxascii = charcode + 1;
179 charcode = FT_Get_Next_Char(face, charcode, &gindex);
182 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
184 for(t=0;t<font->maxascii;t++) {
185 int g = FT_Get_Char_Index(face, t);
186 if(!g || g>=face->num_glyphs)
188 font->ascii2glyph[t] = g;
190 if(!font->glyph2ascii[g]) {
191 font->glyph2ascii[g] = t;
198 glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
200 for(t=0; t < face->num_glyphs; t++) {
209 if(FT_HAS_GLYPH_NAMES(face)) {
210 error = FT_Get_Glyph_Name(face, t, name, 127);
211 if(!error && name[0] && !strstr(name, "notdef")) {
212 font->glyphnames[font->numchars] = strdup(name);
216 if(!font->glyph2ascii[t] && !hasname && skip_unused) {
219 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
221 fprintf(stderr, "Couldn't load glyph %d\n", t);
224 error = FT_Get_Glyph(face->glyph, &glyph);
226 fprintf(stderr, "Couldn't get glyph %d\n", t);
230 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
231 bbox.yMin = -bbox.yMin;
232 bbox.yMax = -bbox.yMax;
233 if(bbox.xMax < bbox.xMin) {
235 bbox.xMax ^= bbox.xMin;
236 bbox.xMin ^= bbox.xMax;
237 bbox.xMax ^= bbox.xMin;
239 if(bbox.yMax < bbox.yMin) {
241 bbox.yMax ^= bbox.yMin;
242 bbox.yMin ^= bbox.yMax;
243 bbox.yMax ^= bbox.yMin;
246 swf_Shape01DrawerInit(&draw, 0);
248 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
249 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
253 fprintf(stderr, "Couldn't decompose glyph %d\n", t);
262 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
264 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
267 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
270 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
272 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
273 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
274 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
275 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
279 FT_Done_Glyph(glyph);
280 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
281 glyph2glyph[t] = font->numchars;
284 /* notice: if skip_unused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
285 have more memory allocated than just font->numchars, but only the first font->numchars
288 for(t=0;t<font->maxascii;t++) {
289 if(font->ascii2glyph[t]>=0) {
290 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
296 FT_Done_FreeType(ftlibrary);ftlibrary=0;
300 #else //HAVE_FREETYPE
302 SWFFONT* swf_LoadTrueTypeFont(char*filename)
304 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
314 static int t1lib_initialized = 0;
316 static int counter = 0;
318 SWFFONT* swf_LoadT1Font(char*filename)
322 float angle,underline;
323 char*fontname,*fullname,*familyname;
330 if(!t1lib_initialized) {
332 if ((T1_InitLib(NO_LOGFILE)==NULL)){
333 fprintf(stderr, "Initialization of t1lib failed\n");
336 t1lib_initialized = 1;
338 nr = T1_AddFont(filename);
341 charnames = T1_GetAllCharNames(nr);
343 angle = T1_GetItalicAngle(nr);
344 fontname = T1_GetFontName(nr);
345 fullname = T1_GetFullName(nr);
346 familyname = T1_GetFamilyName(nr);
347 underline = T1_GetUnderlinePosition(nr);
348 bbox = T1_GetFontBBox(nr);
350 font = (SWFFONT*)malloc(sizeof(SWFFONT));
351 memset(font, 0, sizeof(SWFFONT));
355 font->name = (U8*)strdup(fontname);
358 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
359 memset(font->layout, 0, sizeof(SWFLAYOUT));
362 charname = charnames;
368 font->maxascii = num;
369 font->numchars = num;
371 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
373 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
374 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
375 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
376 memset(font->glyph2ascii, 0, num*sizeof(U16));
377 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
378 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
379 font->layout->ascent = (U16)(underline - bbox.lly);
380 font->layout->descent = (U16)(bbox.ury - underline);
381 font->layout->leading = (U16)(font->layout->ascent -
382 font->layout->descent -
383 (bbox.lly - bbox.ury));
384 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
385 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
386 font->layout->kerningcount = 0;
387 font->layout->kerning = 0;
388 font->glyphnames = malloc(num*sizeof(char*));
389 memset(font->glyphnames, 0, num*sizeof(char*));
393 charname = charnames;
394 for(c=0;c<font->numchars;c++) {
397 T1_OUTLINE * outline;
401 outline = T1_GetCharOutline(nr, c, 100.0, 0);
402 firstx = outline->dest.x/0xffff;
408 font->glyphnames[c] = strdup(*charname);
411 font->ascii2glyph[c] = c;
412 font->glyph2ascii[c] = c;
414 swf_Shape01DrawerInit(&draw, 0);
417 pos.x += (outline->dest.x/(float)0xffff);
418 pos.y += (outline->dest.y/(float)0xffff);
420 if(outline->type == T1_PATHTYPE_MOVE) {
421 draw.moveTo(&draw,&pos);
422 } else if(outline->type == T1_PATHTYPE_LINE) {
423 draw.lineTo(&draw,&pos);
424 } else if(outline->type == T1_PATHTYPE_BEZIER) {
425 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
427 b.x = o2->B.x/(float)0xffff+last.x;
428 b.y = o2->B.y/(float)0xffff+last.y;
429 c.x = o2->C.x/(float)0xffff+last.x;
430 c.y = o2->C.y/(float)0xffff+last.y;
431 draw_cubicTo(&draw,&b,&c,&pos);
433 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
436 outline = outline->link;
441 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
442 bbox = swf_ShapeDrawerGetBBox(&draw);
445 font->layout->bounds[c] = bbox;
446 font->glyph[c].advance = bbox.xmax;
447 if(!font->glyph[c].advance) {
448 font->glyph[c].advance = firstx;
457 SWFFONT* swf_LoadT1Font(char*filename)
459 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
465 SWFFONT* swf_DummyFont()
467 SWFFONT*font = (SWFFONT*)malloc(sizeof(SWFFONT));
468 memset(font, 0, sizeof(SWFFONT));
472 static int isSWF(const char*filename)
474 FILE*fi = fopen(filename, "rb");
480 memset(a, 0, sizeof(a));
484 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
490 SWFFONT* swf_LoadFont(char*filename)
494 return swf_DummyFont();
495 is_swf = isSWF(filename);
499 return swf_ReadFont(filename);
501 #if defined(HAVE_FREETYPE)
502 return swf_LoadTrueTypeFont(filename);
503 #elif defined(HAVE_T1LIB)
504 return swf_LoadT1Font(filename);
506 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);