SkGrFontScaler.cpp revision 05b6b4d746867a9fb02e14edfe1bf3685abeb813
1/*
2    Copyright 2010 Google Inc.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8         http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17
18#include "SkGr.h"
19#include "SkDescriptor.h"
20#include "SkGlyphCache.h"
21
22class SkGrDescKey : public GrKey {
23public:
24    explicit SkGrDescKey(const SkDescriptor& desc);
25    virtual ~SkGrDescKey();
26
27protected:
28    // overrides
29    virtual bool lt(const GrKey& rh) const;
30    virtual bool eq(const GrKey& rh) const;
31
32private:
33    SkDescriptor* fDesc;
34    enum {
35        kMaxStorageInts = 16
36    };
37    uint32_t fStorage[kMaxStorageInts];
38};
39
40///////////////////////////////////////////////////////////////////////////////
41
42SkGrDescKey::SkGrDescKey(const SkDescriptor& desc) : GrKey(desc.getChecksum()) {
43    size_t size = desc.getLength();
44    if (size <= sizeof(fStorage)) {
45        fDesc = GrTCast<SkDescriptor*>(fStorage);
46    } else {
47        fDesc = SkDescriptor::Alloc(size);
48    }
49    memcpy(fDesc, &desc, size);
50}
51
52SkGrDescKey::~SkGrDescKey() {
53    if (fDesc != GrTCast<SkDescriptor*>(fStorage)) {
54        SkDescriptor::Free(fDesc);
55    }
56}
57
58bool SkGrDescKey::lt(const GrKey& rh) const {
59    const SkDescriptor* srcDesc = ((const SkGrDescKey*)&rh)->fDesc;
60    size_t lenLH = fDesc->getLength();
61    size_t lenRH = srcDesc->getLength();
62    int cmp = memcmp(fDesc, srcDesc, SkMin32(lenLH, lenRH));
63    if (0 == cmp) {
64        return lenLH < lenRH;
65    } else {
66        return cmp < 0;
67    }
68}
69
70bool SkGrDescKey::eq(const GrKey& rh) const {
71    const SkDescriptor* srcDesc = ((const SkGrDescKey*)&rh)->fDesc;
72    return fDesc->equals(*srcDesc);
73}
74
75///////////////////////////////////////////////////////////////////////////////
76
77SkGrFontScaler::SkGrFontScaler(SkGlyphCache* strike) {
78    fStrike = strike;
79    fKey = NULL;
80}
81
82SkGrFontScaler::~SkGrFontScaler() {
83    GrSafeUnref(fKey);
84}
85
86const GrKey* SkGrFontScaler::getKey() {
87    if (NULL == fKey) {
88        fKey = new SkGrDescKey(fStrike->getDescriptor());
89    }
90    return fKey;
91}
92
93bool SkGrFontScaler::getPackedGlyphBounds(GrGlyph::PackedID packed,
94                                          GrIRect* bounds) {
95    const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
96                                              GrGlyph::UnpackFixedX(packed),
97                                              GrGlyph::UnpackFixedY(packed));
98    bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
99    return true;
100
101}
102
103bool SkGrFontScaler::getPackedGlyphImage(GrGlyph::PackedID packed,
104                                         int width, int height,
105                                         int dstRB, void* dst) {
106    const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
107                                              GrGlyph::UnpackFixedX(packed),
108                                              GrGlyph::UnpackFixedY(packed));
109    GrAssert(glyph.fWidth == width);
110    GrAssert(glyph.fHeight == height);
111    const void* src = fStrike->findImage(glyph);
112    if (NULL == src) {
113        return false;
114    }
115
116    int srcRB = glyph.rowBytes();
117    if (srcRB == dstRB) {
118        memcpy(dst, src, dstRB * height);
119    } else {
120        for (int y = 0; y < height; y++) {
121            memcpy(dst, src, width);
122            src = (const char*)src + srcRB;
123            dst = (char*)dst + dstRB;
124        }
125    }
126    return true;
127}
128
129bool SkGrFontScaler::getGlyphPath(uint16_t glyphID, GrPath* path) {
130
131    const SkGlyph& glyph = fStrike->getGlyphIDMetrics(glyphID);
132    const SkPath* skPath = fStrike->findPath(glyph);
133    if (skPath) {
134        SkGrPathIter iter(*skPath);
135        path->resetFromIter(&iter);
136        return true;
137    }
138    return false;
139}
140
141
142
143