Skip to content

Commit

Permalink
LibGfx: Handle UTF-16-encoded OpenType font names
Browse files Browse the repository at this point in the history
This change makes OpenType::Name::string_for_id handle fonts whose names
are UTF-16-encoded (along with handling UTF-8-encoded names).

Otherwise, without this change, the existing code assumes the names are
UTF-8-encoded, fails gracelessly if they’re not, and crashes.

Fixes LadybirdBrowser/ladybird#75

(cherry picked from commit 1a9dabe5ff443315a1e3e9c577f93936e6dd45dc)
  • Loading branch information
sideshowbarker authored and nico committed Aug 2, 2024
1 parent e8bfd9e commit 38ea81c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,23 @@ String Name::string_for_id(NameId id) const
auto const platform_id = name_record.platform_id;
auto const length = name_record.length;
auto const offset = name_record.string_offset;
auto const name_bytes = m_string_data.slice(offset, length);

if (platform_id == to_underlying(Platform::Windows)) {
static auto& decoder = *TextCodec::decoder_for("utf-16be"sv);
return decoder.to_utf8(m_string_data.slice(offset, length)).release_value_but_fixme_should_propagate_errors();
return decoder.to_utf8(name_bytes).release_value_but_fixme_should_propagate_errors();
}

return String::from_utf8(m_string_data.slice(offset, length)).release_value_but_fixme_should_propagate_errors();
auto maybe_name = String::from_utf8(name_bytes);
if (maybe_name.is_error()) {
static auto& decoder = *TextCodec::decoder_for("utf-16be"sv);
maybe_name = decoder.to_utf8(name_bytes);
if (!maybe_name.is_error())
return maybe_name.release_value_but_fixme_should_propagate_errors();
dbgln("OpenType::Name: Failed to decode name string as UTF-8 or UTF-16BE");
return String {};
}
return maybe_name.release_value();
}

ErrorOr<Kern> Kern::from_slice(ReadonlyBytes slice)
Expand Down

0 comments on commit 38ea81c

Please sign in to comment.