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 = 1;
26 int swf_SetLoadFontScale(int scale)
28 loadfont_scale = scale;
33 #include <freetype/freetype.h>
34 #include <freetype/ftglyph.h>
35 #include <freetype/ftsnames.h>
36 #include <freetype/ttnameid.h>
37 #include <freetype/ftoutln.h>
39 #define FT_SCALE loadfont_scale
40 #define FT_SUBPIXELS 64
42 static int ft_move_to(FT_Vector* _to, void* user)
44 drawer_t* draw = (drawer_t*)user;
46 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
47 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
48 draw->moveTo(draw, &to);
51 static int ft_line_to(FT_Vector* _to, void* user)
53 drawer_t* draw = (drawer_t*)user;
55 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
56 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
57 draw->lineTo(draw, &to);
60 static int ft_cubic_to(FT_Vector* _c1, FT_Vector* _c2, FT_Vector* _to, void* user)
62 drawer_t* draw = (drawer_t*)user;
64 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
65 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
66 c1.x = _c1->x*FT_SCALE/(float)FT_SUBPIXELS;
67 c1.y = -_c1->y*FT_SCALE/(float)FT_SUBPIXELS;
68 c2.x = _c2->x*FT_SCALE/(float)FT_SUBPIXELS;
69 c2.y = -_c2->y*FT_SCALE/(float)FT_SUBPIXELS;
70 draw_cubicTo(draw, &c1, &c2, &to);
73 static int ft_conic_to(FT_Vector* _c, FT_Vector* _to, void* user)
75 drawer_t* draw = (drawer_t*)user;
77 to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
78 to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
79 c.x = _c->x*FT_SCALE/(float)FT_SUBPIXELS;
80 c.y = -_c->y*FT_SCALE/(float)FT_SUBPIXELS;
81 draw_conicTo(draw, &c, &to);
84 static FT_Outline_Funcs outline_functions =
93 static FT_Library ftlibrary = 0;
95 SWFFONT* swf_LoadTrueTypeFont(char*filename)
108 if(FT_Init_FreeType(&ftlibrary)) {
109 fprintf(stderr, "Couldn't init freetype library!\n");
113 error = FT_New_Face(ftlibrary, filename, 0, &face);
115 fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
118 if(face->num_glyphs <= 0)
121 font = malloc(sizeof(SWFFONT));
122 memset(font, 0, sizeof(SWFFONT));
125 font->layout = malloc(sizeof(SWFLAYOUT));
126 memset(font->layout, 0, sizeof(SWFLAYOUT));
127 font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
128 font->style = ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
129 |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
130 font->encoding = FONT_ENCODING_UNICODE;
131 font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
132 memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
134 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
135 memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
136 if(FT_HAS_GLYPH_NAMES(face)) {
137 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
138 memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
141 font->layout->ascent = face->ascender*20/FT_SUBPIXELS; //face->bbox.xMin;
142 font->layout->descent = abs(face->descender)*20/FT_SUBPIXELS; //face->bbox.xMax;
143 font->layout->leading = abs(face->bbox.yMin - face->bbox.yMax); //-face->bbox.xMin*20/FT_SUBPIXELS;
144 font->layout->kerningcount = 0;
146 name = FT_Get_Postscript_Name(face);
148 font->name = (U8*)strdup(name);
150 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
152 for(t=0;t<65536;t++) {
153 int index = FT_Get_Char_Index(face, t);
154 if(index>=0 && index<face->num_glyphs) {
155 if(font->glyph2ascii[index]<0)
156 font->glyph2ascii[index] = t;
160 // Map Glyphs to Unicode, version 2 (much nicer):
161 // (The third way would be the AGL algorithm, as proposed
162 // by Werner Lemberg on freetype@freetype.org)
164 charcode = FT_Get_First_Char(face, &gindex);
167 if(gindex >= 0 && gindex<face->num_glyphs) {
168 if(!font->glyph2ascii[gindex]) {
169 font->glyph2ascii[gindex] = charcode;
170 if(charcode + 1 > font->maxascii) {
171 font->maxascii = charcode + 1;
175 charcode = FT_Get_Next_Char(face, charcode, &gindex);
178 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
180 for(t=0;t<font->maxascii;t++) {
181 int g = FT_Get_Char_Index(face, t);
182 if(!g || g>=face->num_glyphs)
184 font->ascii2glyph[t] = g;
186 if(!font->glyph2ascii[g]) {
187 font->glyph2ascii[g] = t;
194 glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
196 for(t=0; t < face->num_glyphs; t++) {
205 if(FT_HAS_GLYPH_NAMES(face)) {
206 error = FT_Get_Glyph_Name(face, t, name, 127);
207 if(!error && name[0] && !strstr(name, "notdef")) {
208 font->glyphnames[font->numchars] = strdup(name);
212 if(!font->glyph2ascii[t] && !hasname && skipunused) {
215 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
217 error = FT_Get_Glyph(face->glyph, &glyph);
220 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
221 bbox.yMin = -bbox.yMin;
222 bbox.yMax = -bbox.yMax;
223 if(bbox.xMax < bbox.xMin) {
225 bbox.xMax ^= bbox.xMin;
226 bbox.xMin ^= bbox.xMax;
227 bbox.xMax ^= bbox.xMin;
229 if(bbox.yMax < bbox.yMin) {
231 bbox.yMax ^= bbox.yMin;
232 bbox.yMin ^= bbox.yMax;
233 bbox.yMax ^= bbox.yMin;
236 swf_Shape01DrawerInit(&draw, 0);
238 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
239 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
246 font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
248 font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
251 font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
254 font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
256 font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
257 font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
258 font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
259 font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
263 FT_Done_Glyph(glyph);
264 font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
265 glyph2glyph[t] = font->numchars;
268 /* notice: if skipunused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will
269 have more memory allocated than just font->numchars, but only the first font->numchars
272 for(t=0;t<font->maxascii;t++) {
273 if(font->ascii2glyph[t]>=0) {
274 font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
280 FT_Done_FreeType(ftlibrary);ftlibrary=0;
284 #else //HAVE_FREETYPE
286 SWFFONT* swf_LoadTrueTypeFont(char*filename)
288 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
298 static int t1lib_initialized = 0;
300 SWFFONT* swf_LoadT1Font(char*filename)
304 float angle,underline;
305 char*fontname,*fullname,*familyname;
313 if(!t1lib_initialized) {
315 if ((T1_InitLib(NO_LOGFILE)==NULL)){
316 fprintf(stderr, "Initialization of t1lib failed\n");
319 t1lib_initialized = 1;
321 nr = T1_AddFont(filename);
324 num = T1_SetDefaultEncoding(encoding);
325 for(;num<256;num++) encoding[num] = 0;
327 charnames = T1_GetAllCharNames(nr);
329 angle = T1_GetItalicAngle(nr);
330 fontname = T1_GetFontName(nr);
331 fullname = T1_GetFullName(nr);
332 familyname = T1_GetFamilyName(nr);
333 underline = T1_GetUnderlinePosition(nr);
334 bbox = T1_GetFontBBox(nr);
336 font = (SWFFONT*)malloc(sizeof(SWFFONT));
337 memset(font, 0, sizeof(SWFFONT));
341 font->name = (U8*)strdup(fontname);
344 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
345 memset(font->layout, 0, sizeof(SWFLAYOUT));
348 charname = charnames;
354 font->maxascii = num;
355 font->numchars = num;
357 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
359 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
360 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
361 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
362 memset(font->glyph2ascii, 0, num*sizeof(U16));
363 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
364 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
365 font->layout->ascent = (U16)(underline - bbox.lly);
366 font->layout->descent = (U16)(bbox.ury - underline);
367 font->layout->leading = (U16)(font->layout->ascent -
368 font->layout->descent -
369 (bbox.lly - bbox.ury));
370 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
371 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
372 font->layout->kerningcount = 0;
373 font->layout->kerning = 0;
374 font->glyphnames = malloc(num*sizeof(char*));
375 memset(font->glyphnames, 0, num*sizeof(char*));
379 charname = charnames;
380 for(c=0;c<font->numchars;c++) {
383 T1_OUTLINE * outline;
387 outline = T1_GetCharOutline(nr, c, 100.0, 0);
388 firstx = outline->dest.x/0xffff;
394 font->glyphnames[c] = strdup(*charname);
397 font->ascii2glyph[c] = c;
398 font->glyph2ascii[c] = c;
400 swf_Shape01DrawerInit(&draw, 0);
403 pos.x += (outline->dest.x/(float)0xffff);
404 pos.y += (outline->dest.y/(float)0xffff);
406 if(outline->type == T1_PATHTYPE_MOVE) {
407 draw.moveTo(&draw,&pos);
408 } else if(outline->type == T1_PATHTYPE_LINE) {
409 draw.lineTo(&draw,&pos);
410 } else if(outline->type == T1_PATHTYPE_BEZIER) {
411 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
413 b.x = o2->B.x/(float)0xffff+last.x;
414 b.y = o2->B.y/(float)0xffff+last.y;
415 c.x = o2->C.x/(float)0xffff+last.x;
416 c.y = o2->C.y/(float)0xffff+last.y;
417 draw_cubicTo(&draw,&b,&c,&pos);
419 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
422 outline = outline->link;
427 font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
428 bbox = swf_ShapeDrawerGetBBox(&draw);
431 font->layout->bounds[c] = bbox;
432 font->glyph[c].advance = bbox.xmax;
433 if(!font->glyph[c].advance) {
434 font->glyph[c].advance = firstx;
443 SWFFONT* swf_LoadT1Font(char*filename)
445 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
451 static int isSWF(const char*filename)
453 FILE*fi = fopen(filename, "rb");
459 memset(a, 0, sizeof(a));
463 if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
469 SWFFONT* swf_LoadFont(char*filename)
471 int is_swf = isSWF(filename);
475 return swf_ReadFont(filename);
477 #if defined(HAVE_FREETYPE)
478 return swf_LoadTrueTypeFont(filename);
479 #elif defined(HAVE_T1LIB)
480 return swf_LoadT1Font(filename);
482 fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);