LibPDF: Convert CFF::parse_fdselect to Stream

No behavior change.
This commit is contained in:
Nico Weber 2024-04-13 21:51:03 -04:00 committed by Andreas Kling
commit 4995dfe8f1
Notes: sideshowbarker 2024-07-17 23:07:41 +09:00
2 changed files with 9 additions and 9 deletions

View file

@ -224,7 +224,7 @@ PDFErrorOr<NonnullRefPtr<CFF>> CFF::create(ReadonlyBytes const& cff_bytes, RefPt
// CFF spec, "19 FDSelect" // CFF spec, "19 FDSelect"
Vector<u8> fdselect; Vector<u8> fdselect;
if (top_dict.fdselect_offset != 0) { if (top_dict.fdselect_offset != 0) {
fdselect = TRY(parse_fdselect(Reader { cff_bytes.slice(top_dict.fdselect_offset) }, glyphs.size())); fdselect = TRY(parse_fdselect(FixedMemoryStream { cff_bytes.slice(top_dict.fdselect_offset) }, glyphs.size()));
dbgln_if(CFF_DEBUG, "CFF has {} FDSelect entries", fdselect.size()); dbgln_if(CFF_DEBUG, "CFF has {} FDSelect entries", fdselect.size());
} }
@ -895,12 +895,12 @@ PDFErrorOr<Vector<CFF::SID>> CFF::parse_charset(Reader&& reader, size_t glyph_co
return names; return names;
} }
PDFErrorOr<Vector<u8>> CFF::parse_fdselect(Reader&& reader, size_t glyph_count) PDFErrorOr<Vector<u8>> CFF::parse_fdselect(Stream&& reader, size_t glyph_count)
{ {
Vector<u8> fd_selector_array; // Maps GIDs to their FD index. Vector<u8> fd_selector_array; // Maps GIDs to their FD index.
// CFF spec, "19 FDSelect" // CFF spec, "19 FDSelect"
auto format = TRY(reader.try_read<Card8>()); auto format = TRY(reader.read_value<Card8>());
if (format == 0) { if (format == 0) {
// CFF spec, "Table 27 Format 0" // CFF spec, "Table 27 Format 0"
@ -908,23 +908,23 @@ PDFErrorOr<Vector<u8>> CFF::parse_fdselect(Reader&& reader, size_t glyph_count)
dbgln_if(CFF_DEBUG, "CFF fdselect format 0"); dbgln_if(CFF_DEBUG, "CFF fdselect format 0");
fd_selector_array.ensure_capacity(glyph_count); fd_selector_array.ensure_capacity(glyph_count);
for (size_t i = 0; i < glyph_count; i++) for (size_t i = 0; i < glyph_count; i++)
fd_selector_array.append(TRY(reader.try_read<Card8>())); fd_selector_array.append(TRY(reader.read_value<Card8>()));
} else if (format == 3) { } else if (format == 3) {
// CFF spec, "Table 28 Format 3" // CFF spec, "Table 28 Format 3"
dbgln_if(CFF_DEBUG, "CFF fdselect format 3"); dbgln_if(CFF_DEBUG, "CFF fdselect format 3");
// The spec presents this as n "Card16 first; Card8 fd;" struct entries followed by a Char16 sentinel value. // The spec presents this as n "Card16 first; Card8 fd;" struct entries followed by a Char16 sentinel value.
// But the code is shorter if we treat it as a Char16 start value followed by n "Card8 fd; Card16 end;" struct entries. // But the code is shorter if we treat it as a Char16 start value followed by n "Card8 fd; Card16 end;" struct entries.
Card16 n_ranges = TRY(reader.try_read<BigEndian<Card16>>()); Card16 n_ranges = TRY(reader.read_value<BigEndian<Card16>>());
Card16 begin = TRY(reader.try_read<BigEndian<Card16>>()); Card16 begin = TRY(reader.read_value<BigEndian<Card16>>());
// "The first range must have a 'first' GID of 0." // "The first range must have a 'first' GID of 0."
if (begin != 0) if (begin != 0)
return error("CFF fdselect format 3 first range must have a 'first' GID of 0"); return error("CFF fdselect format 3 first range must have a 'first' GID of 0");
for (Card16 i = 0; i < n_ranges; i++) { for (Card16 i = 0; i < n_ranges; i++) {
auto fd = TRY(reader.try_read<Card8>()); auto fd = TRY(reader.read_value<Card8>());
auto end = TRY(reader.try_read<BigEndian<Card16>>()); auto end = TRY(reader.read_value<BigEndian<Card16>>());
for (Card16 j = begin; j < end; j++) for (Card16 j = begin; j < end; j++)
fd_selector_array.append(fd); fd_selector_array.append(fd);
begin = end; begin = end;

View file

@ -137,7 +137,7 @@ public:
static DeprecatedFlyString resolve_sid(SID, Vector<StringView> const&); static DeprecatedFlyString resolve_sid(SID, Vector<StringView> const&);
static PDFErrorOr<Vector<SID>> parse_charset(Reader&&, size_t); static PDFErrorOr<Vector<SID>> parse_charset(Reader&&, size_t);
static PDFErrorOr<Vector<u8>> parse_fdselect(Reader&&, size_t); static PDFErrorOr<Vector<u8>> parse_fdselect(Stream&&, size_t);
static PDFErrorOr<Vector<u8>> parse_encoding(Stream&&, HashMap<Card8, SID>& supplemental); static PDFErrorOr<Vector<u8>> parse_encoding(Stream&&, HashMap<Card8, SID>& supplemental);
}; };