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.
|
|
|
|
*/
|
|
|
|
|
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"
|
2016-10-20 20:06:52 +00:00
|
|
|
#include "SkRandomScalerContext.h"
|
2015-07-29 17:14:58 +00:00
|
|
|
|
2016-10-20 20:06:52 +00:00
|
|
|
class SkDescriptor;
|
|
|
|
|
2015-07-29 17:14:58 +00:00
|
|
|
class SkRandomScalerContext : public SkScalerContext {
|
|
|
|
public:
|
2016-10-20 20:06:52 +00:00
|
|
|
SkRandomScalerContext(sk_sp<SkRandomTypeface>, const SkScalerContextEffects&,
|
2016-04-12 13:47:05 +00:00
|
|
|
const SkDescriptor*, bool fFakeIt);
|
2015-07-29 17:14:58 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
unsigned generateGlyphCount() override;
|
|
|
|
uint16_t generateCharToGlyph(SkUnichar) override;
|
|
|
|
void generateAdvance(SkGlyph*) override;
|
|
|
|
void generateMetrics(SkGlyph*) override;
|
|
|
|
void generateImage(const SkGlyph&) override;
|
2016-11-11 19:31:06 +00:00
|
|
|
void generatePath(SkGlyphID, SkPath*) override;
|
2015-07-29 17:14:58 +00:00
|
|
|
void generateFontMetrics(SkPaint::FontMetrics*) override;
|
|
|
|
|
|
|
|
private:
|
2016-10-20 20:06:52 +00:00
|
|
|
SkRandomTypeface* getRandomTypeface() const {
|
|
|
|
return static_cast<SkRandomTypeface*>(this->getTypeface());
|
|
|
|
}
|
|
|
|
std::unique_ptr<SkScalerContext> fProxy;
|
2015-07-31 18:45:22 +00:00
|
|
|
bool fFakeIt;
|
2015-07-29 17:14:58 +00:00
|
|
|
};
|
|
|
|
|
2016-10-20 20:06:52 +00:00
|
|
|
SkRandomScalerContext::SkRandomScalerContext(sk_sp<SkRandomTypeface> face,
|
2016-04-12 13:47:05 +00:00
|
|
|
const SkScalerContextEffects& effects,
|
|
|
|
const SkDescriptor* desc,
|
2015-07-31 18:45:22 +00:00
|
|
|
bool fakeIt)
|
2016-10-20 20:06:52 +00:00
|
|
|
: SkScalerContext(std::move(face), effects, desc)
|
2015-07-31 18:45:22 +00:00
|
|
|
, fFakeIt(fakeIt) {
|
2016-10-20 20:06:52 +00:00
|
|
|
fProxy = this->getRandomTypeface()->proxy()->createScalerContext(effects, desc);
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SkRandomScalerContext::generateGlyphCount() {
|
|
|
|
return fProxy->getGlyphCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) {
|
|
|
|
return fProxy->charToGlyphID(uni);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) {
|
|
|
|
fProxy->getAdvance(glyph);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) {
|
|
|
|
// Here we will change the mask format of the glyph
|
|
|
|
// NOTE this is being overridden by the base class
|
2016-04-05 23:55:41 +00:00
|
|
|
SkMask::Format format = SkMask::kARGB32_Format; // init to handle defective compilers
|
2015-08-01 17:33:40 +00:00
|
|
|
switch (glyph->getGlyphID() % 4) {
|
2015-07-29 17:14:58 +00:00
|
|
|
case 0:
|
|
|
|
format = SkMask::kLCD16_Format;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
format = SkMask::kA8_Format;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
format = SkMask::kARGB32_Format;
|
|
|
|
break;
|
2015-08-01 17:33:40 +00:00
|
|
|
case 3:
|
|
|
|
format = SkMask::kBW_Format;
|
|
|
|
break;
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 18:45:22 +00:00
|
|
|
fProxy->getMetrics(glyph);
|
|
|
|
|
2015-07-29 17:14:58 +00:00
|
|
|
glyph->fMaskFormat = format;
|
2015-07-31 18:45:22 +00:00
|
|
|
if (fFakeIt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (SkMask::kARGB32_Format == format) {
|
|
|
|
SkPath path;
|
2016-11-11 19:31:06 +00:00
|
|
|
fProxy->getPath(glyph->getPackedID(), &path);
|
2015-07-31 18:45:22 +00:00
|
|
|
|
|
|
|
SkRect storage;
|
2016-10-20 20:06:52 +00:00
|
|
|
const SkPaint& paint = this->getRandomTypeface()->paint();
|
2015-07-31 18:45:22 +00:00
|
|
|
const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(),
|
|
|
|
&storage,
|
|
|
|
SkPaint::kFill_Style);
|
|
|
|
SkIRect ibounds;
|
|
|
|
newBounds.roundOut(&ibounds);
|
|
|
|
glyph->fLeft = ibounds.fLeft;
|
|
|
|
glyph->fTop = ibounds.fTop;
|
|
|
|
glyph->fWidth = ibounds.width();
|
|
|
|
glyph->fHeight = ibounds.height();
|
|
|
|
} else {
|
|
|
|
SkPath devPath, fillPath;
|
|
|
|
SkMatrix fillToDevMatrix;
|
|
|
|
|
2016-11-11 19:31:06 +00:00
|
|
|
this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix);
|
2015-07-31 18:45:22 +00:00
|
|
|
|
|
|
|
// just use devPath
|
|
|
|
const SkIRect ir = devPath.getBounds().roundOut();
|
|
|
|
|
|
|
|
if (ir.isEmpty() || !ir.is16Bit()) {
|
|
|
|
glyph->fLeft = 0;
|
|
|
|
glyph->fTop = 0;
|
|
|
|
glyph->fWidth = 0;
|
|
|
|
glyph->fHeight = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
glyph->fLeft = ir.fLeft;
|
|
|
|
glyph->fTop = ir.fTop;
|
|
|
|
glyph->fWidth = SkToU16(ir.width());
|
|
|
|
glyph->fHeight = SkToU16(ir.height());
|
|
|
|
|
|
|
|
if (glyph->fWidth > 0) {
|
|
|
|
switch (glyph->fMaskFormat) {
|
|
|
|
case SkMask::kLCD16_Format:
|
|
|
|
glyph->fWidth += 2;
|
|
|
|
glyph->fLeft -= 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkRandomScalerContext::generateImage(const SkGlyph& glyph) {
|
|
|
|
SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
|
2015-08-01 17:33:40 +00:00
|
|
|
switch (glyph.getGlyphID() % 4) {
|
2015-07-29 17:14:58 +00:00
|
|
|
case 0:
|
|
|
|
format = SkMask::kLCD16_Format;
|
|
|
|
break;
|
2015-07-31 18:45:22 +00:00
|
|
|
case 1:
|
2015-07-29 17:14:58 +00:00
|
|
|
format = SkMask::kA8_Format;
|
|
|
|
break;
|
2015-07-31 18:45:22 +00:00
|
|
|
case 2:
|
2015-07-29 17:14:58 +00:00
|
|
|
format = SkMask::kARGB32_Format;
|
|
|
|
break;
|
2015-08-01 17:33:40 +00:00
|
|
|
case 3:
|
|
|
|
format = SkMask::kBW_Format;
|
|
|
|
break;
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
const_cast<SkGlyph&>(glyph).fMaskFormat = format;
|
|
|
|
|
|
|
|
// if the format is ARGB, we just draw the glyph from path ourselves. Otherwise, we force
|
|
|
|
// our proxy context to generate the image from paths.
|
2015-07-31 18:45:22 +00:00
|
|
|
if (!fFakeIt) {
|
|
|
|
if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
|
|
|
|
SkPath path;
|
2016-11-11 19:31:06 +00:00
|
|
|
fProxy->getPath(glyph.getPackedID(), &path);
|
2015-07-31 18:45:22 +00:00
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
|
|
|
|
glyph.fImage, glyph.rowBytes());
|
|
|
|
bm.eraseColor(0);
|
|
|
|
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
canvas.translate(-SkIntToScalar(glyph.fLeft),
|
|
|
|
-SkIntToScalar(glyph.fTop));
|
2016-10-20 20:06:52 +00:00
|
|
|
canvas.drawPath(path, this->getRandomTypeface()->paint());
|
2015-07-31 18:45:22 +00:00
|
|
|
} else {
|
|
|
|
fProxy->forceGenerateImageFromPath();
|
|
|
|
fProxy->getImage(glyph);
|
|
|
|
fProxy->forceOffGenerateImageFromPath();
|
|
|
|
}
|
2015-07-29 17:14:58 +00:00
|
|
|
} else {
|
2015-07-31 18:45:22 +00:00
|
|
|
sk_bzero(glyph.fImage, glyph.computeImageSize());
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:31:06 +00:00
|
|
|
void SkRandomScalerContext::generatePath(SkGlyphID glyph, SkPath* path) {
|
|
|
|
fProxy->generatePath(glyph, path);
|
2015-07-29 17:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) {
|
|
|
|
fProxy->getFontMetrics(metrics);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkTypefaceCache.h"
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt)
|
2016-07-13 12:16:58 +00:00
|
|
|
: SkTypeface(proxy->fontStyle(), false)
|
2016-05-12 17:09:30 +00:00
|
|
|
, fProxy(std::move(proxy))
|
2015-07-31 18:45:22 +00:00
|
|
|
, 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,
|
|
|
|
const SkDescriptor* desc) const {
|
2016-10-20 20:06:52 +00:00
|
|
|
return new SkRandomScalerContext(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);
|
|
|
|
rec->setHinting(SkPaint::kNo_Hinting);
|
|
|
|
rec->fMaskFormat = SkMask::kARGB32_Format;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
SkStreamAsset* SkRandomTypeface::onOpenStream(int* ttcIndex) const {
|
|
|
|
return fProxy->openStream(ttcIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
|
|
|
|
bool* isLocal) const {
|
|
|
|
fProxy->getFontDescriptor(desc, isLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SkRandomTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
|
|
|
|
uint16_t glyphs[], int glyphCount) const {
|
|
|
|
return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SkRandomTypeface::onCountGlyphs() const {
|
|
|
|
return fProxy->countGlyphs();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SkRandomTypeface::onGetUPEM() const {
|
|
|
|
return fProxy->getUnitsPerEm();
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
|
|
|
|
{
|
|
|
|
return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount);
|
|
|
|
}
|
|
|
|
|
2015-07-29 17:14:58 +00:00
|
|
|
int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
|
|
|
|
return fProxy->getTableTags(tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
|
|
|
|
size_t length, void* data) const {
|
|
|
|
return fProxy->getTableData(tag, offset, length, data);
|
|
|
|
}
|
|
|
|
|