blob: 6ab730cdfa4d840047681a46a60a8f10fb634d53 [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
robertphillipsf6703fa2015-09-01 05:36:47 -070023GrTextContext::GrTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
halcanary96fcdcc2015-08-27 07:41:13 -070024 : fFallbackTextContext(nullptr)
joshualitt6e8cd962015-03-20 10:30:14 -070025 , fContext(context)
robertphillipsf6703fa2015-09-01 05:36:47 -070026 , fSurfaceProps(surfaceProps) {
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000027}
tomhudson@google.com375ff852012-06-29 18:37:57 +000028
halcanary385fe4d2015-08-26 13:07:48 -070029GrTextContext::~GrTextContext() { delete fFallbackTextContext; }
jvanverth8c27a182014-10-14 08:45:50 -070030
robertphillipsf6703fa2015-09-01 05:36:47 -070031void GrTextContext::drawText(GrDrawContext* dc, GrRenderTarget* rt,
32 const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080033 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070034 const char text[], size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070035 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
robertphillipsf6703fa2015-09-01 05:36:47 -070036 if (fContext->abandoned()) {
robertphillipsccb1b572015-05-27 11:02:55 -070037 return;
38 }
39
jvanverthaab626c2014-10-16 08:04:39 -070040 GrTextContext* textContext = this;
41 do {
cdaltone68f7362015-03-25 14:02:37 -070042 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
robertphillipsf6703fa2015-09-01 05:36:47 -070043 textContext->onDrawText(dc, rt, clip, paint, skPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -070044 text, byteLength, x, y, clipBounds);
joshualitt6e8cd962015-03-20 10:30:14 -070045 return;
jvanverthaab626c2014-10-16 08:04:39 -070046 }
47 textContext = textContext->fFallbackTextContext;
48 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070049
joshualitt6e8cd962015-03-20 10:30:14 -070050 // fall back to drawing as a path
robertphillipsf6703fa2015-09-01 05:36:47 -070051 this->drawTextAsPath(dc, rt, clip, skPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -070052 text, byteLength, x, y, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070053}
54
robertphillipsf6703fa2015-09-01 05:36:47 -070055void GrTextContext::drawPosText(GrDrawContext* dc, GrRenderTarget* rt,
56 const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080057 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070058 const char text[], size_t byteLength,
59 const SkScalar pos[], int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -070060 const SkPoint& offset, const SkIRect& clipBounds) {
robertphillipsf6703fa2015-09-01 05:36:47 -070061 if (fContext->abandoned()) {
robertphillipsccb1b572015-05-27 11:02:55 -070062 return;
63 }
64
jvanverth8c27a182014-10-14 08:45:50 -070065 GrTextContext* textContext = this;
66 do {
cdaltone68f7362015-03-25 14:02:37 -070067 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
robertphillipsf6703fa2015-09-01 05:36:47 -070068 textContext->onDrawPosText(dc, rt, clip, paint, skPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -070069 text, byteLength, pos,
joshualitt6e8cd962015-03-20 10:30:14 -070070 scalarsPerPosition, offset, clipBounds);
71 return;
jvanverth8c27a182014-10-14 08:45:50 -070072 }
73 textContext = textContext->fFallbackTextContext;
74 } while (textContext);
75
joshualitt6e8cd962015-03-20 10:30:14 -070076 // fall back to drawing as a path
robertphillipsf6703fa2015-09-01 05:36:47 -070077 this->drawPosTextAsPath(dc, rt, clip, skPaint, viewMatrix, text, byteLength, pos,
robertphillipsccb1b572015-05-27 11:02:55 -070078 scalarsPerPosition, offset, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070079}
80
robertphillips9c240a12015-05-28 07:45:59 -070081bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) {
egdaniel27b63352015-09-15 13:13:50 -070082 if (!SkXfermode::AsMode(paint.getXfermode(), nullptr) ||
robertphillips9c240a12015-05-28 07:45:59 -070083 paint.getMaskFilter() ||
84 paint.getRasterizer() ||
robertphillips9c240a12015-05-28 07:45:59 -070085 paint.getPathEffect() ||
86 paint.isFakeBoldText() ||
87 paint.getStyle() != SkPaint::kFill_Style)
88 {
89 return true;
90 }
91 return false;
92}
93
robertphillipsfcf78292015-06-19 11:49:52 -070094uint32_t GrTextContext::FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint) {
robertphillips9c240a12015-05-28 07:45:59 -070095 uint32_t flags = paint.getFlags();
96
97 if (!paint.isLCDRenderText() || !paint.isAntiAlias()) {
98 return flags;
99 }
100
robertphillipsfcf78292015-06-19 11:49:52 -0700101 if (kUnknown_SkPixelGeometry == surfaceProps.pixelGeometry() || ShouldDisableLCD(paint)) {
robertphillips9c240a12015-05-28 07:45:59 -0700102 flags &= ~SkPaint::kLCDRenderText_Flag;
103 flags |= SkPaint::kGenA8FromLCD_Flag;
104 }
105
106 return flags;
107}
108
robertphillipsf6703fa2015-09-01 05:36:47 -0700109void GrTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700110 const GrClip& clip, const SkPaint& skPaint,
joshualitt9c328182015-03-23 08:13:04 -0700111 const SkMatrix& viewMatrix, const SkTextBlob* blob,
112 SkScalar x, SkScalar y,
113 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -0700114 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700115
cdaltone68f7362015-03-25 14:02:37 -0700116 SkTextBlob::RunIterator it(blob);
117 for (;!it.done(); it.next()) {
118 size_t textLen = it.glyphCount() * sizeof(uint16_t);
119 const SkPoint& offset = it.offset();
120 // applyFontToPaint() always overwrites the exact same attributes,
121 // so it is safe to not re-seed the paint for this reason.
122 it.applyFontToPaint(&runPaint);
123
124 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
125 // A false return from filter() means we should abort the current draw.
126 runPaint = skPaint;
127 continue;
joshualitt9c328182015-03-23 08:13:04 -0700128 }
cdaltone68f7362015-03-25 14:02:37 -0700129
robertphillipsfcf78292015-06-19 11:49:52 -0700130 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
cdaltone68f7362015-03-25 14:02:37 -0700131
132 GrPaint grPaint;
joshualitt9df46592015-07-09 10:55:28 -0700133 if (!SkPaint2GrPaint(fContext, rt, runPaint, viewMatrix, true, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700134 return;
135 }
cdaltone68f7362015-03-25 14:02:37 -0700136
137 switch (it.positioning()) {
138 case SkTextBlob::kDefault_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700139 this->drawText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700140 textLen, x + offset.x(), y + offset.y(), clipBounds);
141 break;
142 case SkTextBlob::kHorizontal_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700143 this->drawPosText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700144 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
145 break;
146 case SkTextBlob::kFull_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700147 this->drawPosText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700148 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
149 break;
150 default:
151 SkFAIL("unhandled positioning mode");
152 }
153
154 if (drawFilter) {
155 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
156 runPaint = skPaint;
157 }
158 }
joshualitt9c328182015-03-23 08:13:04 -0700159}
160
robertphillipsf6703fa2015-09-01 05:36:47 -0700161void GrTextContext::drawTextAsPath(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700162 const GrClip& clip,
163 const SkPaint& skPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700164 const char text[], size_t byteLength, SkScalar x, SkScalar y,
165 const SkIRect& clipBounds) {
166 SkTextToPathIter iter(text, byteLength, skPaint, true);
167
168 SkMatrix matrix;
169 matrix.setScale(iter.getPathScale(), iter.getPathScale());
170 matrix.postTranslate(x, y);
171
172 const SkPath* iterPath;
173 SkScalar xpos, prevXPos = 0;
174
175 while (iter.next(&iterPath, &xpos)) {
176 matrix.postTranslate(xpos - prevXPos, 0);
177 if (iterPath) {
178 const SkPaint& pnt = iter.getPaint();
robertphillipsf6703fa2015-09-01 05:36:47 -0700179 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, *iterPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700180 pnt, viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700181 }
182 prevXPos = xpos;
183 }
184}
185
robertphillipsf6703fa2015-09-01 05:36:47 -0700186void GrTextContext::drawPosTextAsPath(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700187 const GrClip& clip,
188 const SkPaint& origPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700189 const char text[], size_t byteLength,
190 const SkScalar pos[], int scalarsPerPosition,
191 const SkPoint& offset, const SkIRect& clipBounds) {
192 // setup our std paint, in hopes of getting hits in the cache
193 SkPaint paint(origPaint);
194 SkScalar matrixScale = paint.setupForAsPaths();
195
196 SkMatrix matrix;
197 matrix.setScale(matrixScale, matrixScale);
198
199 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
200 paint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700201 paint.setPathEffect(nullptr);
joshualitt6e8cd962015-03-20 10:30:14 -0700202
203 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
halcanary96fcdcc2015-08-27 07:41:13 -0700204 SkAutoGlyphCache autoCache(paint, &fSurfaceProps, nullptr);
joshualitt6e8cd962015-03-20 10:30:14 -0700205 SkGlyphCache* cache = autoCache.getCache();
206
207 const char* stop = text + byteLength;
208 SkTextAlignProc alignProc(paint.getTextAlign());
209 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
210
211 // Now restore the original settings, so we "draw" with whatever style/stroking.
212 paint.setStyle(origPaint.getStyle());
213 paint.setPathEffect(origPaint.getPathEffect());
214
215 while (text < stop) {
216 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
217 if (glyph.fWidth) {
218 const SkPath* path = cache->findPath(glyph);
219 if (path) {
220 SkPoint tmsLoc;
221 tmsProc(pos, &tmsLoc);
222 SkPoint loc;
223 alignProc(tmsLoc, glyph, &loc);
224
225 matrix[SkMatrix::kMTransX] = loc.fX;
226 matrix[SkMatrix::kMTransY] = loc.fY;
robertphillipsf6703fa2015-09-01 05:36:47 -0700227 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, *path, paint,
robertphillipsccb1b572015-05-27 11:02:55 -0700228 viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700229 }
230 }
231 pos += scalarsPerPosition;
232 }
233}
jvanverth8c27a182014-10-14 08:45:50 -0700234
halcanary385fe4d2015-08-26 13:07:48 -0700235// *** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700236int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000237 const char text[], size_t byteLength, SkVector* stopVector) {
238 SkFixed x = 0, y = 0;
239 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000240
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000241 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000242
jvanverth73f10532014-10-23 11:57:12 -0700243 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000244 while (text < stop) {
245 // don't need x, y here, since all subpixel variants will have the
246 // same advance
247 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000248
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000249 x += autokern.adjust(glyph) + glyph.fAdvanceX;
250 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700251 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000252 }
253 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000254
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000255 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700256
257 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000258}
259
260static void GlyphCacheAuxProc(void* data) {
261 GrFontScaler* scaler = (GrFontScaler*)data;
262 SkSafeUnref(scaler);
263}
264
265GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
266 void* auxData;
halcanary96fcdcc2015-08-27 07:41:13 -0700267 GrFontScaler* scaler = nullptr;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000268
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000269 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
270 scaler = (GrFontScaler*)auxData;
271 }
halcanary96fcdcc2015-08-27 07:41:13 -0700272 if (nullptr == scaler) {
halcanary385fe4d2015-08-26 13:07:48 -0700273 scaler = new GrFontScaler(cache);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000274 cache->setAuxProc(GlyphCacheAuxProc, scaler);
275 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000276
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000277 return scaler;
278}