| 1 |
/* |
| 2 |
* Copyright 2015 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 |
#include "tools/fonts/RandomScalerContext.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkDrawable.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkStream.h" |
| 15 |
#include "src/core/SkAdvancedTypefaceMetrics.h" |
| 16 |
#include "src/core/SkGlyph.h" |
| 17 |
#include "src/core/SkRectPriv.h" |
| 18 |
#include "src/core/SkTHash.h" |
| 19 |
|
| 20 |
using namespace skia_private; |
| 21 |
|
| 22 |
class SkDescriptor; |
| 23 |
|
| 24 |
class RandomScalerContext : public SkScalerContext { |
| 25 |
public: |
| 26 |
RandomScalerContext(SkRandomTypeface&, |
| 27 |
const SkScalerContextEffects&, |
| 28 |
const SkDescriptor*, |
| 29 |
bool fFakeIt); |
| 30 |
|
| 31 |
protected: |
| 32 |
GlyphMetrics generateMetrics(const SkGlyph&, SkArenaAlloc*) override; |
| 33 |
void generateImage(const SkGlyph&, void*) override; |
| 34 |
std::optional<GeneratedPath> generatePath(const SkGlyph&) override; |
| 35 |
sk_sp<SkDrawable> generateDrawable(const SkGlyph&) override; |
| 36 |
void generateFontMetrics(SkFontMetrics*) override; |
| 37 |
|
| 38 |
private: |
| 39 |
SkRandomTypeface* getRandomTypeface() const { |
| 40 |
return static_cast<SkRandomTypeface*>(this->getTypeface()); |
| 41 |
} |
| 42 |
std::unique_ptr<SkScalerContext> fProxy; |
| 43 |
// Many of the SkGlyphs returned are the same as those created by the fProxy. |
| 44 |
// When they are not, the originals are kept here. |
| 45 |
THashMap<SkPackedGlyphID, SkGlyph> fProxyGlyphs; |
| 46 |
bool fFakeIt; |
| 47 |
}; |
| 48 |
|
| 49 |
RandomScalerContext::RandomScalerContext(SkRandomTypeface& face, |
| 50 |
const SkScalerContextEffects& effects, |
| 51 |
const SkDescriptor* desc, |
| 52 |
bool fakeIt) |
| 53 |
: SkScalerContext(face, effects, desc) |
| 54 |
, fProxy(getRandomTypeface()->proxy()->createScalerContext(SkScalerContextEffects(), desc)) |
| 55 |
, fFakeIt(fakeIt) {} |
| 56 |
|
| 57 |
SkScalerContext::GlyphMetrics RandomScalerContext::generateMetrics(const SkGlyph& origGlyph, |
| 58 |
SkArenaAlloc* alloc) { |
| 59 |
// Here we will change the mask format of the glyph |
| 60 |
// NOTE: this may be overridden by the base class (e.g. if a mask filter is applied). |
| 61 |
SkMask::Format format = SkMask::kA8_Format; |
| 62 |
switch (origGlyph.getGlyphID() % 4) { |
| 63 |
case 0: format = SkMask::kLCD16_Format; break; |
| 64 |
case 1: format = SkMask::kA8_Format; break; |
| 65 |
case 2: format = SkMask::kARGB32_Format; break; |
| 66 |
case 3: format = SkMask::kBW_Format; break; |
| 67 |
} |
| 68 |
|
| 69 |
auto glyph = fProxy->internalMakeGlyph(origGlyph.getPackedID(), format, alloc); |
| 70 |
|
| 71 |
GlyphMetrics mx(SkMask::kA8_Format); |
| 72 |
mx.advance = glyph.advanceVector(); |
| 73 |
mx.bounds = glyph.rect(); |
| 74 |
mx.maskFormat = glyph.maskFormat(); |
| 75 |
mx.extraBits = glyph.extraBits(); |
| 76 |
|
| 77 |
if (fFakeIt || (glyph.getGlyphID() % 4) != 2) { |
| 78 |
mx.neverRequestPath = glyph.setPathHasBeenCalled() && !glyph.path(); |
| 79 |
mx.computeFromPath = !mx.neverRequestPath; |
| 80 |
return mx; |
| 81 |
} |
| 82 |
|
| 83 |
fProxy->getPath(glyph, alloc); |
| 84 |
if (!glyph.path()) { |
| 85 |
mx.neverRequestPath = true; |
| 86 |
return mx; |
| 87 |
} |
| 88 |
|
| 89 |
// The proxy glyph has a path, but this glyph does not. |
| 90 |
// Stash the proxy glyph so it can be used later. |
| 91 |
const auto packedID = glyph.getPackedID(); |
| 92 |
const SkGlyph* proxyGlyph = fProxyGlyphs.set(packedID, std::move(glyph)); |
| 93 |
const SkPath& proxyPath = *proxyGlyph->path(); |
| 94 |
|
| 95 |
mx.neverRequestPath = true; |
| 96 |
mx.maskFormat = SkMask::kARGB32_Format; |
| 97 |
mx.advance = proxyGlyph->advanceVector(); |
| 98 |
mx.extraBits = proxyGlyph->extraBits(); |
| 99 |
|
| 100 |
SkRect storage; |
| 101 |
const SkPaint& paint = this->getRandomTypeface()->paint(); |
| 102 |
const SkRect& newBounds = |
| 103 |
paint.doComputeFastBounds(proxyPath.getBounds(), &storage, SkPaint::kFill_Style); |
| 104 |
newBounds.roundOut(&mx.bounds); |
| 105 |
|
| 106 |
return mx; |
| 107 |
} |
| 108 |
|
| 109 |
void RandomScalerContext::generateImage(const SkGlyph& glyph, void* imageBuffer) { |
| 110 |
if (fFakeIt) { |
| 111 |
sk_bzero(imageBuffer, glyph.imageSize()); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
SkGlyph* proxyGlyph = fProxyGlyphs.find(glyph.getPackedID()); |
| 116 |
if (!proxyGlyph || !proxyGlyph->path()) { |
| 117 |
fProxy->getImage(glyph); |
| 118 |
return; |
| 119 |
} |
| 120 |
const SkPath& path = *proxyGlyph->path(); |
| 121 |
const bool hairline = proxyGlyph->pathIsHairline(); |
| 122 |
|
| 123 |
SkBitmap bm; |
| 124 |
bm.installPixels(SkImageInfo::MakeN32Premul(glyph.width(), glyph.height()), |
| 125 |
imageBuffer, glyph.rowBytes()); |
| 126 |
bm.eraseColor(0); |
| 127 |
|
| 128 |
SkCanvas canvas(bm); |
| 129 |
canvas.translate(-SkIntToScalar(glyph.left()), -SkIntToScalar(glyph.top())); |
| 130 |
SkPaint paint = this->getRandomTypeface()->paint(); |
| 131 |
if (hairline) { |
| 132 |
// We have a device path with effects already applied which is normally a fill path. |
| 133 |
// However here we do not have a fill path and there is no area to fill. |
| 134 |
paint.setStyle(SkPaint::kStroke_Style); |
| 135 |
paint.setStroke(0); |
| 136 |
} |
| 137 |
canvas.drawPath(path, paint); //Need to modify the paint if the devPath is hairline |
| 138 |
} |
| 139 |
|
| 140 |
std::optional<SkScalerContext::GeneratedPath> |
| 141 |
RandomScalerContext::generatePath(const SkGlyph& glyph) { |
| 142 |
SkGlyph* shadowProxyGlyph = fProxyGlyphs.find(glyph.getPackedID()); |
| 143 |
if (shadowProxyGlyph && shadowProxyGlyph->path()) { |
| 144 |
return {}; |
| 145 |
} |
| 146 |
return fProxy->generatePath(glyph); |
| 147 |
} |
| 148 |
|
| 149 |
sk_sp<SkDrawable> RandomScalerContext::generateDrawable(const SkGlyph& glyph) { |
| 150 |
SkGlyph* shadowProxyGlyph = fProxyGlyphs.find(glyph.getPackedID()); |
| 151 |
if (shadowProxyGlyph && shadowProxyGlyph->path()) { |
| 152 |
return nullptr; |
| 153 |
} |
| 154 |
return fProxy->generateDrawable(glyph); |
| 155 |
} |
| 156 |
|
| 157 |
void RandomScalerContext::generateFontMetrics(SkFontMetrics* metrics) { |
| 158 |
fProxy->getFontMetrics(metrics); |
| 159 |
} |
| 160 |
|
| 161 |
/////////////////////////////////////////////////////////////////////////////// |
| 162 |
|
| 163 |
SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt) |
| 164 |
: SkTypeface(proxy->fontStyle(), false) |
| 165 |
, fProxy(std::move(proxy)) |
| 166 |
, fPaint(paint) |
| 167 |
, fFakeIt(fakeIt) {} |
| 168 |
|
| 169 |
std::unique_ptr<SkScalerContext> SkRandomTypeface::onCreateScalerContext( |
| 170 |
const SkScalerContextEffects& effects, const SkDescriptor* desc) const |
| 171 |
{ |
| 172 |
return std::make_unique<RandomScalerContext>( |
| 173 |
*const_cast<SkRandomTypeface*>(this), effects, desc, fFakeIt); |
| 174 |
} |
| 175 |
|
| 176 |
void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const { |
| 177 |
fProxy->filterRec(rec); |
| 178 |
rec->setHinting(SkFontHinting::kNone); |
| 179 |
rec->fMaskFormat = SkMask::kARGB32_Format; |
| 180 |
} |
| 181 |
|
| 182 |
void SkRandomTypeface::getGlyphToUnicodeMap(SkSpan<SkUnichar> glyphToUnicode) const { |
| 183 |
fProxy->getGlyphToUnicodeMap(glyphToUnicode); |
| 184 |
} |
| 185 |
|
| 186 |
std::unique_ptr<SkAdvancedTypefaceMetrics> SkRandomTypeface::onGetAdvancedMetrics() const { |
| 187 |
return fProxy->getAdvancedMetrics(); |
| 188 |
} |
| 189 |
|
| 190 |
std::unique_ptr<SkStreamAsset> SkRandomTypeface::onOpenStream(int* ttcIndex) const { |
| 191 |
return fProxy->openStream(ttcIndex); |
| 192 |
} |
| 193 |
|
| 194 |
sk_sp<SkTypeface> SkRandomTypeface::onMakeClone(const SkFontArguments& args) const { |
| 195 |
sk_sp<SkTypeface> proxy = fProxy->makeClone(args); |
| 196 |
if (!proxy) { |
| 197 |
return nullptr; |
| 198 |
} |
| 199 |
return sk_make_sp<SkRandomTypeface>(proxy, fPaint, fFakeIt); |
| 200 |
} |
| 201 |
|
| 202 |
void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const { |
| 203 |
// TODO: anything that uses this typeface isn't correctly serializable, since this typeface |
| 204 |
// cannot be deserialized. |
| 205 |
fProxy->getFontDescriptor(desc, isLocal); |
| 206 |
} |
| 207 |
|
| 208 |
void SkRandomTypeface::onCharsToGlyphs(SkSpan<const SkUnichar> uni, |
| 209 |
SkSpan<SkGlyphID> glyphs) const { |
| 210 |
fProxy->unicharsToGlyphs(uni, glyphs); |
| 211 |
} |
| 212 |
|
| 213 |
int SkRandomTypeface::onCountGlyphs() const { return fProxy->countGlyphs(); } |
| 214 |
|
| 215 |
int SkRandomTypeface::onGetUPEM() const { return fProxy->getUnitsPerEm(); } |
| 216 |
|
| 217 |
void SkRandomTypeface::onGetFamilyName(SkString* familyName) const { |
| 218 |
fProxy->getFamilyName(familyName); |
| 219 |
} |
| 220 |
|
| 221 |
bool SkRandomTypeface::onGetPostScriptName(SkString* postScriptName) const { |
| 222 |
return fProxy->getPostScriptName(postScriptName); |
| 223 |
} |
| 224 |
|
| 225 |
SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const { |
| 226 |
return fProxy->createFamilyNameIterator(); |
| 227 |
} |
| 228 |
|
| 229 |
void SkRandomTypeface::getPostScriptGlyphNames(SkString* names) const { |
| 230 |
return fProxy->getPostScriptGlyphNames(names); |
| 231 |
} |
| 232 |
|
| 233 |
bool SkRandomTypeface::onGlyphMaskNeedsCurrentColor() const { |
| 234 |
return fProxy->glyphMaskNeedsCurrentColor(); |
| 235 |
} |
| 236 |
|
| 237 |
int SkRandomTypeface::onGetVariationDesignPosition( |
| 238 |
SkSpan<SkFontArguments::VariationPosition::Coordinate> coordinates) const { |
| 239 |
return fProxy->onGetVariationDesignPosition(coordinates); |
| 240 |
} |
| 241 |
|
| 242 |
int SkRandomTypeface::onGetVariationDesignParameters( |
| 243 |
SkSpan<SkFontParameters::Variation::Axis> parameters) const { |
| 244 |
return fProxy->onGetVariationDesignParameters(parameters); |
| 245 |
} |
| 246 |
|
| 247 |
int SkRandomTypeface::onGetTableTags(SkSpan<SkFontTableTag> tags) const { |
| 248 |
return fProxy->readTableTags(tags); |
| 249 |
} |
| 250 |
|
| 251 |
size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, |
| 252 |
size_t offset, |
| 253 |
size_t length, |
| 254 |
void* data) const { |
| 255 |
return fProxy->getTableData(tag, offset, length, data); |
| 256 |
} |