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 */
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>
33 #define FT_SUBPIXELS 64
35 static int ft_move_to(FT_Vector* _to, void* user)
37 drawer_t* draw = (drawer_t*)user;
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);
44 static int ft_line_to(FT_Vector* _to, void* user)
46 drawer_t* draw = (drawer_t*)user;
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);
53 static int ft_cubic_to(FT_Vector* _c1, FT_Vector* _c2, FT_Vector* _to, void* user)
55 drawer_t* draw = (drawer_t*)user;
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);
66 static int ft_conic_to(FT_Vector* _c, FT_Vector* _to, void* user)
68 drawer_t* draw = (drawer_t*)user;
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);
77 static FT_Outline_Funcs outline_functions =
86 static FT_Library ftlibrary = 0;
88 SWFFONT* swf_LoadTrueTypeFont(char*filename)
99 if(FT_Init_FreeType(&ftlibrary)) {
100 fprintf(stderr, "Couldn't init freetype library!\n");
104 error = FT_New_Face(ftlibrary, filename, 0, &face);
106 fprintf(stderr, "Couldn't load file %s- not a TTF file?\n", filename);
109 if(face->num_glyphs <= 0)
112 font = malloc(sizeof(SWFFONT));
113 memset(font, 0, sizeof(SWFFONT));
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));
126 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
127 font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
128 memset(font->glyph, 0, face->num_glyphs*sizeof(U16));
129 if(FT_HAS_GLYPH_NAMES(face)) {
130 font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
133 font->layout->ascent = face->ascender; //face->bbox.xMin;
134 font->layout->descent = face->descender; //face->bbox.xMax;
135 font->layout->leading = -face->bbox.xMin;
136 font->layout->kerningcount = 0;
139 font->name = (U8*)strdup(FT_Get_Postscript_Name(face));
141 /* // Map Glyphs to Unicode, version 1 (quick and dirty):
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;
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)
155 charcode = FT_Get_First_Char(face, &gindex);
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;
166 charcode = FT_Get_Next_Char(face, charcode, &gindex);
169 font->ascii2glyph = malloc(font->maxascii*sizeof(int));
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;
177 for(t=0; t < face->num_glyphs; t++) {
185 if(FT_HAS_GLYPH_NAMES(face)) {
186 error = FT_Get_Glyph_Name(face, t, name, 127);
188 font->glyphnames[t] = strdup(name);
190 error = FT_Load_Glyph(face, t, FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
192 error = FT_Get_Glyph(face->glyph, &glyph);
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) {
200 bbox.xMax ^= bbox.xMin;
201 bbox.xMin ^= bbox.xMax;
202 bbox.xMax ^= bbox.xMin;
204 if(bbox.yMax < bbox.yMin) {
206 bbox.yMax ^= bbox.yMin;
207 bbox.yMin ^= bbox.yMax;
208 bbox.yMax ^= bbox.yMin;
211 swf_Shape01DrawerInit(&draw, 0);
213 //error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
214 error = FT_Outline_Decompose(&face->glyph->outline, &outline_functions, &draw);
221 font->glyph[t].advance = (bbox.xMax*FT_SCALE)/FT_SUBPIXELS;
223 font->glyph[t].advance = ((bbox.xMax - bbox.xMin)*FT_SCALE)/FT_SUBPIXELS;
226 font->glyph[t].advance = glyph->advance.x/65536;
229 font->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
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;
238 FT_Done_Glyph(glyph);
242 FT_Done_FreeType(ftlibrary);ftlibrary=0;
248 SWFFONT* swf_LoadTrueTypeFont(char*filename)
250 fprintf(stderr, "Warning: no freetype library- not able to load %s\n", filename);
260 SWFFONT* swf_LoadT1Font(char*filename)
264 float angle,underline;
265 char*fontname,*fullname,*familyname;
272 T1_SetBitmapPad( 16);
273 if ((T1_InitLib(NO_LOGFILE)==NULL)){
274 fprintf(stderr, "Initialization of t1lib failed\n");
277 nr = T1_AddFont(filename);
280 num = T1_SetDefaultEncoding(encoding);
281 for(;num<256;num++) encoding[num] = 0;
283 charnames = T1_GetAllCharNames(nr);
285 angle = T1_GetItalicAngle(nr);
286 fontname = T1_GetFontName(nr);
287 fullname = T1_GetFullName(nr);
288 familyname = T1_GetFamilyName(nr);
289 underline = T1_GetUnderlinePosition(nr);
290 bbox = T1_GetFontBBox(nr);
292 font = (SWFFONT*)malloc(sizeof(SWFFONT));
293 memset(font, 0, sizeof(SWFFONT));
296 font->name = (U8*)strdup(fontname);
297 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
298 memset(font->layout, 0, sizeof(SWFLAYOUT));
301 charname = charnames[0];
307 font->maxascii = 256;
308 font->numchars = num;
310 font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
312 font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
313 memset(font->glyph, 0, num*sizeof(SWFGLYPH));
314 font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
315 memset(font->glyph2ascii, 0, num*sizeof(U16));
316 font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
317 memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
318 font->layout->ascent = (U16)(underline - bbox.lly);
319 font->layout->descent = (U16)(bbox.ury - underline);
320 font->layout->leading = (U16)(font->layout->ascent -
321 font->layout->descent -
322 (bbox.lly - bbox.ury));
323 font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
324 memset(font->layout->bounds, 0, sizeof(SRECT)*num);
325 font->layout->kerningcount = 0;
326 font->layout->kerning = 0;
330 charname = charnames[0];
333 T1_OUTLINE * outline = T1_GetCharOutline(nr, c, 100.0, 0);
334 int firstx = outline->dest.x/0xffff;
336 font->ascii2glyph[s] = num;
337 font->glyph2ascii[num] = s;
339 /* fix bounding box */
342 shape2 = swf_ShapeToShape2(font->glyph[s].shape);
343 if(!shape2) { fprintf(stderr, "Shape parse error\n");exit(1);}
344 bbox = swf_GetShapeBoundingBox(shape2);
345 swf_Shape2Free(shape2);
346 font->layout->bounds[num] = bbox;
347 //font->glyph[num].advance = (int)(width/6.4); // 128/20
348 font->glyph[num].advance = bbox.xmax/20;
349 if(!font->glyph[num].advance) {
350 font->glyph[num].advance = firstx;
358 SWFFONT* swf_LoadT1Font(char*filename)
360 fprintf(stderr, "Warning: no t1lib- not able to load %s\n", filename);