added custom font scaling.
[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
26 int swf_SetLoadFontScale(int scale)
27 {
28     loadfont_scale = scale;
29 }
30
31 #ifdef HAVE_FREETYPE
32
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>
38
39 #define FT_SCALE loadfont_scale
40 #define FT_SUBPIXELS 64
41
42 static int ft_move_to(FT_Vector* _to, void* user) 
43 {
44     drawer_t* draw = (drawer_t*)user;
45     FPOINT to;
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);
49     return 0;
50 }
51 static int ft_line_to(FT_Vector* _to, void* user) 
52 {
53     drawer_t* draw = (drawer_t*)user;
54     FPOINT to;
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);
58     return 0;
59 }
60 static int ft_cubic_to(FT_Vector* _c1, FT_Vector* _c2, FT_Vector* _to, void* user)
61 {
62     drawer_t* draw = (drawer_t*)user;
63     FPOINT c1,c2,to;
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);
71     return 0;
72 }
73 static int ft_conic_to(FT_Vector* _c, FT_Vector* _to, void* user) 
74 {
75     drawer_t* draw = (drawer_t*)user;
76     FPOINT c,to;
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);
82     return 0;
83 }
84 static FT_Outline_Funcs outline_functions =
85 {
86   ft_move_to,
87   ft_line_to,
88   ft_conic_to,
89   ft_cubic_to,
90   0,0
91 };
92
93 static FT_Library ftlibrary = 0;
94
95 SWFFONT* swf_LoadTrueTypeFont(char*filename)
96 {
97     FT_Face face;
98     FT_Error error;
99     const char* name = 0;
100     FT_ULong charcode;
101     FT_UInt gindex;
102     SWFFONT* font;
103     int t;
104     int skipunused = 1;
105     int*glyph2glyph;
106    
107     if(ftlibrary == 0) {
108         if(FT_Init_FreeType(&ftlibrary)) {
109             fprintf(stderr, "Couldn't init freetype library!\n");
110             exit(1);
111         }
112     }
113     error = FT_New_Face(ftlibrary, filename, 0, &face);
114     if(error) {
115         fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
116         return 0;
117     }
118     if(face->num_glyphs <= 0)
119         return 0;
120
121     font = malloc(sizeof(SWFFONT));
122     memset(font, 0, sizeof(SWFFONT));
123     font->id = -1;
124     font->version = 2;
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));
133     font->maxascii = 0;
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*));
139     }
140
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;
145     
146     name = FT_Get_Postscript_Name(face);
147     if(name && *name)
148         font->name = (U8*)strdup(name);
149
150 /*    // Map Glyphs to Unicode, version 1 (quick and dirty):
151     int t;
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;
157         }
158     }*/
159     
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)
163
164     charcode = FT_Get_First_Char(face, &gindex);
165     while(gindex != 0)
166     {
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;
172                 }
173             }
174         }
175         charcode = FT_Get_Next_Char(face, charcode, &gindex);
176     }
177     
178     font->ascii2glyph = malloc(font->maxascii*sizeof(int));
179     
180     for(t=0;t<font->maxascii;t++) {
181         int g = FT_Get_Char_Index(face, t);
182         if(!g || g>=face->num_glyphs)
183             g = -1;
184         font->ascii2glyph[t] = g;
185         if(g>=0) {
186             if(!font->glyph2ascii[g]) {
187                 font->glyph2ascii[g] = t;
188             }
189         }
190     }
191
192     font->numchars = 0;
193
194     glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
195
196     for(t=0; t < face->num_glyphs; t++) {
197         FT_Glyph glyph;
198         FT_BBox bbox;
199         FT_Matrix matrix;
200         char name[128];
201         drawer_t draw;
202         int ret;
203         char hasname = 0;
204         name[0]=0;
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);
209                 hasname = 1;
210             }
211         }
212         if(!font->glyph2ascii[t] && !hasname && skipunused) {
213             continue;
214         }
215         error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
216         if(error) return 0;
217         error = FT_Get_Glyph(face->glyph, &glyph);
218         if(error) return 0;
219
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) {
224             // swap
225             bbox.xMax ^= bbox.xMin;
226             bbox.xMin ^= bbox.xMax;
227             bbox.xMax ^= bbox.xMin;
228         }
229         if(bbox.yMax < bbox.yMin) {
230             // swap
231             bbox.yMax ^= bbox.yMin;
232             bbox.yMin ^= bbox.yMax;
233             bbox.yMax ^= bbox.yMin;
234         }
235
236         swf_Shape01DrawerInit(&draw, 0);
237
238         //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
239         error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
240         if(error) return 0;
241
242         draw.finish(&draw);
243
244 #if 0
245         if(bbox.xMin > 0) {
246             font->glyph[font->numchars].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
247         } else {
248             font->glyph[font->numchars].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
249         }
250 #else
251         font->glyph[font->numchars].advance = glyph->advance.x*20/65536;
252 #endif
253         
254         font->glyph[font->numchars].shape = swf_ShapeDrawerToShape(&draw);
255         
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;
260
261         draw.dealloc(&draw);
262
263         FT_Done_Glyph(glyph);
264         font->glyph2ascii[font->numchars] = font->glyph2ascii[t];
265         glyph2glyph[t] = font->numchars;
266         font->numchars++;
267     }
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 
270                are used/valid */
271
272     for(t=0;t<font->maxascii;t++) {
273         if(font->ascii2glyph[t]>=0) {
274             font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
275         }
276     }
277     free(glyph2glyph);
278
279     FT_Done_Face(face);
280     FT_Done_FreeType(ftlibrary);ftlibrary=0;
281
282     return font;
283 }
284 #else  //HAVE_FREETYPE
285
286 SWFFONT* swf_LoadTrueTypeFont(char*filename)
287 {
288     fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
289     return 0;
290 }
291
292 #endif
293
294 #ifdef HAVE_T1LIB
295
296 #include <t1lib.h>
297
298 static int t1lib_initialized = 0;
299
300 SWFFONT* swf_LoadT1Font(char*filename)
301 {
302     SWFFONT * font;
303     int nr;
304     float angle,underline;
305     char*fontname,*fullname,*familyname;
306     BBox bbox;
307     int s,num;
308     char*encoding[256];
309     char**charnames;
310     char**charname;
311     int c;
312
313     if(!t1lib_initialized) {
314         T1_SetBitmapPad(16);
315         if ((T1_InitLib(NO_LOGFILE)==NULL)){
316             fprintf(stderr, "Initialization of t1lib failed\n");
317             return 0;
318         }
319         t1lib_initialized = 1;
320     }
321     nr = T1_AddFont(filename);
322     T1_LoadFont(nr);
323
324     num = T1_SetDefaultEncoding(encoding);
325     for(;num<256;num++) encoding[num] = 0;
326
327     charnames = T1_GetAllCharNames(nr);
328
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);
335
336     font = (SWFFONT*)malloc(sizeof(SWFFONT));
337     memset(font, 0, sizeof(SWFFONT));
338
339     font->version = 2;
340     if(fontname) 
341         font->name = (U8*)strdup(fontname);
342     else 
343         font->name = 0;
344     font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
345     memset(font->layout, 0, sizeof(SWFLAYOUT));
346
347     num = 0;
348     charname = charnames;
349     while(*charname) {
350         charname++;
351         num++;
352     }
353
354     font->maxascii = num;
355     font->numchars = num;
356     
357     font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
358
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*));
376   
377     num = 0;
378
379     charname = charnames;
380     for(c=0;c<font->numchars;c++) {
381         drawer_t draw;
382         SRECT bbox;
383         T1_OUTLINE * outline;
384         FPOINT pos,last;
385         int firstx;
386         
387         outline = T1_GetCharOutline(nr, c, 100.0, 0);
388         firstx = outline->dest.x/0xffff;
389
390         pos.x = 0;
391         pos.y = 0;
392         last = pos;
393         
394         font->glyphnames[c] = strdup(*charname);
395
396         if(c<font->maxascii)
397             font->ascii2glyph[c] = c;
398         font->glyph2ascii[c] = c;
399         
400         swf_Shape01DrawerInit(&draw, 0);
401
402         while(outline) {
403             pos.x += (outline->dest.x/(float)0xffff);
404             pos.y += (outline->dest.y/(float)0xffff);
405
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;
412                 FPOINT b,c;
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);
418             } else {
419                 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
420             }
421             last = pos;
422             outline = outline->link;
423         }
424         
425         draw.finish(&draw);
426
427         font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
428         bbox = swf_ShapeDrawerGetBBox(&draw);
429         draw.dealloc(&draw);
430             
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;
435         }
436         charname++;
437     }
438     return font;
439 }
440
441 #else
442
443 SWFFONT* swf_LoadT1Font(char*filename)
444 {
445     fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
446     return 0;
447 }
448
449 #endif
450
451 static int isSWF(const char*filename)
452 {
453     FILE*fi = fopen(filename, "rb");
454     char a[8];
455     if(!fi) {
456         perror(filename);
457         return -1;
458     }
459     memset(a, 0, sizeof(a));
460     fread(a, 4, 1, fi);
461     fclose(fi);
462
463     if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
464         return 1;
465     }
466     return 0;
467 }
468
469 SWFFONT* swf_LoadFont(char*filename)
470 {
471     int is_swf = isSWF(filename);
472     if(is_swf<0)
473         return 0;
474     if(is_swf) {
475         return swf_ReadFont(filename);
476     }
477 #if defined(HAVE_FREETYPE)
478     return swf_LoadTrueTypeFont(filename);
479 #elif defined(HAVE_T1LIB)
480     return swf_LoadT1Font(filename);
481 #else
482     fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);
483     return 0;
484 #endif
485 }
486