blob: 7e03e2aff48f2a067ccd1e6ab5c7d9da8e1c31f9 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
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#ifndef GrTextStrike_impl_DEFINED
19#define GrTextStrike_impl_DEFINED
20
21class GrFontCache::Key {
22public:
23 Key(GrFontScaler* scaler) {
24 fFontScalerKey = scaler->getKey();
25 }
26
27 uint32_t getHash() const { return fFontScalerKey->getHash(); }
28
29 static bool LT(const GrTextStrike& strike, const Key& key) {
30 return *strike.getFontScalerKey() < *key.fFontScalerKey;
31 }
32 static bool EQ(const GrTextStrike& strike, const Key& key) {
33 return *strike.getFontScalerKey() == *key.fFontScalerKey;
34 }
35
36private:
37 const GrKey* fFontScalerKey;
38};
39
40void GrFontCache::detachStrikeFromList(GrTextStrike* strike) {
41 if (strike->fPrev) {
42 GrAssert(fHead != strike);
43 strike->fPrev->fNext = strike->fNext;
44 } else {
45 GrAssert(fHead == strike);
46 fHead = strike->fNext;
47 }
48
49 if (strike->fNext) {
50 GrAssert(fTail != strike);
51 strike->fNext->fPrev = strike->fPrev;
52 } else {
53 GrAssert(fTail == strike);
54 fTail = strike->fPrev;
55 }
56}
57
58GrTextStrike* GrFontCache::getStrike(GrFontScaler* scaler) {
59 this->validate();
60
61 Key key(scaler);
62 GrTextStrike* strike = fCache.find(key);
63 if (NULL == strike) {
64 strike = this->generateStrike(scaler, key);
65 } else if (strike->fPrev) {
66 // Need to put the strike at the head of its dllist, since that is how
67 // we age the strikes for purging (we purge from the back of the list
68 this->detachStrikeFromList(strike);
69 // attach at the head
70 fHead->fPrev = strike;
71 strike->fNext = fHead;
72 strike->fPrev = NULL;
73 fHead = strike;
74 }
75
76 this->validate();
77 return strike;
78}
79
80///////////////////////////////////////////////////////////////////////////////
81
82/**
83 * This Key just wraps a glyphID, and matches the protocol need for
84 * GrTHashTable
85 */
86class GrTextStrike::Key {
87public:
88 Key(GrGlyph::PackedID id) : fPackedID(id) {}
89
90 uint32_t getHash() const { return fPackedID; }
91
92 static bool LT(const GrGlyph& glyph, const Key& key) {
93 return glyph.fPackedID < key.fPackedID;
94 }
95 static bool EQ(const GrGlyph& glyph, const Key& key) {
96 return glyph.fPackedID == key.fPackedID;
97 }
98
99private:
100 GrGlyph::PackedID fPackedID;
101};
102
103GrGlyph* GrTextStrike::getGlyph(GrGlyph::PackedID packed,
104 GrFontScaler* scaler) {
105 GrGlyph* glyph = fCache.find(packed);
106 if (NULL == glyph) {
107 glyph = this->generateGlyph(packed, scaler);
108 }
109 return glyph;
110}
111
112#endif
113