Skip to content

Truly independent web browser

LibWeb: Load a font by the format of its data, not its Content-Type

sideshowbarkerde61d78052b1
diff --git a/Libraries/LibWeb/CSS/FontComputer.cpp b/Libraries/LibWeb/CSS/FontComputer.cpp
index 4ca8702816a..314e06fe337 100644
--- a/Libraries/LibWeb/CSS/FontComputer.cpp
+++ b/Libraries/LibWeb/CSS/FontComputer.cpp
@@ -25,7 +25,6 @@
#include <LibWeb/CSS/StyleSheetState.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ShadowRoot.h>
-#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Response.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WorkerGlobalScope.h>
@@ -36,7 +35,6 @@
#include <LibWeb/Page/RenderingStats.h>
#include <LibWeb/Platform/FontPlugin.h>
#include <LibWeb/SVG/SVGDecodedImageData.h>
-#include <LibWebCommon/MimeSniff/Resource.h>
namespace Web::CSS {
@@ -159,7 +157,7 @@ void FontLoader::start_loading_next_source()
// failure or a byte stream stream:
m_has_received_font_data = false;
m_fetch_controller = fetch_a_style_resource(m_sources.take_first().get<URL>(), m_rule_or_declaration, Fetch::Infrastructure::Request::Destination::Font, CorsMode::Cors,
- GC::create_function(heap(), GC::captures(this), [](FontLoader* loader, GC::Ref<Fetch::Infrastructure::ResponseView> response, Fetch::Infrastructure::FetchAlgorithms::BodyBytes stream) {
+ GC::create_function(heap(), GC::captures(this), [](FontLoader* loader, GC::Ref<Fetch::Infrastructure::ResponseView>, Fetch::Infrastructure::FetchAlgorithms::BodyBytes stream) {
// 1. If stream is null, return.
// 2. Load a font from stream according to its type.
@@ -176,9 +174,8 @@ void FontLoader::start_loading_next_source()
loader->m_has_received_font_data = true;
auto bytes = immutable_bytes->copy_to_byte_buffer().release_value_but_fixme_should_propagate_errors();
- auto mime_type_essence = loader->try_load_font_mime_type_essence(response, bytes);
- if (!requires_off_thread_vector_font_preparation(bytes, mime_type_essence)) {
- auto maybe_typeface = try_load_vector_font(bytes, mime_type_essence);
+ if (!requires_off_thread_vector_font_preparation(bytes)) {
+ auto maybe_typeface = try_load_vector_font(bytes);
if (maybe_typeface.is_error()) {
if (loader->m_sources.is_empty()) {
loader->font_did_load_or_fail(nullptr);
@@ -247,18 +244,6 @@ void FontLoader::font_did_load_or_fail(RefPtr<Gfx::Typeface const> typeface)
m_fetch_controller = nullptr;
}
-Optional<ByteString> FontLoader::try_load_font_mime_type_essence(Fetch::Infrastructure::ResponseView const& response, ByteBuffer const& bytes)
-{
- // FIXME: This could maybe use the format() provided in @font-face as well, since often the mime type is just application/octet-stream and we have to try every format
- auto mime_type = Fetch::Infrastructure::extract_mime_type(response.header_list());
- if (!mime_type.has_value() || !is_supported_font_mimetype(mime_type->essence())) {
- mime_type = MimeSniff::Resource::sniff(bytes, MimeSniff::SniffingConfiguration { .sniffing_context = MimeSniff::SniffingContext::Font });
- }
- if (!mime_type.has_value() || !is_supported_font_mimetype(mime_type->essence()))
- return {};
- return mime_type->essence().to_byte_string();
-}
-
void FontComputer::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
diff --git a/Libraries/LibWeb/CSS/FontComputer.h b/Libraries/LibWeb/CSS/FontComputer.h
index c603a38a08c..df1e7bba1d8 100644
--- a/Libraries/LibWeb/CSS/FontComputer.h
+++ b/Libraries/LibWeb/CSS/FontComputer.h
@@ -178,8 +178,6 @@ public:
private:
virtual void visit_edges(Visitor&) override;
- Optional<ByteString> try_load_font_mime_type_essence(Fetch::Infrastructure::ResponseView const&, ByteBuffer const&);
-
void font_did_load_or_fail(RefPtr<Gfx::Typeface const>);
GC::Ref<FontComputer> m_font_computer;
diff --git a/Libraries/LibWeb/CSS/FontLoading.cpp b/Libraries/LibWeb/CSS/FontLoading.cpp
index b55313bb928..eb6a6e19992 100644
--- a/Libraries/LibWeb/CSS/FontLoading.cpp
+++ b/Libraries/LibWeb/CSS/FontLoading.cpp
@@ -4,8 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/Endian.h>
-#include <AK/GenericShorthands.h>
#include <LibGfx/Font/WOFF/Loader.h>
#include <LibGfx/Font/WOFF2/Loader.h>
#include <LibWeb/CSS/FontLoading.h>
@@ -13,68 +11,34 @@
namespace Web::CSS {
+static constexpr u32 woff_signature = 0x774F4646;
static constexpr u32 woff2_signature = 0x774F4632;
-bool requires_off_thread_vector_font_preparation(ByteBuffer const& data, Optional<ByteString> const& mime_type_essence)
+static u32 font_signature(ByteBuffer const& data)
{
- if (mime_type_essence == "font/woff2"sv || mime_type_essence == "application/font-woff2"sv)
- return true;
- if (data.size() < sizeof(u32))
- return false;
- auto signature = *bit_cast<BigEndian<u32> const*>(data.data());
- return signature == woff2_signature;
+ if (data.size() < 4)
+ return 0;
+ return (static_cast<u32>(data[0]) << 24) | (static_cast<u32>(data[1]) << 16) | (static_cast<u32>(data[2]) << 8) | data[3];
}
-bool is_supported_font_mimetype(StringView const& mime_type_essence)
+bool requires_off_thread_vector_font_preparation(ByteBuffer const& data)
{
- return first_is_one_of(
- mime_type_essence,
- "font/ttf"sv,
- "application/x-font-ttf"sv,
- "font/otf"sv,
- "font/woff"sv,
- "application/font-woff"sv,
- "font/x-woff"sv,
- "font/woff2"sv,
- "application/font-woff2"sv);
+ return font_signature(data) == woff2_signature;
}
-ErrorOr<NonnullRefPtr<Gfx::Typeface const>> try_load_vector_font(ByteBuffer const& data, Optional<ByteString> const& mime_type_essence)
+// https://drafts.csswg.org/css-fonts-4/#fetch-a-font
+// 2. ISSUE: Load a font from stream according to its type.
+// NB: The type is the one the data has, whatever the response's Content-Type says.
+ErrorOr<NonnullRefPtr<Gfx::Typeface const>> try_load_vector_font(ByteBuffer const& data)
{
- auto try_ttf = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Typeface const>> {
- return Gfx::Typeface::try_load_from_temporary_memory(data);
- };
- auto try_woff = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Typeface const>> {
+ switch (font_signature(data)) {
+ case woff_signature:
return WOFF::try_load_from_bytes(data);
- };
- auto try_woff2 = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Typeface const>> {
+ case woff2_signature:
return WOFF2::try_load_from_bytes(data);
- };
-
- // FIXME: We shouldn't treat the mime-type as authoritative as it may have been incorrectly specified in the response
- // headers, we should try all formats if it fails.
- if (mime_type_essence.has_value()) {
- VERIFY(is_supported_font_mimetype(*mime_type_essence));
-
- if (*mime_type_essence == "font/ttf"sv || *mime_type_essence == "application/x-font-ttf"sv || *mime_type_essence == "font/otf"sv)
- return try_ttf();
- // https://drafts.csswg.org/css-fonts-4/#fetch-a-font
- // 2. ISSUE: Load a font from stream according to its type.
- // INTEROP: font/x-woff is a legacy alias that remains in use on the web.
- if (*mime_type_essence == "font/woff"sv || *mime_type_essence == "application/font-woff"sv || *mime_type_essence == "font/x-woff"sv)
- return try_woff();
- if (*mime_type_essence == "font/woff2"sv || *mime_type_essence == "application/font-woff2"sv)
- return try_woff2();
- } else {
- if (auto result = try_ttf(); !result.is_error())
- return result.release_value();
- if (auto result = try_woff(); !result.is_error())
- return result.release_value();
- if (auto result = try_woff2(); !result.is_error())
- return result.release_value();
+ default:
+ return Gfx::Typeface::try_load_from_temporary_memory(data);
}
-
- return Error::from_string_literal("Automatic format detection failed");
}
void prepare_vector_font_data_off_thread(ByteBuffer data, Function<void(ErrorOr<Core::AnonymousBuffer>)>&& on_complete)
diff --git a/Libraries/LibWeb/CSS/FontLoading.h b/Libraries/LibWeb/CSS/FontLoading.h
index 3cb1b13c622..c94db3c5933 100644
--- a/Libraries/LibWeb/CSS/FontLoading.h
+++ b/Libraries/LibWeb/CSS/FontLoading.h
@@ -15,9 +15,8 @@
namespace Web::CSS {
-bool is_supported_font_mimetype(StringView const& mime_type_essence);
-bool requires_off_thread_vector_font_preparation(ByteBuffer const&, Optional<ByteString> const& mime_type_essence = {});
-ErrorOr<NonnullRefPtr<Gfx::Typeface const>> try_load_vector_font(ByteBuffer const&, Optional<ByteString> const& mime_type_essence = {});
+bool requires_off_thread_vector_font_preparation(ByteBuffer const&);
+ErrorOr<NonnullRefPtr<Gfx::Typeface const>> try_load_vector_font(ByteBuffer const&);
void prepare_vector_font_data_off_thread(ByteBuffer, Function<void(ErrorOr<Core::AnonymousBuffer>)>&&);
}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index ba2c9412d99..c2b1dead378 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -74,6 +74,7 @@ Crash/HTML/location-href-setter-during-navigation-from-initial-about-blank.html
Text/input/HTML/csp-frame-ancestors-checks-every-ancestor.html
Text/input/css/font-cache-first-paint.html
Text/input/css/font-display-timeline.html
+Text/input/css/font-format-from-data-not-content-type.html
Text/input/css/font-cascade-stable-display-period.html
Text/input/css/unused-font-document-collectable.html
Text/input/css/canvas-font-display.html
diff --git a/Tests/LibWeb/Text/expected/css/font-format-from-data-not-content-type.txt b/Tests/LibWeb/Text/expected/css/font-format-from-data-not-content-type.txt
new file mode 100644
index 00000000000..9aca4edac7d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/font-format-from-data-not-content-type.txt
@@ -0,0 +1,3 @@
+TrueType served as font/woff2: loaded
+TrueType served as font/woff: loaded
+WOFF served as font/woff2: loaded
diff --git a/Tests/LibWeb/Text/input/css/font-format-from-data-not-content-type.html b/Tests/LibWeb/Text/input/css/font-format-from-data-not-content-type.html
new file mode 100644
index 00000000000..fde3278b9c7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/font-format-from-data-not-content-type.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ async function base64Of(url) {
+ const bytes = new Uint8Array(await (await fetch(url)).arrayBuffer());
+ let binary = "";
+ for (const byte of bytes)
+ binary += String.fromCharCode(byte);
+ return btoa(binary);
+ }
+
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const fonts = [
+ ["TrueType served as font/woff2", "../../../Assets/Lato-Bold.ttf", "font/woff2"],
+ ["TrueType served as font/woff", "../../../Assets/Lato-Bold.ttf", "font/woff"],
+ ["WOFF served as font/woff2", "../../../Assets/HashSans.woff", "font/woff2"],
+ ];
+ for (const [index, [label, asset, contentType]] of fonts.entries()) {
+ const url = await server.createEcho("GET", `/echo/font-${crypto.randomUUID()}`, {
+ status: 200,
+ headers: {
+ "Content-Type": contentType,
+ "Access-Control-Allow-Origin": "*",
+ },
+ body: await base64Of(asset),
+ body_encoding: "base64",
+ });
+ const face = new FontFace(`Test${index}`, `url(${url})`);
+ document.fonts.add(face);
+ try {
+ await face.load();
+ } catch {}
+ println(`${label}: ${face.status}`);
+ }
+ });
+</script>