Bug 2071557 - Keep color-font palette entries as hb_color_t to reduce memory footprint. r=firefox-style-system-reviewers,emilio

Differential Revision: https://phabricator.services.mozilla.com/D325607
This commit is contained in:
Jonathan Kew
2026-09-12 21:37:37 +00:00
committed by jkew@mozilla.com
parent aabe491b92
commit fe9deb509e
5 changed files with 31 additions and 32 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ static int FuzzingRunCOLRv1(const uint8_t* data, size_t size) {
Float f2p = kPixelSize / hb_face_get_upem(hb_data_face);
auto colorPalette =
MakeUnique<nsTArray<sRGBColor>>(COLRFonts::CreateColorPalette(
MakeUnique<nsTArray<hb_color_t>>(COLRFonts::CreateColorPalette(
hb_data_face, nullptr, nullptr, "dummy"_ns));
for (unsigned i = 0; i <= glyph_count; ++i) {
+19 -19
View File
@@ -11,7 +11,6 @@
#include "gfxFontUtils.h"
#include "gfxUtils.h"
#include "harfbuzz/hb-ot.h"
#include "harfbuzz/hb.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/StaticPrefs_gfx.h"
#include "mozilla/gfx/Helpers.h"
@@ -147,7 +146,7 @@ struct PaintState {
const COLRHeader* v0;
const COLRv1Header* v1;
} mHeader;
const sRGBColor* mPalette;
const hb_color_t* mPalette;
DrawTarget* mDrawTarget;
ScaledFont* mScaledFont;
const int* mCoords;
@@ -177,7 +176,11 @@ constexpr uint32_t kPaintRecursionLimit = 256;
DeviceColor PaintState::GetColor(uint16_t aPaletteIndex, float aAlpha) const {
sRGBColor color;
if (aPaletteIndex < mNumColors) {
color = mPalette[uint16_t(aPaletteIndex)];
hb_color_t c = mPalette[uint16_t(aPaletteIndex)];
// Explicitly get the components from the hb_color_t, rather than assuming
// anything about its packing.
color = sRGBColor::FromU8(hb_color_get_red(c), hb_color_get_green(c),
hb_color_get_blue(c), hb_color_get_alpha(c));
} else if (aPaletteIndex == 0xffff) {
color = mCurrentColor;
} else { // Palette index out of range! Return transparent black.
@@ -2464,7 +2467,7 @@ bool COLRFonts::PaintGlyphLayers(
hb_blob_t* aCOLR, hb_face_t* aFace, const GlyphLayers* aLayers,
DrawTarget* aDrawTarget, layout::TextDrawTarget* aTextDrawer,
ScaledFont* aScaledFont, DrawOptions aDrawOptions, const Point& aPoint,
const sRGBColor& aCurrentColor, const nsTArray<sRGBColor>* aColors) {
const sRGBColor& aCurrentColor, const nsTArray<hb_color_t>* aColors) {
const auto* glyphRecord = reinterpret_cast<const BaseGlyphRecord*>(aLayers);
// Default to opaque rendering (non-webrender applies alpha with a layer)
float alpha = 1.0;
@@ -2530,7 +2533,7 @@ bool COLRFonts::PaintGlyphGraph(
hb_blob_t* aCOLR, hb_font_t* aFont, const GlyphPaintGraph* aPaintGraph,
DrawTarget* aDrawTarget, layout::TextDrawTarget* aTextDrawer,
ScaledFont* aScaledFont, DrawOptions aDrawOptions, const Point& aPoint,
const sRGBColor& aCurrentColor, const nsTArray<sRGBColor>* aColors,
const sRGBColor& aCurrentColor, const nsTArray<hb_color_t>* aColors,
uint32_t aGlyphId, float aFontUnitsToPixels) {
if (aTextDrawer) {
// Currently we always punt to a blob for COLRv1 glyphs.
@@ -2612,7 +2615,7 @@ uint16_t COLRFonts::GetColrTableVersion(hb_blob_t* aCOLR) {
return colr->version;
}
nsTArray<sRGBColor> COLRFonts::CreateColorPalette(
nsTArray<hb_color_t> COLRFonts::CreateColorPalette(
hb_face_t* aFace, const FontPaletteValueSet* aPaletteValueSet,
nsAtom* aFontPalette, const nsACString& aFamilyName) {
// Find the base color palette to use, if there are multiple available;
@@ -2659,27 +2662,24 @@ nsTArray<sRGBColor> COLRFonts::CreateColorPalette(
}
}
// Collect the palette colors and convert them to sRGBColor values.
// Collect the palette colors.
count =
hb_ot_color_palette_get_colors(aFace, paletteIndex, 0, nullptr, nullptr);
nsTArray<hb_color_t> colors;
colors.SetLength(count);
nsTArray<hb_color_t> palette;
palette.SetLength(count);
hb_ot_color_palette_get_colors(aFace, paletteIndex, 0, &count,
colors.Elements());
nsTArray<sRGBColor> palette;
palette.SetCapacity(count);
for (const auto c : colors) {
palette.AppendElement(
sRGBColor(hb_color_get_red(c) / 255.0, hb_color_get_green(c) / 255.0,
hb_color_get_blue(c) / 255.0, hb_color_get_alpha(c) / 255.0));
}
palette.Elements());
// Apply @font-palette-values overrides, if present.
if (fpv) {
for (const auto overrideColor : fpv->mOverrides) {
if (overrideColor.mIndex < palette.Length()) {
palette[overrideColor.mIndex] = overrideColor.mColor;
// Override colors contain nscolor, but the palette uses hb_color_t.
// They have different byte packing orders, so we have to explicitly
// map the components, not just assign as a 32-bit value.
nscolor c = overrideColor.mColor;
palette[overrideColor.mIndex] =
HB_COLOR(NS_GET_B(c), NS_GET_G(c), NS_GET_R(c), NS_GET_A(c));
}
}
}
+6 -8
View File
@@ -5,15 +5,13 @@
#ifndef COLR_FONTS_H
#define COLR_FONTS_H
#include "harfbuzz/hb.h"
#include "mozilla/gfx/2D.h"
#include "nsAtom.h"
#include "nsColor.h"
#include "nsTArray.h"
#include "nsTHashtable.h"
struct hb_blob_t;
struct hb_face_t;
struct hb_font_t;
namespace mozilla {
namespace layout {
@@ -28,7 +26,7 @@ class FontPaletteValueSet {
struct OverrideColor {
uint32_t mIndex = 0;
sRGBColor mColor;
nscolor mColor;
};
struct PaletteValues {
@@ -100,7 +98,7 @@ class COLRFonts {
hb_blob_t* aCOLR, hb_face_t* aFace, const GlyphLayers* aLayers,
DrawTarget* aDrawTarget, layout::TextDrawTarget* aTextDrawer,
ScaledFont* aScaledFont, DrawOptions aDrawOptions, const Point& aPoint,
const sRGBColor& aCurrentColor, const nsTArray<sRGBColor>* aColors);
const sRGBColor& aCurrentColor, const nsTArray<hb_color_t>* aColors);
// COLRv1 support: color glyph is represented by a directed acyclic graph of
// paint records.
@@ -114,7 +112,7 @@ class COLRFonts {
hb_blob_t* aCOLR, hb_font_t* aFont, const GlyphPaintGraph* aPaintGraph,
DrawTarget* aDrawTarget, layout::TextDrawTarget* aTextDrawer,
ScaledFont* aScaledFont, DrawOptions aDrawOptions, const Point& aPoint,
const sRGBColor& aCurrentColor, const nsTArray<sRGBColor>* aColors,
const sRGBColor& aCurrentColor, const nsTArray<hb_color_t>* aColors,
uint32_t aGlyphId, float aFontUnitsToPixels);
static Rect GetColorGlyphBounds(hb_blob_t* aCOLR, hb_font_t* aFont,
@@ -124,7 +122,7 @@ class COLRFonts {
static uint16_t GetColrTableVersion(hb_blob_t* aCOLR);
static nsTArray<sRGBColor> CreateColorPalette(
static nsTArray<hb_color_t> CreateColorPalette(
hb_face_t* aFace, const FontPaletteValueSet* aPaletteValueSet,
nsAtom* aFontPalette, const nsACString& aFamilyName);
};
+4 -3
View File
@@ -7,6 +7,7 @@
#include <utility>
#include "harfbuzz/hb.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/MruCache.h"
#include "mozilla/RefPtr.h"
@@ -26,15 +27,15 @@ class FontPalette {
public:
FontPalette() = default;
explicit FontPalette(nsTArray<mozilla::gfx::sRGBColor>&& aColors)
explicit FontPalette(nsTArray<hb_color_t>&& aColors)
: mColors(std::move(aColors)) {}
const nsTArray<mozilla::gfx::sRGBColor>* Colors() const { return &mColors; }
const nsTArray<hb_color_t>* Colors() const { return &mColors; }
private:
~FontPalette() = default;
nsTArray<mozilla::gfx::sRGBColor> mColors;
nsTArray<hb_color_t> mColors;
};
// MRU cache used for resolved color-font palettes, to avoid reconstructing
+1 -1
View File
@@ -1030,7 +1030,7 @@ void Gecko_SetFontPaletteOverride(
return;
}
aValues->mOverrides.AppendElement(gfx::FontPaletteValueSet::OverrideColor{
uint32_t(aIndex), gfx::sRGBColor::FromABGR(aColor->ToColor())});
uint32_t(aIndex), aColor->ToColor()});
}
void Gecko_EnsureImageLayersLength(nsStyleImageLayers* aLayers, size_t aLen,