blob: 0287eb08aa82c1a5db5c15fd3fcf8dd65fb2c791 [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
cdalton7d5c9502015-10-03 13:28:35 -070022 {}
cdalton855d83f2014-09-18 13:51:53 -070023
24 virtual ~GlyphGenerator() {
kkinnunen50b58e62015-05-18 23:02:07 -070025#ifdef SK_DEBUG
cdalton855d83f2014-09-18 13:51:53 -070026 SkDescriptor::Free(fDesc);
kkinnunen50b58e62015-05-18 23:02:07 -070027#endif
cdalton855d83f2014-09-18 13:51:53 -070028 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 int getNumPaths() override {
cdalton855d83f2014-09-18 13:51:53 -070031 return fScalerContext->getGlyphCount();
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 void generatePath(int glyphID, SkPath* out) override {
cdalton855d83f2014-09-18 13:51:53 -070035 SkGlyph skGlyph;
herbb69d0e02015-02-25 06:47:06 -080036 skGlyph.initWithGlyphID(glyphID);
cdalton855d83f2014-09-18 13:51:53 -070037 fScalerContext->getMetrics(&skGlyph);
38
39 fScalerContext->getPath(skGlyph, out);
cdalton855d83f2014-09-18 13:51:53 -070040 }
kkinnunen50b58e62015-05-18 23:02:07 -070041#ifdef SK_DEBUG
mtklein36352bf2015-03-25 18:17:31 -070042 bool isEqualTo(const SkDescriptor& desc) const override {
cdalton855d83f2014-09-18 13:51:53 -070043 return fDesc->equals(desc);
44 }
kkinnunen50b58e62015-05-18 23:02:07 -070045#endif
cdalton855d83f2014-09-18 13:51:53 -070046private:
cdalton855d83f2014-09-18 13:51:53 -070047 const SkAutoTDelete<SkScalerContext> fScalerContext;
kkinnunen50b58e62015-05-18 23:02:07 -070048#ifdef SK_DEBUG
49 SkDescriptor* const fDesc;
50#endif
cdalton855d83f2014-09-18 13:51:53 -070051};
52
53GrPathRange* GrPathRendering::createGlyphs(const SkTypeface* typeface,
54 const SkDescriptor* desc,
kkinnunen50b58e62015-05-18 23:02:07 -070055 const GrStrokeInfo& stroke) {
halcanary96fcdcc2015-08-27 07:41:13 -070056 if (nullptr == typeface) {
cdalton855d83f2014-09-18 13:51:53 -070057 typeface = SkTypeface::GetDefaultTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -070058 SkASSERT(nullptr != typeface);
cdalton855d83f2014-09-18 13:51:53 -070059 }
60
61 if (desc) {
halcanary385fe4d2015-08-26 13:07:48 -070062 SkAutoTUnref<GlyphGenerator> generator(new GlyphGenerator(*typeface, *desc));
cdalton855d83f2014-09-18 13:51:53 -070063 return this->createPathRange(generator, stroke);
64 }
65
66 SkScalerContextRec rec;
67 memset(&rec, 0, sizeof(rec));
68 rec.fFontID = typeface->uniqueID();
69 rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths;
70 rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1;
71 // Don't bake stroke information into the glyphs, we'll let the GPU do the stroking.
72
73 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1));
74 SkDescriptor* genericDesc = ad.getDesc();
75
76 genericDesc->init();
77 genericDesc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
78 genericDesc->computeChecksum();
79
halcanary385fe4d2015-08-26 13:07:48 -070080 SkAutoTUnref<GlyphGenerator> generator(new GlyphGenerator(*typeface, *genericDesc));
cdalton855d83f2014-09-18 13:51:53 -070081 return this->createPathRange(generator, stroke);
82}