tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestCOLRv1.cpp (2977B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "COLRFonts.h"
      7 #include "gfxFontUtils.h"
      8 #include "harfbuzz/hb.h"
      9 #include "MockDrawTarget.h"
     10 #include "MockScaledFont.h"
     11 #include "FuzzingInterface.h"
     12 #include "mozilla/Preferences.h"
     13 
     14 using namespace mozilla;
     15 using namespace mozilla::gfx;
     16 
     17 static int FuzzingRunCOLRv1(const uint8_t* data, size_t size) {
     18  gfxFontUtils::AutoHBBlob hb_data_blob(hb_blob_create(
     19      (const char*)data, size, HB_MEMORY_MODE_READONLY, nullptr, nullptr));
     20 
     21  hb_face_t* hb_data_face = hb_face_create(hb_data_blob, 0);
     22  if (!hb_data_face) {
     23    return 0;
     24  }
     25 
     26  gfxFontUtils::AutoHBBlob colr(
     27      hb_face_reference_table(hb_data_face, TRUETYPE_TAG('C', 'O', 'L', 'R')));
     28  gfxFontUtils::AutoHBBlob cpal(
     29      hb_face_reference_table(hb_data_face, TRUETYPE_TAG('C', 'P', 'A', 'L')));
     30  if (!colr || !cpal || !COLRFonts::ValidateColorGlyphs(colr, cpal)) {
     31    hb_face_destroy(hb_data_face);
     32    return 0;
     33  }
     34 
     35  const float kPixelSize = 16.0f;
     36  unsigned glyph_count = hb_face_get_glyph_count(hb_data_face);
     37  hb_font_t* hb_data_font = hb_font_create(hb_data_face);
     38  uint32_t scale = NS_round(kPixelSize * 65536.0);  // size as 16.16 fixed-point
     39  hb_font_set_scale(hb_data_font, scale, scale);
     40 
     41  RefPtr dt = new MockDrawTarget();
     42  RefPtr uf = new MockUnscaledFont();
     43  RefPtr sf = new MockScaledFont(uf, hb_data_font);
     44  Float f2p = kPixelSize / hb_face_get_upem(hb_data_face);
     45 
     46  auto colorPalette =
     47      MakeUnique<nsTArray<sRGBColor>>(COLRFonts::CreateColorPalette(
     48          hb_data_face, nullptr, nullptr, "dummy"_ns));
     49 
     50  for (unsigned i = 0; i <= glyph_count; ++i) {
     51    if (COLRFonts::GetColrTableVersion(colr) == 1) {
     52      Rect bounds =
     53          COLRFonts::GetColorGlyphBounds(colr, hb_data_font, i, dt, sf, f2p);
     54      const auto* paintGraph = COLRFonts::GetGlyphPaintGraph(colr, i);
     55      if (paintGraph) {
     56        dt->PushClipRect(bounds);
     57        COLRFonts::PaintGlyphGraph(colr, hb_data_font, paintGraph, dt, nullptr,
     58                                   sf, DrawOptions(), Point(), sRGBColor(),
     59                                   colorPalette.get(), i, f2p);
     60        dt->PopClip();
     61      }
     62    }
     63    const auto* layers = COLRFonts::GetGlyphLayers(colr, i);
     64    if (layers) {
     65      COLRFonts::PaintGlyphLayers(colr, hb_data_face, layers, dt, nullptr, sf,
     66                                  DrawOptions(), Point(), sRGBColor(),
     67                                  colorPalette.get());
     68    }
     69  }
     70 
     71  hb_font_destroy(hb_data_font);
     72  hb_face_destroy(hb_data_face);
     73 
     74  return 0;
     75 }
     76 
     77 int FuzzingInitCOLRv1(int* argc, char*** argv) {
     78  Preferences::SetBool("gfx.font_rendering.colr_v1.enabled", true);
     79  return 0;
     80 }
     81 
     82 MOZ_FUZZING_INTERFACE_RAW(FuzzingInitCOLRv1, FuzzingRunCOLRv1, GfxCOLRv1);