📄 tfmtodit.cc
字号:
// -*- C++ -*-/* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc. Written by James Clark (jjc@jclark.com)This file is part of groff.groff is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public License as published by the FreeSoftware Foundation; either version 2, or (at your option) any laterversion.groff is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public Licensefor more details.You should have received a copy of the GNU General Public License alongwith groff; see the file COPYING. If not, write to the Free SoftwareFoundation, 675 Mass Ave, Cambridge, MA 02139, USA. *//* I have tried to incorporate the changes needed for TeX 3.0 tfm files,but I haven't tested them. *//* Groff requires more font metric information than TeX. The reasonfor this is that TeX has separate Math Italic fonts, whereas groffuses normal italic fonts for math. The two additional pieces ofinformation required by groff correspond to the two arguments to themath_fit() macro in the Metafont programs for the CM fonts. In thecase of a font for which math_fitting is false, these two argumentsare normally ignored by Metafont. We need to get hold of these twoparameters and put them in the groff font file.We do this by loading this definition after cmbase when creating cm.base.def ignore_math_fit(expr left_adjustment,right_adjustment) = special "adjustment"; numspecial left_adjustment*16/designsize; numspecial right_adjustment*16/designsize; enddef;This puts the two arguments to the math_fit macro into the gf file.(They will appear in the gf file immediately before the character towhich they apply.) We then create a gf file using this cm.base. Thenwe run tfmtodit and specify this gf file with the -g option.This need only be done for a font for which math_fitting is false;When it's true, the left_correction and subscript_correction shouldboth be zero. */#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#include <errno.h>#include "lib.h"#include "errarg.h"#include "error.h"#include "assert.h"#include "cset.h"/* Values in the tfm file should be multiplied by this. */#define MULTIPLIER 1struct char_info_word { unsigned char width_index; char height_index; char depth_index; char italic_index; char tag; unsigned char remainder;};struct lig_kern_command { unsigned char skip_byte; unsigned char next_char; unsigned char op_byte; unsigned char remainder;};class tfm { int bc; int ec; int nw; int nh; int nd; int ni; int nl; int nk; int np; int cs; int ds; char_info_word *char_info; int *width; int *height; int *depth; int *italic; lig_kern_command *lig_kern; int *kern; int *param;public: tfm(); ~tfm(); int load(const char *); int contains(int); int get_width(int); int get_height(int); int get_depth(int); int get_italic(int); int get_param(int, int *); int get_checksum(); int get_design_size(); int get_lig(unsigned char, unsigned char, unsigned char *); friend class kern_iterator;};class kern_iterator { tfm *t; int c; int i;public: kern_iterator(tfm *); int next(unsigned char *c1, unsigned char *c2, int *k);};kern_iterator::kern_iterator(tfm *p): t(p), i(-1), c(t->bc){}int kern_iterator::next(unsigned char *c1, unsigned char *c2, int *k){ for (; c <= t->ec; c++) if (t->char_info[c - t->bc].tag == 1) { if (i < 0) { i = t->char_info[c - t->bc].remainder; if (t->lig_kern[i].skip_byte > 128) i = (256*t->lig_kern[i].op_byte + t->lig_kern[i].remainder); } for (;;) { int skip = t->lig_kern[i].skip_byte; if (skip <= 128 && t->lig_kern[i].op_byte >= 128) { *c1 = c; *c2 = t->lig_kern[i].next_char; *k = t->kern[256*(t->lig_kern[i].op_byte - 128) + t->lig_kern[i].remainder]; if (skip == 128) { c++; i = -1; } else i += skip + 1; return 1; } if (skip >= 128) break; i += skip + 1; } i = -1; } return 0;} tfm::tfm(): char_info(0), width(0), height(0), depth(0), italic(0), lig_kern(0), kern(0), param(0){}int tfm::get_lig(unsigned char c1, unsigned char c2, unsigned char *cp){ if (contains(c1) && char_info[c1 - bc].tag == 1) { int i = char_info[c1 - bc].remainder; if (lig_kern[i].skip_byte > 128) i = 256*lig_kern[i].op_byte + lig_kern[i].remainder; for (;;) { int skip = lig_kern[i].skip_byte; if (skip > 128) break; // We are only interested in normal ligatures, for which // op_byte == 0. if (lig_kern[i].op_byte == 0 && lig_kern[i].next_char == c2) { *cp = lig_kern[i].remainder; return 1; } if (skip == 128) break; i += skip + 1; } } return 0;}int tfm::contains(int i){ return i >= bc && i <= ec && char_info[i - bc].width_index != 0;}int tfm::get_width(int i){ return width[char_info[i - bc].width_index];}int tfm::get_height(int i){ return height[char_info[i - bc].height_index];}int tfm::get_depth(int i){ return depth[char_info[i - bc].depth_index];}int tfm::get_italic(int i){ return italic[char_info[i - bc].italic_index];}int tfm::get_param(int i, int *p){ if (i <= 0 || i > np) return 0; else { *p = param[i - 1]; return 1; }}int tfm::get_checksum(){ return cs;}int tfm::get_design_size(){ return ds;}tfm::~tfm(){ a_delete char_info; a_delete width; a_delete height; a_delete depth; a_delete italic; a_delete lig_kern; a_delete kern; a_delete param;} int read2(unsigned char *&s){ int n; n = *s++ << 8; n |= *s++; return n;}int read4(unsigned char *&s){ int n; n = *s++ << 24; n |= *s++ << 16; n |= *s++ << 8; n |= *s++; return n;}int tfm::load(const char *file){ errno = 0; FILE *fp = fopen(file, "r"); if (!fp) { error("can't open `%1': %2", file, strerror(errno)); return 0; } int c1 = getc(fp); int c2 = getc(fp); if (c1 == EOF || c2 == EOF) { fclose(fp); error("unexpected end of file on `%1'", file); return 0; } int lf = (c1 << 8) + c2; int toread = lf*4 - 2; unsigned char *buf = new unsigned char[toread]; if (fread(buf, 1, toread, fp) != toread) { if (feof(fp)) error("unexpected end of file on `%1'", file); else error("error on file `%1'", file); a_delete buf; fclose(fp); return 0; } fclose(fp); if (lf < 6) { error("bad tfm file `%1': impossibly short", file); a_delete buf; return 0; } unsigned char *ptr = buf; int lh = read2(ptr); bc = read2(ptr); ec = read2(ptr); nw = read2(ptr); nh = read2(ptr); nd = read2(ptr); ni = read2(ptr); nl = read2(ptr); nk = read2(ptr); int ne = read2(ptr); np = read2(ptr); if (6 + lh + (ec - bc + 1) + nw + nh + nd + ni + nl + nk + ne + np != lf) { error("bad tfm file `%1': lengths do not sum", file); a_delete buf; return 0; } if (lh < 2) { error("bad tfm file `%1': header too short", file); a_delete buf; return 0; } char_info = new char_info_word[ec - bc + 1]; width = new int[nw]; height = new int[nh]; depth = new int[nd]; italic = new int[ni]; lig_kern = new lig_kern_command[nl]; kern = new int[nk]; param = new int[np]; int i; cs = read4(ptr); ds = read4(ptr); ptr += (lh-2)*4; for (i = 0; i < ec - bc + 1; i++) { char_info[i].width_index = *ptr++; unsigned char tem = *ptr++; char_info[i].depth_index = tem & 0xf; char_info[i].height_index = tem >> 4; tem = *ptr++; char_info[i].italic_index = tem >> 2; char_info[i].tag = tem & 3; char_info[i].remainder = *ptr++; } for (i = 0; i < nw; i++) width[i] = read4(ptr); for (i = 0; i < nh; i++) height[i] = read4(ptr); for (i = 0; i < nd; i++) depth[i] = read4(ptr); for (i = 0; i < ni; i++) italic[i] = read4(ptr); for (i = 0; i < nl; i++) { lig_kern[i].skip_byte = *ptr++; lig_kern[i].next_char = *ptr++; lig_kern[i].op_byte = *ptr++; lig_kern[i].remainder = *ptr++; } for (i = 0; i < nk; i++) kern[i] = read4(ptr); ptr += ne*4; for (i = 0; i < np; i++) param[i] = read4(ptr); assert(ptr == buf + lf*4 - 2); a_delete buf; return 1;}class gf { int left[256]; int right[256]; static int sread4(int *p, FILE *fp); static int uread3(int *p, FILE *fp); static int uread2(int *p, FILE *fp); static int skip(int n, FILE *fp);public: gf(); int load(const char *file); int get_left_adjustment(int i) { return left[i]; } int get_right_adjustment(int i) { return right[i]; }};gf::gf(){ for (int i = 0; i < 256; i++) left[i] = right[i] = 0;}int gf::load(const char *file){ enum { paint_0 = 0, paint1 = 64, boc = 67, boc1 = 68, eoc = 69, skip0 = 70, skip1 = 71, new_row_0 = 74, xxx1 = 239, yyy = 243, no_op = 244, pre = 247, post = 248 }; int got_an_adjustment = 0; int pending_adjustment = 0; int left_adj, right_adj; const int gf_id_byte = 131; errno = 0; FILE *fp = fopen(file, "r"); if (!fp) { error("can't open `%1': %2", file, strerror(errno)); return 0; } if (getc(fp) != pre || getc(fp) != gf_id_byte) { error("bad gf file"); return 0; } int n = getc(fp); if (n == EOF)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -