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);
271 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
273 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
276 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
279 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
281 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
282 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
283 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
284 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
288 FT_Done_Glyph(glyph);
289 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
290 glyph2glyph[t] = font->numchars;
293 /* notice: if skip_unused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
294 have more memory allocated than just font->numchars, but only the first font->numchars
297 for(t=0;t<font->maxascii;t++) {
298 if(font->ascii2glyph[t]>=0) {
299 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
305 FT_Done_FreeType(ftlibrary);ftlibrary=0;
309 #else //HAVE_FREETYPE
311 SWFFONT* swf_LoadTrueTypeFont(char*filename)
313 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
323 static int t1lib_initialized = 0;
325 static int counter = 0;
327 SWFFONT* swf_LoadT1Font(char*filename)
331 float angle,underline;
332 char*fontname,*fullname,*familyname;
341 if(!t1lib_initialized) {
343 if ((T1_InitLib(NO_LOGFILE)==NULL)){
344 fprintf(stderr, "Initialization of t1lib failed\n");
347 t1lib_initialized = 1;
349 nr = T1_AddFont(filename);
352 charnames = T1_GetAllCharNames(nr);
354 fprintf(stderr, "No Charnames record- not a Type1 Font?\n");
358 angle = T1_GetItalicAngle(nr);
359 fontname = T1_GetFontName(nr);
360 fullname = T1_GetFullName(nr);
361 familyname = T1_GetFamilyName(nr);
362 underline = T1_GetUnderlinePosition(nr);
363 bbox = T1_GetFontBBox(nr);
365 font = (SWFFONT*)malloc(sizeof(SWFFONT));
366 memset(font, 0, sizeof(SWFFONT));
370 font->name = (U8*)strdup(fontname);
373 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
374 memset(font->layout, 0, sizeof(SWFLAYOUT));
377 charname = charnames;
381 if(*charname) encoding[num] = strdup(*charname);
382 else encoding[num] = strdup(".notdef");
387 encoding[t] = strdup(".notdef");
389 //T1_ReencodeFont(nr, encoding);
391 font->maxascii = num;
392 font->numchars = num;
394 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
396 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
397 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
398 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
399 memset(font->glyph2ascii, 0, num*sizeof(U16));
400 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
401 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
402 font->layout->ascent = (U16)(underline - bbox.lly);
403 font->layout->descent = (U16)(bbox.ury - underline);
404 font->layout->leading = (U16)(font->layout->ascent -
405 font->layout->descent -
406 (bbox.lly - bbox.ury));
407 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
408 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
409 font->layout->kerningcount = 0;
410 font->layout->kerning = 0;
411 font->glyphnames = malloc(num*sizeof(char*));
412 memset(font->glyphnames, 0, num*sizeof(char*));
416 charname = charnames;
417 for(c=0;c<font->numchars;c++) {
420 T1_OUTLINE * outline;
424 outline = T1_GetCharOutline(nr, c, 100.0, 0);
425 firstx = outline->dest.x/0xffff;
431 font->glyphnames[c] = strdup(*charname);
434 font->ascii2glyph[c] = c;
435 font->glyph2ascii[c] = c;
437 swf_Shape01DrawerInit(&draw, 0);
440 pos.x += (outline->dest.x/(float)0xffff);
441 pos.y += (outline->dest.y/(float)0xffff);
443 if(outline->type == T1_PATHTYPE_MOVE) {
444 draw.moveTo(&draw,&pos);
445 } else if(outline->type == T1_PATHTYPE_LINE) {
446 draw.lineTo(&draw,&pos);
447 } else if(outline->type == T1_PATHTYPE_BEZIER) {
448 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
450 b.x = o2->B.x/(float)0xffff+last.x;
451 b.y = o2->B.y/(float)0xffff+last.y;
452 c.x = o2->C.x/(float)0xffff+last.x;
453 c.y = o2->C.y/(float)0xffff+last.y;
454 draw_cubicTo(&draw,&b,&c,&pos);
456 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
459 outline = outline->link;
460 printf("(%f,%f) ", pos.x, pos.y);
466 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
467 bbox = swf_ShapeDrawerGetBBox(&draw);
470 font->layout->bounds[c] = bbox;
471 font->glyph[c].advance = bbox.xmax;
472 if(!font->glyph[c].advance) {
473 font->glyph[c].advance = firstx;
486 SWFFONT* swf_LoadT1Font(char*filename)
488 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
494 SWFFONT* swf_DummyFont()
496 SWFFONT*font = (SWFFONT*)malloc(sizeof(SWFFONT));
497 memset(font, 0, sizeof(SWFFONT));
501 static int isSWF(const char*filename)
503 FILE*fi = fopen(filename, "rb");
509 memset(a, 0, sizeof(a));
513 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
519 SWFFONT* swf_LoadFont(char*filename)
523 return swf_DummyFont();
524 is_swf = isSWF(filename);
528 return swf_ReadFont(filename);
531 #if defined(HAVE_FREETYPE)
532 return swf_LoadTrueTypeFont(filename);
533 #elif defined(HAVE_T1LIB)
534 return swf_LoadT1Font(filename);
536 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);