blob: 1edacfc06e77e07b1d0e267c846493ec28dba6df [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"
robertphillipsccb1b572015-05-27 11:02:55 -07009#include "GrBlurUtils.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000010#include "GrContext.h"
robertphillipsccb1b572015-05-27 11:02:55 -070011#include "GrDrawContext.h"
joshualitt570d2f82015-02-25 13:19:48 -080012#include "GrFontScaler.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000013
14#include "SkAutoKern.h"
joshualitt9c328182015-03-23 08:13:04 -070015#include "SkDrawFilter.h"
joshualitt6e8cd962015-03-20 10:30:14 -070016#include "SkDrawProcs.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000017#include "SkGlyphCache.h"
joshualitt6e8cd962015-03-20 10:30:14 -070018#include "SkGpuDevice.h"
joshualitt9c328182015-03-23 08:13:04 -070019#include "SkTextBlob.h"
joshualitt6e8cd962015-03-20 10:30:14 -070020#include "SkTextMapStateProc.h"
21#include "SkTextToPathIter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
robertphillipsccb1b572015-05-27 11:02:55 -070023GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& properties)
joshualitt6e8cd962015-03-20 10:30:14 -070024 : fFallbackTextContext(NULL)
25 , fContext(context)
robertphillipsea461502015-05-26 11:38:03 -070026 , fDeviceProperties(properties) {
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000027}
tomhudson@google.com375ff852012-06-29 18:37:57 +000028
jvanverth8c27a182014-10-14 08:45:50 -070029GrTextContext::~GrTextContext() {
30 SkDELETE(fFallbackTextContext);
31}
32
joshualitt570d2f82015-02-25 13:19:48 -080033void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint,
joshualitt6e8cd962015-03-20 10:30:14 -070034 const SkPaint& skPaint, const SkIRect& regionClipBounds) {
joshualitt570d2f82015-02-25 13:19:48 -080035 fClip = clip;
robertphillips@google.combeb1af72012-07-26 18:52:16 +000036
joshualitt25d9c152015-02-18 12:29:52 -080037 fRenderTarget.reset(SkRef(rt));
38
joshualitt6e8cd962015-03-20 10:30:14 -070039 fRegionClipBounds = regionClipBounds;
joshualitt570d2f82015-02-25 13:19:48 -080040 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000041
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000042 fPaint = grPaint;
43 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000044}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000045
joshualitt6e8cd962015-03-20 10:30:14 -070046void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080047 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070048 const char text[], size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070049 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
robertphillipsea461502015-05-26 11:38:03 -070050 if (fContext->abandoned()) {
joshualitt6e8cd962015-03-20 10:30:14 -070051 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080052 }
jvanverth8c27a182014-10-14 08:45:50 -070053
robertphillipsccb1b572015-05-27 11:02:55 -070054 GrDrawContext* drawContext = fContext->drawContext();
55 if (!drawContext) {
56 return;
57 }
58
jvanverthaab626c2014-10-16 08:04:39 -070059 GrTextContext* textContext = this;
60 do {
cdaltone68f7362015-03-25 14:02:37 -070061 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
robertphillipsccb1b572015-05-27 11:02:55 -070062 textContext->onDrawText(drawContext, rt, clip, paint, skPaint, viewMatrix,
63 text, byteLength, x, y, clipBounds);
joshualitt6e8cd962015-03-20 10:30:14 -070064 return;
jvanverthaab626c2014-10-16 08:04:39 -070065 }
66 textContext = textContext->fFallbackTextContext;
67 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070068
joshualitt6e8cd962015-03-20 10:30:14 -070069 // fall back to drawing as a path
robertphillipsccb1b572015-05-27 11:02:55 -070070 this->drawTextAsPath(drawContext, rt, clip, skPaint, viewMatrix,
71 text, byteLength, x, y, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070072}
73
joshualitt6e8cd962015-03-20 10:30:14 -070074void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080075 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070076 const char text[], size_t byteLength,
77 const SkScalar pos[], int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -070078 const SkPoint& offset, const SkIRect& clipBounds) {
robertphillipsea461502015-05-26 11:38:03 -070079 if (fContext->abandoned()) {
joshualitt6e8cd962015-03-20 10:30:14 -070080 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080081 }
jvanverth8c27a182014-10-14 08:45:50 -070082
robertphillipsccb1b572015-05-27 11:02:55 -070083 GrDrawContext* drawContext = fContext->drawContext();
84 if (!drawContext) {
85 return;
86 }
87
jvanverth8c27a182014-10-14 08:45:50 -070088 GrTextContext* textContext = this;
89 do {
cdaltone68f7362015-03-25 14:02:37 -070090 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
robertphillipsccb1b572015-05-27 11:02:55 -070091 textContext->onDrawPosText(drawContext, rt, clip, paint, skPaint, viewMatrix,
92 text, byteLength, pos,
joshualitt6e8cd962015-03-20 10:30:14 -070093 scalarsPerPosition, offset, clipBounds);
94 return;
jvanverth8c27a182014-10-14 08:45:50 -070095 }
96 textContext = textContext->fFallbackTextContext;
97 } while (textContext);
98
joshualitt6e8cd962015-03-20 10:30:14 -070099 // fall back to drawing as a path
robertphillipsccb1b572015-05-27 11:02:55 -0700100 this->drawPosTextAsPath(drawContext, rt, clip, skPaint, viewMatrix, text, byteLength, pos,
101 scalarsPerPosition, offset, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -0700102}
103
robertphillipsccb1b572015-05-27 11:02:55 -0700104void GrTextContext::drawTextBlob(SkGpuDevice* gpuDevice, GrRenderTarget* rt,
105 const GrClip& clip, const SkPaint& skPaint,
joshualitt9c328182015-03-23 08:13:04 -0700106 const SkMatrix& viewMatrix, const SkTextBlob* blob,
107 SkScalar x, SkScalar y,
108 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -0700109 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700110
cdaltone68f7362015-03-25 14:02:37 -0700111 SkTextBlob::RunIterator it(blob);
112 for (;!it.done(); it.next()) {
113 size_t textLen = it.glyphCount() * sizeof(uint16_t);
114 const SkPoint& offset = it.offset();
115 // applyFontToPaint() always overwrites the exact same attributes,
116 // so it is safe to not re-seed the paint for this reason.
117 it.applyFontToPaint(&runPaint);
118
119 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
120 // A false return from filter() means we should abort the current draw.
121 runPaint = skPaint;
122 continue;
joshualitt9c328182015-03-23 08:13:04 -0700123 }
cdaltone68f7362015-03-25 14:02:37 -0700124
robertphillipsccb1b572015-05-27 11:02:55 -0700125 runPaint.setFlags(gpuDevice->filterTextFlags(runPaint));
cdaltone68f7362015-03-25 14:02:37 -0700126
127 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700128 if (!SkPaint2GrPaint(fContext, fRenderTarget, runPaint, viewMatrix, true, &grPaint)) {
129 return;
130 }
cdaltone68f7362015-03-25 14:02:37 -0700131
132 switch (it.positioning()) {
133 case SkTextBlob::kDefault_Positioning:
134 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
135 textLen, x + offset.x(), y + offset.y(), clipBounds);
136 break;
137 case SkTextBlob::kHorizontal_Positioning:
138 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
139 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
140 break;
141 case SkTextBlob::kFull_Positioning:
142 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
143 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
144 break;
145 default:
146 SkFAIL("unhandled positioning mode");
147 }
148
149 if (drawFilter) {
150 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
151 runPaint = skPaint;
152 }
153 }
joshualitt9c328182015-03-23 08:13:04 -0700154}
155
robertphillipsccb1b572015-05-27 11:02:55 -0700156void GrTextContext::drawTextAsPath(GrDrawContext* drawContext, GrRenderTarget* rt,
157 const GrClip& clip,
158 const SkPaint& skPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700159 const char text[], size_t byteLength, SkScalar x, SkScalar y,
160 const SkIRect& clipBounds) {
161 SkTextToPathIter iter(text, byteLength, skPaint, true);
162
163 SkMatrix matrix;
164 matrix.setScale(iter.getPathScale(), iter.getPathScale());
165 matrix.postTranslate(x, y);
166
167 const SkPath* iterPath;
168 SkScalar xpos, prevXPos = 0;
169
170 while (iter.next(&iterPath, &xpos)) {
171 matrix.postTranslate(xpos - prevXPos, 0);
172 if (iterPath) {
173 const SkPaint& pnt = iter.getPaint();
robertphillipsccb1b572015-05-27 11:02:55 -0700174 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, clip, *iterPath,
175 pnt, viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700176 }
177 prevXPos = xpos;
178 }
179}
180
robertphillipsccb1b572015-05-27 11:02:55 -0700181void GrTextContext::drawPosTextAsPath(GrDrawContext* drawContext, GrRenderTarget* rt,
182 const GrClip& clip,
183 const SkPaint& origPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700184 const char text[], size_t byteLength,
185 const SkScalar pos[], int scalarsPerPosition,
186 const SkPoint& offset, const SkIRect& clipBounds) {
187 // setup our std paint, in hopes of getting hits in the cache
188 SkPaint paint(origPaint);
189 SkScalar matrixScale = paint.setupForAsPaths();
190
191 SkMatrix matrix;
192 matrix.setScale(matrixScale, matrixScale);
193
194 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
195 paint.setStyle(SkPaint::kFill_Style);
196 paint.setPathEffect(NULL);
197
198 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
199 SkAutoGlyphCache autoCache(paint, NULL, NULL);
200 SkGlyphCache* cache = autoCache.getCache();
201
202 const char* stop = text + byteLength;
203 SkTextAlignProc alignProc(paint.getTextAlign());
204 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
205
206 // Now restore the original settings, so we "draw" with whatever style/stroking.
207 paint.setStyle(origPaint.getStyle());
208 paint.setPathEffect(origPaint.getPathEffect());
209
210 while (text < stop) {
211 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
212 if (glyph.fWidth) {
213 const SkPath* path = cache->findPath(glyph);
214 if (path) {
215 SkPoint tmsLoc;
216 tmsProc(pos, &tmsLoc);
217 SkPoint loc;
218 alignProc(tmsLoc, glyph, &loc);
219
220 matrix[SkMatrix::kMTransX] = loc.fX;
221 matrix[SkMatrix::kMTransY] = loc.fY;
robertphillipsccb1b572015-05-27 11:02:55 -0700222 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, clip, *path, paint,
223 viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700224 }
225 }
226 pos += scalarsPerPosition;
227 }
228}
jvanverth8c27a182014-10-14 08:45:50 -0700229
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000230//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700231int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000232 const char text[], size_t byteLength, SkVector* stopVector) {
233 SkFixed x = 0, y = 0;
234 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000235
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000236 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000237
jvanverth73f10532014-10-23 11:57:12 -0700238 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000239 while (text < stop) {
240 // don't need x, y here, since all subpixel variants will have the
241 // same advance
242 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000243
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000244 x += autokern.adjust(glyph) + glyph.fAdvanceX;
245 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700246 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000247 }
248 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000249
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000250 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700251
252 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000253}
254
255static void GlyphCacheAuxProc(void* data) {
256 GrFontScaler* scaler = (GrFontScaler*)data;
257 SkSafeUnref(scaler);
258}
259
260GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
261 void* auxData;
262 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000263
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000264 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
265 scaler = (GrFontScaler*)auxData;
266 }
267 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700268 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000269 cache->setAuxProc(GlyphCacheAuxProc, scaler);
270 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000271
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000272 return scaler;
273}