1 // Copyright 2019 Google LLC.
2
3 #include "modules/skparagraph/include/FontArguments.h"
4
operator ==(const SkFontArguments::VariationPosition::Coordinate & a,const SkFontArguments::VariationPosition::Coordinate & b)5 static bool operator==(const SkFontArguments::VariationPosition::Coordinate& a,
6 const SkFontArguments::VariationPosition::Coordinate& b) {
7 return a.axis == b.axis && a.value == b.value;
8 }
9
operator ==(const SkFontArguments::Palette::Override & a,const SkFontArguments::Palette::Override & b)10 static bool operator==(const SkFontArguments::Palette::Override& a,
11 const SkFontArguments::Palette::Override& b) {
12 return a.index == b.index && a.color == b.color;
13 }
14
15 namespace std {
16
operator ()(const skia::textlayout::FontArguments & args) const17 size_t hash<skia::textlayout::FontArguments>::operator()(const skia::textlayout::FontArguments& args) const {
18 size_t hash = 0;
19 hash ^= std::hash<int>()(args.fCollectionIndex);
20 for (const auto& coord : args.fCoordinates) {
21 hash ^= std::hash<SkFourByteTag>()(coord.axis);
22 hash ^= std::hash<float>()(coord.value);
23 }
24 hash ^= std::hash<int>()(args.fPaletteIndex);
25 for (const auto& override : args.fPaletteOverrides) {
26 hash ^= std::hash<int>()(override.index);
27 hash ^= std::hash<SkColor>()(override.color);
28 }
29 return hash;
30 }
31
32 } // namespace std
33
34 namespace skia {
35 namespace textlayout {
36
FontArguments(const SkFontArguments & args)37 FontArguments::FontArguments(const SkFontArguments& args)
38 : fCollectionIndex(args.getCollectionIndex()),
39 fCoordinates(args.getVariationDesignPosition().coordinates,
40 args.getVariationDesignPosition().coordinates +
41 args.getVariationDesignPosition().coordinateCount),
42 fPaletteIndex(args.getPalette().index),
43 fPaletteOverrides(args.getPalette().overrides,
44 args.getPalette().overrides +
45 args.getPalette().overrideCount) {}
46
operator ==(const FontArguments & a,const FontArguments & b)47 bool operator==(const FontArguments& a, const FontArguments& b) {
48 return a.fCollectionIndex == b.fCollectionIndex &&
49 a.fCoordinates == b.fCoordinates &&
50 a.fPaletteIndex == b.fPaletteIndex &&
51 a.fPaletteOverrides == b.fPaletteOverrides;
52 }
53
operator !=(const skia::textlayout::FontArguments & a,const skia::textlayout::FontArguments & b)54 bool operator!=(const skia::textlayout::FontArguments& a, const skia::textlayout::FontArguments& b) {
55 return !(a == b);
56 }
57
58 #ifndef USE_SKIA_TXT
CloneTypeface(sk_sp<SkTypeface> typeface) const59 sk_sp<SkTypeface> FontArguments::CloneTypeface(sk_sp<SkTypeface> typeface) const {
60 SkFontArguments::VariationPosition position{
61 fCoordinates.data(),
62 static_cast<int>(fCoordinates.size())
63 };
64
65 SkFontArguments::Palette palette{
66 fPaletteIndex,
67 fPaletteOverrides.data(),
68 static_cast<int>(fPaletteOverrides.size())
69 };
70
71 SkFontArguments args;
72 args.setCollectionIndex(fCollectionIndex);
73 args.setVariationDesignPosition(position);
74 args.setPalette(palette);
75
76 return typeface->makeClone(args);
77 }
78 #else
CloneTypeface(std::shared_ptr<RSTypeface> typeface) const79 std::shared_ptr<RSTypeface> FontArguments::CloneTypeface(std::shared_ptr<RSTypeface> typeface) const
80 {
81 RSFontArguments::VariationPosition position{
82 (RSFontArguments::VariationPosition::Coordinate*)fCoordinates.data(),
83 static_cast<int>(fCoordinates.size())
84 };
85
86 RSFontArguments::Palette palette{
87 fPaletteIndex,
88 (RSFontArguments::Palette::Override*)fPaletteOverrides.data(),
89 static_cast<int>(fPaletteOverrides.size())
90 };
91
92 RSFontArguments args;
93 args.SetCollectionIndex(fCollectionIndex);
94 args.SetVariationDesignPosition(position);
95 args.SetPalette(palette);
96
97 return typeface->MakeClone(args);
98 }
99 #endif
100
101 } // namespace textlayout
102 } // namespace skia
103