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;
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>
44 #define FT_SCALE loadfont_scale
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/(float)FT_SUBPIXELS;
52 to.y = -_to->y/(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/(float)FT_SUBPIXELS;
61 to.y = -_to->y/(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/(float)FT_SUBPIXELS;
70 to.y = -_to->y/(float)FT_SUBPIXELS;
71 c1.x = _c1->x/(float)FT_SUBPIXELS;
72 c1.y = -_c1->y/(float)FT_SUBPIXELS;
73 c2.x = _c2->x/(float)FT_SUBPIXELS;
74 c2.y = -_c2->y/(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/(float)FT_SUBPIXELS;
83 to.y = -_to->y/(float)FT_SUBPIXELS;
84 c.x = _c->x/(float)FT_SUBPIXELS;
85 c.y = -_c->y/(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;
114 if(FT_Init_FreeType(&ftlibrary)) {
115 fprintf(stderr, "Couldn't init freetype library!\n");
119 error = FT_New_Face(ftlibrary, filename, 0, &face);
120 FT_Set_Pixel_Sizes (face, 16*FT_SCALE, 16*FT_SCALE);
123 fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
126 if(face->num_glyphs <= 0) {
127 fprintf(stderr, "File %s contains %d glyphs\n", face->num_glyphs);
131 font = malloc(sizeof(SWFFONT));
132 memset(font, 0, sizeof(SWFFONT));
135 font->layout = malloc(sizeof(SWFLAYOUT));
136 memset(font->layout, 0, sizeof(SWFLAYOUT));
137 font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
138 font->style = ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
139 |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
140 font->encoding = FONT_ENCODING_UNICODE;
141 font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
142 memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
144 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
145 memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
146 if(FT_HAS_GLYPH_NAMES(face)) {
147 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
148 memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
151 font->layout->ascent = face->ascender*20/FT_SUBPIXELS; //face->bbox.xMin;
152 font->layout->descent = abs(face->descender)*20/FT_SUBPIXELS; //face->bbox.xMax;
153 font->layout->leading = abs(face->bbox.yMin - face->bbox.yMax); //-face->bbox.xMin*20/FT_SUBPIXELS;
154 font->layout->kerningcount = 0;
156 name = FT_Get_Postscript_Name(face);
158 font->name = (U8*)strdup(name);
160 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
162 for(t=0;t<65536;t++) {
163 int index = FT_Get_Char_Index(face, t);
164 if(index>=0 && index<face->num_glyphs) {
165 if(font->glyph2ascii[index]<0)
166 font->glyph2ascii[index] = t;
170 // Map Glyphs to Unicode, version 2 (much nicer):
171 // (The third way would be the AGL algorithm, as proposed
172 // by Werner Lemberg on freetype@freetype.org)
174 charcode = FT_Get_First_Char(face, &gindex);
177 if(gindex >= 0 && gindex<face->num_glyphs) {
178 if(!font->glyph2ascii[gindex]) {
179 font->glyph2ascii[gindex] = charcode;
180 if(charcode + 1 > font->maxascii) {
181 font->maxascii = charcode + 1;
185 charcode = FT_Get_Next_Char(face, charcode, &gindex);
189 font->maxascii = 65535;
191 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
193 for(t=0;t<font->maxascii;t++) {
194 int g = FT_Get_Char_Index(face, t);
195 if(!g || g>=face->num_glyphs)
197 font->ascii2glyph[t] = g;
200 if(!font->glyph2ascii[g]) {
201 font->glyph2ascii[g] = t;
205 font->maxascii = max_unicode;
209 glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
211 for(t=0; t < face->num_glyphs; t++) {
220 if(FT_HAS_GLYPH_NAMES(face)) {
221 error = FT_Get_Glyph_Name(face, t, name, 127);
222 if(!error && name[0] && !strstr(name, "notdef")) {
223 font->glyphnames[font->numchars] = strdup(name);
227 if(!font->glyph2ascii[t] && !hasname && skip_unused) {
230 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP);
232 fprintf(stderr, "Couldn't load glyph %d, error:%d\n", t, error);
235 error = FT_Get_Glyph(face->glyph, &glyph);
237 fprintf(stderr, "Couldn't get glyph %d, error:%d\n", t, error);
241 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
242 bbox.yMin = -bbox.yMin;
243 bbox.yMax = -bbox.yMax;
244 if(bbox.xMax < bbox.xMin) {
246 bbox.xMax ^= bbox.xMin;
247 bbox.xMin ^= bbox.xMax;
248 bbox.xMax ^= bbox.xMin;
250 if(bbox.yMax < bbox.yMin) {
252 bbox.yMax ^= bbox.yMin;
253 bbox.yMin ^= bbox.yMax;
254 bbox.yMax ^= bbox.yMin;
257 swf_Shape01DrawerInit(&draw, 0);
259 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
260 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
264 fprintf(stderr, "Couldn't decompose glyph %d\n", t);
273 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
275 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
278 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
281 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
283 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
284 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
285 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
286 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
290 FT_Done_Glyph(glyph);
291 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
292 glyph2glyph[t] = font->numchars;
295 /* notice: if skip_unused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
296 have more memory allocated than just font->numchars, but only the first font->numchars
299 for(t=0;t<font->maxascii;t++) {
300 if(font->ascii2glyph[t]>=0) {
301 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
307 FT_Done_FreeType(ftlibrary);ftlibrary=0;
311 #else //HAVE_FREETYPE
313 SWFFONT* swf_LoadTrueTypeFont(char*filename)
315 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
325 static int t1lib_initialized = 0;
327 static int counter = 0;
329 SWFFONT* swf_LoadT1Font(char*filename)
333 float angle,underline;
334 char*fontname,*fullname,*familyname;
343 if(!t1lib_initialized) {
345 if ((T1_InitLib(NO_LOGFILE)==NULL)){
346 fprintf(stderr, "Initialization of t1lib failed\n");
349 t1lib_initialized = 1;
351 nr = T1_AddFont(filename);
354 charnames = T1_GetAllCharNames(nr);
356 fprintf(stderr, "No Charnames record- not a Type1 Font?\n");
360 angle = T1_GetItalicAngle(nr);
361 fontname = T1_GetFontName(nr);
362 fullname = T1_GetFullName(nr);
363 familyname = T1_GetFamilyName(nr);
364 underline = T1_GetUnderlinePosition(nr);
365 bbox = T1_GetFontBBox(nr);
367 font = (SWFFONT*)malloc(sizeof(SWFFONT));
368 memset(font, 0, sizeof(SWFFONT));
372 font->name = (U8*)strdup(fontname);
375 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
376 memset(font->layout, 0, sizeof(SWFLAYOUT));
379 charname = charnames;
383 if(*charname) encoding[num] = strdup(*charname);
384 else encoding[num] = strdup(".notdef");
389 encoding[t] = strdup(".notdef");
391 //T1_ReencodeFont(nr, encoding);
393 font->maxascii = num;
394 font->numchars = num;
396 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
398 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
399 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
400 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
401 memset(font->glyph2ascii, 0, num*sizeof(U16));
402 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
403 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
404 font->layout->ascent = (U16)(underline - bbox.lly);
405 font->layout->descent = (U16)(bbox.ury - underline);
406 font->layout->leading = (U16)(font->layout->ascent -
407 font->layout->descent -
408 (bbox.lly - bbox.ury));
409 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
410 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
411 font->layout->kerningcount = 0;
412 font->layout->kerning = 0;
413 font->glyphnames = malloc(num*sizeof(char*));
414 memset(font->glyphnames, 0, num*sizeof(char*));
418 charname = charnames;
419 for(c=0;c<font->numchars;c++) {
422 T1_OUTLINE * outline;
426 outline = T1_GetCharOutline(nr, c, 100.0, 0);
427 firstx = outline->dest.x/0xffff;
433 font->glyphnames[c] = strdup(*charname);
436 font->ascii2glyph[c] = c;
437 font->glyph2ascii[c] = c;
439 swf_Shape01DrawerInit(&draw, 0);
442 pos.x += (outline->dest.x/(float)0xffff);
443 pos.y += (outline->dest.y/(float)0xffff);
445 if(outline->type == T1_PATHTYPE_MOVE) {
446 draw.moveTo(&draw,&pos);
447 } else if(outline->type == T1_PATHTYPE_LINE) {
448 draw.lineTo(&draw,&pos);
449 } else if(outline->type == T1_PATHTYPE_BEZIER) {
450 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
452 b.x = o2->B.x/(float)0xffff+last.x;
453 b.y = o2->B.y/(float)0xffff+last.y;
454 c.x = o2->C.x/(float)0xffff+last.x;
455 c.y = o2->C.y/(float)0xffff+last.y;
456 draw_cubicTo(&draw,&b,&c,&pos);
458 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
461 outline = outline->link;
462 printf("(%f,%f) ", pos.x, pos.y);
468 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
469 bbox = swf_ShapeDrawerGetBBox(&draw);
472 font->layout->bounds[c] = bbox;
473 font->glyph[c].advance = bbox.xmax;
474 if(!font->glyph[c].advance) {
475 font->glyph[c].advance = firstx;
488 SWFFONT* swf_LoadT1Font(char*filename)
490 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
496 SWFFONT* swf_DummyFont()
498 SWFFONT*font = (SWFFONT*)malloc(sizeof(SWFFONT));
499 memset(font, 0, sizeof(SWFFONT));
503 static int isSWF(const char*filename)
505 FILE*fi = fopen(filename, "rb");
511 memset(a, 0, sizeof(a));
515 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
521 SWFFONT* swf_LoadFont(char*filename)
525 return swf_DummyFont();
526 is_swf = isSWF(filename);
530 return swf_ReadFont(filename);
533 #if defined(HAVE_FREETYPE)
534 return swf_LoadTrueTypeFont(filename);
535 #elif defined(HAVE_T1LIB)
536 return swf_LoadT1Font(filename);
538 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);