blob: 5cfcf64c1abe184f9534d9d2a45f8246ca0f5841 [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"
bsalomonf1b7a1d2015-09-28 06:26:28 -070019#include "SkGrPriv.h"
halcanary33779752015-10-27 14:01:05 -070020#include "SkTextBlobRunIterator.h"
joshualitt6e8cd962015-03-20 10:30:14 -070021#include "SkTextMapStateProc.h"
22#include "SkTextToPathIter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
robertphillipsf6703fa2015-09-01 05:36:47 -070024GrTextContext::GrTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
halcanary96fcdcc2015-08-27 07:41:13 -070025 : fFallbackTextContext(nullptr)
joshualitt6e8cd962015-03-20 10:30:14 -070026 , fContext(context)
robertphillipsf6703fa2015-09-01 05:36:47 -070027 , fSurfaceProps(surfaceProps) {
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000028}
tomhudson@google.com375ff852012-06-29 18:37:57 +000029
robertphillips2d70dcb2015-10-06 07:38:23 -070030GrTextContext::~GrTextContext() {
31 delete fFallbackTextContext;
32}
jvanverth8c27a182014-10-14 08:45:50 -070033
robertphillipsf6703fa2015-09-01 05:36:47 -070034void GrTextContext::drawText(GrDrawContext* dc, GrRenderTarget* rt,
35 const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080036 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070037 const char text[], size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070038 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
robertphillipsf6703fa2015-09-01 05:36:47 -070039 if (fContext->abandoned()) {
robertphillipsccb1b572015-05-27 11:02:55 -070040 return;
41 }
42
jvanverthaab626c2014-10-16 08:04:39 -070043 GrTextContext* textContext = this;
44 do {
cdaltone68f7362015-03-25 14:02:37 -070045 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
robertphillipsf6703fa2015-09-01 05:36:47 -070046 textContext->onDrawText(dc, rt, clip, paint, skPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -070047 text, byteLength, x, y, clipBounds);
joshualitt6e8cd962015-03-20 10:30:14 -070048 return;
jvanverthaab626c2014-10-16 08:04:39 -070049 }
50 textContext = textContext->fFallbackTextContext;
51 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070052
joshualitt6e8cd962015-03-20 10:30:14 -070053 // fall back to drawing as a path
robertphillips7bceedc2015-12-01 12:51:26 -080054 this->drawTextAsPath(dc, clip, skPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -070055 text, byteLength, x, y, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070056}
57
robertphillipsf6703fa2015-09-01 05:36:47 -070058void GrTextContext::drawPosText(GrDrawContext* dc, GrRenderTarget* rt,
59 const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080060 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070061 const char text[], size_t byteLength,
62 const SkScalar pos[], int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -070063 const SkPoint& offset, const SkIRect& clipBounds) {
robertphillipsf6703fa2015-09-01 05:36:47 -070064 if (fContext->abandoned()) {
robertphillipsccb1b572015-05-27 11:02:55 -070065 return;
66 }
67
jvanverth8c27a182014-10-14 08:45:50 -070068 GrTextContext* textContext = this;
69 do {
cdaltone68f7362015-03-25 14:02:37 -070070 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
robertphillipsf6703fa2015-09-01 05:36:47 -070071 textContext->onDrawPosText(dc, rt, clip, paint, skPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -070072 text, byteLength, pos,
joshualitt6e8cd962015-03-20 10:30:14 -070073 scalarsPerPosition, offset, clipBounds);
74 return;
jvanverth8c27a182014-10-14 08:45:50 -070075 }
76 textContext = textContext->fFallbackTextContext;
77 } while (textContext);
78
joshualitt6e8cd962015-03-20 10:30:14 -070079 // fall back to drawing as a path
robertphillips7bceedc2015-12-01 12:51:26 -080080 this->drawPosTextAsPath(dc, clip, skPaint, viewMatrix, text, byteLength, pos,
robertphillipsccb1b572015-05-27 11:02:55 -070081 scalarsPerPosition, offset, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070082}
83
robertphillips9c240a12015-05-28 07:45:59 -070084bool GrTextContext::ShouldDisableLCD(const SkPaint& paint) {
egdaniel27b63352015-09-15 13:13:50 -070085 if (!SkXfermode::AsMode(paint.getXfermode(), nullptr) ||
robertphillips9c240a12015-05-28 07:45:59 -070086 paint.getMaskFilter() ||
87 paint.getRasterizer() ||
robertphillips9c240a12015-05-28 07:45:59 -070088 paint.getPathEffect() ||
89 paint.isFakeBoldText() ||
90 paint.getStyle() != SkPaint::kFill_Style)
91 {
92 return true;
93 }
94 return false;
95}
96
robertphillipsfcf78292015-06-19 11:49:52 -070097uint32_t GrTextContext::FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint) {
robertphillips9c240a12015-05-28 07:45:59 -070098 uint32_t flags = paint.getFlags();
99
100 if (!paint.isLCDRenderText() || !paint.isAntiAlias()) {
101 return flags;
102 }
103
robertphillipsfcf78292015-06-19 11:49:52 -0700104 if (kUnknown_SkPixelGeometry == surfaceProps.pixelGeometry() || ShouldDisableLCD(paint)) {
robertphillips9c240a12015-05-28 07:45:59 -0700105 flags &= ~SkPaint::kLCDRenderText_Flag;
106 flags |= SkPaint::kGenA8FromLCD_Flag;
107 }
108
109 return flags;
110}
111
robertphillipsf6703fa2015-09-01 05:36:47 -0700112void GrTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700113 const GrClip& clip, const SkPaint& skPaint,
joshualitt9c328182015-03-23 08:13:04 -0700114 const SkMatrix& viewMatrix, const SkTextBlob* blob,
115 SkScalar x, SkScalar y,
116 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -0700117 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700118
halcanary33779752015-10-27 14:01:05 -0700119 SkTextBlobRunIterator it(blob);
cdaltone68f7362015-03-25 14:02:37 -0700120 for (;!it.done(); it.next()) {
121 size_t textLen = it.glyphCount() * sizeof(uint16_t);
122 const SkPoint& offset = it.offset();
123 // applyFontToPaint() always overwrites the exact same attributes,
124 // so it is safe to not re-seed the paint for this reason.
125 it.applyFontToPaint(&runPaint);
126
127 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
128 // A false return from filter() means we should abort the current draw.
129 runPaint = skPaint;
130 continue;
joshualitt9c328182015-03-23 08:13:04 -0700131 }
cdaltone68f7362015-03-25 14:02:37 -0700132
robertphillipsfcf78292015-06-19 11:49:52 -0700133 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
cdaltone68f7362015-03-25 14:02:37 -0700134
135 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700136 if (!SkPaintToGrPaint(fContext, runPaint, viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700137 return;
138 }
cdaltone68f7362015-03-25 14:02:37 -0700139
140 switch (it.positioning()) {
141 case SkTextBlob::kDefault_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700142 this->drawText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700143 textLen, x + offset.x(), y + offset.y(), clipBounds);
144 break;
145 case SkTextBlob::kHorizontal_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700146 this->drawPosText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700147 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
148 break;
149 case SkTextBlob::kFull_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -0700150 this->drawPosText(dc, rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
cdaltone68f7362015-03-25 14:02:37 -0700151 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
152 break;
153 default:
154 SkFAIL("unhandled positioning mode");
155 }
156
157 if (drawFilter) {
158 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
159 runPaint = skPaint;
160 }
161 }
joshualitt9c328182015-03-23 08:13:04 -0700162}
163
robertphillips7bceedc2015-12-01 12:51:26 -0800164void GrTextContext::drawTextAsPath(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700165 const GrClip& clip,
166 const SkPaint& skPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700167 const char text[], size_t byteLength, SkScalar x, SkScalar y,
168 const SkIRect& clipBounds) {
169 SkTextToPathIter iter(text, byteLength, skPaint, true);
170
171 SkMatrix matrix;
172 matrix.setScale(iter.getPathScale(), iter.getPathScale());
173 matrix.postTranslate(x, y);
174
175 const SkPath* iterPath;
176 SkScalar xpos, prevXPos = 0;
177
178 while (iter.next(&iterPath, &xpos)) {
179 matrix.postTranslate(xpos - prevXPos, 0);
180 if (iterPath) {
181 const SkPaint& pnt = iter.getPaint();
robertphillips7bceedc2015-12-01 12:51:26 -0800182 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, *iterPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700183 pnt, viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700184 }
185 prevXPos = xpos;
186 }
187}
188
robertphillips7bceedc2015-12-01 12:51:26 -0800189void GrTextContext::drawPosTextAsPath(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700190 const GrClip& clip,
191 const SkPaint& origPaint, const SkMatrix& viewMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -0700192 const char text[], size_t byteLength,
193 const SkScalar pos[], int scalarsPerPosition,
194 const SkPoint& offset, const SkIRect& clipBounds) {
195 // setup our std paint, in hopes of getting hits in the cache
196 SkPaint paint(origPaint);
197 SkScalar matrixScale = paint.setupForAsPaths();
198
199 SkMatrix matrix;
200 matrix.setScale(matrixScale, matrixScale);
201
202 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
203 paint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700204 paint.setPathEffect(nullptr);
joshualitt6e8cd962015-03-20 10:30:14 -0700205
206 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
halcanary96fcdcc2015-08-27 07:41:13 -0700207 SkAutoGlyphCache autoCache(paint, &fSurfaceProps, nullptr);
joshualitt6e8cd962015-03-20 10:30:14 -0700208 SkGlyphCache* cache = autoCache.getCache();
209
210 const char* stop = text + byteLength;
211 SkTextAlignProc alignProc(paint.getTextAlign());
212 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
213
214 // Now restore the original settings, so we "draw" with whatever style/stroking.
215 paint.setStyle(origPaint.getStyle());
216 paint.setPathEffect(origPaint.getPathEffect());
217
218 while (text < stop) {
219 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
220 if (glyph.fWidth) {
221 const SkPath* path = cache->findPath(glyph);
222 if (path) {
223 SkPoint tmsLoc;
224 tmsProc(pos, &tmsLoc);
225 SkPoint loc;
226 alignProc(tmsLoc, glyph, &loc);
227
228 matrix[SkMatrix::kMTransX] = loc.fX;
229 matrix[SkMatrix::kMTransY] = loc.fY;
robertphillips7bceedc2015-12-01 12:51:26 -0800230 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, *path, paint,
robertphillipsccb1b572015-05-27 11:02:55 -0700231 viewMatrix, &matrix, clipBounds, false);
joshualitt6e8cd962015-03-20 10:30:14 -0700232 }
233 }
234 pos += scalarsPerPosition;
235 }
236}
jvanverth8c27a182014-10-14 08:45:50 -0700237
halcanary385fe4d2015-08-26 13:07:48 -0700238// *** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700239int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000240 const char text[], size_t byteLength, SkVector* stopVector) {
241 SkFixed x = 0, y = 0;
242 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000243
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000244 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000245
jvanverth73f10532014-10-23 11:57:12 -0700246 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000247 while (text < stop) {
248 // don't need x, y here, since all subpixel variants will have the
249 // same advance
250 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000251
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000252 x += autokern.adjust(glyph) + glyph.fAdvanceX;
253 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700254 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000255 }
256 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000257
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000258 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700259
260 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000261}
262
263static void GlyphCacheAuxProc(void* data) {
264 GrFontScaler* scaler = (GrFontScaler*)data;
265 SkSafeUnref(scaler);
266}
267
268GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
269 void* auxData;
halcanary96fcdcc2015-08-27 07:41:13 -0700270 GrFontScaler* scaler = nullptr;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000271
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000272 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
273 scaler = (GrFontScaler*)auxData;
274 }
halcanary96fcdcc2015-08-27 07:41:13 -0700275 if (nullptr == scaler) {
halcanary385fe4d2015-08-26 13:07:48 -0700276 scaler = new GrFontScaler(cache);
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000277 cache->setAuxProc(GlyphCacheAuxProc, scaler);
278 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000279
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000280 return scaler;
281}