blob: 91c8e851783b64d7d0e41f43bc7d562932a393c7 [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
robertphillips9c240a12015-05-28 07:45:59 -0700104bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) {
105 if (paint.getShader() ||
106 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
107 paint.getMaskFilter() ||
108 paint.getRasterizer() ||
109 paint.getColorFilter() ||
110 paint.getPathEffect() ||
111 paint.isFakeBoldText() ||
112 paint.getStyle() != SkPaint::kFill_Style)
113 {
114 return true;
115 }
116 return false;
117}
118
119uint32_t GrTextContext::FilterTextFlags(const SkDeviceProperties& devProps, const SkPaint& paint) {
120 uint32_t flags = paint.getFlags();
121
122 if (!paint.isLCDRenderText() || !paint.isAntiAlias()) {
123 return flags;
124 }
125
126 if (kUnknown_SkPixelGeometry == devProps.pixelGeometry() || ShouldDisableLCD(paint)) {
127 flags &= ~SkPaint::kLCDRenderText_Flag;
128 flags |= SkPaint::kGenA8FromLCD_Flag;
129 }
130
131 return flags;
132}
133
134void GrTextContext::drawTextBlob(GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700135 const GrClip& clip, const SkPaint& skPaint,
joshualitt9c328182015-03-23 08:13:04 -0700136 const SkMatrix& viewMatrix, const SkTextBlob* blob,
137 SkScalar x, SkScalar y,
138 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -0700139 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700140
cdaltone68f7362015-03-25 14:02:37 -0700141 SkTextBlob::RunIterator it(blob);
142 for (;!it.done(); it.next()) {
143 size_t textLen = it.glyphCount() * sizeof(uint16_t);
144 const SkPoint& offset = it.offset();
145 // applyFontToPaint() always overwrites the exact same attributes,
146 // so it is safe to not re-seed the paint for this reason.
147 it.applyFontToPaint(&runPaint);
148
149 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
150 // A false return from filter() means we should abort the current draw.
151 runPaint = skPaint;
152 continue;
joshualitt9c328182015-03-23 08:13:04 -0700153 }
cdaltone68f7362015-03-25 14:02:37 -0700154
robertphillips9c240a12015-05-28 07:45:59 -0700155 runPaint.setFlags(FilterTextFlags(fDeviceProperties, runPaint));
cdaltone68f7362015-03-25 14:02:37 -0700156
157 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700158 if (!SkPaint2GrPaint(fContext, fRenderTarget, runPaint, viewMatrix, true, &grPaint)) {
159 return;
160 }
cdaltone68f7362015-03-25 14:02:37 -0700161
162 switch (it.positioning()) {
163 case SkTextBlob::kDefault_Positioning:
164 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
165 textLen, x + offset.x(), y + offset.y(), clipBounds);
166 break;
167 case SkTextBlob::kHorizontal_Positioning:
168 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
169 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
170 break;
171 case SkTextBlob::kFull_Positioning:
172 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
173 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
174 break;
175 default:
176 SkFAIL("unhandled positioning mode");
177 }
178
179 if (drawFilter) {
180 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
181 runPaint = skPaint;
182 }
183 }
joshualitt9c328182015-03-23 08:13:04 -0700184}
185
robertphillipsccb1b572015-05-27 11:02:55 -0700186void GrTextContext::drawTextAsPath(GrDrawContext* drawContext, GrRenderTarget* rt,
187 const GrClip& clip,
188 const SkPaint& skPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700189 const char text[], size_t byteLength, SkScalar x, SkScalar y,
190 const SkIRect& clipBounds) {
191 SkTextToPathIter iter(text, byteLength, skPaint, true);
192
193 SkMatrix matrix;
194 matrix.setScale(iter.getPathScale(), iter.getPathScale());
195 matrix.postTranslate(x, y);
196
197 const SkPath* iterPath;
198 SkScalar xpos, prevXPos = 0;
199
200 while (iter.next(&iterPath, &xpos)) {
201 matrix.postTranslate(xpos - prevXPos, 0);
202 if (iterPath) {
203 const SkPaint& pnt = iter.getPaint();
robertphillipsccb1b572015-05-27 11:02:55 -0700204 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, clip, *iterPath,
205 pnt, viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700206 }
207 prevXPos = xpos;
208 }
209}
210
robertphillipsccb1b572015-05-27 11:02:55 -0700211void GrTextContext::drawPosTextAsPath(GrDrawContext* drawContext, GrRenderTarget* rt,
212 const GrClip& clip,
213 const SkPaint& origPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700214 const char text[], size_t byteLength,
215 const SkScalar pos[], int scalarsPerPosition,
216 const SkPoint& offset, const SkIRect& clipBounds) {
217 // setup our std paint, in hopes of getting hits in the cache
218 SkPaint paint(origPaint);
219 SkScalar matrixScale = paint.setupForAsPaths();
220
221 SkMatrix matrix;
222 matrix.setScale(matrixScale, matrixScale);
223
224 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
225 paint.setStyle(SkPaint::kFill_Style);
226 paint.setPathEffect(NULL);
227
228 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
229 SkAutoGlyphCache autoCache(paint, NULL, NULL);
230 SkGlyphCache* cache = autoCache.getCache();
231
232 const char* stop = text + byteLength;
233 SkTextAlignProc alignProc(paint.getTextAlign());
234 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
235
236 // Now restore the original settings, so we "draw" with whatever style/stroking.
237 paint.setStyle(origPaint.getStyle());
238 paint.setPathEffect(origPaint.getPathEffect());
239
240 while (text < stop) {
241 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
242 if (glyph.fWidth) {
243 const SkPath* path = cache->findPath(glyph);
244 if (path) {
245 SkPoint tmsLoc;
246 tmsProc(pos, &tmsLoc);
247 SkPoint loc;
248 alignProc(tmsLoc, glyph, &loc);
249
250 matrix[SkMatrix::kMTransX] = loc.fX;
251 matrix[SkMatrix::kMTransY] = loc.fY;
robertphillipsccb1b572015-05-27 11:02:55 -0700252 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, clip, *path, paint,
253 viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700254 }
255 }
256 pos += scalarsPerPosition;
257 }
258}
jvanverth8c27a182014-10-14 08:45:50 -0700259
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000260//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700261int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000262 const char text[], size_t byteLength, SkVector* stopVector) {
263 SkFixed x = 0, y = 0;
264 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000265
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000266 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000267
jvanverth73f10532014-10-23 11:57:12 -0700268 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000269 while (text < stop) {
270 // don't need x, y here, since all subpixel variants will have the
271 // same advance
272 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000273
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000274 x += autokern.adjust(glyph) + glyph.fAdvanceX;
275 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700276 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000277 }
278 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000279
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000280 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700281
282 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000283}
284
285static void GlyphCacheAuxProc(void* data) {
286 GrFontScaler* scaler = (GrFontScaler*)data;
287 SkSafeUnref(scaler);
288}
289
290GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
291 void* auxData;
292 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000293
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000294 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
295 scaler = (GrFontScaler*)auxData;
296 }
297 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700298 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000299 cache->setAuxProc(GlyphCacheAuxProc, scaler);
300 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000301
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000302 return scaler;
303}