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;
36 #include <freetype/freetype.h>
37 #include <freetype/ftglyph.h>
38 #include <freetype/ftsizes.h>
39 #include <freetype/ftsnames.h>
40 #include <freetype/ttnameid.h>
41 #include <freetype/ftoutln.h>
43 #define FT_SCALE loadfont_scale
44 #define FT_SUBPIXELS 64
46 static int ft_move_to(FT_Vector* _to, void* user)
48 drawer_t* draw = (drawer_t*)user;
50 to.x = _to->x/(float)FT_SUBPIXELS;
51 to.y = -_to->y/(float)FT_SUBPIXELS;
52 draw->moveTo(draw, &to);
55 static int ft_line_to(FT_Vector* _to, void* user)
57 drawer_t* draw = (drawer_t*)user;
59 to.x = _to->x/(float)FT_SUBPIXELS;
60 to.y = -_to->y/(float)FT_SUBPIXELS;
61 draw->lineTo(draw, &to);
64 static int ft_cubic_to(FT_Vector* _c1, FT_Vector* _c2, FT_Vector* _to, void* user)
66 drawer_t* draw = (drawer_t*)user;
68 to.x = _to->x/(float)FT_SUBPIXELS;
69 to.y = -_to->y/(float)FT_SUBPIXELS;
70 c1.x = _c1->x/(float)FT_SUBPIXELS;
71 c1.y = -_c1->y/(float)FT_SUBPIXELS;
72 c2.x = _c2->x/(float)FT_SUBPIXELS;
73 c2.y = -_c2->y/(float)FT_SUBPIXELS;
74 draw_cubicTo(draw, &c1, &c2, &to);
77 static int ft_conic_to(FT_Vector* _c, FT_Vector* _to, void* user)
79 drawer_t* draw = (drawer_t*)user;
81 to.x = _to->x/(float)FT_SUBPIXELS;
82 to.y = -_to->y/(float)FT_SUBPIXELS;
83 c.x = _c->x/(float)FT_SUBPIXELS;
84 c.y = -_c->y/(float)FT_SUBPIXELS;
85 draw_conicTo(draw, &c, &to);
88 static FT_Outline_Funcs outline_functions =
97 static FT_Library ftlibrary = 0;
99 SWFFONT* swf_LoadTrueTypeFont(char*filename)
103 const char* name = 0;
113 if(FT_Init_FreeType(&ftlibrary)) {
114 fprintf(stderr, "Couldn't init freetype library!\n");
118 error = FT_New_Face(ftlibrary, filename, 0, &face);
119 FT_Set_Pixel_Sizes (face, 16*FT_SCALE, 16*FT_SCALE);
122 fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
125 if(face->num_glyphs <= 0) {
126 fprintf(stderr, "File %s contains %d glyphs\n", face->num_glyphs);
130 font = malloc(sizeof(SWFFONT));
131 memset(font, 0, sizeof(SWFFONT));
134 font->layout = malloc(sizeof(SWFLAYOUT));
135 memset(font->layout, 0, sizeof(SWFLAYOUT));
136 font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
137 font->style = ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
138 |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
139 font->encoding = FONT_ENCODING_UNICODE;
140 font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
141 memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
143 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
144 memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
145 if(FT_HAS_GLYPH_NAMES(face)) {
146 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
147 memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
150 font->layout->ascent = face->ascender*20/FT_SUBPIXELS; //face->bbox.xMin;
151 font->layout->descent = abs(face->descender)*20/FT_SUBPIXELS; //face->bbox.xMax;
152 font->layout->leading = abs(face->bbox.yMin - face->bbox.yMax); //-face->bbox.xMin*20/FT_SUBPIXELS;
153 font->layout->kerningcount = 0;
155 name = FT_Get_Postscript_Name(face);
157 font->name = (U8*)strdup(name);
159 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
161 for(t=0;t<65536;t++) {
162 int index = FT_Get_Char_Index(face, t);
163 if(index>=0 && index<face->num_glyphs) {
164 if(font->glyph2ascii[index]<0)
165 font->glyph2ascii[index] = t;
169 // Map Glyphs to Unicode, version 2 (much nicer):
170 // (The third way would be the AGL algorithm, as proposed
171 // by Werner Lemberg on freetype@freetype.org)
173 charcode = FT_Get_First_Char(face, &gindex);
176 if(gindex >= 0 && gindex<face->num_glyphs) {
177 if(!font->glyph2ascii[gindex]) {
178 font->glyph2ascii[gindex] = charcode;
179 if(charcode + 1 > font->maxascii) {
180 font->maxascii = charcode + 1;
184 charcode = FT_Get_Next_Char(face, charcode, &gindex);
188 font->maxascii = 65535;
190 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
192 for(t=0;t<font->maxascii;t++) {
193 int g = FT_Get_Char_Index(face, t);
194 if(!g || g>=face->num_glyphs)
196 font->ascii2glyph[t] = g;
199 if(!font->glyph2ascii[g]) {
200 font->glyph2ascii[g] = t;
204 font->maxascii = max_unicode;
208 glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
210 for(t=0; t < face->num_glyphs; t++) {
219 if(FT_HAS_GLYPH_NAMES(face)) {
220 error = FT_Get_Glyph_Name(face, t, name, 127);
221 if(!error && name[0] && !strstr(name, "notdef")) {
222 font->glyphnames[font->numchars] = strdup(name);
226 if(!font->glyph2ascii[t] && !hasname && skip_unused) {
229 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP);
231 fprintf(stderr, "Couldn't load glyph %d, error:%d\n", t, error);
234 error = FT_Get_Glyph(face->glyph, &glyph);
236 fprintf(stderr, "Couldn't get glyph %d, error:%d\n", t, error);
240 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
241 bbox.yMin = -bbox.yMin;
242 bbox.yMax = -bbox.yMax;
243 if(bbox.xMax < bbox.xMin) {
245 bbox.xMax ^= bbox.xMin;
246 bbox.xMin ^= bbox.xMax;
247 bbox.xMax ^= bbox.xMin;
249 if(bbox.yMax < bbox.yMin) {
251 bbox.yMax ^= bbox.yMin;
252 bbox.yMin ^= bbox.yMax;
253 bbox.yMax ^= bbox.yMin;
256 swf_Shape01DrawerInit(&draw, 0);
258 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
259 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
263 fprintf(stderr, "Couldn't decompose glyph %d\n", t);
272 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
274 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
277 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
280 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
282 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
283 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
284 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
285 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
289 FT_Done_Glyph(glyph);
290 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
291 glyph2glyph[t] = font->numchars;
294 /* notice: if skip_unused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
295 have more memory allocated than just font->numchars, but only the first font->numchars
298 for(t=0;t<font->maxascii;t++) {
299 if(font->ascii2glyph[t]>=0) {
300 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
306 FT_Done_FreeType(ftlibrary);ftlibrary=0;
310 #else //HAVE_FREETYPE
312 SWFFONT* swf_LoadTrueTypeFont(char*filename)
314 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
324 static int t1lib_initialized = 0;
326 static int counter = 0;
328 SWFFONT* swf_LoadT1Font(char*filename)
332 float angle,underline;
333 char*fontname,*fullname,*familyname;
342 if(!t1lib_initialized) {
344 if ((T1_InitLib(NO_LOGFILE)==NULL)){
345 fprintf(stderr, "Initialization of t1lib failed\n");
348 t1lib_initialized = 1;
350 nr = T1_AddFont(filename);
353 charnames = T1_GetAllCharNames(nr);
355 fprintf(stderr, "No Charnames record- not a Type1 Font?\n");
359 angle = T1_GetItalicAngle(nr);
360 fontname = T1_GetFontName(nr);
361 fullname = T1_GetFullName(nr);
362 familyname = T1_GetFamilyName(nr);
363 underline = T1_GetUnderlinePosition(nr);
364 bbox = T1_GetFontBBox(nr);
366 font = (SWFFONT*)malloc(sizeof(SWFFONT));
367 memset(font, 0, sizeof(SWFFONT));
371 font->name = (U8*)strdup(fontname);
374 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
375 memset(font->layout, 0, sizeof(SWFLAYOUT));
378 charname = charnames;
382 if(*charname) encoding[num] = strdup(*charname);
383 else encoding[num] = strdup(".notdef");
388 encoding[t] = strdup(".notdef");
390 //T1_ReencodeFont(nr, encoding);
392 font->maxascii = num;
393 font->numchars = num;
395 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
397 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
398 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
399 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
400 memset(font->glyph2ascii, 0, num*sizeof(U16));
401 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
402 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
403 font->layout->ascent = (U16)(underline - bbox.lly);
404 font->layout->descent = (U16)(bbox.ury - underline);
405 font->layout->leading = (U16)(font->layout->ascent -
406 font->layout->descent -
407 (bbox.lly - bbox.ury));
408 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
409 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
410 font->layout->kerningcount = 0;
411 font->layout->kerning = 0;
412 font->glyphnames = malloc(num*sizeof(char*));
413 memset(font->glyphnames, 0, num*sizeof(char*));
417 charname = charnames;
418 for(c=0;c<font->numchars;c++) {
421 T1_OUTLINE * outline;
425 outline = T1_GetCharOutline(nr, c, 100.0, 0);
426 firstx = outline->dest.x/0xffff;
432 font->glyphnames[c] = strdup(*charname);
435 font->ascii2glyph[c] = c;
436 font->glyph2ascii[c] = c;
438 swf_Shape01DrawerInit(&draw, 0);
441 pos.x += (outline->dest.x/(float)0xffff);
442 pos.y += (outline->dest.y/(float)0xffff);
444 if(outline->type == T1_PATHTYPE_MOVE) {
445 draw.moveTo(&draw,&pos);
446 } else if(outline->type == T1_PATHTYPE_LINE) {
447 draw.lineTo(&draw,&pos);
448 } else if(outline->type == T1_PATHTYPE_BEZIER) {
449 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
451 b.x = o2->B.x/(float)0xffff+last.x;
452 b.y = o2->B.y/(float)0xffff+last.y;
453 c.x = o2->C.x/(float)0xffff+last.x;
454 c.y = o2->C.y/(float)0xffff+last.y;
455 draw_cubicTo(&draw,&b,&c,&pos);
457 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
460 outline = outline->link;
461 printf("(%f,%f) ", pos.x, pos.y);
467 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
468 bbox = swf_ShapeDrawerGetBBox(&draw);
471 font->layout->bounds[c] = bbox;
472 font->glyph[c].advance = bbox.xmax;
473 if(!font->glyph[c].advance) {
474 font->glyph[c].advance = firstx;
487 SWFFONT* swf_LoadT1Font(char*filename)
489 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
495 SWFFONT* swf_DummyFont()
497 SWFFONT*font = (SWFFONT*)malloc(sizeof(SWFFONT));
498 memset(font, 0, sizeof(SWFFONT));
502 static int isSWF(const char*filename)
504 FILE*fi = fopen(filename, "rb");
510 memset(a, 0, sizeof(a));
514 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
520 SWFFONT* swf_LoadFont(char*filename)
524 return swf_DummyFont();
525 is_swf = isSWF(filename);
529 return swf_ReadFont(filename);
532 #if defined(HAVE_FREETYPE)
533 return swf_LoadTrueTypeFont(filename);
534 #elif defined(HAVE_T1LIB)
535 return swf_LoadT1Font(filename);
537 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);