| 1 | /*************************************************************** |
|---|
| 2 | ** |
|---|
| 3 | ** Broadcom Corp. Confidential |
|---|
| 4 | ** Copyright 2002 Broadcom Corp. All Rights Reserved. |
|---|
| 5 | ** |
|---|
| 6 | ** THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED |
|---|
| 7 | ** SOFTWARE LICENSE AGREEMENT BETWEEN THE USER AND BROADCOM. |
|---|
| 8 | ** YOU HAVE NO RIGHT TO USE OR EXPLOIT THIS MATERIAL EXCEPT |
|---|
| 9 | ** SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 10 | ** |
|---|
| 11 | ** Description: Create SGIL bitmap fonts from freetype fonts. |
|---|
| 12 | ** |
|---|
| 13 | ** Created: 8/22/2002 by Jeffrey P. Fisher |
|---|
| 14 | ** |
|---|
| 15 | ** |
|---|
| 16 | ** |
|---|
| 17 | ****************************************************************/ |
|---|
| 18 | |
|---|
| 19 | #include <stdio.h> |
|---|
| 20 | #include <ft2build.h> |
|---|
| 21 | //#include FT_FREETYPE_H |
|---|
| 22 | #include "freetype/config/ftheader.h" |
|---|
| 23 | #include "freetype/freetype.h" |
|---|
| 24 | #include "freetype/ftglyph.h" |
|---|
| 25 | #include "utf8lib.c" /* Function for converting ASCII to UTF8 */ |
|---|
| 26 | |
|---|
| 27 | static int unicode = ft_encoding_unicode; |
|---|
| 28 | |
|---|
| 29 | #define FILTER_CHINESE_FONT_LEVEL2 |
|---|
| 30 | |
|---|
| 31 | #ifdef FILTER_CHINESE_FONT_LEVEL2 |
|---|
| 32 | #define FILTER_CHINESE_FONT_SET |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #ifdef FILTER_CHINESE_FONT_LEVEL2 |
|---|
| 36 | #ifdef FILTER_CHINESE_FONT_SET |
|---|
| 37 | #include <DTA_font_set.c> |
|---|
| 38 | int in_filter_list(unsigned short charcode) |
|---|
| 39 | { |
|---|
| 40 | int i; |
|---|
| 41 | |
|---|
| 42 | if (charcode < 0xa4a1) |
|---|
| 43 | return 1; |
|---|
| 44 | |
|---|
| 45 | for (i = 0; i < g_DTA_font_set_size; i++) { |
|---|
| 46 | if (g_DTA_font_set[i] == charcode) |
|---|
| 47 | return 1; |
|---|
| 48 | } |
|---|
| 49 | return 0; |
|---|
| 50 | } |
|---|
| 51 | #endif |
|---|
| 52 | |
|---|
| 53 | int in_chinese_font_level2(unsigned short charcode) |
|---|
| 54 | { |
|---|
| 55 | if (ft_encoding_gb2312 == unicode) { |
|---|
| 56 | /* take first set only to reduce size */ |
|---|
| 57 | if (charcode >= 0xd8a0) |
|---|
| 58 | return 1; |
|---|
| 59 | } |
|---|
| 60 | return 0; |
|---|
| 61 | } |
|---|
| 62 | #endif |
|---|
| 63 | |
|---|
| 64 | typedef struct fontripper_t |
|---|
| 65 | { |
|---|
| 66 | FILE *out_fp; /* Output file pointer */ |
|---|
| 67 | FILE *log_fp; /* Log file pointer */ |
|---|
| 68 | int type; /* Type of bitmap font to generate 0-mono,1-grayscale */ |
|---|
| 69 | FT_Library ft_library; /* Freetype library reference */ |
|---|
| 70 | FT_Face face; /* Typeface reference */ |
|---|
| 71 | int ascender; /* The font accent in pixels */ |
|---|
| 72 | int descender; /* The font descent in pixels */ |
|---|
| 73 | int height; /* The font height (size) */ |
|---|
| 74 | int maxadvance; /* Maximum horizontal distance to advance after drawing a character in this font */ |
|---|
| 75 | }fontripper_t; |
|---|
| 76 | |
|---|
| 77 | /**************************************************************** |
|---|
| 78 | * INPUTS: fr_p - font ripper parameters |
|---|
| 79 | * OUTPUTS: none |
|---|
| 80 | * RETURNS: number of glyph entries |
|---|
| 81 | * DESCRIPTION: Get the maximum number of glyph index entries. |
|---|
| 82 | * |
|---|
| 83 | ****************************************************************/ |
|---|
| 84 | |
|---|
| 85 | int get_max_glyph_index(fontripper_t *fr_p) |
|---|
| 86 | { |
|---|
| 87 | FT_ULong charcode; |
|---|
| 88 | FT_UInt gindex; |
|---|
| 89 | int cnt = 0; |
|---|
| 90 | |
|---|
| 91 | charcode = FT_Get_First_Char( fr_p->face, &gindex ); |
|---|
| 92 | while ( gindex != 0 ) |
|---|
| 93 | { |
|---|
| 94 | #ifdef FILTER_CHINESE_FONT_LEVEL2 |
|---|
| 95 | if (in_chinese_font_level2(charcode)) |
|---|
| 96 | break; |
|---|
| 97 | #ifdef FILTER_CHINESE_FONT_SET |
|---|
| 98 | if (!in_filter_list(charcode)) { |
|---|
| 99 | charcode = FT_Get_Next_Char( fr_p->face, charcode, &gindex ); |
|---|
| 100 | continue; |
|---|
| 101 | } |
|---|
| 102 | #endif |
|---|
| 103 | #endif |
|---|
| 104 | ++cnt; |
|---|
| 105 | charcode = FT_Get_Next_Char( fr_p->face, charcode, &gindex ); |
|---|
| 106 | } |
|---|
| 107 | return cnt; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /**************************************************************** |
|---|
| 111 | * INPUTS: fr_p - font ripper parameters |
|---|
| 112 | * OUTPUTS: none |
|---|
| 113 | * RETURNS: non-zero on failure |
|---|
| 114 | * DESCRIPTION: Output glyph index. |
|---|
| 115 | * |
|---|
| 116 | ****************************************************************/ |
|---|
| 117 | |
|---|
| 118 | int output_glyph_index(fontripper_t *fr_p) |
|---|
| 119 | { |
|---|
| 120 | FT_ULong charcode; |
|---|
| 121 | FT_UInt gindex; |
|---|
| 122 | int entries = get_max_glyph_index(fr_p); |
|---|
| 123 | |
|---|
| 124 | printf("Entries=%d\n", entries); |
|---|
| 125 | /* Write number of entries */ |
|---|
| 126 | if (fwrite(&entries,1,sizeof(entries),fr_p->out_fp) != sizeof(entries)) |
|---|
| 127 | return -1; |
|---|
| 128 | |
|---|
| 129 | charcode = FT_Get_First_Char( fr_p->face, &gindex ); |
|---|
| 130 | while ( gindex != 0 ) |
|---|
| 131 | { |
|---|
| 132 | #ifdef FILTER_CHINESE_FONT_LEVEL2 |
|---|
| 133 | if (in_chinese_font_level2(charcode)) |
|---|
| 134 | break; |
|---|
| 135 | #ifdef FILTER_CHINESE_FONT_SET |
|---|
| 136 | if (!in_filter_list(charcode)) { |
|---|
| 137 | charcode = FT_Get_Next_Char( fr_p->face, charcode, &gindex ); |
|---|
| 138 | continue; |
|---|
| 139 | } |
|---|
| 140 | #endif |
|---|
| 141 | #endif |
|---|
| 142 | if (fwrite(&charcode,1,sizeof(FT_ULong),fr_p->out_fp) != sizeof(FT_ULong)) |
|---|
| 143 | return -1; |
|---|
| 144 | |
|---|
| 145 | charcode = FT_Get_Next_Char( fr_p->face, charcode, &gindex ); |
|---|
| 146 | } |
|---|
| 147 | return 0; |
|---|
| 148 | } |
|---|
| 149 | /**************************************************************** |
|---|
| 150 | * INPUTS: fr_p - font ripper parameters |
|---|
| 151 | * OUTPUTS: none |
|---|
| 152 | * RETURNS: non-zero on failure |
|---|
| 153 | * DESCRIPTION: Create and ouput bitmap font. |
|---|
| 154 | * |
|---|
| 155 | ****************************************************************/ |
|---|
| 156 | |
|---|
| 157 | int output_font(fontripper_t *fr_p) |
|---|
| 158 | { |
|---|
| 159 | FT_ULong charcode; |
|---|
| 160 | FT_UInt gindex; |
|---|
| 161 | FT_Int ld_flags; |
|---|
| 162 | char left; /* horizontal offset to character start location */ |
|---|
| 163 | char top; /* vertical offset to character start location */ |
|---|
| 164 | char advance; /* horizontal offset to next character start location */ |
|---|
| 165 | unsigned char width; /* width of the rendered character bitmap in pixels */ |
|---|
| 166 | unsigned char height; /* height of the rendered character bitmap in pixels */ |
|---|
| 167 | unsigned char pitch; /* number of bytes in each bitmap row */ |
|---|
| 168 | int ascender; /* The font accent in pixels */ |
|---|
| 169 | int descender; /* The font descent in pixels */ |
|---|
| 170 | int f_height; /* The font height (size) */ |
|---|
| 171 | int maxadvance; /* Maximum horizontal distance to advance after drawing a character in this font */ |
|---|
| 172 | unsigned long format; /* The format flags for this font */ |
|---|
| 173 | |
|---|
| 174 | /* Output font info */ |
|---|
| 175 | ascender = fr_p->face->size->metrics.ascender >> 6; |
|---|
| 176 | descender = fr_p->face->size->metrics.descender >> 6; |
|---|
| 177 | f_height = (fr_p->face->size->metrics.ascender - |
|---|
| 178 | fr_p->face->size->metrics.descender) >> 6; |
|---|
| 179 | maxadvance = fr_p->face->size->metrics.max_advance >> 6; |
|---|
| 180 | format = (fr_p->type == 0) ? 1 : 0; |
|---|
| 181 | if (fwrite(&ascender,1,sizeof(ascender),fr_p->out_fp) != sizeof(ascender)) |
|---|
| 182 | return -1; |
|---|
| 183 | if (fwrite(&descender,1,sizeof(descender),fr_p->out_fp) != sizeof(descender)) |
|---|
| 184 | return -1; |
|---|
| 185 | if (fwrite(&f_height,1,sizeof(f_height),fr_p->out_fp) != sizeof(f_height)) |
|---|
| 186 | return -1; |
|---|
| 187 | if (fwrite(&maxadvance,1,sizeof(maxadvance),fr_p->out_fp) != sizeof(maxadvance)) |
|---|
| 188 | return -1; |
|---|
| 189 | if (fwrite(&format,1,sizeof(format),fr_p->out_fp) != sizeof(format)) |
|---|
| 190 | return -1; |
|---|
| 191 | |
|---|
| 192 | /* Output the glyph character index */ |
|---|
| 193 | if (output_glyph_index(fr_p)) |
|---|
| 194 | return -1; |
|---|
| 195 | |
|---|
| 196 | ld_flags = (FT_Int) fr_p->face->generic.data; |
|---|
| 197 | ld_flags |= FT_LOAD_RENDER; |
|---|
| 198 | |
|---|
| 199 | charcode = FT_Get_First_Char( fr_p->face, &gindex ); |
|---|
| 200 | while ( gindex != 0 ) |
|---|
| 201 | { |
|---|
| 202 | #ifdef FILTER_CHINESE_FONT_LEVEL2 |
|---|
| 203 | if (in_chinese_font_level2(charcode)) |
|---|
| 204 | break; |
|---|
| 205 | #ifdef FILTER_CHINESE_FONT_SET |
|---|
| 206 | if (!in_filter_list(charcode)) { |
|---|
| 207 | charcode = FT_Get_Next_Char( fr_p->face, charcode, &gindex ); |
|---|
| 208 | continue; |
|---|
| 209 | } |
|---|
| 210 | #endif |
|---|
| 211 | #endif |
|---|
| 212 | if (FT_Load_Glyph( fr_p->face, gindex, ld_flags )) |
|---|
| 213 | return -1; |
|---|
| 214 | /* Re-asign to more sane types for compactness */ |
|---|
| 215 | left = (char)fr_p->face->glyph->bitmap_left; |
|---|
| 216 | top = (char)fr_p->face->glyph->bitmap_top; |
|---|
| 217 | advance = (char)(fr_p->face->glyph->advance.x >> 6); |
|---|
| 218 | width = (unsigned char)fr_p->face->glyph->bitmap.width; |
|---|
| 219 | height = (unsigned char)fr_p->face->glyph->bitmap.rows; |
|---|
| 220 | pitch = (unsigned char)fr_p->face->glyph->bitmap.pitch; |
|---|
| 221 | |
|---|
| 222 | if (fwrite(&left,1,sizeof(left),fr_p->out_fp) != sizeof(left)) |
|---|
| 223 | return -1; |
|---|
| 224 | |
|---|
| 225 | if (fwrite(&top,1,sizeof(top),fr_p->out_fp) != sizeof(top)) |
|---|
| 226 | return -1; |
|---|
| 227 | |
|---|
| 228 | if (fwrite(&advance,1,sizeof(advance),fr_p->out_fp) != sizeof(advance)) |
|---|
| 229 | return -1; |
|---|
| 230 | |
|---|
| 231 | if (fwrite(&width,1,sizeof(width),fr_p->out_fp) != sizeof(width)) |
|---|
| 232 | return -1; |
|---|
| 233 | |
|---|
| 234 | if (fwrite(&height,1,sizeof(height),fr_p->out_fp) != sizeof(height)) |
|---|
| 235 | return -1; |
|---|
| 236 | |
|---|
| 237 | if (fwrite(&pitch,1,sizeof(pitch),fr_p->out_fp) != sizeof(pitch)) |
|---|
| 238 | return -1; |
|---|
| 239 | |
|---|
| 240 | if (fwrite(fr_p->face->glyph->bitmap.buffer,1,(pitch * height),fr_p->out_fp) != (pitch * height)) |
|---|
| 241 | return -1; |
|---|
| 242 | |
|---|
| 243 | fprintf(fr_p->log_fp,"Char: 0x%04x,%d,%d,%d,%d,%d,%d,%d\n",charcode,gindex,top,left,width,height,advance,top+height); |
|---|
| 244 | |
|---|
| 245 | charcode = FT_Get_Next_Char( fr_p->face, charcode, &gindex ); |
|---|
| 246 | } |
|---|
| 247 | return 0; |
|---|
| 248 | } |
|---|
| 249 | /**************************************************************** |
|---|
| 250 | * INPUTS: arc - argument count |
|---|
| 251 | * argv - array of arguments |
|---|
| 252 | * OUTPUTS: none |
|---|
| 253 | * RETURNS: non-zero on failure |
|---|
| 254 | * DESCRIPTION: Main entry point convert fonts to raw bitmap. |
|---|
| 255 | * |
|---|
| 256 | ****************************************************************/ |
|---|
| 257 | |
|---|
| 258 | int main(int argc, char *argv[]) |
|---|
| 259 | { |
|---|
| 260 | int i,italic; |
|---|
| 261 | int fcnt = 0; |
|---|
| 262 | int fsize[8]; |
|---|
| 263 | static unsigned char outname[1024]; |
|---|
| 264 | fontripper_t fr; |
|---|
| 265 | memset(&fr,0,sizeof(fr)); |
|---|
| 266 | |
|---|
| 267 | fr.log_fp = stdout; |
|---|
| 268 | italic = 0; |
|---|
| 269 | |
|---|
| 270 | /* Initialize the freetype library */ |
|---|
| 271 | if (FT_Init_FreeType( &fr.ft_library )) |
|---|
| 272 | { |
|---|
| 273 | fprintf(fr.log_fp,"Error initializing freetype library.\n"); |
|---|
| 274 | return -1; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /* Check argument count and ouput usage */ |
|---|
| 278 | if (argc < 3) |
|---|
| 279 | { |
|---|
| 280 | fprintf(fr.log_fp,"USAGE: fontripper <font filename> <typeface name> [-s <size> -t <0|1>] [ -c ]\n"); |
|---|
| 281 | fprintf(fr.log_fp,"\t-s <size> specify the size of the bitmap font (default 24)\n"); |
|---|
| 282 | fprintf(fr.log_fp,"\t-t <0|1> specify the bitmap font type (0-mono,1-greyscale))\n"); |
|---|
| 283 | fprintf(fr.log_fp,"\t-i apply horizontal hear to simulate italic\n"); |
|---|
| 284 | fprintf(fr.log_fp,"\t-c to convert a Chinese True Font with GB2312 standard\n"); |
|---|
| 285 | goto done; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /* Parse arguments */ |
|---|
| 289 | for (i = 3; i < argc; i += 2) |
|---|
| 290 | { |
|---|
| 291 | if ((strcmp(argv[i],"-s") == 0) || (strcmp(argv[i],"-S") == 0)) |
|---|
| 292 | { |
|---|
| 293 | sscanf(argv[i + 1],"%d",&fsize[fcnt]); |
|---|
| 294 | printf("Font Size %d\n",fsize[fcnt]); |
|---|
| 295 | fcnt++; |
|---|
| 296 | } |
|---|
| 297 | else if ((strcmp(argv[i],"-i") == 0) || (strcmp(argv[i],"-I") == 0)) |
|---|
| 298 | { |
|---|
| 299 | italic = 1; |
|---|
| 300 | printf("Italic.\n"); |
|---|
| 301 | } |
|---|
| 302 | else if ((strcmp(argv[i],"-t") == 0) || (strcmp(argv[i],"-T") == 0)) |
|---|
| 303 | { |
|---|
| 304 | sscanf(argv[i + 1],"%d",&fr.type); |
|---|
| 305 | printf("Font Type %s\n",(fr.type == 0) ? "mono":"grayscale"); |
|---|
| 306 | } |
|---|
| 307 | else if ((strcmp(argv[i],"-c") == 0) || (strcmp(argv[i],"-C") == 0)) |
|---|
| 308 | { |
|---|
| 309 | unicode = ft_encoding_gb2312; |
|---|
| 310 | } |
|---|
| 311 | if (fcnt >= 8) |
|---|
| 312 | break; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | /* Set defaults */ |
|---|
| 316 | if (fcnt == 0) |
|---|
| 317 | { |
|---|
| 318 | fcnt = 1; |
|---|
| 319 | fsize[0] = 24; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | /* Create freetype typeface */ |
|---|
| 323 | printf("Open %s\n",argv[1]); |
|---|
| 324 | if (FT_New_Face( fr.ft_library, argv[1],0, &fr.face )) |
|---|
| 325 | { |
|---|
| 326 | fprintf(fr.log_fp,"Error creating font fr.face %s\n",argv[1]); |
|---|
| 327 | goto done; |
|---|
| 328 | } |
|---|
| 329 | printf("Opened %s, 0x%08x\n",argv[1],fr.face); |
|---|
| 330 | #if 1 |
|---|
| 331 | printf("Num Faces: %d\n",fr.face->num_faces); |
|---|
| 332 | printf("Face Index: %d\n",fr.face->face_index); |
|---|
| 333 | printf("Family: %s\n",fr.face->family_name); |
|---|
| 334 | printf("Style: %s\n",fr.face->style_name); |
|---|
| 335 | printf("Face Flags: %d\n",fr.face->face_flags); |
|---|
| 336 | printf("Style Flags: %d\n",fr.face->style_flags); |
|---|
| 337 | printf("Units per EM: %d\n",fr.face->units_per_EM); |
|---|
| 338 | #endif |
|---|
| 339 | if (FT_Select_Charmap( fr.face, unicode )) |
|---|
| 340 | { |
|---|
| 341 | fprintf(fr.log_fp,"Error font fr.face %s does not have unicode encoding\n",argv[1]); |
|---|
| 342 | goto done; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | if (italic) |
|---|
| 346 | { |
|---|
| 347 | FT_Matrix matrix; |
|---|
| 348 | |
|---|
| 349 | matrix.xx = 0x10000L; |
|---|
| 350 | matrix.xy = 0x10000L * 0.12; |
|---|
| 351 | matrix.yx = 0; |
|---|
| 352 | matrix.yy = 0x10000L; |
|---|
| 353 | |
|---|
| 354 | FT_Set_Transform( fr.face, &matrix,0 ); |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | /* Loop through sizes and ouput bitmap formats */ |
|---|
| 358 | for (i = 0; i < fcnt; ++i) |
|---|
| 359 | { |
|---|
| 360 | if (FT_Set_Pixel_Sizes( fr.face, 0, fsize[i])) |
|---|
| 361 | { |
|---|
| 362 | fprintf(fr.log_fp,"Could net set font fr.face %s size %d\n",argv[1],fsize[i]); |
|---|
| 363 | goto done; |
|---|
| 364 | } |
|---|
| 365 | printf("x_scale: %d\n",fr.face->size->metrics.x_scale); |
|---|
| 366 | printf("y_scale: %d\n",fr.face->size->metrics.y_scale); |
|---|
| 367 | printf("Underline: %d\n",FT_MulFix(fr.face->underline_position,fr.face->size->metrics.y_scale)/64); |
|---|
| 368 | printf("Thickness: %d\n",FT_MulFix(fr.face->underline_thickness,fr.face->size->metrics.y_scale)/64); |
|---|
| 369 | |
|---|
| 370 | if ( fr.type == 0) |
|---|
| 371 | fr.face->generic.data = (void *) (FT_LOAD_DEFAULT | FT_LOAD_MONOCHROME); |
|---|
| 372 | else |
|---|
| 373 | fr.face->generic.data = (void *) FT_LOAD_DEFAULT; |
|---|
| 374 | |
|---|
| 375 | /* Create file to save font too */ |
|---|
| 376 | sprintf(outname,"%s_%d_%s.bff",argv[2],fsize[i],( fr.type == 0) ? "mono":"aa"); |
|---|
| 377 | fr.out_fp = fopen(outname, "wb"); |
|---|
| 378 | if (! fr.out_fp) |
|---|
| 379 | { |
|---|
| 380 | fprintf(fr.log_fp,"Error opening file %s\n",outname); |
|---|
| 381 | goto done; |
|---|
| 382 | } |
|---|
| 383 | /* output the font definition */ |
|---|
| 384 | output_font(&fr); |
|---|
| 385 | |
|---|
| 386 | fclose( fr.out_fp); |
|---|
| 387 | fr.out_fp = NULL; |
|---|
| 388 | } |
|---|
| 389 | done: |
|---|
| 390 | |
|---|
| 391 | /* do freetype cleanup */ |
|---|
| 392 | FT_Done_FreeType( fr.ft_library ); |
|---|
| 393 | |
|---|
| 394 | if ( fr.out_fp) |
|---|
| 395 | fclose( fr.out_fp); |
|---|
| 396 | |
|---|
| 397 | return 0; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | |
|---|