blob: 710b828ac9642ebd931317fe501bd62593d219f4 [file] [log] [blame]
cdalton855d83f2014-09-18 13:51:53 -07001/*
2 * Copyright 2014 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 "GrPathRendering.h"
9#include "SkDescriptor.h"
10#include "SkGlyph.h"
11#include "SkMatrix.h"
12#include "SkTypeface.h"
13#include "GrPathRange.h"
14
15class GlyphGenerator : public GrPathRange::PathGenerator {
16public:
17 GlyphGenerator(const SkTypeface& typeface, const SkDescriptor& desc)
kkinnunen50b58e62015-05-18 23:02:07 -070018 : fScalerContext(typeface.createScalerContext(&desc))
19#ifdef SK_DEBUG
20 , fDesc(desc.copy())
21#endif
22 {
cdalton855d83f2014-09-18 13:51:53 -070023 fFlipMatrix.setScale(1, -1);
24 }
25
26 virtual ~GlyphGenerator() {
kkinnunen50b58e62015-05-18 23:02:07 -070027#ifdef SK_DEBUG
cdalton855d83f2014-09-18 13:51:53 -070028 SkDescriptor::Free(fDesc);
kkinnunen50b58e62015-05-18 23:02:07 -070029#endif
cdalton855d83f2014-09-18 13:51:53 -070030 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 int getNumPaths() override {
cdalton855d83f2014-09-18 13:51:53 -070033 return fScalerContext->getGlyphCount();
34 }
35
mtklein36352bf2015-03-25 18:17:31 -070036 void generatePath(int glyphID, SkPath* out) override {
cdalton855d83f2014-09-18 13:51:53 -070037 SkGlyph skGlyph;
herbb69d0e02015-02-25 06:47:06 -080038 skGlyph.initWithGlyphID(glyphID);
cdalton855d83f2014-09-18 13:51:53 -070039 fScalerContext->getMetrics(&skGlyph);
40
41 fScalerContext->getPath(skGlyph, out);
42 out->transform(fFlipMatrix); // Load glyphs with the inverted y-direction.
43 }
kkinnunen50b58e62015-05-18 23:02:07 -070044#ifdef SK_DEBUG
mtklein36352bf2015-03-25 18:17:31 -070045 bool isEqualTo(const SkDescriptor& desc) const override {
cdalton855d83f2014-09-18 13:51:53 -070046 return fDesc->equals(desc);
47 }
kkinnunen50b58e62015-05-18 23:02:07 -070048#endif
cdalton855d83f2014-09-18 13:51:53 -070049private:
cdalton855d83f2014-09-18 13:51:53 -070050 const SkAutoTDelete<SkScalerContext> fScalerContext;
51 SkMatrix fFlipMatrix;
kkinnunen50b58e62015-05-18 23:02:07 -070052#ifdef SK_DEBUG
53 SkDescriptor* const fDesc;
54#endif
cdalton855d83f2014-09-18 13:51:53 -070055};
56
57GrPathRange* GrPathRendering::createGlyphs(const SkTypeface* typeface,
58 const SkDescriptor* desc,
kkinnunen50b58e62015-05-18 23:02:07 -070059 const GrStrokeInfo& stroke) {
cdalton855d83f2014-09-18 13:51:53 -070060 if (NULL == typeface) {
61 typeface = SkTypeface::GetDefaultTypeface();
62 SkASSERT(NULL != typeface);
63 }
64
65 if (desc) {
halcanary385fe4d2015-08-26 13:07:48 -070066 SkAutoTUnref<GlyphGenerator> generator(new GlyphGenerator(*typeface, *desc));
cdalton855d83f2014-09-18 13:51:53 -070067 return this->createPathRange(generator, stroke);
68 }
69
70 SkScalerContextRec rec;
71 memset(&rec, 0, sizeof(rec));
72 rec.fFontID = typeface->uniqueID();
73 rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths;
74 rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1;
75 // Don't bake stroke information into the glyphs, we'll let the GPU do the stroking.
76
77 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1));
78 SkDescriptor* genericDesc = ad.getDesc();
79
80 genericDesc->init();
81 genericDesc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
82 genericDesc->computeChecksum();
83
halcanary385fe4d2015-08-26 13:07:48 -070084 SkAutoTUnref<GlyphGenerator> generator(new GlyphGenerator(*typeface, *genericDesc));
cdalton855d83f2014-09-18 13:51:53 -070085 return this->createPathRange(generator, stroke);
86}