aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar joshualitt <joshualitt@chromium.org>2015-07-29 10:14:58 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-07-29 10:14:58 -0700
commitaa2f65818dd626f87bb011c993a03015eb29db50 (patch)
treece5cf2d9e535da2bdd470a3521e62b30f05bfd5f
parent1e8a58b5680508c241f25f03af5b57502221f215 (diff)
Adding debug SkScalerContext which returns random mask formats
TBR=reed@google.com BUG=skia: Review URL: https://codereview.chromium.org/1260473004
-rw-r--r--gyp/utils.gypi2
-rw-r--r--include/core/SkTypeface.h1
-rw-r--r--src/core/SkScalerContext.h1
-rw-r--r--src/fonts/SkRandomScalerContext.cpp264
-rw-r--r--src/fonts/SkRandomScalerContext.h54
5 files changed, 322 insertions, 0 deletions
diff --git a/gyp/utils.gypi b/gyp/utils.gypi
index 50f8ed618c..e4033e9020 100644
--- a/gyp/utils.gypi
+++ b/gyp/utils.gypi
@@ -126,6 +126,8 @@
#testing
'<(skia_src_path)/fonts/SkGScalerContext.cpp',
'<(skia_src_path)/fonts/SkGScalerContext.h',
+ '<(skia_src_path)/fonts/SkRandomScalerContext.cpp',
+ '<(skia_src_path)/fonts/SkRandomScalerContext.h',
'<(skia_src_path)/fonts/SkTestScalerContext.cpp',
'<(skia_src_path)/fonts/SkTestScalerContext.h',
],
diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h
index 9eac50e03f..a9204a6df9 100644
--- a/include/core/SkTypeface.h
+++ b/include/core/SkTypeface.h
@@ -377,6 +377,7 @@ protected:
private:
friend class SkGTypeface;
+ friend class SkRandomTypeface;
friend class SkPDFFont;
friend class SkPDFCIDFont;
friend class GrPathRendering;
diff --git a/src/core/SkScalerContext.h b/src/core/SkScalerContext.h
index 0362816e02..7fa1ef5fcd 100644
--- a/src/core/SkScalerContext.h
+++ b/src/core/SkScalerContext.h
@@ -298,6 +298,7 @@ protected:
virtual SkUnichar generateGlyphToChar(uint16_t glyphId);
void forceGenerateImageFromPath() { fGenerateImageFromPath = true; }
+ void forceOffGenerateImageFromPath() { fGenerateImageFromPath = false; }
private:
// never null
diff --git a/src/fonts/SkRandomScalerContext.cpp b/src/fonts/SkRandomScalerContext.cpp
new file mode 100644
index 0000000000..2bfa289937
--- /dev/null
+++ b/src/fonts/SkRandomScalerContext.cpp
@@ -0,0 +1,264 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkRandomScalerContext.h"
+#include "SkGlyph.h"
+#include "SkPath.h"
+#include "SkCanvas.h"
+
+class SkRandomScalerContext : public SkScalerContext {
+public:
+ SkRandomScalerContext(SkRandomTypeface*, const SkDescriptor*);
+ virtual ~SkRandomScalerContext();
+
+protected:
+ unsigned generateGlyphCount() override;
+ uint16_t generateCharToGlyph(SkUnichar) override;
+ void generateAdvance(SkGlyph*) override;
+ void generateMetrics(SkGlyph*) override;
+ void generateImage(const SkGlyph&) override;
+ void generatePath(const SkGlyph&, SkPath*) override;
+ void generateFontMetrics(SkPaint::FontMetrics*) override;
+
+private:
+ SkRandomTypeface* fFace;
+ SkScalerContext* fProxy;
+ SkMatrix fMatrix;
+};
+
+#define STD_SIZE 1
+
+#include "SkDescriptor.h"
+
+SkRandomScalerContext::SkRandomScalerContext(SkRandomTypeface* face, const SkDescriptor* desc)
+ : SkScalerContext(face, desc)
+ , fFace(face) {
+ size_t descSize = SkDescriptor::ComputeOverhead(1) + sizeof(SkScalerContext::Rec);
+ SkAutoDescriptor ad(descSize);
+ SkDescriptor* newDesc = ad.getDesc();
+
+ newDesc->init();
+ void* entry = newDesc->addEntry(kRec_SkDescriptorTag,
+ sizeof(SkScalerContext::Rec), &fRec);
+ {
+ SkScalerContext::Rec* rec = (SkScalerContext::Rec*)entry;
+ rec->fTextSize = STD_SIZE;
+ rec->fPreScaleX = SK_Scalar1;
+ rec->fPreSkewX = 0;
+ rec->fPost2x2[0][0] = rec->fPost2x2[1][1] = SK_Scalar1;
+ rec->fPost2x2[1][0] = rec->fPost2x2[0][1] = 0;
+ }
+ SkASSERT(descSize == newDesc->getLength());
+ newDesc->computeChecksum();
+
+ fProxy = face->proxy()->createScalerContext(newDesc);
+
+ fRec.getSingleMatrix(&fMatrix);
+ fMatrix.preScale(SK_Scalar1 / STD_SIZE, SK_Scalar1 / STD_SIZE);
+}
+
+SkRandomScalerContext::~SkRandomScalerContext() {
+ SkDELETE(fProxy);
+}
+
+unsigned SkRandomScalerContext::generateGlyphCount() {
+ return fProxy->getGlyphCount();
+}
+
+uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) {
+ return fProxy->charToGlyphID(uni);
+}
+
+void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) {
+ fProxy->getAdvance(glyph);
+
+ SkVector advance;
+ fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX),
+ SkFixedToScalar(glyph->fAdvanceY), &advance);
+ glyph->fAdvanceX = SkScalarToFixed(advance.fX);
+ glyph->fAdvanceY = SkScalarToFixed(advance.fY);
+}
+
+void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) {
+ fProxy->getAdvance(glyph);
+
+ SkVector advance;
+ fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX),
+ SkFixedToScalar(glyph->fAdvanceY), &advance);
+ glyph->fAdvanceX = SkScalarToFixed(advance.fX);
+ glyph->fAdvanceY = SkScalarToFixed(advance.fY);
+
+ SkPath path;
+ fProxy->getPath(*glyph, &path);
+ path.transform(fMatrix);
+
+ SkRect storage;
+ const SkPaint& paint = fFace->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();
+
+ // Here we will change the mask format of the glyph
+ // NOTE this is being overridden by the base class
+ SkMask::Format format;
+ switch (glyph->getGlyphID() % 6) {
+ case 0:
+ format = SkMask::kLCD16_Format;
+ break;
+ case 1:
+ format = SkMask::kA8_Format;
+ break;
+ case 2:
+ format = SkMask::kARGB32_Format;
+ break;
+ default:
+ // we will fiddle with these in generate image
+ format = (SkMask::Format)MASK_FORMAT_UNKNOWN;
+ }
+
+ glyph->fMaskFormat = format;
+}
+
+void SkRandomScalerContext::generateImage(const SkGlyph& glyph) {
+ SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
+ switch (glyph.getGlyphID() % 6) {
+ case 0:
+ case 1:
+ case 2:
+ break;
+ case 3:
+ format = SkMask::kLCD16_Format;
+ break;
+ case 4:
+ format = SkMask::kA8_Format;
+ break;
+ case 5:
+ format = SkMask::kARGB32_Format;
+ break;
+ }
+ 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.
+ if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
+ SkPath path;
+ fProxy->getPath(glyph, &path);
+
+ 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);
+ canvas.drawPath(path, fFace->paint());
+ } else {
+ this->forceGenerateImageFromPath();
+ fProxy->getImage(glyph);
+ this->forceOffGenerateImageFromPath();
+ }
+}
+
+void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) {
+ fProxy->getPath(glyph, path);
+ path->transform(fMatrix);
+}
+
+void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) {
+ fProxy->getFontMetrics(metrics);
+ if (metrics) {
+ SkScalar scale = fMatrix.getScaleY();
+ metrics->fTop = SkScalarMul(metrics->fTop, scale);
+ metrics->fAscent = SkScalarMul(metrics->fAscent, scale);
+ metrics->fDescent = SkScalarMul(metrics->fDescent, scale);
+ metrics->fBottom = SkScalarMul(metrics->fBottom, scale);
+ metrics->fLeading = SkScalarMul(metrics->fLeading, scale);
+ metrics->fAvgCharWidth = SkScalarMul(metrics->fAvgCharWidth, scale);
+ metrics->fXMin = SkScalarMul(metrics->fXMin, scale);
+ metrics->fXMax = SkScalarMul(metrics->fXMax, scale);
+ metrics->fXHeight = SkScalarMul(metrics->fXHeight, scale);
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+#include "SkTypefaceCache.h"
+
+SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint)
+ : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false)
+ , fProxy(SkRef(proxy))
+ , fPaint(paint) {}
+
+SkRandomTypeface::~SkRandomTypeface() {
+ fProxy->unref();
+}
+
+SkScalerContext* SkRandomTypeface::onCreateScalerContext(
+ const SkDescriptor* desc) const {
+ return SkNEW_ARGS(SkRandomScalerContext, (const_cast<SkRandomTypeface*>(this), desc));
+}
+
+void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
+ fProxy->filterRec(rec);
+ rec->setHinting(SkPaint::kNo_Hinting);
+ rec->fMaskFormat = SkMask::kARGB32_Format;
+}
+
+SkAdvancedTypefaceMetrics* SkRandomTypeface::onGetAdvancedTypefaceMetrics(
+ PerGlyphInfo info,
+ const uint32_t* glyphIDs,
+ uint32_t glyphIDsCount) const {
+ return fProxy->getAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
+}
+
+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();
+}
+
+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);
+}
+
diff --git a/src/fonts/SkRandomScalerContext.h b/src/fonts/SkRandomScalerContext.h
new file mode 100644
index 0000000000..8ad22beff0
--- /dev/null
+++ b/src/fonts/SkRandomScalerContext.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkRandomScalerContext_DEFINED
+#define SkRandomScalerContext_DEFINED
+
+#include "SkScalerContext.h"
+#include "SkTypeface.h"
+
+/*
+ * This scaler context is for debug only purposes. It will 'randomly' but deterministically return
+ * LCD / A8 / BW / RBGA masks based off of the Glyph ID
+ */
+
+class SkRandomTypeface : public SkTypeface {
+public:
+ SkRandomTypeface(SkTypeface* proxy, const SkPaint&);
+ virtual ~SkRandomTypeface();
+
+ SkTypeface* proxy() const { return fProxy; }
+ const SkPaint& paint() const { return fPaint; }
+
+protected:
+ SkScalerContext* onCreateScalerContext(const SkDescriptor*) const override;
+ void onFilterRec(SkScalerContextRec*) const override;
+ SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
+ PerGlyphInfo,
+ const uint32_t* glyphIDs,
+ uint32_t glyphIDsCount) const override;
+ SkStreamAsset* onOpenStream(int* ttcIndex) const override;
+ void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const override;
+
+ int onCharsToGlyphs(const void* chars, Encoding encoding,
+ uint16_t glyphs[], int glyphCount) const override;
+ int onCountGlyphs() const override;
+ int onGetUPEM() const override;
+
+ void onGetFamilyName(SkString* familyName) const override;
+ SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
+
+ int onGetTableTags(SkFontTableTag tags[]) const override;
+ size_t onGetTableData(SkFontTableTag, size_t offset,
+ size_t length, void* data) const override;
+
+private:
+ SkTypeface* fProxy;
+ SkPaint fPaint;
+};
+
+#endif