Skip to content

fix: Resolve /C... char representation to proper ASCII #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/v2/pdf_resources/page_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace pdflib

std::string get_correct_character(uint32_t c);
std::string get_character_from_encoding(uint32_t c);
std::string decode_cname(const std::string& name);

void init_encoding();
void init_subtype();
Expand Down Expand Up @@ -569,9 +570,42 @@ namespace pdflib
<< "; Encoding: " << to_string(encoding)
<< "; font-name: " << font_name;

return notdef;
return notdef;
}
}
}

// Decode glyph names of the form '/Cnnn'.
// - For 0<=nnn<128 we interpret it as standard ASCII.
// - For 128<=nnn<256 we use the font's encoding and
// fall back to ISO-8859-1 if undefined.
// - Otherwise return the name unchanged.
std::string pdf_resource<PAGE_FONT>::decode_cname(const std::string& name)
{
static const std::regex re_ascii(R"(^\/C(\d+)$)");

std::smatch m;
if(std::regex_match(name, m, re_ascii))
{
int code = std::stoi(m[1].str());
if(code >= 0 && code < 128)
{
return std::string(1, static_cast<char>(code));
}
else if(code >= 0 && code < 256)
{
std::string decoded = get_character_from_encoding(code);
if(decoded.rfind("GLYPH<", 0) == 0)
{
std::string tmp;
utf8::append(code, std::back_inserter(tmp));
return tmp;
}
return decoded;
}
}

return name;
}

void pdf_resource<PAGE_FONT>::set(std::string font_key_,
Expand Down Expand Up @@ -1829,7 +1863,7 @@ namespace pdflib
}
else
{
diff_numb_to_char[numb] = name;
diff_numb_to_char[numb] = decode_cname(name);
LOG_S(WARNING) << "differences["<<numb<<"] -> " << name;
}

Expand Down
Loading