2014-06-23 18:25:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-05-12 16:50:04 +00:00
|
|
|
#include "SkAdvancedTypefaceMetrics.h"
|
2014-06-23 18:25:00 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDescriptor.h"
|
2014-07-31 12:58:44 +00:00
|
|
|
#include "SkFontDescriptor.h"
|
2014-06-23 18:25:00 +00:00
|
|
|
#include "SkGlyph.h"
|
2016-10-20 20:06:52 +00:00
|
|
|
#include "SkMakeUnique.h"
|
2014-06-23 18:25:00 +00:00
|
|
|
#include "SkMask.h"
|
2017-11-09 18:45:10 +00:00
|
|
|
#include "SkOTUtils.h"
|
2017-02-14 01:44:33 +00:00
|
|
|
#include "SkPaintPriv.h"
|
2014-06-23 18:25:00 +00:00
|
|
|
#include "SkScalerContext.h"
|
|
|
|
#include "SkTestScalerContext.h"
|
|
|
|
#include "SkTypefaceCache.h"
|
2017-11-10 18:42:29 +00:00
|
|
|
#include "SkUtils.h"
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
SkTestFont::SkTestFont(const SkTestFontData& fontData)
|
|
|
|
: INHERITED()
|
|
|
|
, fCharCodes(fontData.fCharCodes)
|
2016-06-16 16:52:35 +00:00
|
|
|
, fCharCodesCount(fontData.fCharCodes ? fontData.fCharCodesCount : 0)
|
2014-07-31 12:58:44 +00:00
|
|
|
, fWidths(fontData.fWidths)
|
|
|
|
, fMetrics(fontData.fMetrics)
|
|
|
|
, fName(fontData.fName)
|
2015-08-27 14:41:13 +00:00
|
|
|
, fPaths(nullptr)
|
2014-07-31 12:58:44 +00:00
|
|
|
{
|
|
|
|
init(fontData.fPoints, fontData.fVerbs);
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
SkTestFont::~SkTestFont() {
|
|
|
|
for (unsigned index = 0; index < fCharCodesCount; ++index) {
|
2015-08-26 20:07:48 +00:00
|
|
|
delete fPaths[index];
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
2015-08-26 20:07:48 +00:00
|
|
|
delete[] fPaths;
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
int SkTestFont::codeToIndex(SkUnichar charCode) const {
|
|
|
|
for (unsigned index = 0; index < fCharCodesCount; ++index) {
|
|
|
|
if (fCharCodes[index] == (unsigned) charCode) {
|
|
|
|
return (int) index;
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
void SkTestFont::init(const SkScalar* pts, const unsigned char* verbs) {
|
2015-08-26 20:07:48 +00:00
|
|
|
fPaths = new SkPath* [fCharCodesCount];
|
2014-07-31 12:58:44 +00:00
|
|
|
for (unsigned index = 0; index < fCharCodesCount; ++index) {
|
2015-08-26 20:07:48 +00:00
|
|
|
SkPath* path = new SkPath;
|
2014-07-31 12:58:44 +00:00
|
|
|
SkPath::Verb verb;
|
|
|
|
while ((verb = (SkPath::Verb) *verbs++) != SkPath::kDone_Verb) {
|
|
|
|
switch (verb) {
|
|
|
|
case SkPath::kMove_Verb:
|
|
|
|
path->moveTo(pts[0], pts[1]);
|
|
|
|
pts += 2;
|
|
|
|
break;
|
|
|
|
case SkPath::kLine_Verb:
|
|
|
|
path->lineTo(pts[0], pts[1]);
|
|
|
|
pts += 2;
|
|
|
|
break;
|
|
|
|
case SkPath::kQuad_Verb:
|
|
|
|
path->quadTo(pts[0], pts[1], pts[2], pts[3]);
|
|
|
|
pts += 4;
|
|
|
|
break;
|
|
|
|
case SkPath::kCubic_Verb:
|
|
|
|
path->cubicTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
|
|
|
|
pts += 6;
|
|
|
|
break;
|
|
|
|
case SkPath::kClose_Verb:
|
|
|
|
path->close();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SkDEBUGFAIL("bad verb");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-10-05 14:29:22 +00:00
|
|
|
// This should make SkPath::getBounds() queries threadsafe.
|
|
|
|
path->updateBoundsCache();
|
2014-07-31 12:58:44 +00:00
|
|
|
fPaths[index] = path;
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
2014-10-20 20:33:19 +00:00
|
|
|
|
2016-10-20 20:06:52 +00:00
|
|
|
SkTestTypeface::SkTestTypeface(sk_sp<SkTestFont> testFont, const SkFontStyle& style)
|
2016-07-13 12:16:58 +00:00
|
|
|
: SkTypeface(style, false)
|
2016-10-20 20:06:52 +00:00
|
|
|
, fTestFont(std::move(testFont)) {
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
void SkTestTypeface::getAdvance(SkGlyph* glyph) {
|
2016-03-25 02:07:58 +00:00
|
|
|
// TODO(benjaminwagner): Update users to use floats.
|
|
|
|
glyph->fAdvanceX = SkFixedToFloat(fTestFont->fWidths[glyph->getGlyphID()]);
|
2014-07-31 12:58:44 +00:00
|
|
|
glyph->fAdvanceY = 0;
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
void SkTestTypeface::getFontMetrics(SkPaint::FontMetrics* metrics) {
|
|
|
|
*metrics = fTestFont->fMetrics;
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
void SkTestTypeface::getMetrics(SkGlyph* glyph) {
|
2017-11-09 18:45:10 +00:00
|
|
|
SkGlyphID glyphID = glyph->getGlyphID();
|
|
|
|
glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0;
|
|
|
|
|
2016-03-25 02:07:58 +00:00
|
|
|
// TODO(benjaminwagner): Update users to use floats.
|
2017-11-09 18:45:10 +00:00
|
|
|
glyph->fAdvanceX = SkFixedToFloat(fTestFont->fWidths[glyphID]);
|
2014-07-31 12:58:44 +00:00
|
|
|
glyph->fAdvanceY = 0;
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2017-11-09 18:45:10 +00:00
|
|
|
void SkTestTypeface::getPath(SkGlyphID glyphID, SkPath* path) {
|
|
|
|
glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0;
|
|
|
|
*path = *fTestFont->fPaths[glyphID];
|
2014-07-31 12:58:44 +00:00
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
void SkTestTypeface::onFilterRec(SkScalerContextRec* rec) const {
|
|
|
|
rec->setHinting(SkPaint::kNo_Hinting);
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2017-05-04 18:23:55 +00:00
|
|
|
std::unique_ptr<SkAdvancedTypefaceMetrics> SkTestTypeface::onGetAdvancedMetrics() const { // pdf only
|
|
|
|
std::unique_ptr<SkAdvancedTypefaceMetrics> info(new SkAdvancedTypefaceMetrics);
|
2015-06-11 13:46:21 +00:00
|
|
|
info->fFontName.set(fTestFont->fName);
|
2016-08-31 19:52:35 +00:00
|
|
|
int glyphCount = this->onCountGlyphs();
|
|
|
|
|
|
|
|
SkTDArray<SkUnichar>& toUnicode = info->fGlyphToUnicode;
|
|
|
|
toUnicode.setCount(glyphCount);
|
|
|
|
SkASSERT(glyphCount == SkToInt(fTestFont->fCharCodesCount));
|
|
|
|
for (int gid = 0; gid < glyphCount; ++gid) {
|
|
|
|
toUnicode[gid] = SkToS32(fTestFont->fCharCodes[gid]);
|
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
return info;
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
void SkTestTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
|
|
|
|
desc->setFamilyName(fTestFont->fName);
|
2016-07-25 23:54:59 +00:00
|
|
|
desc->setStyle(this->fontStyle());
|
2014-07-31 12:58:44 +00:00
|
|
|
*isLocal = false;
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
int SkTestTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
|
2017-01-11 18:26:43 +00:00
|
|
|
uint16_t glyphs[], int glyphCount) const {
|
2017-11-10 18:42:29 +00:00
|
|
|
auto utf8 = (const char*)chars;
|
|
|
|
auto utf16 = (const uint16_t*)chars;
|
|
|
|
auto utf32 = (const SkUnichar*)chars;
|
|
|
|
|
|
|
|
for (int i = 0; i < glyphCount; i++) {
|
|
|
|
SkUnichar ch;
|
|
|
|
switch (encoding) {
|
|
|
|
case kUTF8_Encoding: ch = SkUTF8_NextUnichar(&utf8 ); break;
|
|
|
|
case kUTF16_Encoding: ch = SkUTF16_NextUnichar(&utf16); break;
|
|
|
|
case kUTF32_Encoding: ch = *utf32++; break;
|
|
|
|
}
|
|
|
|
glyphs[i] = fTestFont->codeToIndex(ch);
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
2014-07-31 12:58:44 +00:00
|
|
|
return glyphCount;
|
|
|
|
}
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2014-09-17 14:48:59 +00:00
|
|
|
void SkTestTypeface::onGetFamilyName(SkString* familyName) const {
|
|
|
|
*familyName = fTestFont->fName;
|
|
|
|
}
|
|
|
|
|
2014-07-31 12:58:44 +00:00
|
|
|
SkTypeface::LocalizedStrings* SkTestTypeface::onCreateFamilyNameIterator() const {
|
|
|
|
SkString familyName(fTestFont->fName);
|
|
|
|
SkString language("und"); //undetermined
|
2017-11-09 18:45:10 +00:00
|
|
|
return new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SkTestScalerContext : public SkScalerContext {
|
|
|
|
public:
|
2016-10-20 20:06:52 +00:00
|
|
|
SkTestScalerContext(sk_sp<SkTestTypeface> face, const SkScalerContextEffects& effects,
|
2016-04-12 13:47:05 +00:00
|
|
|
const SkDescriptor* desc)
|
2016-10-20 20:06:52 +00:00
|
|
|
: SkScalerContext(std::move(face), effects, desc)
|
2014-06-23 18:25:00 +00:00
|
|
|
{
|
|
|
|
fRec.getSingleMatrix(&fMatrix);
|
|
|
|
this->forceGenerateImageFromPath();
|
|
|
|
}
|
|
|
|
|
2016-10-20 20:06:52 +00:00
|
|
|
protected:
|
|
|
|
SkTestTypeface* getTestTypeface() const {
|
|
|
|
return static_cast<SkTestTypeface*>(this->getTypeface());
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
unsigned generateGlyphCount() override {
|
2016-10-20 20:06:52 +00:00
|
|
|
return this->getTestTypeface()->onCountGlyphs();
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
uint16_t generateCharToGlyph(SkUnichar uni) override {
|
2014-06-23 18:25:00 +00:00
|
|
|
uint16_t glyph;
|
2016-10-20 20:06:52 +00:00
|
|
|
(void) this->getTestTypeface()->onCharsToGlyphs((const void *) &uni,
|
2017-01-11 18:26:43 +00:00
|
|
|
SkTypeface::kUTF32_Encoding, &glyph, 1);
|
2014-06-23 18:25:00 +00:00
|
|
|
return glyph;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void generateAdvance(SkGlyph* glyph) override {
|
2016-10-20 20:06:52 +00:00
|
|
|
this->getTestTypeface()->getAdvance(glyph);
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2016-03-25 02:07:58 +00:00
|
|
|
const SkVector advance = fMatrix.mapXY(SkFloatToScalar(glyph->fAdvanceX),
|
|
|
|
SkFloatToScalar(glyph->fAdvanceY));
|
|
|
|
glyph->fAdvanceX = SkScalarToFloat(advance.fX);
|
|
|
|
glyph->fAdvanceY = SkScalarToFloat(advance.fY);
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void generateMetrics(SkGlyph* glyph) override {
|
2016-10-20 20:06:52 +00:00
|
|
|
this->getTestTypeface()->getMetrics(glyph);
|
2014-06-23 18:25:00 +00:00
|
|
|
|
2016-03-25 02:07:58 +00:00
|
|
|
const SkVector advance = fMatrix.mapXY(SkFloatToScalar(glyph->fAdvanceX),
|
|
|
|
SkFloatToScalar(glyph->fAdvanceY));
|
|
|
|
glyph->fAdvanceX = SkScalarToFloat(advance.fX);
|
|
|
|
glyph->fAdvanceY = SkScalarToFloat(advance.fY);
|
2014-06-23 18:25:00 +00:00
|
|
|
|
|
|
|
SkPath path;
|
2016-11-11 19:31:06 +00:00
|
|
|
this->getTestTypeface()->getPath(glyph->getGlyphID(), &path);
|
2014-06-23 18:25:00 +00:00
|
|
|
path.transform(fMatrix);
|
|
|
|
|
|
|
|
SkRect storage;
|
|
|
|
const SkPaint paint;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void generateImage(const SkGlyph& glyph) override {
|
2014-06-23 18:25:00 +00:00
|
|
|
SkPath path;
|
2016-11-11 19:31:06 +00:00
|
|
|
this->getTestTypeface()->getPath(glyph.getGlyphID(), &path);
|
2014-06-23 18:25:00 +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));
|
|
|
|
canvas.concat(fMatrix);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas.drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:31:06 +00:00
|
|
|
void generatePath(SkGlyphID glyph, SkPath* path) override {
|
2016-10-20 20:06:52 +00:00
|
|
|
this->getTestTypeface()->getPath(glyph, path);
|
2014-06-23 18:25:00 +00:00
|
|
|
path->transform(fMatrix);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void generateFontMetrics(SkPaint::FontMetrics* metrics) override {
|
2016-10-20 20:06:52 +00:00
|
|
|
this->getTestTypeface()->getFontMetrics(metrics);
|
2017-02-14 01:44:33 +00:00
|
|
|
SkPaintPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY());
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkMatrix fMatrix;
|
|
|
|
};
|
|
|
|
|
2016-10-20 20:06:52 +00:00
|
|
|
SkScalerContext* SkTestTypeface::onCreateScalerContext(
|
|
|
|
const SkScalerContextEffects& effects, const SkDescriptor* desc) const
|
|
|
|
{
|
|
|
|
return new SkTestScalerContext(sk_ref_sp(const_cast<SkTestTypeface*>(this)), effects, desc);
|
2014-06-23 18:25:00 +00:00
|
|
|
}
|