blob: 456f6d84c0b37fb4a2b0f10dd1c745b27af8a183 [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)
18 : fDesc(desc.copy()),
19 fScalerContext(typeface.createScalerContext(fDesc)) {
20 fFlipMatrix.setScale(1, -1);
21 }
22
23 virtual ~GlyphGenerator() {
24 SkDescriptor::Free(fDesc);
25 }
26
mtklein72c9faa2015-01-09 10:06:39 -080027 int getNumPaths() SK_OVERRIDE {
cdalton855d83f2014-09-18 13:51:53 -070028 return fScalerContext->getGlyphCount();
29 }
30
mtklein72c9faa2015-01-09 10:06:39 -080031 void generatePath(int glyphID, SkPath* out) SK_OVERRIDE {
cdalton855d83f2014-09-18 13:51:53 -070032 SkGlyph skGlyph;
herbb69d0e02015-02-25 06:47:06 -080033 skGlyph.initWithGlyphID(glyphID);
cdalton855d83f2014-09-18 13:51:53 -070034 fScalerContext->getMetrics(&skGlyph);
35
36 fScalerContext->getPath(skGlyph, out);
37 out->transform(fFlipMatrix); // Load glyphs with the inverted y-direction.
38 }
39
mtklein72c9faa2015-01-09 10:06:39 -080040 bool isEqualTo(const SkDescriptor& desc) const SK_OVERRIDE {
cdalton855d83f2014-09-18 13:51:53 -070041 return fDesc->equals(desc);
42 }
43
44private:
45 SkDescriptor* const fDesc;
46 const SkAutoTDelete<SkScalerContext> fScalerContext;
47 SkMatrix fFlipMatrix;
48};
49
50GrPathRange* GrPathRendering::createGlyphs(const SkTypeface* typeface,
51 const SkDescriptor* desc,
52 const SkStrokeRec& stroke) {
53 if (NULL == typeface) {
54 typeface = SkTypeface::GetDefaultTypeface();
55 SkASSERT(NULL != typeface);
56 }
57
58 if (desc) {
59 SkAutoTUnref<GlyphGenerator> generator(SkNEW_ARGS(GlyphGenerator, (*typeface, *desc)));
60 return this->createPathRange(generator, stroke);
61 }
62
63 SkScalerContextRec rec;
64 memset(&rec, 0, sizeof(rec));
65 rec.fFontID = typeface->uniqueID();
66 rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths;
67 rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1;
68 // Don't bake stroke information into the glyphs, we'll let the GPU do the stroking.
69
70 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1));
71 SkDescriptor* genericDesc = ad.getDesc();
72
73 genericDesc->init();
74 genericDesc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
75 genericDesc->computeChecksum();
76
77 SkAutoTUnref<GlyphGenerator> generator(SkNEW_ARGS(GlyphGenerator, (*typeface, *genericDesc)));
78 return this->createPathRange(generator, stroke);
79}