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 #ifdef HAVE_FREETYPE_FT2BUILD_H
38 #include <freetype/ft2build.h>
39 #include FT_FREETYPE_H
41 #include <freetype/freetype.h>
42 #include <freetype/ftglyph.h>
43 #include <freetype/ftsizes.h>
44 #include <freetype/ftsnames.h>
45 #include <freetype/ttnameid.h>
46 #include <freetype/ftoutln.h>
50 #define FT_SUBPIXELS 64
52 static int ft_move_to(FT_Vector* _to, void* user)
54 drawer_t* draw = (drawer_t*)user;
56 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
57 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
58 draw->moveTo(draw, &to);
61 static int ft_line_to(FT_Vector* _to, void* user)
63 drawer_t* draw = (drawer_t*)user;
65 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
66 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
67 draw->lineTo(draw, &to);
70 static int ft_cubic_to(FT_Vector* _c1, FT_Vector* _c2, FT_Vector* _to, void* user)
72 drawer_t* draw = (drawer_t*)user;
74 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
75 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
76 c1.x = _c1->x*FT_SCALE/(float)FT_SUBPIXELS;
77 c1.y = -_c1->y*FT_SCALE/(float)FT_SUBPIXELS;
78 c2.x = _c2->x*FT_SCALE/(float)FT_SUBPIXELS;
79 c2.y = -_c2->y*FT_SCALE/(float)FT_SUBPIXELS;
80 draw_cubicTo(draw, &c1, &c2, &to);
83 static int ft_conic_to(FT_Vector* _c, FT_Vector* _to, void* user)
85 drawer_t* draw = (drawer_t*)user;
87 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
88 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
89 c.x = _c->x*FT_SCALE/(float)FT_SUBPIXELS;
90 c.y = -_c->y*FT_SCALE/(float)FT_SUBPIXELS;
91 draw_conicTo(draw, &c, &to);
94 static FT_Outline_Funcs outline_functions =
103 static FT_Library ftlibrary = 0;
105 SWFFONT* swf_LoadTrueTypeFont(char*filename)
109 const char* name = 0;
120 if(FT_Init_FreeType(&ftlibrary)) {
121 fprintf(stderr, "Couldn't init freetype library!\n");
125 error = FT_New_Face(ftlibrary, filename, 0, &face);
126 FT_Set_Pixel_Sizes (face, 16*loadfont_scale, 16*loadfont_scale);
129 fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
132 if(face->num_glyphs <= 0) {
133 fprintf(stderr, "File %s contains %d glyphs\n", face->num_glyphs);
137 font = malloc(sizeof(SWFFONT));
138 memset(font, 0, sizeof(SWFFONT));
141 font->layout = malloc(sizeof(SWFLAYOUT));
142 memset(font->layout, 0, sizeof(SWFLAYOUT));
143 font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
144 font->style = ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
145 |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
146 font->encoding = FONT_ENCODING_UNICODE;
147 font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
148 memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
150 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
151 memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
152 if(FT_HAS_GLYPH_NAMES(face)) {
153 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
154 memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
157 font->layout->ascent = abs(face->ascender)*FT_SCALE*loadfont_scale*20/FT_SUBPIXELS/2; //face->bbox.xMin;
158 font->layout->descent = abs(face->descender)*FT_SCALE*loadfont_scale*20/FT_SUBPIXELS/2; //face->bbox.xMax;
159 font->layout->leading = font->layout->ascent + font->layout->descent;
160 font->layout->kerningcount = 0;
162 name = FT_Get_Postscript_Name(face);
164 font->name = (U8*)strdup(name);
168 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
170 for(t=0;t<65536;t++) {
171 int index = FT_Get_Char_Index(face, t);
172 if(index>=0 && index<face->num_glyphs) {
173 if(font->glyph2ascii[index]<0)
174 font->glyph2ascii[index] = t;
178 // Map Glyphs to Unicode, version 2 (much nicer):
179 // (The third way would be the AGL algorithm, as proposed
180 // by Werner Lemberg on freetype@freetype.org)
182 charcode = FT_Get_First_Char(face, &gindex);
185 if(gindex >= 0 && gindex<face->num_glyphs) {
186 if(!font->glyph2ascii[gindex]) {
187 font->glyph2ascii[gindex] = charcode;
188 if(charcode + 1 > font->maxascii) {
189 font->maxascii = charcode + 1;
193 charcode = FT_Get_Next_Char(face, charcode, &gindex);
196 /* if we didn't find a single encoding character, try
197 the font's charmaps instead. That usually means that
198 the encoding is no longer unicode.
199 TODO: find a way to convert the encoding to unicode
201 if(font->maxascii == 0 && charmap < face->num_charmaps - 1) {
203 FT_Set_Charmap(face, face->charmaps[charmap]);
204 font->encoding = 0;//anything but unicode FIXME
210 font->maxascii = 65535;
212 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
214 for(t=0;t<font->maxascii;t++) {
215 int g = FT_Get_Char_Index(face, t);
216 if(!g || g>=face->num_glyphs)
218 font->ascii2glyph[t] = g;
221 if(!font->glyph2ascii[g]) {
222 font->glyph2ascii[g] = t;
226 font->maxascii = max_unicode;
230 glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
232 for(t=0; t < face->num_glyphs; t++) {
241 if(FT_HAS_GLYPH_NAMES(face)) {
242 error = FT_Get_Glyph_Name(face, t, name, 127);
243 if(!error && name[0] && !strstr(name, "notdef")) {
244 font->glyphnames[font->numchars] = strdup(name);
248 if(!font->glyph2ascii[t] && !hasname && skip_unused) {
251 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP);
253 fprintf(stderr, "Couldn't load glyph %d, error:%d\n", t, error);
256 error = FT_Get_Glyph(face->glyph, &glyph);
258 fprintf(stderr, "Couldn't get glyph %d, error:%d\n", t, error);
262 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
263 bbox.yMin = -bbox.yMin;
264 bbox.yMax = -bbox.yMax;
265 if(bbox.xMax < bbox.xMin) {
267 bbox.xMax ^= bbox.xMin;
268 bbox.xMin ^= bbox.xMax;
269 bbox.xMax ^= bbox.xMin;
271 if(bbox.yMax < bbox.yMin) {
273 bbox.yMax ^= bbox.yMin;
274 bbox.yMin ^= bbox.yMax;
275 bbox.yMax ^= bbox.yMin;
278 swf_Shape01DrawerInit(&draw, 0);
280 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
281 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
285 fprintf(stderr, "Couldn't decompose glyph %d\n", t);
292 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
294 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
297 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
300 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
302 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
303 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
304 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
305 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
309 FT_Done_Glyph(glyph);
310 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
311 glyph2glyph[t] = font->numchars;
314 /* notice: if skip_unused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
315 have more memory allocated than just font->numchars, but only the first font->numchars
318 for(t=0;t<font->maxascii;t++) {
319 if(font->ascii2glyph[t]>=0) {
320 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
326 FT_Done_FreeType(ftlibrary);ftlibrary=0;
330 #else //HAVE_FREETYPE
332 SWFFONT* swf_LoadTrueTypeFont(char*filename)
334 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
344 static int t1lib_initialized = 0;
346 static int counter = 0;
348 SWFFONT* swf_LoadT1Font(char*filename)
352 float angle,underline;
353 char*fontname,*fullname,*familyname;
362 if(!t1lib_initialized) {
364 if ((T1_InitLib(NO_LOGFILE)==NULL)){
365 fprintf(stderr, "Initialization of t1lib failed\n");
368 t1lib_initialized = 1;
370 nr = T1_AddFont(filename);
373 charnames = T1_GetAllCharNames(nr);
375 fprintf(stderr, "No Charnames record- not a Type1 Font?\n");
379 angle = T1_GetItalicAngle(nr);
380 fontname = T1_GetFontName(nr);
381 fullname = T1_GetFullName(nr);
382 familyname = T1_GetFamilyName(nr);
383 underline = T1_GetUnderlinePosition(nr);
384 bbox = T1_GetFontBBox(nr);
386 font = (SWFFONT*)malloc(sizeof(SWFFONT));
387 memset(font, 0, sizeof(SWFFONT));
391 font->name = (U8*)strdup(fontname);
394 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
395 memset(font->layout, 0, sizeof(SWFLAYOUT));
398 charname = charnames;
402 if(*charname) encoding[num] = strdup(*charname);
403 else encoding[num] = strdup(".notdef");
408 encoding[t] = strdup(".notdef");
410 //T1_ReencodeFont(nr, encoding);
412 font->maxascii = num;
413 font->numchars = num;
415 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
417 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
418 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
419 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
420 memset(font->glyph2ascii, 0, num*sizeof(U16));
421 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
422 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
423 font->layout->ascent = (U16)(underline - bbox.lly);
424 font->layout->descent = (U16)(bbox.ury - underline);
425 font->layout->leading = (U16)(font->layout->ascent -
426 font->layout->descent -
427 (bbox.lly - bbox.ury));
428 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
429 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
430 font->layout->kerningcount = 0;
431 font->layout->kerning = 0;
432 font->glyphnames = malloc(num*sizeof(char*));
433 memset(font->glyphnames, 0, num*sizeof(char*));
437 charname = charnames;
438 for(c=0;c<font->numchars;c++) {
441 T1_OUTLINE * outline;
445 outline = T1_GetCharOutline(nr, c, 100.0, 0);
446 firstx = outline->dest.x/0xffff;
452 font->glyphnames[c] = strdup(*charname);
455 font->ascii2glyph[c] = c;
456 font->glyph2ascii[c] = c;
458 swf_Shape01DrawerInit(&draw, 0);
461 pos.x += (outline->dest.x/(float)0xffff);
462 pos.y += (outline->dest.y/(float)0xffff);
464 if(outline->type == T1_PATHTYPE_MOVE) {
465 draw.moveTo(&draw,&pos);
466 } else if(outline->type == T1_PATHTYPE_LINE) {
467 draw.lineTo(&draw,&pos);
468 } else if(outline->type == T1_PATHTYPE_BEZIER) {
469 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
471 b.x = o2->B.x/(float)0xffff+last.x;
472 b.y = o2->B.y/(float)0xffff+last.y;
473 c.x = o2->C.x/(float)0xffff+last.x;
474 c.y = o2->C.y/(float)0xffff+last.y;
475 draw_cubicTo(&draw,&b,&c,&pos);
477 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
480 outline = outline->link;
481 printf("(%f,%f) ", pos.x, pos.y);
487 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
488 bbox = swf_ShapeDrawerGetBBox(&draw);
491 font->layout->bounds[c] = bbox;
492 font->glyph[c].advance = bbox.xmax;
493 if(!font->glyph[c].advance) {
494 font->glyph[c].advance = firstx;
507 SWFFONT* swf_LoadT1Font(char*filename)
509 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
515 SWFFONT* swf_DummyFont()
517 SWFFONT*font = (SWFFONT*)malloc(sizeof(SWFFONT));
518 memset(font, 0, sizeof(SWFFONT));
522 static int isSWF(const char*filename)
524 FILE*fi = fopen(filename, "rb");
530 memset(a, 0, sizeof(a));
534 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
540 SWFFONT* swf_LoadFont(char*filename)
544 return swf_DummyFont();
545 is_swf = isSWF(filename);
549 return swf_ReadFont(filename);
552 #if defined(HAVE_FREETYPE)
553 return swf_LoadTrueTypeFont(filename);
554 #elif defined(HAVE_T1LIB)
555 return swf_LoadT1Font(filename);
557 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);