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