| 1 |
/* |
| 2 |
* Copyright 2023 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkTypeface_Fontations_priv_DEFINED |
| 9 |
#define SkTypeface_Fontations_priv_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkFontArguments.h" |
| 12 |
#include "include/core/SkFontParameters.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkSpan.h" |
| 16 |
#include "include/core/SkStream.h" |
| 17 |
#include "include/core/SkTypeface.h" |
| 18 |
#include "include/private/base/SkOnce.h" |
| 19 |
#include "include/private/base/SkTArray.h" |
| 20 |
#include "src/core/SkAdvancedTypefaceMetrics.h" |
| 21 |
#include "src/core/SkScalerContext.h" |
| 22 |
#include "src/ports/fontations/src/ffi.rs.h" |
| 23 |
#include "src/ports/SkTypeface_fontations_factory.h" |
| 24 |
|
| 25 |
#include <memory> |
| 26 |
|
| 27 |
class SkStreamAsset; |
| 28 |
class SkFontationsScalerContext; |
| 29 |
|
| 30 |
namespace sk_fontations { |
| 31 |
|
| 32 |
/** Implementation of AxisWrapper FFI C++ interface, allowing Rust to call back into |
| 33 |
* C++ for populating variable axis availability information, see skpath_bridge.h. */ |
| 34 |
class AxisWrapper : public fontations_ffi::AxisWrapper { |
| 35 |
public: |
| 36 |
AxisWrapper(SkFontParameters::Variation::Axis axisArray[], size_t axisCount); |
| 37 |
AxisWrapper() = delete; |
| 38 |
/* From fontations_ffi::AxisWrapper. */ |
| 39 |
bool populate_axis( |
| 40 |
size_t i, uint32_t axisTag, float min, float def, float max, bool hidden) override; |
| 41 |
size_t size() const override; |
| 42 |
|
| 43 |
private: |
| 44 |
SkFontParameters::Variation::Axis* fAxisArray; |
| 45 |
size_t fAxisCount; |
| 46 |
}; |
| 47 |
|
| 48 |
class ColorPainter : public fontations_ffi::ColorPainterWrapper { |
| 49 |
public: |
| 50 |
ColorPainter() = delete; |
| 51 |
ColorPainter(SkFontationsScalerContext& scaler_context, |
| 52 |
SkCanvas& canvas, |
| 53 |
SkSpan<const SkColor> palette, |
| 54 |
SkColor foregroundColor, |
| 55 |
bool antialias, |
| 56 |
uint16_t upem); |
| 57 |
|
| 58 |
// fontations_ffi::ColorPainter interface. |
| 59 |
virtual bool is_bounds_mode() override { return false; } |
| 60 |
virtual void push_transform(const fontations_ffi::Transform& transform) override; |
| 61 |
virtual void pop_transform() override; |
| 62 |
virtual void push_clip_glyph(uint16_t glyph_id) override; |
| 63 |
virtual void push_clip_rectangle(float x_min, float y_min, float x_max, float y_max) override; |
| 64 |
virtual void pop_clip() override; |
| 65 |
|
| 66 |
// Paint*Gradient equivalents: |
| 67 |
virtual void fill_solid(uint16_t palette_index, float alpha) override; |
| 68 |
virtual void fill_radial(const fontations_ffi::FillRadialParams& fill_radial_params, |
| 69 |
fontations_ffi::BridgeColorStops&, |
| 70 |
uint8_t extend_mode) override; |
| 71 |
virtual void fill_linear(const fontations_ffi::FillLinearParams& fill_linear_params, |
| 72 |
fontations_ffi::BridgeColorStops&, |
| 73 |
uint8_t extend_mode) override; |
| 74 |
virtual void fill_sweep(const fontations_ffi::FillSweepParams& fill_sweep_params, |
| 75 |
fontations_ffi::BridgeColorStops&, |
| 76 |
uint8_t extend_mode) override; |
| 77 |
|
| 78 |
// Optimized calls that allow a SkCanvas::drawPath() call. |
| 79 |
virtual void fill_glyph_solid(uint16_t glyph_id, uint16_t palette_index, float alpha) override; |
| 80 |
virtual void fill_glyph_radial(uint16_t glyph_id, |
| 81 |
const fontations_ffi::Transform& transform, |
| 82 |
const fontations_ffi::FillRadialParams& fill_radial_params, |
| 83 |
fontations_ffi::BridgeColorStops& stops, |
| 84 |
uint8_t) override; |
| 85 |
virtual void fill_glyph_linear(uint16_t glyph_id, |
| 86 |
const fontations_ffi::Transform& transform, |
| 87 |
const fontations_ffi::FillLinearParams& fill_linear_params, |
| 88 |
fontations_ffi::BridgeColorStops& stops, |
| 89 |
uint8_t) override; |
| 90 |
virtual void fill_glyph_sweep(uint16_t glyph_id, |
| 91 |
const fontations_ffi::Transform& transform, |
| 92 |
const fontations_ffi::FillSweepParams& fill_sweep_params, |
| 93 |
fontations_ffi::BridgeColorStops& stops, |
| 94 |
uint8_t) override; |
| 95 |
|
| 96 |
// compositeMode arg matches composite mode values from the OpenType COLR table spec. |
| 97 |
virtual void push_layer(uint8_t compositeMode) override; |
| 98 |
virtual void pop_layer() override; |
| 99 |
|
| 100 |
private: |
| 101 |
void configure_solid_paint(uint16_t palette_index, float alpha, SkPaint& paint); |
| 102 |
void configure_linear_paint(const fontations_ffi::FillLinearParams& fill_linear_params, |
| 103 |
fontations_ffi::BridgeColorStops& bridge_stops, |
| 104 |
uint8_t extend_mode, |
| 105 |
SkPaint& paint, |
| 106 |
SkMatrix* = nullptr); |
| 107 |
void configure_radial_paint(const fontations_ffi::FillRadialParams& fill_radial_params, |
| 108 |
fontations_ffi::BridgeColorStops& bridge_stops, |
| 109 |
uint8_t extend_mode, |
| 110 |
SkPaint& paint, |
| 111 |
SkMatrix* = nullptr); |
| 112 |
void configure_sweep_paint(const fontations_ffi::FillSweepParams& sweep_params, |
| 113 |
fontations_ffi::BridgeColorStops& bridge_stops, |
| 114 |
uint8_t extend_mode, |
| 115 |
SkPaint& paint, |
| 116 |
SkMatrix* = nullptr); |
| 117 |
SkFontationsScalerContext& fScalerContext; |
| 118 |
SkCanvas& fCanvas; |
| 119 |
SkSpan<const SkColor> fPalette; |
| 120 |
SkColor fForegroundColor; |
| 121 |
bool fAntialias; |
| 122 |
uint16_t fUpem; |
| 123 |
}; |
| 124 |
|
| 125 |
/** Tracks transforms and clips to compute a bounding box without drawing pixels. */ |
| 126 |
class BoundsPainter : public fontations_ffi::ColorPainterWrapper { |
| 127 |
public: |
| 128 |
BoundsPainter() = delete; |
| 129 |
BoundsPainter(SkFontationsScalerContext& scaler_context, |
| 130 |
SkMatrix initialTransfom, |
| 131 |
uint16_t upem); |
| 132 |
|
| 133 |
SkRect getBoundingBox(); |
| 134 |
|
| 135 |
// fontations_ffi::ColorPainter interface. |
| 136 |
virtual bool is_bounds_mode() override { return true; } |
| 137 |
virtual void push_transform(const fontations_ffi::Transform& transform) override; |
| 138 |
virtual void pop_transform() override; |
| 139 |
virtual void push_clip_glyph(uint16_t glyph_id) override; |
| 140 |
virtual void push_clip_rectangle(float x_min, float y_min, float x_max, float y_max) override; |
| 141 |
virtual void pop_clip() override {} |
| 142 |
|
| 143 |
// Paint*Gradient equivalents: |
| 144 |
virtual void fill_solid(uint16_t palette_index, float alpha) override {} |
| 145 |
virtual void fill_radial(const fontations_ffi::FillRadialParams& fill_radial_params, |
| 146 |
fontations_ffi::BridgeColorStops& stops, |
| 147 |
uint8_t) override {} |
| 148 |
virtual void fill_linear(const fontations_ffi::FillLinearParams& fill_linear_params, |
| 149 |
fontations_ffi::BridgeColorStops& stops, |
| 150 |
uint8_t) override {} |
| 151 |
virtual void fill_sweep(const fontations_ffi::FillSweepParams& fill_sweep_params, |
| 152 |
fontations_ffi::BridgeColorStops& stops, |
| 153 |
uint8_t extend_mode) override {} |
| 154 |
|
| 155 |
virtual void push_layer(uint8_t) override {} |
| 156 |
virtual void pop_layer() override {} |
| 157 |
|
| 158 |
// Stubs for optimized calls. We're only interested in the glyph bounds, so we forward this to |
| 159 |
// push_clip_glyph() |
| 160 |
virtual void fill_glyph_solid(uint16_t glyph_id, uint16_t, float) override; |
| 161 |
virtual void fill_glyph_radial(uint16_t glyph_id, |
| 162 |
const fontations_ffi::Transform&, |
| 163 |
const fontations_ffi::FillRadialParams&, |
| 164 |
fontations_ffi::BridgeColorStops&, |
| 165 |
uint8_t) override; |
| 166 |
virtual void fill_glyph_linear(uint16_t glyph_id, |
| 167 |
const fontations_ffi::Transform&, |
| 168 |
const fontations_ffi::FillLinearParams&, |
| 169 |
fontations_ffi::BridgeColorStops&, |
| 170 |
uint8_t) override; |
| 171 |
virtual void fill_glyph_sweep(uint16_t glyph_id, |
| 172 |
const fontations_ffi::Transform&, |
| 173 |
const fontations_ffi::FillSweepParams&, |
| 174 |
fontations_ffi::BridgeColorStops&, |
| 175 |
uint8_t) override; |
| 176 |
|
| 177 |
private: |
| 178 |
SkFontationsScalerContext& fScalerContext; |
| 179 |
skia_private::STArray<4, SkMatrix> fMatrixStack; |
| 180 |
|
| 181 |
uint16_t fUpem; |
| 182 |
SkRect fBounds; |
| 183 |
}; |
| 184 |
|
| 185 |
} // namespace sk_fontations |
| 186 |
|
| 187 |
/** SkTypeface implementation based on Google Fonts Fontations Rust libraries. */ |
| 188 |
class SkTypeface_Fontations : public SkTypeface { |
| 189 |
private: |
| 190 |
SkTypeface_Fontations(sk_sp<const SkData> fontData, |
| 191 |
const SkFontStyle& style, |
| 192 |
uint32_t ttcIndex, |
| 193 |
rust::Box<fontations_ffi::BridgeFontRef>&& fontRef, |
| 194 |
rust::Box<fontations_ffi::BridgeMappingIndex>&& mappingIndex, |
| 195 |
rust::Box<fontations_ffi::BridgeNormalizedCoords>&& normalizedCoords, |
| 196 |
rust::Box<fontations_ffi::BridgeOutlineCollection>&& outlines, |
| 197 |
rust::Box<fontations_ffi::BridgeGlyphStyles>&& glyph_styles, |
| 198 |
rust::Vec<uint32_t>&& palette); |
| 199 |
|
| 200 |
public: |
| 201 |
const fontations_ffi::BridgeFontRef& getBridgeFontRef() const { return *fBridgeFontRef; } |
| 202 |
const fontations_ffi::BridgeNormalizedCoords& getBridgeNormalizedCoords() const { |
| 203 |
return *fBridgeNormalizedCoords; |
| 204 |
} |
| 205 |
const fontations_ffi::BridgeOutlineCollection& getOutlines() const { return *fOutlines; } |
| 206 |
const fontations_ffi::BridgeGlyphStyles& getGlyphStyles() const { return *fGlyphStyles; } |
| 207 |
const fontations_ffi::BridgeMappingIndex& getMappingIndex() const { return *fMappingIndex; } |
| 208 |
SkSpan<const SkColor> getPalette() const { |
| 209 |
return SkSpan(reinterpret_cast<const SkColor*>(fPalette.data()), fPalette.size()); |
| 210 |
} |
| 211 |
|
| 212 |
static constexpr SkTypeface::FactoryId FactoryId = SkTypefaces::Fontations::FactoryId; |
| 213 |
|
| 214 |
static sk_sp<SkTypeface> MakeFromData(sk_sp<const SkData> fontData, const SkFontArguments&); |
| 215 |
static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&); |
| 216 |
|
| 217 |
protected: |
| 218 |
std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override; |
| 219 |
sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override; |
| 220 |
std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects& effects, |
| 221 |
const SkDescriptor* desc) const override; |
| 222 |
std::unique_ptr<SkScalerContext> onCreateScalerContextAsProxyTypeface( |
| 223 |
const SkScalerContextEffects&, |
| 224 |
const SkDescriptor*, |
| 225 |
SkTypeface* proxyTypeface) const override; |
| 226 |
void onFilterRec(SkScalerContextRec*) const override; |
| 227 |
std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; |
| 228 |
void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; |
| 229 |
void onCharsToGlyphs(SkSpan<const SkUnichar>, SkSpan<SkGlyphID>) const override; |
| 230 |
int onCountGlyphs() const override; |
| 231 |
void getPostScriptGlyphNames(SkString*) const override {} |
| 232 |
void getGlyphToUnicodeMap(SkSpan<SkUnichar>) const override; |
| 233 |
int onGetUPEM() const override; |
| 234 |
void onGetFamilyName(SkString* familyName) const override; |
| 235 |
bool onGetPostScriptName(SkString*) const override; |
| 236 |
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; |
| 237 |
bool onGlyphMaskNeedsCurrentColor() const override; |
| 238 |
int onGetVariationDesignPosition( |
| 239 |
SkSpan<SkFontArguments::VariationPosition::Coordinate>) const override; |
| 240 |
int onGetVariationDesignParameters(SkSpan<SkFontParameters::Variation::Axis>) const override; |
| 241 |
int onGetTableTags(SkSpan<SkFontTableTag>) const override; |
| 242 |
size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override; |
| 243 |
|
| 244 |
private: |
| 245 |
sk_sp<const SkData> fFontData; |
| 246 |
// Incoming ttc index requested when this typeface was instantiated from data. |
| 247 |
uint32_t fTtcIndex = 0; |
| 248 |
// fBridgeFontRef accesses the data in fFontData. fFontData needs to be kept around for the |
| 249 |
// lifetime of fBridgeFontRef to safely request parsed data. |
| 250 |
rust::Box<fontations_ffi::BridgeFontRef> fBridgeFontRef; |
| 251 |
rust::Box<fontations_ffi::BridgeMappingIndex> fMappingIndex; |
| 252 |
rust::Box<fontations_ffi::BridgeNormalizedCoords> fBridgeNormalizedCoords; |
| 253 |
rust::Box<fontations_ffi::BridgeOutlineCollection> fOutlines; |
| 254 |
rust::Box<fontations_ffi::BridgeGlyphStyles> fGlyphStyles; |
| 255 |
rust::Vec<uint32_t> fPalette; |
| 256 |
|
| 257 |
mutable SkOnce fGlyphMasksMayNeedCurrentColorOnce; |
| 258 |
mutable bool fGlyphMasksMayNeedCurrentColor; |
| 259 |
}; |
| 260 |
|
| 261 |
#endif // SkTypeface_Fontations_DEFINED |