Skip to content

Commit

Permalink
Fonts: cidMap is a vector
Browse files Browse the repository at this point in the history
  • Loading branch information
svuorela authored and tsdgeos committed Jan 8, 2025
1 parent d54ada8 commit e3773a7
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 383 deletions.
48 changes: 23 additions & 25 deletions fofi/FoFiTrueType.cc
Original file line number Diff line number Diff line change
Expand Up @@ -659,21 +659,19 @@ bool FoFiTrueType::getCFFBlock(char **start, int *length) const
return true;
}

int *FoFiTrueType::getCIDToGIDMap(int *nCIDs) const
std::vector<int> FoFiTrueType::getCIDToGIDMap() const
{
char *start;
int length;
FoFiType1C *ff;
int *map;

*nCIDs = 0;
if (!getCFFBlock(&start, &length)) {
return nullptr;
return {};
}
if (!(ff = FoFiType1C::make((unsigned char *)start, length))) {
return nullptr;
return {};
}
map = ff->getCIDToGIDMap(nCIDs);
std::vector<int> map = ff->getCIDToGIDMap();
delete ff;
return map;
}
Expand Down Expand Up @@ -719,7 +717,7 @@ void FoFiTrueType::getFontMatrix(double *mat) const
delete ff;
}

void FoFiTrueType::convertToType42(const char *psName, char **encoding, int *codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const
void FoFiTrueType::convertToType42(const char *psName, char **encoding, const std::vector<int> &codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const
{
int maxUsedGlyph;
bool ok;
Expand Down Expand Up @@ -769,7 +767,7 @@ void FoFiTrueType::convertToType1(const char *psName, const char **newEncoding,
delete ff;
}

void FoFiTrueType::convertToCIDType2(const char *psName, const int *cidMap, int nCIDs, bool needVerticalMetrics, FoFiOutputFunc outputFunc, void *outputStream) const
void FoFiTrueType::convertToCIDType2(const char *psName, const std::vector<int> &cidMap, bool needVerticalMetrics, FoFiOutputFunc outputFunc, void *outputStream) const
{
int cid, maxUsedGlyph;
bool ok;
Expand Down Expand Up @@ -797,16 +795,16 @@ void FoFiTrueType::convertToCIDType2(const char *psName, const int *cidMap, int
(*outputFunc)(outputStream, " /Supplement 0 def\n", 20);
(*outputFunc)(outputStream, " end def\n", 10);
(*outputFunc)(outputStream, "/GDBytes 2 def\n", 15);
if (cidMap) {
buf = GooString::format("/CIDCount {0:d} def\n", nCIDs);
if (!cidMap.empty()) {
buf = GooString::format("/CIDCount {0:d} def\n", int(cidMap.size()));
(*outputFunc)(outputStream, buf.c_str(), buf.size());
if (nCIDs > 32767) {
if (cidMap.size() > 32767) {
(*outputFunc)(outputStream, "/CIDMap [", 9);
for (i = 0; i < nCIDs; i += 32768 - 16) {
for (i = 0; i < int(cidMap.size()); i += 32768 - 16) {
(*outputFunc)(outputStream, "<\n", 2);
for (j = 0; j < 32768 - 16 && i + j < nCIDs; j += 16) {
for (j = 0; j < 32768 - 16 && i + j < int(cidMap.size()); j += 16) {
(*outputFunc)(outputStream, " ", 2);
for (k = 0; k < 16 && i + j + k < nCIDs; ++k) {
for (k = 0; k < 16 && i + j + k < int(cidMap.size()); ++k) {
cid = cidMap[i + j + k];
buf = GooString::format("{0:02x}{1:02x}", (cid >> 8) & 0xff, cid & 0xff);
(*outputFunc)(outputStream, buf.c_str(), buf.size());
Expand All @@ -819,9 +817,9 @@ void FoFiTrueType::convertToCIDType2(const char *psName, const int *cidMap, int
(*outputFunc)(outputStream, "] def\n", 6);
} else {
(*outputFunc)(outputStream, "/CIDMap <\n", 10);
for (i = 0; i < nCIDs; i += 16) {
for (i = 0; i < int(cidMap.size()); i += 16) {
(*outputFunc)(outputStream, " ", 2);
for (j = 0; j < 16 && i + j < nCIDs; ++j) {
for (j = 0; j < 16 && i + j < int(cidMap.size()); ++j) {
cid = cidMap[i + j];
buf = GooString::format("{0:02x}{1:02x}", (cid >> 8) & 0xff, cid & 0xff);
(*outputFunc)(outputStream, buf.c_str(), buf.size());
Expand Down Expand Up @@ -876,7 +874,7 @@ void FoFiTrueType::convertToCIDType2(const char *psName, const int *cidMap, int
(*outputFunc)(outputStream, "CIDFontName currentdict end /CIDFont defineresource pop\n", 56);
}

void FoFiTrueType::convertToCIDType0(const char *psName, int *cidMap, int nCIDs, FoFiOutputFunc outputFunc, void *outputStream) const
void FoFiTrueType::convertToCIDType0(const char *psName, const std::vector<int> &cidMap, FoFiOutputFunc outputFunc, void *outputStream) const
{
char *start;
int length;
Expand All @@ -888,11 +886,11 @@ void FoFiTrueType::convertToCIDType0(const char *psName, int *cidMap, int nCIDs,
if (!(ff = FoFiType1C::make((unsigned char *)start, length))) {
return;
}
ff->convertToCIDType0(psName, cidMap, nCIDs, outputFunc, outputStream);
ff->convertToCIDType0(psName, cidMap, outputFunc, outputStream);
delete ff;
}

void FoFiTrueType::convertToType0(const char *psName, int *cidMap, int nCIDs, bool needVerticalMetrics, int *maxValidGlyph, FoFiOutputFunc outputFunc, void *outputStream) const
void FoFiTrueType::convertToType0(const char *psName, const std::vector<int> &cidMap, bool needVerticalMetrics, int *maxValidGlyph, FoFiOutputFunc outputFunc, void *outputStream) const
{
GooString *sfntsName;
int maxUsedGlyph, n, i, j;
Expand Down Expand Up @@ -926,8 +924,8 @@ void FoFiTrueType::convertToType0(const char *psName, int *cidMap, int nCIDs, bo
// referencing zero-length glyphs that we trimmed.
// This allows pdftops to avoid writing huge files while still
// handling the rare PDF that uses a zero-length glyph.
if (cidMap) {
n = nCIDs;
if (!cidMap.empty()) {
n = cidMap.size();
} else if (nGlyphs > maxUsedGlyph + 256) {
if (maxUsedGlyph <= 255) {
n = 256;
Expand Down Expand Up @@ -961,7 +959,7 @@ void FoFiTrueType::convertToType0(const char *psName, int *cidMap, int nCIDs, bo
(*outputFunc)(outputStream, "/CharStrings 257 dict dup begin\n", 32);
(*outputFunc)(outputStream, "/.notdef 0 def\n", 15);
for (j = 0; j < 256 && i + j < n; ++j) {
buf = GooString::format("/c{0:02x} {1:d} def\n", j, cidMap ? cidMap[i + j] : i + j);
buf = GooString::format("/c{0:02x} {1:d} def\n", j, !cidMap.empty() ? cidMap[i + j] : i + j);
(*outputFunc)(outputStream, buf.c_str(), buf.size());
}
(*outputFunc)(outputStream, "end readonly def\n", 17);
Expand Down Expand Up @@ -993,7 +991,7 @@ void FoFiTrueType::convertToType0(const char *psName, int *cidMap, int nCIDs, bo
(*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40);
}

void FoFiTrueType::convertToType0(const char *psName, int *cidMap, int nCIDs, FoFiOutputFunc outputFunc, void *outputStream) const
void FoFiTrueType::convertToType0(const char *psName, const std::vector<int> &cidMap, FoFiOutputFunc outputFunc, void *outputStream) const
{
char *start;
int length;
Expand All @@ -1005,7 +1003,7 @@ void FoFiTrueType::convertToType0(const char *psName, int *cidMap, int nCIDs, Fo
if (!(ff = FoFiType1C::make((unsigned char *)start, length))) {
return;
}
ff->convertToType0(psName, cidMap, nCIDs, outputFunc, outputStream);
ff->convertToType0(psName, cidMap, outputFunc, outputStream);
delete ff;
}

Expand Down Expand Up @@ -1034,7 +1032,7 @@ void FoFiTrueType::cvtEncoding(char **encoding, FoFiOutputFunc outputFunc, void
(*outputFunc)(outputStream, "readonly def\n", 13);
}

void FoFiTrueType::cvtCharStrings(char **encoding, const int *codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const
void FoFiTrueType::cvtCharStrings(char **encoding, const std::vector<int> &codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const
{
const char *name;
char buf2[16];
Expand Down
18 changes: 9 additions & 9 deletions fofi/FoFiTrueType.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ class POPPLER_PRIVATE_EXPORT FoFiTrueType : public FoFiBase
// font does not have a post table.
int mapNameToGID(const char *name) const;

// Return the mapping from CIDs to GIDs, and return the number of
// CIDs in *<nCIDs>. This is only useful for CID fonts. (Only
// Return the mapping from CIDs to GIDs
// This is only useful for CID fonts. (Only
// useful for OpenType CFF fonts.)
int *getCIDToGIDMap(int *nCIDs) const;
std::vector<int> getCIDToGIDMap() const;

// Returns the least restrictive embedding licensing right (as
// defined by the TrueType spec):
Expand All @@ -112,7 +112,7 @@ class POPPLER_PRIVATE_EXPORT FoFiTrueType : public FoFiBase
// If <encoding> is NULL, the encoding is unknown or undefined. The
// <codeToGID> array specifies the mapping from char codes to GIDs.
// (Not useful for OpenType CFF fonts.)
void convertToType42(const char *psName, char **encoding, int *codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const;
void convertToType42(const char *psName, char **encoding, const std::vector<int> &codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const;

// Convert to a Type 1 font, suitable for embedding in a PostScript
// file. This is only useful with 8-bit fonts. If <newEncoding> is
Expand All @@ -128,24 +128,24 @@ class POPPLER_PRIVATE_EXPORT FoFiTrueType : public FoFiBase
// name (so we don't need to depend on the 'name' table in the
// font). The <cidMap> array maps CIDs to GIDs; it has <nCIDs>
// entries. (Not useful for OpenType CFF fonts.)
void convertToCIDType2(const char *psName, const int *cidMap, int nCIDs, bool needVerticalMetrics, FoFiOutputFunc outputFunc, void *outputStream) const;
void convertToCIDType2(const char *psName, const std::vector<int> &cidMap, bool needVerticalMetrics, FoFiOutputFunc outputFunc, void *outputStream) const;

// Convert to a Type 0 CIDFont, suitable for embedding in a
// PostScript file. <psName> will be used as the PostScript font
// name. (Only useful for OpenType CFF fonts.)
void convertToCIDType0(const char *psName, int *cidMap, int nCIDs, FoFiOutputFunc outputFunc, void *outputStream) const;
void convertToCIDType0(const char *psName, const std::vector<int> &cidMap, FoFiOutputFunc outputFunc, void *outputStream) const;

// Convert to a Type 0 (but non-CID) composite font, suitable for
// embedding in a PostScript file. <psName> will be used as the
// PostScript font name (so we don't need to depend on the 'name'
// table in the font). The <cidMap> array maps CIDs to GIDs; it has
// <nCIDs> entries. (Not useful for OpenType CFF fonts.)
void convertToType0(const char *psName, int *cidMap, int nCIDs, bool needVerticalMetrics, int *maxValidGlyph, FoFiOutputFunc outputFunc, void *outputStream) const;
void convertToType0(const char *psName, const std::vector<int> &cidMap, bool needVerticalMetrics, int *maxValidGlyph, FoFiOutputFunc outputFunc, void *outputStream) const;

// Convert to a Type 0 (but non-CID) composite font, suitable for
// embedding in a PostScript file. <psName> will be used as the
// PostScript font name. (Only useful for OpenType CFF fonts.)
void convertToType0(const char *psName, int *cidMap, int nCIDs, FoFiOutputFunc outputFunc, void *outputStream) const;
void convertToType0(const char *psName, const std::vector<int> &cidMap, FoFiOutputFunc outputFunc, void *outputStream) const;

// Returns a pointer to the CFF font embedded in this OpenType font.
// If successful, sets *<start> and *<length>, and returns true.
Expand All @@ -161,7 +161,7 @@ class POPPLER_PRIVATE_EXPORT FoFiTrueType : public FoFiBase
private:
FoFiTrueType(const unsigned char *fileA, int lenA, bool freeFileDataA, int faceIndexA);
void cvtEncoding(char **encoding, FoFiOutputFunc outputFunc, void *outputStream) const;
void cvtCharStrings(char **encoding, const int *codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const;
void cvtCharStrings(char **encoding, const std::vector<int> &codeToGID, FoFiOutputFunc outputFunc, void *outputStream) const;
void cvtSfnts(FoFiOutputFunc outputFunc, void *outputStream, const GooString *name, bool needVerticalMetrics, int *maxUsedGlyph) const;
static void dumpString(std::span<const unsigned char> s, FoFiOutputFunc outputFunc, void *outputStream);
static unsigned int computeTableChecksum(std::span<const unsigned char> data);
Expand Down
Loading

0 comments on commit e3773a7

Please sign in to comment.