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 = 4;
25 static int skip_unused = 1;
26 static int full_unicode = 0;
28 void swf_SetLoadFontParameters(int _scale, int _skip_unused, int _full_unicode)
30 if(_scale) loadfont_scale = _scale;
31 skip_unused = _skip_unused;
32 full_unicode = _full_unicode;
37 #include <freetype/freetype.h>
38 #include <freetype/ftglyph.h>
39 #include <freetype/ftsizes.h>
40 #include <freetype/ftsnames.h>
41 #include <freetype/ttnameid.h>
42 #include <freetype/ftoutln.h>
45 #define FT_SUBPIXELS 64
47 static int ft_move_to(FT_Vector* _to, void* user)
49 drawer_t* draw = (drawer_t*)user;
51 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
52 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
53 draw->moveTo(draw, &to);
56 static int ft_line_to(FT_Vector* _to, void* user)
58 drawer_t* draw = (drawer_t*)user;
60 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
61 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
62 draw->lineTo(draw, &to);
65 static int ft_cubic_to(FT_Vector* _c1, FT_Vector* _c2, FT_Vector* _to, void* user)
67 drawer_t* draw = (drawer_t*)user;
69 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
70 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
71 c1.x = _c1->x*FT_SCALE/(float)FT_SUBPIXELS;
72 c1.y = -_c1->y*FT_SCALE/(float)FT_SUBPIXELS;
73 c2.x = _c2->x*FT_SCALE/(float)FT_SUBPIXELS;
74 c2.y = -_c2->y*FT_SCALE/(float)FT_SUBPIXELS;
75 draw_cubicTo(draw, &c1, &c2, &to);
78 static int ft_conic_to(FT_Vector* _c, FT_Vector* _to, void* user)
80 drawer_t* draw = (drawer_t*)user;
82 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
83 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
84 c.x = _c->x*FT_SCALE/(float)FT_SUBPIXELS;
85 c.y = -_c->y*FT_SCALE/(float)FT_SUBPIXELS;
86 draw_conicTo(draw, &c, &to);
89 static FT_Outline_Funcs outline_functions =
98 static FT_Library ftlibrary = 0;
100 SWFFONT* swf_LoadTrueTypeFont(char*filename)
104 const char* name = 0;
115 if(FT_Init_FreeType(&ftlibrary)) {
116 fprintf(stderr, "Couldn't init freetype library!\n");
120 error = FT_New_Face(ftlibrary, filename, 0, &face);
121 FT_Set_Pixel_Sizes (face, 16*loadfont_scale, 16*loadfont_scale);
124 fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
127 if(face->num_glyphs <= 0) {
128 fprintf(stderr, "File %s contains %d glyphs\n", face->num_glyphs);
132 font = malloc(sizeof(SWFFONT));
133 memset(font, 0, sizeof(SWFFONT));
136 font->layout = malloc(sizeof(SWFLAYOUT));
137 memset(font->layout, 0, sizeof(SWFLAYOUT));
138 font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
139 font->style = ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
140 |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
141 font->encoding = FONT_ENCODING_UNICODE;
142 font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
143 memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
145 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
146 memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
147 if(FT_HAS_GLYPH_NAMES(face)) {
148 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
149 memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
152 font->layout->ascent = abs(face->ascender)*FT_SCALE*loadfont_scale*20/FT_SUBPIXELS/2; //face->bbox.xMin;
153 font->layout->descent = abs(face->descender)*FT_SCALE*loadfont_scale*20/FT_SUBPIXELS/2; //face->bbox.xMax;
154 font->layout->leading = font->layout->ascent + font->layout->descent;
155 font->layout->kerningcount = 0;
157 name = FT_Get_Postscript_Name(face);
159 font->name = (U8*)strdup(name);
163 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
165 for(t=0;t<65536;t++) {
166 int index = FT_Get_Char_Index(face, t);
167 if(index>=0 && index<face->num_glyphs) {
168 if(font->glyph2ascii[index]<0)
169 font->glyph2ascii[index] = t;
173 // Map Glyphs to Unicode, version 2 (much nicer):
174 // (The third way would be the AGL algorithm, as proposed
175 // by Werner Lemberg on freetype@freetype.org)
177 charcode = FT_Get_First_Char(face, &gindex);
180 if(gindex >= 0 && gindex<face->num_glyphs) {
181 if(!font->glyph2ascii[gindex]) {
182 font->glyph2ascii[gindex] = charcode;
183 if(charcode + 1 > font->maxascii) {
184 font->maxascii = charcode + 1;
188 charcode = FT_Get_Next_Char(face, charcode, &gindex);
191 /* if we didn't find a single encoding character, try
192 the font's charmaps instead. That usually means that
193 the encoding is no longer unicode.
194 TODO: find a way to convert the encoding to unicode
196 if(font->maxascii == 0 && charmap < face->num_charmaps - 1) {
198 FT_Set_Charmap(face, face->charmaps[charmap]);
199 font->encoding = 0;//anything but unicode FIXME
205 font->maxascii = 65535;
207 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
209 for(t=0;t<font->maxascii;t++) {
210 int g = FT_Get_Char_Index(face, t);
211 if(!g || g>=face->num_glyphs)
213 font->ascii2glyph[t] = g;
216 if(!font->glyph2ascii[g]) {
217 font->glyph2ascii[g] = t;
221 font->maxascii = max_unicode;
225 glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
227 for(t=0; t < face->num_glyphs; t++) {
236 if(FT_HAS_GLYPH_NAMES(face)) {
237 error = FT_Get_Glyph_Name(face, t, name, 127);
238 if(!error && name[0] && !strstr(name, "notdef")) {
239 font->glyphnames[font->numchars] = strdup(name);
243 if(!font->glyph2ascii[t] && !hasname && skip_unused) {
246 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP);
248 fprintf(stderr, "Couldn't load glyph %d, error:%d\n", t, error);
251 error = FT_Get_Glyph(face->glyph, &glyph);
253 fprintf(stderr, "Couldn't get glyph %d, error:%d\n", t, error);
257 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
258 bbox.yMin = -bbox.yMin;
259 bbox.yMax = -bbox.yMax;
260 if(bbox.xMax < bbox.xMin) {
262 bbox.xMax ^= bbox.xMin;
263 bbox.xMin ^= bbox.xMax;
264 bbox.xMax ^= bbox.xMin;
266 if(bbox.yMax < bbox.yMin) {
268 bbox.yMax ^= bbox.yMin;
269 bbox.yMin ^= bbox.yMax;
270 bbox.yMax ^= bbox.yMin;
273 swf_Shape01DrawerInit(&draw, 0);
275 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
276 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
280 fprintf(stderr, "Couldn't decompose glyph %d\n", t);
287 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
289 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
292 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
295 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
297 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
298 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
299 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
300 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
304 FT_Done_Glyph(glyph);
305 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
306 glyph2glyph[t] = font->numchars;
309 /* notice: if skip_unused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
310 have more memory allocated than just font->numchars, but only the first font->numchars
313 for(t=0;t<font->maxascii;t++) {
314 if(font->ascii2glyph[t]>=0) {
315 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
321 FT_Done_FreeType(ftlibrary);ftlibrary=0;
325 #else //HAVE_FREETYPE
327 SWFFONT* swf_LoadTrueTypeFont(char*filename)
329 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
339 static int t1lib_initialized = 0;
341 static int counter = 0;
343 SWFFONT* swf_LoadT1Font(char*filename)
347 float angle,underline;
348 char*fontname,*fullname,*familyname;
357 if(!t1lib_initialized) {
359 if ((T1_InitLib(NO_LOGFILE)==NULL)){
360 fprintf(stderr, "Initialization of t1lib failed\n");
363 t1lib_initialized = 1;
365 nr = T1_AddFont(filename);
368 charnames = T1_GetAllCharNames(nr);
370 fprintf(stderr, "No Charnames record- not a Type1 Font?\n");
374 angle = T1_GetItalicAngle(nr);
375 fontname = T1_GetFontName(nr);
376 fullname = T1_GetFullName(nr);
377 familyname = T1_GetFamilyName(nr);
378 underline = T1_GetUnderlinePosition(nr);
379 bbox = T1_GetFontBBox(nr);
381 font = (SWFFONT*)malloc(sizeof(SWFFONT));
382 memset(font, 0, sizeof(SWFFONT));
386 font->name = (U8*)strdup(fontname);
389 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
390 memset(font->layout, 0, sizeof(SWFLAYOUT));
393 charname = charnames;
397 if(*charname) encoding[num] = strdup(*charname);
398 else encoding[num] = strdup(".notdef");
403 encoding[t] = strdup(".notdef");
405 //T1_ReencodeFont(nr, encoding);
407 font->maxascii = num;
408 font->numchars = num;
410 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
412 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
413 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
414 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
415 memset(font->glyph2ascii, 0, num*sizeof(U16));
416 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
417 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
418 font->layout->ascent = (U16)(underline - bbox.lly);
419 font->layout->descent = (U16)(bbox.ury - underline);
420 font->layout->leading = (U16)(font->layout->ascent -
421 font->layout->descent -
422 (bbox.lly - bbox.ury));
423 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
424 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
425 font->layout->kerningcount = 0;
426 font->layout->kerning = 0;
427 font->glyphnames = malloc(num*sizeof(char*));
428 memset(font->glyphnames, 0, num*sizeof(char*));
432 charname = charnames;
433 for(c=0;c<font->numchars;c++) {
436 T1_OUTLINE * outline;
440 outline = T1_GetCharOutline(nr, c, 100.0, 0);
441 firstx = outline->dest.x/0xffff;
447 font->glyphnames[c] = strdup(*charname);
450 font->ascii2glyph[c] = c;
451 font->glyph2ascii[c] = c;
453 swf_Shape01DrawerInit(&draw, 0);
456 pos.x += (outline->dest.x/(float)0xffff);
457 pos.y += (outline->dest.y/(float)0xffff);
459 if(outline->type == T1_PATHTYPE_MOVE) {
460 draw.moveTo(&draw,&pos);
461 } else if(outline->type == T1_PATHTYPE_LINE) {
462 draw.lineTo(&draw,&pos);
463 } else if(outline->type == T1_PATHTYPE_BEZIER) {
464 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
466 b.x = o2->B.x/(float)0xffff+last.x;
467 b.y = o2->B.y/(float)0xffff+last.y;
468 c.x = o2->C.x/(float)0xffff+last.x;
469 c.y = o2->C.y/(float)0xffff+last.y;
470 draw_cubicTo(&draw,&b,&c,&pos);
472 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
475 outline = outline->link;
476 printf("(%f,%f) ", pos.x, pos.y);
482 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
483 bbox = swf_ShapeDrawerGetBBox(&draw);
486 font->layout->bounds[c] = bbox;
487 font->glyph[c].advance = bbox.xmax;
488 if(!font->glyph[c].advance) {
489 font->glyph[c].advance = firstx;
502 SWFFONT* swf_LoadT1Font(char*filename)
504 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
510 SWFFONT* swf_DummyFont()
512 SWFFONT*font = (SWFFONT*)malloc(sizeof(SWFFONT));
513 memset(font, 0, sizeof(SWFFONT));
517 static int isSWF(const char*filename)
519 FILE*fi = fopen(filename, "rb");
525 memset(a, 0, sizeof(a));
529 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
535 SWFFONT* swf_LoadFont(char*filename)
539 return swf_DummyFont();
540 is_swf = isSWF(filename);
544 return swf_ReadFont(filename);
547 #if defined(HAVE_FREETYPE)
548 return swf_LoadTrueTypeFont(filename);
549 #elif defined(HAVE_T1LIB)
550 return swf_LoadT1Font(filename);
552 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);