removed superfluous memset.
[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    
98     if(ftlibrary == 0) {
99         if(FT_Init_FreeType(&ftlibrary)) {
100             fprintf(stderr, "Couldn't init freetype library!\n");
101             exit(1);
102         }
103     }
104     error = FT_New_Face(ftlibrary, filename, 0, &face);
105     if(error) {
106         fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
107         return 0;
108     }
109     if(face->num_glyphs <= 0)
110         return 0;
111
112     font = malloc(sizeof(SWFFONT));
113     memset(font, 0, sizeof(SWFFONT));
114     font->id = -1;
115     font->version = 2;
116     font->layout = malloc(sizeof(SWFLAYOUT));
117     memset(font->layout, 0, sizeof(SWFLAYOUT));
118     font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
119     font->numchars = face->num_glyphs;
120     font->style =  ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
121                   |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
122     font->encoding = FONT_ENCODING_UNICODE;
123     font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
124     memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
125     font->maxascii = 0;
126     font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
127     memset(font->glyph, 0, face->num_glyphs*sizeof(U16));
128     if(FT_HAS_GLYPH_NAMES(face)) {
129         font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
130     }
131
132     font->layout->ascent = face->ascender*20/FT_SUBPIXELS; //face->bbox.xMin;
133     font->layout->descent = abs(face->descender)*20/FT_SUBPIXELS; //face->bbox.xMax;
134     font->layout->leading = -face->bbox.xMin*20/FT_SUBPIXELS;
135     font->layout->kerningcount = 0;
136     
137     name = FT_Get_Postscript_Name(face);
138     if(name && *name)
139         font->name = (U8*)strdup(name);
140
141 /*    // Map Glyphs to Unicode, version 1 (quick and dirty):
142     int t;
143     for(t=0;t<65536;t++) {
144         int index = FT_Get_Char_Index(face, t);
145         if(index>=0 && index<face->num_glyphs) {
146             if(font->glyph2ascii[index]<0)
147                 font->glyph2ascii[index] = t;
148         }
149     }*/
150     
151     // Map Glyphs to Unicode, version 2 (much nicer):
152     // (The third way would be the AGL algorithm, as proposed
153     //  by Werner Lemberg on freetype@freetype.org)
154
155     charcode = FT_Get_First_Char(face, &gindex);
156     while(gindex != 0)
157     {
158         if(gindex >= 0 && gindex<face->num_glyphs) {
159             if(!font->glyph2ascii[gindex]) {
160                 font->glyph2ascii[gindex] = charcode;
161                 if(charcode + 1 > font->maxascii) {
162                     font->maxascii = charcode + 1;
163                 }
164             }
165         }
166         charcode = FT_Get_Next_Char(face, charcode, &gindex);
167     }
168     
169     font->ascii2glyph = malloc(font->maxascii*sizeof(int));
170     
171     for(t=0;t<font->maxascii;t++) {
172         font->ascii2glyph[t] = FT_Get_Char_Index(face, t);
173         if(!font->ascii2glyph[t])
174             font->ascii2glyph[t] = -1;
175     }
176
177     for(t=0; t < face->num_glyphs; t++) {
178         FT_Glyph glyph;
179         FT_BBox bbox;
180         FT_Matrix matrix;
181         char name[128];
182         drawer_t draw;
183         int ret;
184         name[0]=0;
185         if(FT_HAS_GLYPH_NAMES(face)) {
186             error = FT_Get_Glyph_Name(face, t, name, 127);
187             if(!error) 
188                 font->glyphnames[t] = strdup(name);
189         }
190         error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
191         if(error) return 0;
192         error = FT_Get_Glyph(face->glyph, &glyph);
193         if(error) return 0;
194
195         FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_unscaled, &bbox);
196         bbox.yMin = -bbox.yMin;
197         bbox.yMax = -bbox.yMax;
198         if(bbox.xMax < bbox.xMin) {
199             // swap
200             bbox.xMax ^= bbox.xMin;
201             bbox.xMin ^= bbox.xMax;
202             bbox.xMax ^= bbox.xMin;
203         }
204         if(bbox.yMax < bbox.yMin) {
205             // swap
206             bbox.yMax ^= bbox.yMin;
207             bbox.yMin ^= bbox.yMax;
208             bbox.yMax ^= bbox.yMin;
209         }
210
211         swf_Shape01DrawerInit(&draw, 0);
212
213         //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
214         error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
215         if(error) return 0;
216
217         draw.finish(&draw);
218
219 #if 0
220         if(bbox.xMin > 0) {
221             font->glyph[t].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
222         } else {
223             font->glyph[t].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
224         }
225 #else
226         font->glyph[t].advance = glyph->advance.x*20/65536;
227 #endif
228         
229         font->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
230         
231         font->layout->bounds[t].xmin = (bbox.xMin*FT_SCALE*20)/FT_SUBPIXELS;
232         font->layout->bounds[t].ymin = (bbox.yMin*FT_SCALE*20)/FT_SUBPIXELS;
233         font->layout->bounds[t].xmax = (bbox.xMax*FT_SCALE*20)/FT_SUBPIXELS;
234         font->layout->bounds[t].ymax = (bbox.yMax*FT_SCALE*20)/FT_SUBPIXELS;
235
236         draw.dealloc(&draw);
237
238         FT_Done_Glyph(glyph);
239     }
240
241     FT_Done_Face(face);
242     FT_Done_FreeType(ftlibrary);ftlibrary=0;
243
244     return font;
245 }
246 #else  //HAVE_FREETYPE
247
248 SWFFONT* swf_LoadTrueTypeFont(char*filename)
249 {
250     fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
251     return 0;
252 }
253
254 #endif
255
256 #ifdef HAVE_T1LIB
257
258 #include <t1lib.h>
259
260 static int t1lib_initialized = 0;
261
262 SWFFONT* swf_LoadT1Font(char*filename)
263 {
264     SWFFONT * font;
265     int nr;
266     float angle,underline;
267     char*fontname,*fullname,*familyname;
268     BBox bbox;
269     int s,num;
270     char*encoding[256];
271     char**charnames;
272     char**charname;
273     int c;
274
275     if(!t1lib_initialized) {
276         T1_SetBitmapPad(16);
277         if ((T1_InitLib(NO_LOGFILE)==NULL)){
278             fprintf(stderr, "Initialization of t1lib failed\n");
279             return 0;
280         }
281         t1lib_initialized = 1;
282     }
283     nr = T1_AddFont(filename);
284     T1_LoadFont(nr);
285
286     num = T1_SetDefaultEncoding(encoding);
287     for(;num<256;num++) encoding[num] = 0;
288
289     charnames = T1_GetAllCharNames(nr);
290
291     angle = T1_GetItalicAngle(nr);
292     fontname = T1_GetFontName(nr);
293     fullname = T1_GetFullName(nr);
294     familyname = T1_GetFamilyName(nr);
295     underline = T1_GetUnderlinePosition(nr);
296     bbox = T1_GetFontBBox(nr);
297
298     font = (SWFFONT*)malloc(sizeof(SWFFONT));
299     memset(font, 0, sizeof(SWFFONT));
300
301     font->version = 2;
302     if(fontname) 
303         font->name = (U8*)strdup(fontname);
304     else 
305         font->name = 0;
306     font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
307     memset(font->layout, 0, sizeof(SWFLAYOUT));
308
309     num = 0;
310     charname = charnames;
311     while(*charname) {
312         charname++;
313         num++;
314     }
315
316     font->maxascii = num;
317     font->numchars = num;
318     
319     font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
320
321     font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
322     memset(font->glyph, 0, num*sizeof(SWFGLYPH));
323     font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
324     memset(font->glyph2ascii, 0, num*sizeof(U16));
325     font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
326     memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
327     font->layout->ascent = (U16)(underline - bbox.lly);
328     font->layout->descent = (U16)(bbox.ury - underline);
329     font->layout->leading = (U16)(font->layout->ascent - 
330                              font->layout->descent -
331                              (bbox.lly - bbox.ury));
332     font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
333     memset(font->layout->bounds, 0, sizeof(SRECT)*num);
334     font->layout->kerningcount = 0;
335     font->layout->kerning = 0;
336     font->glyphnames = malloc(num*sizeof(char*));
337     memset(font->glyphnames, 0, num*sizeof(char*));
338   
339     num = 0;
340
341     charname = charnames;
342     for(c=0;c<font->numchars;c++) {
343         drawer_t draw;
344         SRECT bbox;
345         T1_OUTLINE * outline;
346         FPOINT pos,last;
347         int firstx;
348         
349         outline = T1_GetCharOutline(nr, c, 100.0, 0);
350         firstx = outline->dest.x/0xffff;
351
352         pos.x = 0;
353         pos.y = 0;
354         last = pos;
355         
356         font->glyphnames[c] = strdup(*charname);
357
358         if(c<font->maxascii)
359             font->ascii2glyph[c] = c;
360         font->glyph2ascii[c] = c;
361         
362         swf_Shape01DrawerInit(&draw, 0);
363
364         while(outline) {
365             pos.x += (outline->dest.x/(float)0xffff);
366             pos.y += (outline->dest.y/(float)0xffff);
367
368             if(outline->type == T1_PATHTYPE_MOVE) {
369                 draw.moveTo(&draw,&pos);
370             } else if(outline->type == T1_PATHTYPE_LINE) {
371                 draw.lineTo(&draw,&pos);
372             } else if(outline->type == T1_PATHTYPE_BEZIER) {
373                 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
374                 FPOINT b,c;
375                 b.x = o2->B.x/(float)0xffff+last.x;
376                 b.y = o2->B.y/(float)0xffff+last.y;
377                 c.x = o2->C.x/(float)0xffff+last.x;
378                 c.y = o2->C.y/(float)0xffff+last.y;
379                 draw_cubicTo(&draw,&b,&c,&pos);
380             } else {
381                 fprintf(stderr, "loadT1Font: unknown outline type:%d\n", outline->type);
382             }
383             last = pos;
384             outline = outline->link;
385         }
386         
387         draw.finish(&draw);
388
389         font->glyph[c].shape = swf_ShapeDrawerToShape(&draw);
390         bbox = swf_ShapeDrawerGetBBox(&draw);
391         draw.dealloc(&draw);
392             
393         font->layout->bounds[c] = bbox;
394         font->glyph[c].advance = bbox.xmax;
395         if(!font->glyph[c].advance) {
396             font->glyph[c].advance = firstx;
397         }
398         charname++;
399     }
400     return font;
401 }
402
403 #else
404
405 SWFFONT* swf_LoadT1Font(char*filename)
406 {
407     fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);
408     return 0;
409 }
410
411 #endif
412
413 static int isSWF(const char*filename)
414 {
415     FILE*fi = fopen(filename, "rb");
416     char a[8];
417     if(!fi) {
418         perror(filename);
419         return -1;
420     }
421     memset(a, 0, sizeof(a));
422     fread(a, 4, 1, fi);
423     fclose(fi);
424
425     if(!strncmp(a, "FWS", 3) || !strncmp(a, "CWS", 3)) {
426         return 1;
427     }
428     return 0;
429 }
430
431 SWFFONT* swf_LoadFont(char*filename)
432 {
433     int is_swf = isSWF(filename);
434     if(is_swf<0)
435         return 0;
436     if(is_swf) {
437         return swf_ReadFont(filename);
438     }
439 #if defined(HAVE_FREETYPE)
440     return swf_LoadTrueTypeFont(filename);
441 #elif defined(HAVE_T1LIB)
442     return swf_LoadT1Font(filename);
443 #else
444     fprintf(stderr, "Error: Neither T1lib nor FreeType support compiled in. Could not load %s\n", filename);
445     return 0;
446 #endif
447 }
448