📄 ttf2bdf.c
字号:
* End the font and do memory cleanup on the glyph and raster structures. */ eof = fprintf(out, "ENDFONT\n"); return eof;}static int#ifdef __STDC__generate_bdf(FILE *out, char *iname, char *oname)#elsegenerate_bdf(out, iname, oname)FILE *out;char *iname, *oname;#endif{ TT_Long i; TT_UShort p, e; /* * Get the requested cmap. */ for (i = 0; i < TT_Get_CharMap_Count(face); i++) { if (!TT_Get_CharMap_ID(face, i, &p, &e) && p == pid && e == eid) break; } if (i == TT_Get_CharMap_Count(face) && pid == 3 && eid == 1) { /* * Make a special case when this fails with pid == 3 and eid == 1. * Change to eid == 0 and try again. This captures the two possible * cases for MS fonts. Some other method should be used to cycle * through all the alternatives later. */ for (i = 0; i < TT_Get_CharMap_Count(face); i++) { if (!TT_Get_CharMap_ID(face, i, &p, &e) && p == pid && e == 0) break; } if (i < TT_Get_CharMap_Count(face)) { if (!TT_Get_CharMap(face, i, &cmap)) eid = 0; else nocmap = 1; } } else { /* * A CMap was found for the platform and encoding IDs. */ if (i < TT_Get_CharMap_Count(face) && TT_Get_CharMap(face, i, &cmap)) nocmap = 1; else nocmap = 0; } if (nocmap && verbose) { fprintf(stderr, "%s: no character map for platform %d encoding %d. ", prog, pid, eid); fprintf(stderr, "Generating all glyphs.\n"); } /* * Now go through and generate the glyph bitmaps themselves. */ return generate_font(out, iname, oname);}#define isdig(cc) ((cc) >= '0' && (cc) <= '9')/* * Routine to parse a subset specification supplied on the command line. * The syntax for this specification is the same as the syntax used for * the XLFD font names (XLFD documentation, page 9). * * Example: * * "60 70 80_90" means the glyphs at codes 60, 70, and between 80 and * 90 inclusive. */static void#ifdef __STDC__parse_subset(char *s)#elseparse_subset(s)char *s;#endif{ long l, r; /* * Make sure to clear the flag and bitmap in case more than one subset is * specified on the command line. */ maxcode = 0; do_subset = 0; (void) memset((char *) subset, 0, sizeof(unsigned long) * 2048); while (*s) { /* * Collect the next code value. */ for (l = r = 0; *s && isdig(*s); s++) l = (l * 10) + (*s - '0'); /* * If the next character is an '_', advance and collect the end of the * specified range. */ if (*s == '_') { s++; for (; *s && isdig(*s); s++) r = (r * 10) + (*s - '0'); } else r = l; /* * Add the range just collected to the subset bitmap and set the flag * that indicates a subset is wanted. */ for (; l <= r; l++) { do_subset = 1; subset[l >> 5] |= (1 << (l & 31)); if (l > maxcode) maxcode = l; } /* * Skip all non-digit characters. */ while (*s && !isdig(*s)) s++; }}static void#ifdef __STDC__usage(int eval)#elseusage(eval)int eval;#endif{ fprintf(stderr, "Usage: %s [options below] font.ttf\n", prog); fprintf(stderr, "-h\t\tThis message.\n"); fprintf(stderr, "-v\t\tPrint warning messages during conversion.\n"); fprintf(stderr, "-l \"subset\"\tSpecify a subset of glyphs to generate.\n"); fprintf(stderr, "-m mapfile\tGlyph reencoding file.\n"); fprintf(stderr, "-n\t\tTurn off glyph hinting.\n"); fprintf(stderr, "-c c\t\tSet the character spacing (default: from font).\n"); fprintf(stderr, "-f name\t\tSet the foundry name (default: freetype).\n"); fprintf(stderr, "-t name\t\tSet the typeface name (default: from font).\n"); fprintf(stderr, "-w name\t\tSet the weight name (default: Medium).\n"); fprintf(stderr, "-s name\t\tSet the slant name (default: R).\n"); fprintf(stderr, "-k name\t\tSet the width name (default: Normal).\n"); fprintf(stderr, "-d name\t\tSet the additional style name (default: empty).\n"); fprintf(stderr, "-u char\t\tSet the character to replace '-' in names "); fprintf(stderr, "(default: space).\n"); fprintf(stderr, "-pid id\t\tSet the platform ID for encoding (default: %d).\n", DEFAULT_PLATFORM_ID); fprintf(stderr, "-eid id\t\tSet the encoding ID for encoding (default: %d).\n", DEFAULT_ENCODING_ID); fprintf(stderr, "-p n\t\tSet the point size (default: %dpt).\n", DEFAULT_POINT_SIZE); fprintf(stderr, "-r n\t\tSet the horizontal and vertical resolution "); fprintf(stderr, "(default: %ddpi).\n", DEFAULT_RESOLUTION); fprintf(stderr, "-rh n\t\tSet the horizontal resolution "); fprintf(stderr, "(default: %ddpi)\n", DEFAULT_RESOLUTION); fprintf(stderr, "-rv n\t\tSet the vertical resolution "); fprintf(stderr, "(default: %ddpi)\n", DEFAULT_RESOLUTION); fprintf(stderr, "-o outfile\tSet the output filename (default: stdout).\n"); exit(eval);}int#ifdef __STDC__main(int argc, char *argv[])#elsemain(argc, argv)int argc;char *argv[];#endif{ int res; char *infile, *outfile, *iname, *oname; FILE *out, *mapin; if ((prog = strrchr(argv[0], '/'))) prog++; else prog = argv[0]; out = stdout; infile = outfile = 0; argc--; argv++; while (argc > 0) { if (argv[0][0] == '-') { switch (argv[0][1]) { case 'v': case 'V': verbose = 1; break; case 'l': case 'L': argc--; argv++; parse_subset(argv[0]); break; case 'n': case 'N': load_flags &= ~TTLOAD_HINT_GLYPH; break; case 'c': case 'C': argc--; argv++; spacing = argv[0][0]; break; case 't': case 'T': argc--; argv++; face_name = argv[0]; break; case 'w': case 'W': argc--; argv++; weight_name = argv[0]; break; case 's': case 'S': argc--; argv++; slant_name = argv[0]; break; case 'k': case 'K': argc--; argv++; width_name = argv[0]; break; case 'd': case 'D': argc--; argv++; style_name = argv[0]; break; case 'f': case 'F': argc--; argv++; foundry_name = argv[0]; break; case 'u': case 'U': argc--; argv++; dashchar = argv[0][0]; break; case 'p': case 'P': res = argv[0][2]; argc--; argv++; if (res == 'i' || res == 'I') /* * Set the platform ID. */ pid = atoi(argv[0]); else /* * Set the point size. */ point_size = atoi(argv[0]); break; case 'e': case 'E': /* * Set the encoding ID. */ argc--; argv++; eid = atoi(argv[0]); break; case 'r': /* * Set the horizontal and vertical resolutions. */ if (argv[0][2] == 'h') hres = atoi(argv[1]); else if (argv[0][2] == 'v') vres = atoi(argv[1]); else hres = vres = atoi(argv[1]); argc--; argv++; break; case 'm': case 'M': /* * Try to load a remap table. */ argc--; argv++; /* * Always reset the `do_remap' variable here in case more than * one map file appears on the command line. */ do_remap = 0; if ((mapin = fopen(argv[0], "r")) == 0) fprintf(stderr, "%s: unable to open the remap table '%s'.\n", prog, argv[0]); else { if (ttf2bdf_load_map(mapin) < 0) { fprintf(stderr, "%s: problem loading remap table '%s'.\n", prog, argv[0]); do_remap = 0; } else do_remap = 1; fclose(mapin); } break; case 'o': case 'O': /* * Set the output file name. */ argc--; argv++; outfile = argv[0]; break; default: usage(1); } } else /* * Set the input file name. */ infile = argv[0]; argc--; argv++; } /* * Validate the values passed on the command line. */ if (infile == 0) { fprintf(stderr, "%s: no input file provided.\n", prog); usage(1); } /* * Set the input filename that will be passed to the generator * routine. */ if ((iname = strrchr(infile, '/'))) iname++; else iname = infile; /* * Check the platform and encoding IDs. */ if (pid < 0 || pid > 255) { fprintf(stderr, "%s: invalid platform ID '%d'.\n", prog, pid); exit(1); } if (eid < 0 || eid > 65535) { fprintf(stderr, "%s: invalid encoding ID '%d'.\n", prog, eid); exit(1); } /* * Arbitrarily limit the point size to a minimum of 2pt and maximum of * 256pt. */ if (point_size < 2 || point_size > 256) { fprintf(stderr, "%s: invalid point size '%dpt'.\n", prog, point_size); exit(1); } /* * Arbitrarily limit the resolutions to a minimum of 10dpi and a maximum * of 1200dpi. */ if (hres < 10 || hres > 1200) { fprintf(stderr, "%s: invalid horizontal resolution '%ddpi'.\n", prog, hres); exit(1); } if (vres < 10 || vres > 1200) { fprintf(stderr, "%s: invalid vertical resolution '%ddpi'.\n", prog, vres); exit(1); } /* * Open the output file if specified. */ if (outfile != 0) { /* * Attempt to open the output file. */ if ((out = fopen(outfile, "w")) == 0) { fprintf(stderr, "%s: unable to open the output file '%s'.\n", prog, outfile); exit(1); } /* * Set the output filename to be passed to the generator routine. */ if ((oname = strrchr(outfile, '/'))) oname++; else oname = outfile; } else /* * Set the default output file name to <stdout>. */ oname = "<stdout>"; /* * Intialize Freetype. */ if ((res = TT_Init_FreeType(&engine)) || (res = TT_Init_SBit_Extension(engine))) { /* * Close the output file. */ if (out != stdout) { fclose(out); (void) unlink(outfile); } fprintf(stderr, "%s[%d]: unable to initialize renderer.\n", prog, res); exit(1); } /* * Open the input file. */ if ((res = TT_Open_Face(engine, infile, &face))) { if (out != stdout) { fclose(out); (void) unlink(outfile); } fprintf(stderr, "%s[%d]: unable to open input file '%s'.\n", prog, res, infile); exit(1); } /* * Create a new instance. */ if ((res = TT_New_Instance(face, &instance))) { (void) TT_Close_Face(face); if (out != stdout) { fclose(out); (void) unlink(outfile); } fprintf(stderr, "%s[%d]: unable to create instance.\n", prog, res); exit(1); } /* * Set the instance resolution and point size and the relevant * metrics. */ (void) TT_Set_Instance_Resolutions(instance, hres, vres); (void) TT_Set_Instance_PointSize(instance, point_size); (void) TT_Get_Instance_Metrics(instance, &imetrics); /* * Check to see if the font has a strike (bitmaps) for the point size * resolution. If it does, then create storage for the bitmap image. */ have_strike = (TT_Get_SBit_Strike(face, instance, &strike)) ? 0 : 1; if (have_strike) /* * Create the SBIT image here because it will be used early in the * bitmap generattion code. */ (void) TT_New_SBit_Image(&sbit); /* * Get the face properties and set the global units per em value for * convenience. */ (void) TT_Get_Face_Properties(face, &properties); upm = properties.header->Units_Per_EM; if (!have_strike) { /* * Create a new glyph container. */ if ((res = TT_New_Glyph(face, &glyph))) { (void) TT_Done_Instance(instance); (void) TT_Close_Face(face); if (out != stdout) { fclose(out); (void) unlink(outfile); } fprintf(stderr, "%s[%d]: unable to create glyph.\n", prog, res); exit(1); } } /* * Generate the BDF font from the TrueType font. */ res = generate_bdf(out, iname, oname); /* * Free up the mapping table if one was loaded. */ ttf2bdf_free_map(); /* * Close the input and output files. */ (void) TT_Close_Face(face); if (out != stdout) { fclose(out); if (res < 0) /* * An error occured when generating the font, so delete the * output file. */ (void) unlink(outfile); } /* * Shut down the renderer. */ (void) TT_Done_FreeType(engine); exit(res); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -