/* read .pfb, write .pfa */ /* * Copyright (C) 1999 Roger Willcocks * rogerw@centipede.co.uk * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include /* if WRITE_PFB is defined, a one (text) section pfb is written */ //#define WRITE_PFB char buffer[30000]; char tempbuff[140]; char toPath[100]; int translate(int from, int to) { unsigned char packet[6]; unsigned long len; _lseek(from, 0, 0); #ifdef WRITE_PFB packet[0] = packet[1] = packet[2] = packet[3] = packet[4] = packet[5] = 0; _write(to, packet, 6); /* leave space for later */ #endif for (;;) { int got; if ((got = _read(from, packet, 6)) == 0) break; if (got < 2) return -1; if (packet[0] != 128) return -2; if (packet[1] == 3) break; if (got != 6) return -3; len = packet[5]; len <<= 8; len += packet[4]; len <<= 8; len += packet[3]; len <<= 8; len += packet[2]; #if 0 printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x (%d)\n", packet[0], packet[1], packet[2], packet[3], packet[4], packet[5], len); #endif if (packet[1] == 1) { /* text */ while (len > 0) { int i; got = _read(from, buffer, (len > sizeof(buffer) ? sizeof(buffer) : len)); if (got < 0) return -4; for (i = 0; i < got; i++) if (buffer[i] == '\r') buffer[i] = '\n'; _write(to, buffer, got); /* will do crlf translation */ len -= got; } } else if (packet[1] == 2) { /* binary */ while (len > 0) { int i = 0; int j = 0; got = _read(from, buffer, (len > sizeof(buffer) ? sizeof(buffer) : len)); if (got < 0) return -5; for (i = 0; i < got; i++) { tempbuff[j++] = "0123456789abcdef"[(buffer[i] >> 4) & 0xf]; tempbuff[j++] = "0123456789abcdef"[buffer[i] & 0xf]; if (j == 60 || i == got - 1) { tempbuff[j++] = '\n'; _write(to, tempbuff, j); j = 0; } } len -= got; } } } #ifdef WRITE_PFB packet[0] = 128; packet[1] = 3; _write(to, packet, 2); len = _lseek(to, 0L, 2) - 8; _lseek(to, 0, 0); packet[0] = 128; packet[1] = 1; packet[2] = len; len >>= 8; packet[3] = len; len >>= 8; packet[4] = len; len >>= 8; packet[5] = len; _write(to, packet, 6); #endif return 0; } void process(char *pfbFileName) { char *p, *q; char cc; int i, res; int fb = _open(pfbFileName, O_RDONLY|O_BINARY); int fa = 0; int got = _read(fb, buffer, 10000); if (got < 0) { fprintf(stderr, "%s: read error\n", pfbFileName); _close(fb); return; } buffer[got] = 0; for (i = 0; i < got; i++) if (buffer[i] == 0) buffer[i] = ' '; p = strstr(buffer + 6, "/FontName /"); q = 0; if (p) { p += 11; q = strstr(p, " def"); } if (!p || !q) { fprintf(stderr, "%s: can't find font name\n", pfbFileName); _close(fb); return; } cc = *q; *q = 0; strcpy(tempbuff, toPath); strcat(tempbuff, "\\"); #ifdef WRITE_PFB strcat(tempbuff, pfbFileName); #else strcat(tempbuff, p); #endif *q = cc; fa = _open(tempbuff, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); if (fa == 0) { fprintf(stderr, "%s: can't create\n", tempbuff); _close(fb); return; } fprintf(stderr, "%s -> %s", pfbFileName, tempbuff); if ((res = translate(fb, fa)) != 0) fprintf(stderr, " << error %d >>", res); fprintf(stderr, "\n"); _close(fa); _close(fb); } main(int argc, char *argv[]) { struct _finddata_t fdata; int status = 0; if (argc < 3) { fprintf(stderr, "usage: pfbtopfa \n"); return 0; } argv++; argc--; if (argc) { strcpy(toPath, argv[0]); argv++; argc--; } while (argc) { int done = 0; long tag = _findfirst(argv[0], &fdata); if (tag == -1) { fprintf(stderr, "%s: no match\n", argv[0]); status = 1; } argv++; argc--; while (! done) { process(fdata.name); done = _findnext(tag, &fdata); } _findclose(tag); } return status; }