From 41e0a0f0aa99b8304d8d63c450b31daedc81f0ef Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 28 Feb 2024 08:57:47 -0500 Subject: [PATCH] LibGfx/OpenType: Add named constants for header tags ...together with spec comments. No behavior change. --- Userland/Libraries/LibGfx/Font/OpenType/Font.cpp | 6 +++--- Userland/Libraries/LibGfx/Font/OpenType/Font.h | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp index 3ac9c04ff4..9d0d17722f 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp @@ -165,7 +165,7 @@ static ErrorOr read_tag(ReadonlyBytes buffer) ErrorOr> Font::try_load_from_externally_owned_memory(ReadonlyBytes buffer, Options options) { auto tag = TRY(read_tag(buffer)); - if (tag == Tag("ttcf")) { + if (tag == HeaderTag_FontCollection) { // It's a font collection FixedMemoryStream stream { buffer }; auto ttc_header_v1 = TRY(stream.read_in_place()); @@ -178,10 +178,10 @@ ErrorOr> Font::try_load_from_externally_owned_memory(Readonl auto offset = TRY(stream.read_value>()); return try_load_from_offset(buffer, offset, move(options)); } - if (tag == Tag("OTTO")) + if (tag == HeaderTag_CFFOutlines) return Error::from_string_literal("CFF fonts not supported yet"); - if (tag.to_u32() != 0x00010000 && tag != Tag("true")) + if (tag != HeaderTag_TrueTypeOutlines && tag != HeaderTag_TrueTypeOutlinesApple) return Error::from_string_literal("Not a valid font"); return try_load_from_offset(buffer, 0, move(options)); diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.h b/Userland/Libraries/LibGfx/Font/OpenType/Font.h index d257b5473c..ed78a75a36 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.h +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.h @@ -72,6 +72,19 @@ public: Optional control_value_program() const; Optional glyph_program(u32 glyph_id) const; + // https://learn.microsoft.com/en-us/typography/opentype/spec/otff + // "OpenType fonts that contain TrueType outlines should use the value of 0x00010000 for the sfntVersion. + // OpenType fonts containing CFF data (version 1 or 2) should use 0x4F54544F ('OTTO', when re-interpreted as a Tag) for sfntVersion. + // Note: The Apple specification for TrueType fonts allows for 'true' and 'typ1' for sfnt version. + // These version tags should not be used for OpenType fonts." + // "Font Collection ID string: 'ttcf' (used for fonts with CFF or CFF2 outlines as well as TrueType outlines)" + // The old Apple TrueType spec said "Fonts with TrueType outlines produced for OS X or iOS only are encouraged to use 'true'", + // so 'true' is somewhat common, especially in PDFs. + static constexpr Tag HeaderTag_TrueTypeOutlines = Tag::from_u32(0x00010000); + static constexpr Tag HeaderTag_TrueTypeOutlinesApple = Tag { "true" }; + static constexpr Tag HeaderTag_CFFOutlines = Tag { "OTTO" }; + static constexpr Tag HeaderTag_FontCollection = Tag { "ttcf" }; + private: struct AscenderAndDescender { i16 ascender;