SkRandomScalerContext.cpp revision 853336c532504b3436d7dcbf252419f00c79066d
1/*
2 * Copyright 2015 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 "SkRandomScalerContext.h"
9#include "SkGlyph.h"
10#include "SkPath.h"
11#include "SkCanvas.h"
12#include "SkRasterizer.h"
13
14class SkRandomScalerContext : public SkScalerContext {
15public:
16    SkRandomScalerContext(SkRandomTypeface*, const SkDescriptor*, bool fFakeIt);
17    virtual ~SkRandomScalerContext();
18
19protected:
20    unsigned generateGlyphCount() override;
21    uint16_t generateCharToGlyph(SkUnichar) override;
22    void generateAdvance(SkGlyph*) override;
23    void generateMetrics(SkGlyph*) override;
24    void generateImage(const SkGlyph&) override;
25    void generatePath(const SkGlyph&, SkPath*) override;
26    void generateFontMetrics(SkPaint::FontMetrics*) override;
27
28private:
29    SkRandomTypeface*     fFace;
30    SkScalerContext* fProxy;
31    bool fFakeIt;
32};
33
34#define STD_SIZE    1
35
36#include "SkDescriptor.h"
37
38SkRandomScalerContext::SkRandomScalerContext(SkRandomTypeface* face, const SkDescriptor* desc,
39                                             bool fakeIt)
40        : SkScalerContext(face, desc)
41        , fFace(face)
42        , fFakeIt(fakeIt) {
43    fProxy = face->proxy()->createScalerContext(desc);
44}
45
46SkRandomScalerContext::~SkRandomScalerContext() {
47    SkDELETE(fProxy);
48}
49
50unsigned SkRandomScalerContext::generateGlyphCount() {
51    return fProxy->getGlyphCount();
52}
53
54uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) {
55    return fProxy->charToGlyphID(uni);
56}
57
58void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) {
59    fProxy->getAdvance(glyph);
60}
61
62void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) {
63    // Here we will change the mask format of the glyph
64    // NOTE this is being overridden by the base class
65    SkMask::Format format;
66    switch (glyph->getGlyphID() % 3) {
67        case 0:
68            format = SkMask::kLCD16_Format;
69            break;
70        case 1:
71            format = SkMask::kA8_Format;
72            break;
73        case 2:
74            format = SkMask::kARGB32_Format;
75            break;
76    }
77
78    fProxy->getMetrics(glyph);
79
80    glyph->fMaskFormat = format;
81    if (fFakeIt) {
82        return;
83    }
84    if (SkMask::kARGB32_Format == format) {
85        SkPath path;
86        fProxy->getPath(*glyph, &path);
87
88        SkRect storage;
89        const SkPaint& paint = fFace->paint();
90        const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(),
91                                                            &storage,
92                                                            SkPaint::kFill_Style);
93        SkIRect ibounds;
94        newBounds.roundOut(&ibounds);
95        glyph->fLeft = ibounds.fLeft;
96        glyph->fTop = ibounds.fTop;
97        glyph->fWidth = ibounds.width();
98        glyph->fHeight = ibounds.height();
99    } else {
100        SkPath      devPath, fillPath;
101        SkMatrix    fillToDevMatrix;
102
103        this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix);
104
105        // just use devPath
106        const SkIRect ir = devPath.getBounds().roundOut();
107
108        if (ir.isEmpty() || !ir.is16Bit()) {
109            glyph->fLeft    = 0;
110            glyph->fTop     = 0;
111            glyph->fWidth   = 0;
112            glyph->fHeight  = 0;
113            return;
114        }
115        glyph->fLeft    = ir.fLeft;
116        glyph->fTop     = ir.fTop;
117        glyph->fWidth   = SkToU16(ir.width());
118        glyph->fHeight  = SkToU16(ir.height());
119
120        if (glyph->fWidth > 0) {
121            switch (glyph->fMaskFormat) {
122            case SkMask::kLCD16_Format:
123                glyph->fWidth += 2;
124                glyph->fLeft -= 1;
125                break;
126            default:
127                break;
128            }
129        }
130    }
131}
132
133void SkRandomScalerContext::generateImage(const SkGlyph& glyph) {
134    SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
135    switch (glyph.getGlyphID() % 3) {
136        case 0:
137            format = SkMask::kLCD16_Format;
138            break;
139        case 1:
140            format = SkMask::kA8_Format;
141            break;
142        case 2:
143            format = SkMask::kARGB32_Format;
144            break;
145    }
146    const_cast<SkGlyph&>(glyph).fMaskFormat = format;
147
148    // if the format is ARGB, we just draw the glyph from path ourselves.  Otherwise, we force
149    // our proxy context to generate the image from paths.
150    if (!fFakeIt) {
151        if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
152            SkPath path;
153            fProxy->getPath(glyph, &path);
154
155            SkBitmap bm;
156            bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
157                             glyph.fImage, glyph.rowBytes());
158            bm.eraseColor(0);
159
160            SkCanvas canvas(bm);
161            canvas.translate(-SkIntToScalar(glyph.fLeft),
162                             -SkIntToScalar(glyph.fTop));
163            canvas.drawPath(path, fFace->paint());
164        } else {
165            fProxy->forceGenerateImageFromPath();
166            fProxy->getImage(glyph);
167            fProxy->forceOffGenerateImageFromPath();
168        }
169    } else {
170        sk_bzero(glyph.fImage, glyph.computeImageSize());
171    }
172}
173
174void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) {
175    fProxy->getPath(glyph, path);
176}
177
178void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) {
179    fProxy->getFontMetrics(metrics);
180}
181
182///////////////////////////////////////////////////////////////////////////////
183
184#include "SkTypefaceCache.h"
185
186SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint, bool fakeIt)
187    : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false)
188    , fProxy(SkRef(proxy))
189    , fPaint(paint)
190    , fFakeIt(fakeIt) {}
191
192SkRandomTypeface::~SkRandomTypeface() {
193    fProxy->unref();
194}
195
196SkScalerContext* SkRandomTypeface::onCreateScalerContext(
197                                            const SkDescriptor* desc) const {
198    return SkNEW_ARGS(SkRandomScalerContext, (const_cast<SkRandomTypeface*>(this), desc, fFakeIt));
199}
200
201void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
202    fProxy->filterRec(rec);
203    rec->setHinting(SkPaint::kNo_Hinting);
204    rec->fMaskFormat = SkMask::kARGB32_Format;
205}
206
207SkAdvancedTypefaceMetrics* SkRandomTypeface::onGetAdvancedTypefaceMetrics(
208                                PerGlyphInfo info,
209                                const uint32_t* glyphIDs,
210                                uint32_t glyphIDsCount) const {
211    return fProxy->getAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
212}
213
214SkStreamAsset* SkRandomTypeface::onOpenStream(int* ttcIndex) const {
215    return fProxy->openStream(ttcIndex);
216}
217
218void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
219                                      bool* isLocal) const {
220    fProxy->getFontDescriptor(desc, isLocal);
221}
222
223int SkRandomTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
224                                 uint16_t glyphs[], int glyphCount) const {
225    return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
226}
227
228int SkRandomTypeface::onCountGlyphs() const {
229    return fProxy->countGlyphs();
230}
231
232int SkRandomTypeface::onGetUPEM() const {
233    return fProxy->getUnitsPerEm();
234}
235
236void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
237    fProxy->getFamilyName(familyName);
238}
239
240SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
241    return fProxy->createFamilyNameIterator();
242}
243
244int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
245    return fProxy->getTableTags(tags);
246}
247
248size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
249                                    size_t length, void* data) const {
250    return fProxy->getTableData(tag, offset, length, data);
251}
252
253