📄 fontfile.cc
字号:
ptr += 2; sprintf(buf, "dup %d /", c); (*outputFunc)(outputStream, buf, strlen(buf)); getString(sid, buf); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, " put\n", 5); } } } (*outputFunc)(outputStream, "readonly def\n", 13); } (*outputFunc)(outputStream, "currentdict end\n", 16); // start the binary section (*outputFunc)(outputStream, "currentfile eexec\n", 18); r1 = 55665; line = 0; // get private dictionary eexecWrite("\x83\xca\x73\xd5"); eexecWrite("dup /Private 32 dict dup begin\n"); eexecWrite("/RD {string currentfile exch readstring pop} executeonly def\n"); eexecWrite("/ND {noaccess def} executeonly def\n"); eexecWrite("/NP {noaccess put} executeonly def\n"); eexecWrite("/MinFeature {16 16} ND\n"); readPrivateDict(&privateDict, dict.privateOffset, dict.privateSize); eexecWrite(privateDict.dictData->getCString()); defaultWidthX = privateDict.defaultWidthX; defaultWidthXFP = privateDict.defaultWidthXFP; nominalWidthX = privateDict.nominalWidthX; nominalWidthXFP = privateDict.nominalWidthXFP; // get subrs if (privateDict.subrsOffset != 0) { subrsIdxPtr = (Guchar *)file + dict.privateOffset + privateDict.subrsOffset; nSubrs = getIndexLen(subrsIdxPtr); sprintf(eBuf, "/Subrs %d array\n", nSubrs); eexecWrite(eBuf); idxPtr1 = getIndexValPtr(subrsIdxPtr, 0); for (i = 0; i < nSubrs; ++i) { idxPtr0 = idxPtr1; idxPtr1 = getIndexValPtr(subrsIdxPtr, i+1); n = idxPtr1 - idxPtr0;#if 1 //~ Type 2 subrs are unimplemented error(-1, "Unimplemented Type 2 subrs");#else sprintf(eBuf, "dup %d %d RD ", i, n); eexecWrite(eBuf); eexecCvtGlyph(idxPtr0, n); eexecWrite(" NP\n");#endif } eexecWrite("ND\n"); } // get CharStrings charStringsIdxPtr = (Guchar *)file + dict.charStrings; nCharStrings = getIndexLen(charStringsIdxPtr); sprintf(eBuf, "2 index /CharStrings %d dict dup begin\n", nCharStrings); eexecWrite(eBuf); idxPtr1 = getIndexValPtr(charStringsIdxPtr, 0); for (i = 0; i < nCharStrings; ++i) { idxPtr0 = idxPtr1; idxPtr1 = getIndexValPtr(charStringsIdxPtr, i+1); n = idxPtr1 - idxPtr0; eexecCvtGlyph(getString(glyphNames[i], buf), idxPtr0, n); } eexecWrite("end\n"); eexecWrite("end\n"); eexecWrite("readonly put\n"); eexecWrite("noaccess put\n"); eexecWrite("dup /FontName get exch definefont pop\n"); eexecWrite("mark currentfile closefile\n"); // trailer if (line > 0) { (*outputFunc)(outputStream, "\n", 1); } for (i = 0; i < 8; ++i) { (*outputFunc)(outputStream, "0000000000000000000000000000000000000000000000000000000000000000\n", 65); } (*outputFunc)(outputStream, "cleartomark\n", 12); // clean up delete privateDict.dictData; if (dict.charset > 2) { gfree(glyphNames); }}void Type1CFontFile::convertToCIDType0(char *psName, FontFileOutputFunc outputFuncA, void *outputStreamA) { Type1CTopDict dict; Type1CPrivateDict *privateDicts; GString *charStrings; int *charStringOffsets; Gushort *charset; int *cidMap; Guchar *fdSelect; Guchar *charStringsIdxPtr, *fdArrayIdx, *idxPtr0, *idxPtr1, *ptr; char buf[512], buf2[16]; int nGlyphs, nCIDs, gdBytes, nFDs; int fdSelectFmt, nRanges, gid0, gid1, fd, offset; int key; double x; GBool isFP; int i, j, k, n; outputFunc = outputFuncA; outputStream = outputStreamA; (*outputFunc)(outputStream, "/CIDInit /ProcSet findresource begin\n", 37); // read top dict (first font only) readTopDict(&dict); // read the FDArray dictionaries and Private dictionaries if (dict.fdArrayOffset == 0) { nFDs = 1; privateDicts = (Type1CPrivateDict *) gmalloc(nFDs * sizeof(Type1CPrivateDict)); privateDicts[0].dictData = new GString(); privateDicts[0].subrsOffset = 0; privateDicts[0].defaultWidthX = 0; privateDicts[0].defaultWidthXFP = gFalse; privateDicts[0].nominalWidthX = 0; privateDicts[0].nominalWidthXFP = gFalse; } else { fdArrayIdx = (Guchar *)file + dict.fdArrayOffset; nFDs = getIndexLen(fdArrayIdx); privateDicts = (Type1CPrivateDict *) gmalloc(nFDs * sizeof(Type1CPrivateDict)); idxPtr1 = getIndexValPtr(fdArrayIdx, 0); for (i = 0; i < nFDs; ++i) { privateDicts[i].dictData = NULL; idxPtr0 = idxPtr1; idxPtr1 = getIndexValPtr(fdArrayIdx, i + 1); ptr = idxPtr0; j = 0; while (ptr < idxPtr1) { if (*ptr <= 27 || *ptr == 31) { key = *ptr++; if (key == 0x0c) { key = (key << 8) | *ptr++; } if (key == 0x0012) { readPrivateDict(&privateDicts[i], (int)op[1], (int)op[0]); } j = 0; } else { x = getNum(&ptr, &isFP); if (j < 48) { op[j] = x; fp[j++] = isFP; } } } if (!privateDicts[i].dictData) { privateDicts[i].dictData = new GString(); privateDicts[i].subrsOffset = 0; privateDicts[i].defaultWidthX = 0; privateDicts[i].defaultWidthXFP = gFalse; privateDicts[i].nominalWidthX = 0; privateDicts[i].nominalWidthXFP = gFalse; } } } // get the glyph count charStringsIdxPtr = (Guchar *)file + dict.charStrings; nGlyphs = getIndexLen(charStringsIdxPtr); // read the FDSelect table fdSelect = (Guchar *)gmalloc(nGlyphs); if (dict.fdSelectOffset == 0) { for (i = 0; i < nGlyphs; ++i) { fdSelect[i] = 0; } } else { ptr = (Guchar *)file + dict.fdSelectOffset; fdSelectFmt = *ptr++; if (fdSelectFmt == 0) { memcpy(fdSelect, ptr, nGlyphs); } else if (fdSelectFmt == 3) { nRanges = getWord(ptr, 2); ptr += 2; gid0 = getWord(ptr, 2); ptr += 2; for (i = 1; i <= nRanges; ++i) { fd = *ptr++; gid1 = getWord(ptr, 2); ptr += 2; for (j = gid0; j < gid1; ++j) { fdSelect[j] = fd; } gid0 = gid1; } } else { error(-1, "Unknown FDSelect table format in CID font"); for (i = 0; i < nGlyphs; ++i) { fdSelect[i] = 0; } } } // read the charset, compute the CID-to-GID mapping charset = readCharset(dict.charset, nGlyphs); nCIDs = 0; for (i = 0; i < nGlyphs; ++i) { if (charset[i] >= nCIDs) { nCIDs = charset[i] + 1; } } cidMap = (int *)gmalloc(nCIDs * sizeof(int)); for (i = 0; i < nCIDs; ++i) { cidMap[i] = -1; } for (i = 0; i < nGlyphs; ++i) { cidMap[charset[i]] = i; } // build the charstrings charStrings = new GString(); charStringOffsets = (int *)gmalloc((nCIDs + 1) * sizeof(int)); for (i = 0; i < nCIDs; ++i) { charStringOffsets[i] = charStrings->getLength(); if (cidMap[i] >= 0) { idxPtr0 = getIndexValPtr(charStringsIdxPtr, cidMap[i]); idxPtr1 = getIndexValPtr(charStringsIdxPtr, cidMap[i]+1); n = idxPtr1 - idxPtr0; j = fdSelect[cidMap[i]]; defaultWidthX = privateDicts[j].defaultWidthX; defaultWidthXFP = privateDicts[j].defaultWidthXFP; nominalWidthX = privateDicts[j].nominalWidthX; nominalWidthXFP = privateDicts[j].nominalWidthXFP; cvtGlyph(idxPtr0, n); charStrings->append(charBuf); delete charBuf; } } charStringOffsets[nCIDs] = charStrings->getLength(); // compute gdBytes = number of bytes needed for charstring offsets // (offset size needs to account for the charstring offset table, // with a worst case of five bytes per entry, plus the charstrings // themselves) i = (nCIDs + 1) * 5 + charStrings->getLength(); if (i < 0x100) { gdBytes = 1; } else if (i < 0x10000) { gdBytes = 2; } else if (i < 0x1000000) { gdBytes = 3; } else { gdBytes = 4; } // begin the font dictionary (*outputFunc)(outputStream, "20 dict begin\n", 14); (*outputFunc)(outputStream, "/CIDFontName /", 14); (*outputFunc)(outputStream, psName, strlen(psName)); (*outputFunc)(outputStream, " def\n", 5); (*outputFunc)(outputStream, "/CIDFontType 0 def\n", 19); (*outputFunc)(outputStream, "/CIDSystemInfo 3 dict dup begin\n", 32); if (dict.registry > 0 && dict.ordering > 0) { getString(dict.registry, buf); (*outputFunc)(outputStream, " /Registry (", 13); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, ") def\n", 6); getString(dict.ordering, buf); (*outputFunc)(outputStream, " /Ordering (", 13); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, ") def\n", 6); } else { (*outputFunc)(outputStream, " /Registry (Adobe) def\n", 24); (*outputFunc)(outputStream, " /Ordering (Identity) def\n", 27); } sprintf(buf, " /Supplement %d def\n", dict.supplement); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, "end def\n", 8); sprintf(buf, "/FontMatrix [%g %g %g %g %g %g] def\n", dict.fontMatrix[0], dict.fontMatrix[1], dict.fontMatrix[2], dict.fontMatrix[3], dict.fontMatrix[4], dict.fontMatrix[5]); (*outputFunc)(outputStream, buf, strlen(buf)); sprintf(buf, "/FontBBox [%g %g %g %g] def\n", dict.fontBBox[0], dict.fontBBox[1], dict.fontBBox[2], dict.fontBBox[3]); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, "/FontInfo 1 dict dup begin\n", 27); (*outputFunc)(outputStream, " /FSType 8 def\n", 16); (*outputFunc)(outputStream, "end def\n", 8); // CIDFont-specific entries sprintf(buf, "/CIDCount %d def\n", nCIDs); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, "/FDBytes 1 def\n", 15); sprintf(buf, "/GDBytes %d def\n", gdBytes); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, "/CIDMapOffset 0 def\n", 20); if (dict.paintType != 0) { sprintf(buf, "/PaintType %d def\n", dict.paintType); (*outputFunc)(outputStream, buf, strlen(buf)); sprintf(buf, "/StrokeWidth %g def\n", dict.strokeWidth); (*outputFunc)(outputStream, buf, strlen(buf)); } // FDArray entry sprintf(buf, "/FDArray %d array\n", nFDs); (*outputFunc)(outputStream, buf, strlen(buf)); for (i = 0; i < nFDs; ++i) { sprintf(buf, "dup %d 10 dict begin\n", i); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, "/FontType 1 def\n", 16); (*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30); sprintf(buf, "/PaintType %d def\n", dict.paintType); (*outputFunc)(outputStream, buf, strlen(buf)); (*outputFunc)(outputStream, "/Private 32 dict begin\n", 23); (*outputFunc)(outputStream, privateDicts[i].dictData->getCString(), privateDicts[i].dictData->getLength()); (*outputFunc)(outputStream, "currentdict end def\n", 20); (*outputFunc)(outputStream, "currentdict end put\n", 20); } (*outputFunc)(outputStream, "def\n", 4); //~ need to deal with subrs // start the binary section offset = (nCIDs + 1) * (1 + gdBytes); sprintf(buf, "(Hex) %d StartData\n", offset + charStrings->getLength()); (*outputFunc)(outputStream, buf, strlen(buf)); // write the charstring offset (CIDMap) table for (i = 0; i <= nCIDs; i += 6) { for (j = 0; j < 6 && i+j <= nCIDs; ++j) { if (i+j < nCIDs && cidMap[i+j] >= 0) { buf[0] = (char)fdSelect[cidMap[i+j]]; } else { buf[0] = (char)0; } n = offset + charStringOffsets[i+j]; for (k = gdBytes; k >= 1; --k) { buf[k] = (char)(n & 0xff); n >>= 8; } for (k = 0; k <= gdBytes; ++k) { sprintf(buf2, "%02x", buf[k] & 0xff); (*outputFunc)(outputStream, buf2, 2); } } (*outputFunc)(outputStream, "\n", 1); } // write the charstring data n = charStrings->getLength(); for (i = 0; i < n; i += 32) { for (j = 0; j < 32 && i+j < n; ++j) { sprintf(buf, "%02x", charStrings->getChar(i+j) & 0xff); (*outputFunc)(outputStream, buf, strlen(buf)); } if (i + 32 >= n) { (*outputFunc)(outputStream, ">", 1); } (*outputFunc)(outputStream, "\n", 1); } for (i = 0; i < nFDs; ++i) { delete privateDicts[i].dictData; } gfree(privateDicts); gfree(cidMap); gfree(charset); gfree(charStringOffsets); delete charStrings; gfree(fdSelect);}void Type1CFontFile::convertToType0(char *psName, FontFileOutputFunc outputFuncA, void *outputStreamA) { Type1CTopDict dict; Type1CPrivateDict *privateDicts; Gushort *charset; int *cidMap; Guchar *fdSelect; Guchar *charStringsIdxPtr, *fdArrayIdx, *idxPtr0, *idxPtr1, *ptr; char buf[512]; char eBuf[256]; int nGlyphs, nCIDs, nFDs; int fdSelectFmt, nRanges, gid0, gid1, fd; int key; double x; GBool isFP; int i, j, n; outputFunc = outputFuncA; outputStream = outputStreamA; // read top dict (first font only) readTopDict(&dict); // read the FDArray dictionaries and Private dictionaries if (dict.fdArrayOffset == 0) { nFDs = 1; privateDicts = (Type1CPrivateDict *) gmalloc(nFDs * sizeof(Type1CPrivateDict)); privateDicts[0].dictData = new GString(); privateDicts[0].subrsOffset = 0; privateDicts[0].defaultWidthX = 0; privateDicts[0].defaultWidthXFP = gFalse; privateDicts[0].nominalWidthX = 0; privateDicts[0].nominalWidthXFP = gFalse; } else { fdArrayIdx = (Guchar *)file + dict.fdArrayOffset; nFDs = getIndexLen(fdArrayIdx); privateDicts = (Type1CPrivateDict *) gmalloc(nFDs * sizeof(Type1CPrivateDict)); idxPtr1 = getIndexValPtr(fdArrayIdx, 0); for (i = 0; i < nFDs; ++i) { privateDicts[i].dictData = NULL; idxPtr0 = idxPtr1; idxPtr1 = getIndexValPtr(fdArrayIdx, i + 1); ptr = idxPtr0; j = 0; while (ptr < idxPtr1) { if (*ptr <= 27 || *ptr == 31) { key = *ptr++; if (key == 0x0c) { key = (key << 8) | *ptr++; } if (key == 0x0012) { readPrivateDict(&privateDicts[i], (int)op[1], (int)op[0]); } j = 0; } else { x = getNum(&ptr, &isFP); if (j < 48) { op[j] = x; fp[j++] = isFP; } } } if (!privateDicts[i].dictData) { privateDicts[i].dictData = new GString(); privateDicts[i].subrsOffset = 0; privateDicts[i].defaultWidthX = 0; privateDicts[i].defaultWidthXFP = gFalse; privateDicts[i].nominalWidthX = 0; privateDicts[i].nominalWidthXFP = gFalse; } } } // get the glyph count charStringsIdxPtr = (Guchar *)file + dict.charStrings; nGlyphs = getIndexLen(charStringsIdxPtr); // read the FDSelect table fdSelect = (Guchar *)gmalloc(nGlyphs); if (dict.fdSelectOffset == 0) { for (i = 0; i < nGlyphs; ++i) { fdSelect[i] = 0; } } else { ptr = (Guchar *)file + dict.fdSelectOffset; fdSelectFmt = *ptr++; if (fdSelectFmt == 0) { memcpy(fdSelect, ptr, nGlyphs); } else if (fdSelectFmt == 3) { nRanges = getWord(ptr, 2); ptr += 2; gid0 = getWord(ptr, 2); ptr += 2; for (i = 1; i <= nRanges; ++i) { fd = *ptr++; gid1 = getWord(ptr, 2); ptr += 2; for (j = gid0; j < gid1; ++j) { fdSelect[j] = fd; } gid0 = gid1; } } else { error(-1, "Unknown FDSelect table format in CID font"); for (i = 0; i < nGlyphs; ++i) { fdSelect[i] = 0; } } } // read the charset, compute the CID-to-GID mapping charset = readCharset(dict.charset, nGlyphs); nCIDs = 0; for (i = 0; i < nGlyphs; ++i) { if (charset[i] >= nCIDs) { nCIDs = charset[i] + 1; } } cidMap = (int *)gmalloc(nCIDs * sizeof(int)); for (i = 0; i < nCIDs; ++i) { cidMap[i] = -1; } for (i = 0; i < nGlyphs; ++i) { cidMap[charset[i]] = i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -