OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrTextContext.h" | 8 #include "GrTextContext.h" |
9 #include "GrContext.h" | 9 #include "GrContext.h" |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 fDrawTarget = fContext->getTextTarget(); | 37 fDrawTarget = fContext->getTextTarget(); |
38 | 38 |
39 fPaint = grPaint; | 39 fPaint = grPaint; |
40 fSkPaint = skPaint; | 40 fSkPaint = skPaint; |
41 } | 41 } |
42 | 42 |
43 bool GrTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint, | 43 bool GrTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint, |
44 const char text[], size_t byteLength, | 44 const char text[], size_t byteLength, |
45 SkScalar x, SkScalar y) { | 45 SkScalar x, SkScalar y) { |
46 | 46 |
47 SkASSERT(byteLength == 0 || text != NULL); | 47 GrTextContext* textContext = this; |
| 48 do { |
| 49 if (textContext->canDraw(skPaint)) { |
| 50 textContext->onDrawText(paint, skPaint, text, byteLength, x, y); |
| 51 return true; |
| 52 } |
| 53 textContext = textContext->fFallbackTextContext; |
| 54 } while (textContext); |
48 | 55 |
49 // nothing to draw | 56 return false; |
50 if (text == NULL || byteLength == 0) { | |
51 return true; | |
52 } | |
53 | |
54 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc(); | |
55 SkAutoGlyphCache autoCache(skPaint, &fDeviceProperties, NULL); | |
56 SkGlyphCache* cache = autoCache.getCache(); | |
57 | |
58 SkTArray<SkScalar> positions; | |
59 | |
60 const char* textPtr = text; | |
61 SkFixed stopX = 0; | |
62 SkFixed stopY = 0; | |
63 SkFixed origin; | |
64 switch (skPaint.getTextAlign()) { | |
65 case SkPaint::kRight_Align: origin = SK_Fixed1; break; | |
66 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break; | |
67 case SkPaint::kLeft_Align: origin = 0; break; | |
68 default: SkFAIL("Invalid paint origin"); return false; | |
69 } | |
70 | |
71 SkAutoKern autokern; | |
72 const char* stop = text + byteLength; | |
73 while (textPtr < stop) { | |
74 // don't need x, y here, since all subpixel variants will have the | |
75 // same advance | |
76 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0); | |
77 | |
78 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph); | |
79 positions.push_back(SkFixedToScalar(stopX + SkFixedMul_portable(origin,
width))); | |
80 | |
81 SkFixed height = glyph.fAdvanceY; | |
82 positions.push_back(SkFixedToScalar(stopY + SkFixedMul_portable(origin,
height))); | |
83 | |
84 stopX += width; | |
85 stopY += height; | |
86 } | |
87 SkASSERT(textPtr == stop); | |
88 | |
89 // now adjust starting point depending on alignment | |
90 SkScalar alignX = SkFixedToScalar(stopX); | |
91 SkScalar alignY = SkFixedToScalar(stopY); | |
92 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) { | |
93 alignX = SkScalarHalf(alignX); | |
94 alignY = SkScalarHalf(alignY); | |
95 } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) { | |
96 alignX = 0; | |
97 alignY = 0; | |
98 } | |
99 x -= alignX; | |
100 y -= alignY; | |
101 SkPoint offset = SkPoint::Make(x, y); | |
102 | |
103 return this->drawPosText(paint, skPaint, text, byteLength, positions.begin()
, 2, offset); | |
104 } | 57 } |
105 | 58 |
106 bool GrTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint, | 59 bool GrTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint, |
107 const char text[], size_t byteLength, | 60 const char text[], size_t byteLength, |
108 const SkScalar pos[], int scalarsPerPosition, | 61 const SkScalar pos[], int scalarsPerPosition, |
109 const SkPoint& offset) { | 62 const SkPoint& offset) { |
110 | 63 |
111 GrTextContext* textContext = this; | 64 GrTextContext* textContext = this; |
112 do { | 65 do { |
113 if (textContext->canDraw(skPaint)) { | 66 if (textContext->canDraw(skPaint)) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { | 108 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
156 scaler = (GrFontScaler*)auxData; | 109 scaler = (GrFontScaler*)auxData; |
157 } | 110 } |
158 if (NULL == scaler) { | 111 if (NULL == scaler) { |
159 scaler = SkNEW_ARGS(GrFontScaler, (cache)); | 112 scaler = SkNEW_ARGS(GrFontScaler, (cache)); |
160 cache->setAuxProc(GlyphCacheAuxProc, scaler); | 113 cache->setAuxProc(GlyphCacheAuxProc, scaler); |
161 } | 114 } |
162 | 115 |
163 return scaler; | 116 return scaler; |
164 } | 117 } |
OLD | NEW |