From 185573c03f67723054ac93143f652d711237844b Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 18 Oct 2023 09:27:27 -0400 Subject: [PATCH] LibPDF: Implement subr_number biasing for CFF font programs --- .../Libraries/LibPDF/Fonts/Type1FontProgram.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.cpp b/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.cpp index 571a33a874..8e0604f194 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.cpp @@ -373,6 +373,21 @@ PDFErrorOr Type1FontProgram::parse_glyph(ReadonlyBytes case CallSubr: { auto subr_number = pop(); + + if (is_type2) { + // Type 2 spec: + // "The numbering of subroutines is encoded more compactly by using the negative half of the number space, which effectively + // doubles the number of compactly encodable subroutine numbers. The bias applied depends on the number of subrs (gsubrs). + // If the number of subrs (gsubrs) is less than 1240, the bias is 107. Otherwise if it is less than 33900, it is 1131; otherwise + // it is 32768. This bias is added to the encoded subr (gsubr) number to find the appropriate entry in the subr (gsubr) array." + if (subroutines.size() < 1240) + subr_number += 107; + else if (subroutines.size() < 33900) + subr_number += 1131; + else + subr_number += 32768; + } + if (static_cast(subr_number) >= subroutines.size()) return error("Subroutine index out of range");