blob: d4c01719b7da20817b02885db2ac031dcb1060b3 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com375ff852012-06-29 18:37:57 +00008#include "GrTextContext.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00009#include "GrContext.h"
joshualitt570d2f82015-02-25 13:19:48 -080010#include "GrDrawTarget.h"
11#include "GrFontScaler.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000012
13#include "SkAutoKern.h"
joshualitt9c328182015-03-23 08:13:04 -070014#include "SkDrawFilter.h"
joshualitt6e8cd962015-03-20 10:30:14 -070015#include "SkDrawProcs.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000016#include "SkGlyphCache.h"
joshualitt6e8cd962015-03-20 10:30:14 -070017#include "SkGpuDevice.h"
joshualitt9c328182015-03-23 08:13:04 -070018#include "SkTextBlob.h"
joshualitt6e8cd962015-03-20 10:30:14 -070019#include "SkTextMapStateProc.h"
20#include "SkTextToPathIter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
joshualitt6e8cd962015-03-20 10:30:14 -070022GrTextContext::GrTextContext(GrContext* context, SkGpuDevice* gpuDevice,
23 const SkDeviceProperties& properties)
24 : fFallbackTextContext(NULL)
25 , fContext(context)
26 , fGpuDevice(gpuDevice)
27 , fDeviceProperties(properties)
28 , fDrawTarget(NULL) {
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000029}
tomhudson@google.com375ff852012-06-29 18:37:57 +000030
jvanverth8c27a182014-10-14 08:45:50 -070031GrTextContext::~GrTextContext() {
32 SkDELETE(fFallbackTextContext);
33}
34
joshualitt570d2f82015-02-25 13:19:48 -080035void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint,
joshualitt6e8cd962015-03-20 10:30:14 -070036 const SkPaint& skPaint, const SkIRect& regionClipBounds) {
joshualitt570d2f82015-02-25 13:19:48 -080037 fClip = clip;
robertphillips@google.combeb1af72012-07-26 18:52:16 +000038
joshualitt25d9c152015-02-18 12:29:52 -080039 fRenderTarget.reset(SkRef(rt));
40
joshualitt6e8cd962015-03-20 10:30:14 -070041 fRegionClipBounds = regionClipBounds;
joshualitt570d2f82015-02-25 13:19:48 -080042 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000043
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000044 fDrawTarget = fContext->getTextTarget();
45
46 fPaint = grPaint;
47 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000048}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000049
joshualitt6e8cd962015-03-20 10:30:14 -070050void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080051 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070052 const char text[], size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070053 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
joshualitt5f5a8d72015-02-25 14:09:45 -080054 if (!fContext->getTextTarget()) {
joshualitt6e8cd962015-03-20 10:30:14 -070055 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080056 }
jvanverth8c27a182014-10-14 08:45:50 -070057
jvanverthaab626c2014-10-16 08:04:39 -070058 GrTextContext* textContext = this;
59 do {
joshualitt5531d512014-12-17 15:50:11 -080060 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt6e8cd962015-03-20 10:30:14 -070061 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y,
62 clipBounds);
63 return;
jvanverthaab626c2014-10-16 08:04:39 -070064 }
65 textContext = textContext->fFallbackTextContext;
66 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070067
joshualitt6e8cd962015-03-20 10:30:14 -070068 // fall back to drawing as a path
69 this->drawTextAsPath(skPaint, viewMatrix, text, byteLength, x, y, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070070}
71
joshualitt6e8cd962015-03-20 10:30:14 -070072void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080073 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070074 const char text[], size_t byteLength,
75 const SkScalar pos[], int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -070076 const SkPoint& offset, const SkIRect& clipBounds) {
joshualitt5f5a8d72015-02-25 14:09:45 -080077 if (!fContext->getTextTarget()) {
joshualitt6e8cd962015-03-20 10:30:14 -070078 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080079 }
jvanverth8c27a182014-10-14 08:45:50 -070080
81 GrTextContext* textContext = this;
82 do {
joshualitt5531d512014-12-17 15:50:11 -080083 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt570d2f82015-02-25 13:19:48 -080084 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, pos,
joshualitt6e8cd962015-03-20 10:30:14 -070085 scalarsPerPosition, offset, clipBounds);
86 return;
jvanverth8c27a182014-10-14 08:45:50 -070087 }
88 textContext = textContext->fFallbackTextContext;
89 } while (textContext);
90
joshualitt6e8cd962015-03-20 10:30:14 -070091 // fall back to drawing as a path
92 this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsPerPosition, offset,
93 clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070094}
95
joshualitt9c328182015-03-23 08:13:04 -070096void GrTextContext::drawTextBlob(GrRenderTarget* rt, const GrClip& clip, const SkPaint& skPaint,
97 const SkMatrix& viewMatrix, const SkTextBlob* blob,
98 SkScalar x, SkScalar y,
99 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
100 if (!fContext->getTextTarget()) {
101 return;
102 }
103
104 GrTextContext* textContext = this;
105 do {
106 if (textContext->canDraw(skPaint, viewMatrix)) {
107 textContext->onDrawTextBlob(rt, clip, skPaint, viewMatrix, blob, x, y, drawFilter,
108 clipBounds);
109 return;
110 }
111 textContext = textContext->fFallbackTextContext;
112 } while (textContext);
113}
114
joshualitt6e8cd962015-03-20 10:30:14 -0700115void GrTextContext::drawTextAsPath(const SkPaint& skPaint, const SkMatrix& viewMatrix,
116 const char text[], size_t byteLength, SkScalar x, SkScalar y,
117 const SkIRect& clipBounds) {
118 SkTextToPathIter iter(text, byteLength, skPaint, true);
119
120 SkMatrix matrix;
121 matrix.setScale(iter.getPathScale(), iter.getPathScale());
122 matrix.postTranslate(x, y);
123
124 const SkPath* iterPath;
125 SkScalar xpos, prevXPos = 0;
126
127 while (iter.next(&iterPath, &xpos)) {
128 matrix.postTranslate(xpos - prevXPos, 0);
129 if (iterPath) {
130 const SkPaint& pnt = iter.getPaint();
131 fGpuDevice->internalDrawPath(*iterPath, pnt, viewMatrix, &matrix, clipBounds, false);
132 }
133 prevXPos = xpos;
134 }
135}
136
137void GrTextContext::drawPosTextAsPath(const SkPaint& origPaint, const SkMatrix& viewMatrix,
138 const char text[], size_t byteLength,
139 const SkScalar pos[], int scalarsPerPosition,
140 const SkPoint& offset, const SkIRect& clipBounds) {
141 // setup our std paint, in hopes of getting hits in the cache
142 SkPaint paint(origPaint);
143 SkScalar matrixScale = paint.setupForAsPaths();
144
145 SkMatrix matrix;
146 matrix.setScale(matrixScale, matrixScale);
147
148 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
149 paint.setStyle(SkPaint::kFill_Style);
150 paint.setPathEffect(NULL);
151
152 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
153 SkAutoGlyphCache autoCache(paint, NULL, NULL);
154 SkGlyphCache* cache = autoCache.getCache();
155
156 const char* stop = text + byteLength;
157 SkTextAlignProc alignProc(paint.getTextAlign());
158 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
159
160 // Now restore the original settings, so we "draw" with whatever style/stroking.
161 paint.setStyle(origPaint.getStyle());
162 paint.setPathEffect(origPaint.getPathEffect());
163
164 while (text < stop) {
165 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
166 if (glyph.fWidth) {
167 const SkPath* path = cache->findPath(glyph);
168 if (path) {
169 SkPoint tmsLoc;
170 tmsProc(pos, &tmsLoc);
171 SkPoint loc;
172 alignProc(tmsLoc, glyph, &loc);
173
174 matrix[SkMatrix::kMTransX] = loc.fX;
175 matrix[SkMatrix::kMTransY] = loc.fY;
176 fGpuDevice->internalDrawPath(*path, paint, viewMatrix, &matrix, clipBounds, false);
177 }
178 }
179 pos += scalarsPerPosition;
180 }
181}
jvanverth8c27a182014-10-14 08:45:50 -0700182
joshualitt9c328182015-03-23 08:13:04 -0700183void GrTextContext::onDrawTextBlob(GrRenderTarget* rt, const GrClip& clip,
184 const SkPaint& skPaint, const SkMatrix& viewMatrix,
185 const SkTextBlob* blob, SkScalar x, SkScalar y,
186 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
187 SkPaint runPaint = skPaint;
188
189 SkTextBlob::RunIterator it(blob);
190 for (;!it.done(); it.next()) {
191 size_t textLen = it.glyphCount() * sizeof(uint16_t);
192 const SkPoint& offset = it.offset();
193 // applyFontToPaint() always overwrites the exact same attributes,
194 // so it is safe to not re-seed the paint for this reason.
195 it.applyFontToPaint(&runPaint);
196
197 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
198 // A false return from filter() means we should abort the current draw.
199 runPaint = skPaint;
200 continue;
201 }
202
203 runPaint.setFlags(fGpuDevice->filterTextFlags(runPaint));
204
205 GrPaint grPaint;
206 SkPaint2GrPaintShader(fContext, fRenderTarget, runPaint, viewMatrix, true, &grPaint);
207
208 switch (it.positioning()) {
209 case SkTextBlob::kDefault_Positioning:
210 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
211 textLen, x + offset.x(), y + offset.y(), clipBounds);
212 break;
213 case SkTextBlob::kHorizontal_Positioning:
214 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
215 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
216 break;
217 case SkTextBlob::kFull_Positioning:
218 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
219 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
220 break;
221 default:
222 SkFAIL("unhandled positioning mode");
223 }
224
225 if (drawFilter) {
226 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
227 runPaint = skPaint;
228 }
229 }
230}
231
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000232//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700233int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000234 const char text[], size_t byteLength, SkVector* stopVector) {
235 SkFixed x = 0, y = 0;
236 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000237
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000238 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000239
jvanverth73f10532014-10-23 11:57:12 -0700240 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000241 while (text < stop) {
242 // don't need x, y here, since all subpixel variants will have the
243 // same advance
244 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000245
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000246 x += autokern.adjust(glyph) + glyph.fAdvanceX;
247 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700248 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000249 }
250 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000251
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000252 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700253
254 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000255}
256
257static void GlyphCacheAuxProc(void* data) {
258 GrFontScaler* scaler = (GrFontScaler*)data;
259 SkSafeUnref(scaler);
260}
261
262GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
263 void* auxData;
264 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000265
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000266 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
267 scaler = (GrFontScaler*)auxData;
268 }
269 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700270 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000271 cache->setAuxProc(GlyphCacheAuxProc, scaler);
272 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000273
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000274 return scaler;
275}