removed a debug statement.
[swftools.git] / lib / modules / swffont.c
1 /* swffont.c
2
3    Functions for loading external fonts.
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2003, 2004 Matthias Kramm
9  
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.
14
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.
19
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 */
23
24 #ifdef HAVE_FREETYPE
25
26 #include <freetype/freetype.h>
27 #include <freetype/ftglyph.h>
28 #include <freetype/ftsnames.h>
29 #include <freetype/ttnameid.h>
30 #include <freetype/ftoutln.h>
31
32 #define FT_SCALE 1
33 #define FT_SUBPIXELS 64
34
35 static int ft_move_to(FT_Vector* _to, void* user) 
36 {
37     drawer_t* draw = (drawer_t*)user;
38     FPOINT to;
39     to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
40     to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
41     draw->moveTo(draw, &to);
42     return 0;
43 }
44 static int ft_line_to(FT_Vector* _to, void* user) 
45 {
46     drawer_t* draw = (drawer_t*)user;
47     FPOINT to;
48     to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
49     to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
50     draw->lineTo(draw, &to);
51     return 0;
52 }
53 static int ft_cubic_to(FT_Vector* _c1, FT_Vector* _c2, FT_Vector* _to, void* user)
54 {
55     drawer_t* draw = (drawer_t*)user;
56     FPOINT c1,c2,to;
57     to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
58     to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
59     c1.x = _c1->x*FT_SCALE/(float)FT_SUBPIXELS;
60     c1.y = -_c1->y*FT_SCALE/(float)FT_SUBPIXELS;
61     c2.x = _c2->x*FT_SCALE/(float)FT_SUBPIXELS;
62     c2.y = -_c2->y*FT_SCALE/(float)FT_SUBPIXELS;
63     draw_cubicTo(draw, &c1, &c2, &to);
64     return 0;
65 }
66 static int ft_conic_to(FT_Vector* _c, FT_Vector* _to, void* user) 
67 {
68     drawer_t* draw = (drawer_t*)user;
69     FPOINT c,to;
70     to.x = _to->x*FT_SCALE/(float)FT_SUBPIXELS;
71     to.y = -_to->y*FT_SCALE/(float)FT_SUBPIXELS;
72     c.x = _c->x*FT_SCALE/(float)FT_SUBPIXELS;
73     c.y = -_c->y*FT_SCALE/(float)FT_SUBPIXELS;
74     draw_conicTo(draw, &c, &to);
75     return 0;
76 }
77 static FT_Outline_Funcs outline_functions =
78 {
79   ft_move_to,
80   ft_line_to,
81   ft_conic_to,
82   ft_cubic_to,
83   0,0
84 };
85
86 static FT_Library ftlibrary = 0;
87
88 SWFFONT* swf_LoadTrueTypeFont(char*filename)
89 {
90     FT_Face face;
91     FT_Error error;
92     const char* name = 0;
93     FT_ULong charcode;
94     FT_UInt gindex;
95     SWFFONT* font;
96     int t;
97     int skipunused = 1;
98     int*glyph2glyph;
99    
100     if(ftlibrary == 0) {
101         if(FT_Init_FreeType(&ftlibrary)) {
102             fprintf(stderr, "Couldn't init freetype library!\n");
103             exit(1);
104         }
105     }
106     error = FT_New_Face(ftlibrary, filename, 0, &face);
107     if(error) {
108         fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
109         return 0;
110     }
111     if(face->num_glyphs <= 0)
112         return 0;
113
114     font = malloc(sizeof(SWFFONT));
115     memset(font, 0, sizeof(SWFFONT));
116     font->id = -1;
117     font->version = 2;
118     font->layout = malloc(sizeof(SWFLAYOUT));
119     memset(font->layout, 0, sizeof(SWFLAYOUT));
120     font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
121     font->style =  ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
122                   |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
123     font->encoding = FONT_ENCODING_UNICODE;
124     font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
125     memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
126     font->maxascii = 0;
127     font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
128     memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
129     if(FT_HAS_GLYPH_NAMES(face)) {
130         font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
131         memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
132     }
133
134     font->layout->ascent = face->ascender*20/FT_SUBPIXELS; //face->bbox.xMin;
135     font->layout->descent = abs(face->descender)*20/FT_SUBPIXELS; //face->bbox.xMax;
136     font->layout->leading = -face->bbox.xMin*20/FT_SUBPIXELS;
137     font->layout->kerningcount = 0;
138     
139     name = FT_Get_Postscript_Name(face);
140     if(name && *name)
141         font->name = (U8*)strdup(name);
142
143 /*    // Map Glyphs to Unicode, version 1 (quick and dirty):
144     int t;
145     for(t=0;t<65536;t++) {
146         int index = FT_Get_Char_Index(face, t);
147         if(index>=0 && index<face->num_glyphs) {
148             if(font->glyph2ascii[index]<0)
149                 font->glyph2ascii[index] = t;
150         }
151     }*/
152     
153     // Map Glyphs to Unicode, version 2 (much nicer):
154     // (The third way would be the AGL algorithm, as proposed
155     //  by Werner Lemberg on freetype@freetype.org)
156
157     charcode = FT_Get_First_Char(face, &gindex);
158     while(gindex != 0)
159     {
160         if(gindex >= 0 && gindex<face->num_glyphs) {
161             if(!font->glyph2ascii[gindex]) {
162                 font->glyph2ascii[gindex] = charcode;
163                 if(charcode + 1 > font->maxascii) {
164                     font->maxascii = charcode + 1;
165                 }
166             }
167         }
168         charcode = FT_Get_Next_Char(face, charcode, &gindex);
169     }
170     
171     font->ascii2glyph = malloc(font->maxascii*sizeof(int));
172     
173     for(t=0;t<font->maxascii;t++) {
174         int g = FT_Get_Char_Index(face, t);
175         if(!g || g>=face->num_glyphs)
176             g = -1;
177         font->ascii2glyph[t] = g;
178         if(g>=0) {
179             if(!font->glyph2ascii[g]) {
180                 font->glyph2ascii[g] = t;
181             }
182         }
183     }
184
185     font->numchars = 0;
186
187     glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
188
189     for(t=0; t < face->num_glyphs; t++) {
190         FT_Glyph glyph;
191         FT_BBox bbox;
192         FT_Matrix matrix;
193         char name[128];
194         drawer_t draw;
195         int ret;
196         char hasname = 0;
197         name[0]=0;
198         if(FT_HAS_GLYPH_NAMES(face)) {
199             error = FT_Get_Glyph_Name(face, t, name, 127);
200             if(!error && name[0] && !strstr(name, "notdef")) {
201                 font->glyphnames[font->numchars] = strdup(name);
202                 hasname = 1;
203             }
204         }
205         if(!font->glyph2ascii[t] && !hasname && skipunused) {
206             continue;
207         }
208         error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
209         if(error) return 0;
210         error = FT_Get_Glyph(face->glyph, &glyph);
211         if(error) return 0;
212
213         FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
214         bbox.yMin = -bbox.yMin;
215         bbox.yMax = -bbox.yMax;
216         if(bbox.xMax < bbox.xMin) {
217             // swap
218             bbox.xMax ^= bbox.xMin;
219             bbox.xMin ^= bbox.xMax;
220             bbox.xMax ^= bbox.xMin;
221         }
222         if(bbox.yMax < bbox.yMin) {
223             // swap
224             bbox.yMax ^= bbox.yMin;
225             bbox.yMin ^= bbox.yMax;
226             bbox.yMax ^= bbox.yMin;
227         }
228
229         swf_Shape01DrawerInit(&draw, 0);
230
231         //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
232         error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
233         if(error) return 0;
234
235         draw.finish(&draw);
236
237 #if 0
238         if(bbox.xMin > 0) {
239             font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
240         } else {
241             font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
242         }
243 #else
244         font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
245 #endif
246         
247         font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
248         
249         font->layout->bounds[font->numchars].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
250         font->layout->bounds[font->numchars].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
251         font->layout->bounds[font->numchars].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
252         font->layout->bounds[font->numchars].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
253
254         draw.dealloc(&draw);
255
256         FT_Done_Glyph(glyph);
257         font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
258         glyph2glyph[t] = font->numchars;
259         font->numchars++;
260     }
261     /* notice: if skipunused is true, font->glyph2ascii, font->glyphnames and font->layout->bounds will 
262                have more memory allocated than just font->numchars, but only the first font->numchars 
263                are used/valid */
264
265     for(t=0;t<font->maxascii;t++) {
266         if(font->ascii2glyph[t]>=0) {
267             font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
268         }
269     }
270     free(glyph2glyph);
271
272     FT_Done_Face(face);
273     FT_Done_FreeType(ftlibrary);ftlibrary=0;
274
275     return font;
276 }
277 #else  //HAVE_FREETYPE
278
279 SWFFONT* swf_LoadTrueTypeFont(char*filename)
280 {
281     fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
282     return 0;
283 }
284
285 #endif
286
287 #ifdef HAVE_T1LIB
288
289 #include <t1lib.h>
290
291 static int t1lib_initialized = 0;
292
293 SWFFONT* swf_LoadT1Font(char*filename)
294 {
295     SWFFONT * font;
296     int nr;
297     float angle,underline;
298     char*fontname,*fullname,*familyname;
299     BBox bbox;
300     int s,num;
301     char*encoding[256];
302     char**charnames;
303     char**charname;
304     int c;
305
306     if(!t1lib_initialized) {
307         T1_SetBitmapPad(16);
308         if ((T1_InitLib(NO_LOGFILE)==NULL)){
309             fprintf(stderr, "Initialization of t1lib failed\n");
310             return 0;
311         }
312         t1lib_initialized = 1;
313     }
314     nr = T1_AddFont(filename);
315     T1_LoadFont(nr);
316
317     num = T1_SetDefaultEncoding(encoding);
318     for(;num<256;num++) encoding[num] = 0;
319
320     charnames = T1_GetAllCharNames(nr);
321
322     angle = T1_GetItalicAngle(nr);
323     fontname = T1_GetFontName(nr);
324     fullname = T1_GetFullName(nr);
325     familyname = T1_GetFamilyName(nr);
326     underline = T1_GetUnderlinePosition(nr);
327     bbox = T1_GetFontBBox(nr);
328
329     font = (SWFFONT*)malloc(sizeof(SWFFONT));
330     memset(font, 0, sizeof(SWFFONT));
331
332     font->version = 2;
333     if(fontname) 
334         font->name = (U8*)strdup(fontname);
335     else 
336         font->name = 0;
337     font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
338     memset(font->layout, 0, sizeof(SWFLAYOUT));
339
340     num = 0;
341     charname = charnames;
342     while(*charname) {
343         charname++;
344         num++;
345     }
346
347     font->maxascii = num;
348     font->numchars = num;
349     
350     font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
351
352     font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
353     memset(font->glyph, 0, num*sizeof(SWFGLYPH));
354     font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
355     memset(font->glyph2ascii, 0, num*sizeof(U16));
356     font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
357     memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
358     font->layout->ascent = (U16)(underline - bbox.lly);
359     font->layout->descent = (U16)(bbox.ury - underline);
360     font->layout->leading = (U16)(font->layout->ascent - 
361                              font->layout->descent -
362                              (bbox.lly - bbox.ury));
363     font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
364     memset(font->layout->bounds, 0, sizeof(SRECT)*num);
365     font->layout->kerningcount = 0;
366     font->layout->kerning = 0;
367     font->glyphnames = malloc(num*sizeof(char*));
368     memset(font->glyphnames, 0, num*sizeof(char*));
369   
370     num = 0;
371
372     charname = charnames;
373     for(c=0;c<font->numchars;c++) {
374         drawer_t draw;
375         SRECT bbox;
376         T1_OUTLINE * outline;
377         FPOINT pos,last;
378         int firstx;
379         
380         outline = T1_GetCharOutline(nr, c, 100.0, 0);
381         firstx = outline->dest.x/0xffff;
382
383         pos.x = 0;
384         pos.y = 0;
385         last = pos;
386         
387         font->glyphnames[c] = strdup(*charname);
388
389         if(c<font->maxascii)
390             font->ascii2glyph[c] = c;
391         font->glyph2ascii[c] = c;
392         
393         swf_Shape01DrawerInit(&draw, 0);
394
395         while(outline) {
396             pos.x += (outline->dest.x/(float)0xffff);
397             pos.y += (outline->dest.y/(float)0xffff);
398
399             if(outline->type == T1_PATHTYPE_MOVE) {
400                 draw.moveTo(&draw,&pos);
401             } else if(outline->type == T1_PATHTYPE_LINE) {
402                 draw.lineTo(&draw,&pos);
403             } else if(outline->type == T1_PATHTYPE_BEZIER) {
404                 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
405                 FPOINT b,c;
406                 b.x = o2->B.x/(float)0xffff+last.x;
407                 b.y = o2->B.y/(float)0xffff+last.y;
408                 c.x = o2->C.x/(float)0xffff+last.x;
409                 c.y = o2->C.y/(float)0xffff+last.y;
410                 draw_cubicTo(&draw,&b,&c,&pos);
411             } else {
412                 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
413             }
414             last = pos;
415             outline = outline->link;
416         }
417         
418         draw.finish(&draw);
419
420         font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
421         bbox = swf_ShapeDrawerGetBBox(&draw);
422         draw.dealloc(&draw);
423             
424         font->layout->bounds[c] = bbox;
425         font->glyph[c].advance = bbox.xmax;
426         if(!font->glyph[c].advance) {
427             font->glyph[c].advance = firstx;
428         }
429         charname++;
430     }
431     return font;
432 }
433
434 #else
435
436 SWFFONT* swf_LoadT1Font(char*filename)
437 {
438     fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
439     return 0;
440 }
441
442 #endif
443
444 static int isSWF(const char*filename)
445 {
446     FILE*fi = fopen(filename, "rb");
447     char a[8];
448     if(!fi) {
449         perror(filename);
450         return -1;
451     }
452     memset(a, 0, sizeof(a));
453     fread(a, 4, 1, fi);
454     fclose(fi);
455
456     if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
457         return 1;
458     }
459     return 0;
460 }
461
462 SWFFONT* swf_LoadFont(char*filename)
463 {
464     int is_swf = isSWF(filename);
465     if(is_swf<0)
466         return 0;
467     if(is_swf) {
468         return swf_ReadFont(filename);
469     }
470 #if defined(HAVE_FREETYPE)
471     return swf_LoadTrueTypeFont(filename);
472 #elif defined(HAVE_T1LIB)
473     return swf_LoadT1Font(filename);
474 #else
475     fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);
476     return 0;
477 #endif
478 }
479