summaryrefslogtreecommitdiffstats
path: root/gfx/tests/fuzz/TestCOLRv1.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /gfx/tests/fuzz/TestCOLRv1.cpp
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--gfx/tests/fuzz/TestCOLRv1.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/gfx/tests/fuzz/TestCOLRv1.cpp b/gfx/tests/fuzz/TestCOLRv1.cpp
new file mode 100644
index 0000000000..9d8dc6186a
--- /dev/null
+++ b/gfx/tests/fuzz/TestCOLRv1.cpp
@@ -0,0 +1,81 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "COLRFonts.h"
+#include "gfxFontUtils.h"
+#include "harfbuzz/hb.h"
+#include "MockDrawTarget.h"
+#include "MockScaledFont.h"
+#include "FuzzingInterface.h"
+#include "mozilla/Preferences.h"
+
+using namespace mozilla;
+using namespace mozilla::gfx;
+
+static int FuzzingRunCOLRv1(const uint8_t* data, size_t size) {
+ gfxFontUtils::AutoHBBlob hb_data_blob(hb_blob_create(
+ (const char*)data, size, HB_MEMORY_MODE_READONLY, nullptr, nullptr));
+
+ hb_face_t* hb_data_face = hb_face_create(hb_data_blob, 0);
+ if (!hb_data_face) {
+ return 0;
+ }
+
+ gfxFontUtils::AutoHBBlob colr(
+ hb_face_reference_table(hb_data_face, TRUETYPE_TAG('C', 'O', 'L', 'R')));
+ gfxFontUtils::AutoHBBlob cpal(
+ hb_face_reference_table(hb_data_face, TRUETYPE_TAG('C', 'P', 'A', 'L')));
+ if (!colr || !cpal || !COLRFonts::ValidateColorGlyphs(colr, cpal)) {
+ hb_face_destroy(hb_data_face);
+ return 0;
+ }
+
+ const float kPixelSize = 16.0f;
+ unsigned glyph_count = hb_face_get_glyph_count(hb_data_face);
+ hb_font_t* hb_data_font = hb_font_create(hb_data_face);
+ uint32_t scale = NS_round(kPixelSize * 65536.0); // size as 16.16 fixed-point
+ hb_font_set_scale(hb_data_font, scale, scale);
+
+ RefPtr dt = new MockDrawTarget();
+ RefPtr uf = new MockUnscaledFont();
+ RefPtr sf = new MockScaledFont(uf, hb_data_font);
+ Float f2p = kPixelSize / hb_face_get_upem(hb_data_face);
+
+ UniquePtr<nsTArray<sRGBColor>> colorPalette =
+ COLRFonts::SetupColorPalette(hb_data_face, nullptr, nullptr, "dummy"_ns);
+
+ for (unsigned i = 0; i <= glyph_count; ++i) {
+ if (COLRFonts::GetColrTableVersion(colr) == 1) {
+ Rect bounds =
+ COLRFonts::GetColorGlyphBounds(colr, hb_data_font, i, dt, sf, f2p);
+ const auto* paintGraph = COLRFonts::GetGlyphPaintGraph(colr, i);
+ if (paintGraph) {
+ dt->PushClipRect(bounds);
+ COLRFonts::PaintGlyphGraph(colr, hb_data_font, paintGraph, dt, nullptr,
+ sf, DrawOptions(), Point(), sRGBColor(),
+ colorPalette.get(), i, f2p);
+ dt->PopClip();
+ }
+ }
+ const auto* layers = COLRFonts::GetGlyphLayers(colr, i);
+ if (layers) {
+ COLRFonts::PaintGlyphLayers(colr, hb_data_face, layers, dt, nullptr, sf,
+ DrawOptions(), Point(), sRGBColor(),
+ colorPalette.get());
+ }
+ }
+
+ hb_font_destroy(hb_data_font);
+ hb_face_destroy(hb_data_face);
+
+ return 0;
+}
+
+int FuzzingInitCOLRv1(int* argc, char*** argv) {
+ Preferences::SetBool("gfx.font_rendering.colr_v1.enabled", true);
+ return 0;
+}
+
+MOZ_FUZZING_INTERFACE_RAW(FuzzingInitCOLRv1, FuzzingRunCOLRv1, GfxCOLRv1);