blob: 92a851bc5a59765542ce1a770671d37844f81657 [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) {
82 if (paint.getShader() ||
83 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
84 paint.getMaskFilter() ||
85 paint.getRasterizer() ||
86 paint.getColorFilter() ||
87 paint.getPathEffect() ||
88 paint.isFakeBoldText() ||
89 paint.getStyle() != SkPaint::kFill_Style)
90 {
91 return true;
92 }
93 return false;
94}
95
robertphillipsfcf78292015-06-19 11:49:52 -070096uint32_t GrTextContext::FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint) {
robertphillips9c240a12015-05-28 07:45:59 -070097 uint32_t flags = paint.getFlags();
98
99 if (!paint.isLCDRenderText() || !paint.isAntiAlias()) {
100 return flags;
101 }
102
robertphillipsfcf78292015-06-19 11:49:52 -0700103 if (kUnknown_SkPixelGeometry == surfaceProps.pixelGeometry() || ShouldDisableLCD(paint)) {
robertphillips9c240a12015-05-28 07:45:59 -0700104 flags &= ~SkPaint::kLCDRenderText_Flag;
105 flags |= SkPaint::kGenA8FromLCD_Flag;
106 }
107
108 return flags;
109}
110
robertphillipsf6703fa2015-09-01 05:36:47 -0700111void GrTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700112 const GrClip& clip, const SkPaint& skPaint,
joshualitt9c328182015-03-23 08:13:04 -0700113 const SkMatrix& viewMatrix, const SkTextBlob* blob,
114 SkScalar x, SkScalar y,
115 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -0700116 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700117
cdaltone68f7362015-03-25 14:02:37 -0700118 SkTextBlob::RunIterator it(blob);
119 for (;!it.done(); it.next()) {
120 size_t textLen = it.glyphCount() * sizeof(uint16_t);
121 const SkPoint& offset = it.offset();
122 // applyFontToPaint() always overwrites the exact same attributes,
123 // so it is safe to not re-seed the paint for this reason.
124 it.applyFontToPaint(&runPaint);
125
126 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
127 // A false return from filter() means we should abort the current draw.
128 runPaint = skPaint;
129 continue;
joshualitt9c328182015-03-23 08:13:04 -0700130 }
cdaltone68f7362015-03-25 14:02:37 -0700131
robertphillipsfcf78292015-06-19 11:49:52 -0700132 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
cdaltone68f7362015-03-25 14:02:37 -0700133
134 GrPaint grPaint;
joshualitt9df46592015-07-09 10:55:28 -0700135 if (!SkPaint2GrPaint(fContext, rt, runPaint, viewMatrix, true, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700136 return;
137 }
cdaltone68f7362015-03-25 14:02:37 -0700138
139 switch (it.positioning()) {
140 case SkTextBlob::kDefault_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700141 this->drawText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700142 textLen, x + offset.x(), y + offset.y(), clipBounds);
143 break;
144 case SkTextBlob::kHorizontal_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700145 this->drawPosText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700146 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
147 break;
148 case SkTextBlob::kFull_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700149 this->drawPosText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700150 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
151 break;
152 default:
153 SkFAIL("unhandled positioning mode");
154 }
155
156 if (drawFilter) {
157 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
158 runPaint = skPaint;
159 }
160 }
joshualitt9c328182015-03-23 08:13:04 -0700161}
162
robertphillipsf6703fa2015-09-01 05:36:47 -0700163void GrTextContext::drawTextAsPath(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700164 const GrClip& clip,
165 const SkPaint& skPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700166 const char text[], size_t byteLength, SkScalar x, SkScalar y,
167 const SkIRect& clipBounds) {
168 SkTextToPathIter iter(text, byteLength, skPaint, true);
169
170 SkMatrix matrix;
171 matrix.setScale(iter.getPathScale(), iter.getPathScale());
172 matrix.postTranslate(x, y);
173
174 const SkPath* iterPath;
175 SkScalar xpos, prevXPos = 0;
176
177 while (iter.next(&iterPath, &xpos)) {
178 matrix.postTranslate(xpos - prevXPos, 0);
179 if (iterPath) {
180 const SkPaint& pnt = iter.getPaint();
robertphillipsf6703fa2015-09-01 05:36:47 -0700181 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, *iterPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700182 pnt, viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700183 }
184 prevXPos = xpos;
185 }
186}
187
robertphillipsf6703fa2015-09-01 05:36:47 -0700188void GrTextContext::drawPosTextAsPath(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700189 const GrClip& clip,
190 const SkPaint& origPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700191 const char text[], size_t byteLength,
192 const SkScalar pos[], int scalarsPerPosition,
193 const SkPoint& offset, const SkIRect& clipBounds) {
194 // setup our std paint, in hopes of getting hits in the cache
195 SkPaint paint(origPaint);
196 SkScalar matrixScale = paint.setupForAsPaths();
197
198 SkMatrix matrix;
199 matrix.setScale(matrixScale, matrixScale);
200
201 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
202 paint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700203 paint.setPathEffect(nullptr);
joshualitt6e8cd962015-03-20 10:30:14 -0700204
205 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
halcanary96fcdcc2015-08-27 07:41:13 -0700206 SkAutoGlyphCache autoCache(paint, &fSurfaceProps, nullptr);
joshualitt6e8cd962015-03-20 10:30:14 -0700207 SkGlyphCache* cache = autoCache.getCache();
208
209 const char* stop = text + byteLength;
210 SkTextAlignProc alignProc(paint.getTextAlign());
211 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
212
213 // Now restore the original settings, so we "draw" with whatever style/stroking.
214 paint.setStyle(origPaint.getStyle());
215 paint.setPathEffect(origPaint.getPathEffect());
216
217 while (text < stop) {
218 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
219 if (glyph.fWidth) {
220 const SkPath* path = cache->findPath(glyph);
221 if (path) {
222 SkPoint tmsLoc;
223 tmsProc(pos, &tmsLoc);
224 SkPoint loc;
225 alignProc(tmsLoc, glyph, &loc);
226
227 matrix[SkMatrix::kMTransX] = loc.fX;
228 matrix[SkMatrix::kMTransY] = loc.fY;
robertphillipsf6703fa2015-09-01 05:36:47 -0700229 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, *path, paint,
robertphillipsccb1b572015-05-27 11:02:55 -0700230 viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700231 }
232 }
233 pos += scalarsPerPosition;
234 }
235}
jvanverth8c27a182014-10-14 08:45:50 -0700236
halcanary385fe4d2015-08-26 13:07:48 -0700237// *** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700238int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000239 const char text[], size_t byteLength, SkVector* stopVector) {
240 SkFixed x = 0, y = 0;
241 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000242
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000243 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000244
jvanverth73f10532014-10-23 11:57:12 -0700245 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000246 while (text < stop) {
247 // don't need x, y here, since all subpixel variants will have the
248 // same advance
249 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000250
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000251 x += autokern.adjust(glyph) + glyph.fAdvanceX;
252 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700253 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000254 }
255 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000256
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000257 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700258
259 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000260}
261
262static void GlyphCacheAuxProc(void* data) {
263 GrFontScaler* scaler = (GrFontScaler*)data;
264 SkSafeUnref(scaler);
265}
266
267GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
268 void* auxData;
halcanary96fcdcc2015-08-27 07:41:13 -0700269 GrFontScaler* scaler = nullptr;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000270
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000271 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
272 scaler = (GrFontScaler*)auxData;
273 }
halcanary96fcdcc2015-08-27 07:41:13 -0700274 if (nullptr == scaler) {
halcanary385fe4d2015-08-26 13:07:48 -0700275 scaler = new GrFontScaler(cache);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000276 cache->setAuxProc(GlyphCacheAuxProc, scaler);
277 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000278
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000279 return scaler;
280}