2015-07-29 17:14:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
#include "RandomScalerContext.h"
|
2017-05-04 18:23:55 +00:00
|
|
|
#include "SkAdvancedTypefaceMetrics.h"
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkBitmap.h"
|
2016-10-20 20:06:52 +00:00
|
|
|
#include "SkCanvas.h"
|
2015-07-29 17:14:58 +00:00
|
|
|
#include "SkGlyph.h"
|
2016-10-20 20:06:52 +00:00
|
|
|
#include "SkMakeUnique.h"
|
2015-07-29 17:14:58 +00:00
|
|
|
#include "SkPath.h"
|
2018-04-11 18:30:17 +00:00
|
|
|
#include "SkRectPriv.h"
|
2015-07-29 17:14:58 +00:00
|
|
|
|
2016-10-20 20:06:52 +00:00
|
|
|
class SkDescriptor;
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
class RandomScalerContext : public SkScalerContext {
|
2015-07-29 17:14:58 +00:00
|
|
|
public:
|
2019-03-20 16:08:46 +00:00
|
|
|
RandomScalerContext(sk_sp<SkRandomTypeface>,
|
|
|
|
const SkScalerContextEffects&,
|
|
|
|
const SkDescriptor*,
|
|
|
|
bool fFakeIt);
|
2015-07-29 17:14:58 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
unsigned generateGlyphCount() override;
|
|
|
|
uint16_t generateCharToGlyph(SkUnichar) override;
|
2019-03-20 16:08:46 +00:00
|
|
|
bool generateAdvance(SkGlyph*) override;
|
|
|
|
void generateMetrics(SkGlyph*) override;
|
|
|
|
void generateImage(const SkGlyph&) override;
|
|
|
|
bool generatePath(SkGlyphID, SkPath*) override;
|
|
|
|
void generateFontMetrics(SkFontMetrics*) override;
|
2015-07-29 17:14:58 +00:00
|
|
|
|
|
|
|
private:
|
2016-10-20 20:06:52 +00:00
|
|
|
SkRandomTypeface* getRandomTypeface() const {
|
|
|
|
return static_cast<SkRandomTypeface*>(this->getTypeface());
|
|
|
|
}
|
|
|
|
std::unique_ptr<SkScalerContext> fProxy;
|
2019-03-20 16:08:46 +00:00
|
|
|
bool fFakeIt;
|
2015-07-29 17:14:58 +00:00
|
|
|
};
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
RandomScalerContext::RandomScalerContext(sk_sp<SkRandomTypeface> face,
|
|
|
|
const SkScalerContextEffects& effects,
|
|
|
|
const SkDescriptor* desc,
|
|
|
|
bool fakeIt)
|
2016-10-20 20:06:52 +00:00
|
|
|
: SkScalerContext(std::move(face), effects, desc)
|
2018-04-30 19:39:15 +00:00
|
|
|
, fProxy(getRandomTypeface()->proxy()->createScalerContext(SkScalerContextEffects(), desc))
|
2019-03-20 16:08:46 +00:00
|
|
|
, fFakeIt(fakeIt) {
|
2018-04-30 19:39:15 +00:00
|
|
|
fProxy->forceGenerateImageFromPath();
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
unsigned RandomScalerContext::generateGlyphCount() { return fProxy->getGlyphCount(); }
|
2015-07-29 17:14:58 +00:00
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
uint16_t RandomScalerContext::generateCharToGlyph(SkUnichar uni) {
|
2015-07-29 17:14:58 +00:00
|
|
|
return fProxy->charToGlyphID(uni);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
bool RandomScalerContext::generateAdvance(SkGlyph* glyph) { return fProxy->generateAdvance(glyph); }
|
2015-07-29 17:14:58 +00:00
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
void RandomScalerContext::generateMetrics(SkGlyph* glyph) {
|
2015-07-29 17:14:58 +00:00
|
|
|
// Here we will change the mask format of the glyph
|
2018-04-30 19:39:15 +00:00
|
|
|
// NOTE: this may be overridden by the base class (e.g. if a mask filter is applied).
|
2015-08-01 17:33:40 +00:00
|
|
|
switch (glyph->getGlyphID() % 4) {
|
2018-04-30 19:39:15 +00:00
|
|
|
case 0: glyph->fMaskFormat = SkMask::kLCD16_Format; break;
|
|
|
|
case 1: glyph->fMaskFormat = SkMask::kA8_Format; break;
|
|
|
|
case 2: glyph->fMaskFormat = SkMask::kARGB32_Format; break;
|
|
|
|
case 3: glyph->fMaskFormat = SkMask::kBW_Format; break;
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 18:45:22 +00:00
|
|
|
fProxy->getMetrics(glyph);
|
|
|
|
|
2018-04-30 19:39:15 +00:00
|
|
|
if (fFakeIt || (glyph->getGlyphID() % 4) != 2) {
|
2015-07-31 18:45:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-30 19:39:15 +00:00
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
if (!fProxy->getPath(glyph->getPackedID(), &path)) {
|
|
|
|
return;
|
2015-07-31 18:45:22 +00:00
|
|
|
}
|
2018-04-30 19:39:15 +00:00
|
|
|
glyph->fMaskFormat = SkMask::kARGB32_Format;
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
SkRect storage;
|
2018-04-30 19:39:15 +00:00
|
|
|
const SkPaint& paint = this->getRandomTypeface()->paint();
|
2019-03-20 16:08:46 +00:00
|
|
|
const SkRect& newBounds =
|
|
|
|
paint.doComputeFastBounds(path.getBounds(), &storage, SkPaint::kFill_Style);
|
2018-04-30 19:39:15 +00:00
|
|
|
SkIRect ibounds;
|
|
|
|
newBounds.roundOut(&ibounds);
|
2019-03-20 16:08:46 +00:00
|
|
|
glyph->fLeft = ibounds.fLeft;
|
|
|
|
glyph->fTop = ibounds.fTop;
|
|
|
|
glyph->fWidth = ibounds.width();
|
2018-04-30 19:39:15 +00:00
|
|
|
glyph->fHeight = ibounds.height();
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
void RandomScalerContext::generateImage(const SkGlyph& glyph) {
|
2018-04-30 19:39:15 +00:00
|
|
|
// TODO: can force down but not up
|
|
|
|
/*
|
2015-07-29 17:14:58 +00:00
|
|
|
SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
|
2015-08-01 17:33:40 +00:00
|
|
|
switch (glyph.getGlyphID() % 4) {
|
2018-04-30 19:39:15 +00:00
|
|
|
case 0: format = SkMask::kLCD16_Format; break;
|
|
|
|
case 1: format = SkMask::kA8_Format; break;
|
|
|
|
case 2: format = SkMask::kARGB32_Format; break;
|
|
|
|
case 3: format = SkMask::kBW_Format; break;
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
const_cast<SkGlyph&>(glyph).fMaskFormat = format;
|
2018-04-30 19:39:15 +00:00
|
|
|
*/
|
2015-07-29 17:14:58 +00:00
|
|
|
|
2018-04-30 19:39:15 +00:00
|
|
|
if (fFakeIt) {
|
2015-07-31 18:45:22 +00:00
|
|
|
sk_bzero(glyph.fImage, glyph.computeImageSize());
|
2018-04-30 19:39:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SkMask::kARGB32_Format != glyph.fMaskFormat) {
|
|
|
|
fProxy->getImage(glyph);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the format is ARGB, just draw the glyph from path.
|
|
|
|
SkPath path;
|
|
|
|
if (!fProxy->getPath(glyph.getPackedID(), &path)) {
|
|
|
|
fProxy->getImage(glyph);
|
|
|
|
return;
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
2018-04-30 19:39:15 +00:00
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
|
2019-03-20 16:08:46 +00:00
|
|
|
glyph.fImage,
|
|
|
|
glyph.rowBytes());
|
2018-04-30 19:39:15 +00:00
|
|
|
bm.eraseColor(0);
|
|
|
|
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
canvas.translate(-SkIntToScalar(glyph.fLeft), -SkIntToScalar(glyph.fTop));
|
|
|
|
canvas.drawPath(path, this->getRandomTypeface()->paint());
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
bool RandomScalerContext::generatePath(SkGlyphID glyph, SkPath* path) {
|
2018-03-29 15:18:06 +00:00
|
|
|
return fProxy->generatePath(glyph, path);
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
void RandomScalerContext::generateFontMetrics(SkFontMetrics* metrics) {
|
2015-07-29 17:14:58 +00:00
|
|
|
fProxy->getFontMetrics(metrics);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt)
|
2019-03-20 16:08:46 +00:00
|
|
|
: SkTypeface(proxy->fontStyle(), false)
|
|
|
|
, fProxy(std::move(proxy))
|
|
|
|
, fPaint(paint)
|
|
|
|
, fFakeIt(fakeIt) {}
|
2015-07-29 17:14:58 +00:00
|
|
|
|
2016-04-12 13:47:05 +00:00
|
|
|
SkScalerContext* SkRandomTypeface::onCreateScalerContext(const SkScalerContextEffects& effects,
|
2019-03-20 16:08:46 +00:00
|
|
|
const SkDescriptor* desc) const {
|
|
|
|
return new RandomScalerContext(
|
|
|
|
sk_ref_sp(const_cast<SkRandomTypeface*>(this)), effects, desc, fFakeIt);
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
|
|
|
|
fProxy->filterRec(rec);
|
2018-11-05 17:45:32 +00:00
|
|
|
rec->setHinting(kNo_SkFontHinting);
|
2015-07-29 17:14:58 +00:00
|
|
|
rec->fMaskFormat = SkMask::kARGB32_Format;
|
|
|
|
}
|
|
|
|
|
2018-05-09 15:50:34 +00:00
|
|
|
void SkRandomTypeface::getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const {
|
|
|
|
fProxy->getGlyphToUnicodeMap(glyphToUnicode);
|
|
|
|
}
|
|
|
|
|
2017-05-04 18:23:55 +00:00
|
|
|
std::unique_ptr<SkAdvancedTypefaceMetrics> SkRandomTypeface::onGetAdvancedMetrics() const {
|
|
|
|
return fProxy->getAdvancedMetrics();
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 19:27:46 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> SkRandomTypeface::onOpenStream(int* ttcIndex) const {
|
2019-02-26 20:39:41 +00:00
|
|
|
return fProxy->openStream(ttcIndex);
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 15:37:25 +00:00
|
|
|
sk_sp<SkTypeface> SkRandomTypeface::onMakeClone(const SkFontArguments& args) const {
|
|
|
|
sk_sp<SkTypeface> proxy = fProxy->makeClone(args);
|
|
|
|
if (!proxy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return sk_make_sp<SkRandomTypeface>(proxy, fPaint, fFakeIt);
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:39:15 +00:00
|
|
|
void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
|
|
|
|
// TODO: anything that uses this typeface isn't correctly serializable, since this typeface
|
|
|
|
// cannot be deserialized.
|
2015-07-29 17:14:58 +00:00
|
|
|
fProxy->getFontDescriptor(desc, isLocal);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
int SkRandomTypeface::onCharsToGlyphs(const void* chars,
|
|
|
|
Encoding encoding,
|
|
|
|
uint16_t glyphs[],
|
|
|
|
int glyphCount) const {
|
2015-07-29 17:14:58 +00:00
|
|
|
return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
int SkRandomTypeface::onCountGlyphs() const { return fProxy->countGlyphs(); }
|
2015-07-29 17:14:58 +00:00
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
int SkRandomTypeface::onGetUPEM() const { return fProxy->getUnitsPerEm(); }
|
2015-07-29 17:14:58 +00:00
|
|
|
|
|
|
|
void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
|
|
|
|
fProxy->getFamilyName(familyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
|
|
|
|
return fProxy->createFamilyNameIterator();
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:15:26 +00:00
|
|
|
int SkRandomTypeface::onGetVariationDesignPosition(
|
2019-03-20 16:08:46 +00:00
|
|
|
SkFontArguments::VariationPosition::Coordinate coordinates[],
|
|
|
|
int coordinateCount) const {
|
2017-02-24 16:15:26 +00:00
|
|
|
return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
int SkRandomTypeface::onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
|
|
|
|
int parameterCount) const {
|
2018-06-26 15:22:37 +00:00
|
|
|
return fProxy->onGetVariationDesignParameters(parameters, parameterCount);
|
|
|
|
}
|
|
|
|
|
2015-07-29 17:14:58 +00:00
|
|
|
int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
|
|
|
|
return fProxy->getTableTags(tags);
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:08:46 +00:00
|
|
|
size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag,
|
|
|
|
size_t offset,
|
|
|
|
size_t length,
|
|
|
|
void* data) const {
|
2015-07-29 17:14:58 +00:00
|
|
|
return fProxy->getTableData(tag, offset, length, data);
|
|
|
|
}
|