blob: 4c6dabcf7d981ab1802a77be5d58d1d60adfdfa5 [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"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00009#include "GrContext.h"
joshualitt570d2f82015-02-25 13:19:48 -080010#include "GrDrawTarget.h"
11#include "GrFontScaler.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000012
13#include "SkAutoKern.h"
joshualitt9c328182015-03-23 08:13:04 -070014#include "SkDrawFilter.h"
joshualitt6e8cd962015-03-20 10:30:14 -070015#include "SkDrawProcs.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000016#include "SkGlyphCache.h"
joshualitt6e8cd962015-03-20 10:30:14 -070017#include "SkGpuDevice.h"
joshualitt9c328182015-03-23 08:13:04 -070018#include "SkTextBlob.h"
joshualitt6e8cd962015-03-20 10:30:14 -070019#include "SkTextMapStateProc.h"
20#include "SkTextToPathIter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
joshualitt6e8cd962015-03-20 10:30:14 -070022GrTextContext::GrTextContext(GrContext* context, SkGpuDevice* gpuDevice,
23 const SkDeviceProperties& properties)
24 : fFallbackTextContext(NULL)
25 , fContext(context)
26 , fGpuDevice(gpuDevice)
27 , fDeviceProperties(properties)
28 , fDrawTarget(NULL) {
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000029}
tomhudson@google.com375ff852012-06-29 18:37:57 +000030
jvanverth8c27a182014-10-14 08:45:50 -070031GrTextContext::~GrTextContext() {
32 SkDELETE(fFallbackTextContext);
33}
34
joshualitt570d2f82015-02-25 13:19:48 -080035void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint,
joshualitt6e8cd962015-03-20 10:30:14 -070036 const SkPaint& skPaint, const SkIRect& regionClipBounds) {
joshualitt570d2f82015-02-25 13:19:48 -080037 fClip = clip;
robertphillips@google.combeb1af72012-07-26 18:52:16 +000038
joshualitt25d9c152015-02-18 12:29:52 -080039 fRenderTarget.reset(SkRef(rt));
40
joshualitt6e8cd962015-03-20 10:30:14 -070041 fRegionClipBounds = regionClipBounds;
joshualitt570d2f82015-02-25 13:19:48 -080042 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000043
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000044 fDrawTarget = fContext->getTextTarget();
45
46 fPaint = grPaint;
47 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000048}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000049
joshualitt6e8cd962015-03-20 10:30:14 -070050void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080051 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070052 const char text[], size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070053 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
joshualitt5f5a8d72015-02-25 14:09:45 -080054 if (!fContext->getTextTarget()) {
joshualitt6e8cd962015-03-20 10:30:14 -070055 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080056 }
jvanverth8c27a182014-10-14 08:45:50 -070057
jvanverthaab626c2014-10-16 08:04:39 -070058 GrTextContext* textContext = this;
59 do {
cdaltone68f7362015-03-25 14:02:37 -070060 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
joshualitt6e8cd962015-03-20 10:30:14 -070061 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y,
62 clipBounds);
63 return;
jvanverthaab626c2014-10-16 08:04:39 -070064 }
65 textContext = textContext->fFallbackTextContext;
66 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070067
joshualitt6e8cd962015-03-20 10:30:14 -070068 // fall back to drawing as a path
joshualitt79dfb2b2015-05-11 08:58:08 -070069 SkASSERT(fGpuDevice);
joshualitt6e8cd962015-03-20 10:30:14 -070070 this->drawTextAsPath(skPaint, viewMatrix, text, byteLength, x, y, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070071}
72
joshualitt6e8cd962015-03-20 10:30:14 -070073void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080074 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070075 const char text[], size_t byteLength,
76 const SkScalar pos[], int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -070077 const SkPoint& offset, const SkIRect& clipBounds) {
joshualitt5f5a8d72015-02-25 14:09:45 -080078 if (!fContext->getTextTarget()) {
joshualitt6e8cd962015-03-20 10:30:14 -070079 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080080 }
jvanverth8c27a182014-10-14 08:45:50 -070081
82 GrTextContext* textContext = this;
83 do {
cdaltone68f7362015-03-25 14:02:37 -070084 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
joshualitt570d2f82015-02-25 13:19:48 -080085 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, pos,
joshualitt6e8cd962015-03-20 10:30:14 -070086 scalarsPerPosition, offset, clipBounds);
87 return;
jvanverth8c27a182014-10-14 08:45:50 -070088 }
89 textContext = textContext->fFallbackTextContext;
90 } while (textContext);
91
joshualitt6e8cd962015-03-20 10:30:14 -070092 // fall back to drawing as a path
joshualitt79dfb2b2015-05-11 08:58:08 -070093 SkASSERT(fGpuDevice);
joshualitt6e8cd962015-03-20 10:30:14 -070094 this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsPerPosition, offset,
95 clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070096}
97
joshualitt9c328182015-03-23 08:13:04 -070098void GrTextContext::drawTextBlob(GrRenderTarget* rt, const GrClip& clip, const SkPaint& skPaint,
99 const SkMatrix& viewMatrix, const SkTextBlob* blob,
100 SkScalar x, SkScalar y,
101 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -0700102 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700103
cdaltone68f7362015-03-25 14:02:37 -0700104 SkTextBlob::RunIterator it(blob);
105 for (;!it.done(); it.next()) {
106 size_t textLen = it.glyphCount() * sizeof(uint16_t);
107 const SkPoint& offset = it.offset();
108 // applyFontToPaint() always overwrites the exact same attributes,
109 // so it is safe to not re-seed the paint for this reason.
110 it.applyFontToPaint(&runPaint);
111
112 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
113 // A false return from filter() means we should abort the current draw.
114 runPaint = skPaint;
115 continue;
joshualitt9c328182015-03-23 08:13:04 -0700116 }
cdaltone68f7362015-03-25 14:02:37 -0700117
118 runPaint.setFlags(fGpuDevice->filterTextFlags(runPaint));
119
120 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700121 if (!SkPaint2GrPaint(fContext, fRenderTarget, runPaint, viewMatrix, true, &grPaint)) {
122 return;
123 }
cdaltone68f7362015-03-25 14:02:37 -0700124
125 switch (it.positioning()) {
126 case SkTextBlob::kDefault_Positioning:
127 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
128 textLen, x + offset.x(), y + offset.y(), clipBounds);
129 break;
130 case SkTextBlob::kHorizontal_Positioning:
131 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
132 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
133 break;
134 case SkTextBlob::kFull_Positioning:
135 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
136 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
137 break;
138 default:
139 SkFAIL("unhandled positioning mode");
140 }
141
142 if (drawFilter) {
143 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
144 runPaint = skPaint;
145 }
146 }
joshualitt9c328182015-03-23 08:13:04 -0700147}
148
joshualitt6e8cd962015-03-20 10:30:14 -0700149void GrTextContext::drawTextAsPath(const SkPaint& skPaint, const SkMatrix& viewMatrix,
150 const char text[], size_t byteLength, SkScalar x, SkScalar y,
151 const SkIRect& clipBounds) {
152 SkTextToPathIter iter(text, byteLength, skPaint, true);
153
154 SkMatrix matrix;
155 matrix.setScale(iter.getPathScale(), iter.getPathScale());
156 matrix.postTranslate(x, y);
157
158 const SkPath* iterPath;
159 SkScalar xpos, prevXPos = 0;
160
161 while (iter.next(&iterPath, &xpos)) {
162 matrix.postTranslate(xpos - prevXPos, 0);
163 if (iterPath) {
164 const SkPaint& pnt = iter.getPaint();
165 fGpuDevice->internalDrawPath(*iterPath, pnt, viewMatrix, &matrix, clipBounds, false);
166 }
167 prevXPos = xpos;
168 }
169}
170
171void GrTextContext::drawPosTextAsPath(const SkPaint& origPaint, const SkMatrix& viewMatrix,
172 const char text[], size_t byteLength,
173 const SkScalar pos[], int scalarsPerPosition,
174 const SkPoint& offset, const SkIRect& clipBounds) {
175 // setup our std paint, in hopes of getting hits in the cache
176 SkPaint paint(origPaint);
177 SkScalar matrixScale = paint.setupForAsPaths();
178
179 SkMatrix matrix;
180 matrix.setScale(matrixScale, matrixScale);
181
182 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
183 paint.setStyle(SkPaint::kFill_Style);
184 paint.setPathEffect(NULL);
185
186 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
187 SkAutoGlyphCache autoCache(paint, NULL, NULL);
188 SkGlyphCache* cache = autoCache.getCache();
189
190 const char* stop = text + byteLength;
191 SkTextAlignProc alignProc(paint.getTextAlign());
192 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
193
194 // Now restore the original settings, so we "draw" with whatever style/stroking.
195 paint.setStyle(origPaint.getStyle());
196 paint.setPathEffect(origPaint.getPathEffect());
197
198 while (text < stop) {
199 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
200 if (glyph.fWidth) {
201 const SkPath* path = cache->findPath(glyph);
202 if (path) {
203 SkPoint tmsLoc;
204 tmsProc(pos, &tmsLoc);
205 SkPoint loc;
206 alignProc(tmsLoc, glyph, &loc);
207
208 matrix[SkMatrix::kMTransX] = loc.fX;
209 matrix[SkMatrix::kMTransY] = loc.fY;
210 fGpuDevice->internalDrawPath(*path, paint, viewMatrix, &matrix, clipBounds, false);
211 }
212 }
213 pos += scalarsPerPosition;
214 }
215}
jvanverth8c27a182014-10-14 08:45:50 -0700216
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000217//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700218int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000219 const char text[], size_t byteLength, SkVector* stopVector) {
220 SkFixed x = 0, y = 0;
221 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000222
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000223 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000224
jvanverth73f10532014-10-23 11:57:12 -0700225 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000226 while (text < stop) {
227 // don't need x, y here, since all subpixel variants will have the
228 // same advance
229 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000230
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000231 x += autokern.adjust(glyph) + glyph.fAdvanceX;
232 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700233 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000234 }
235 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000236
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000237 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700238
239 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000240}
241
242static void GlyphCacheAuxProc(void* data) {
243 GrFontScaler* scaler = (GrFontScaler*)data;
244 SkSafeUnref(scaler);
245}
246
247GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
248 void* auxData;
249 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000250
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000251 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
252 scaler = (GrFontScaler*)auxData;
253 }
254 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700255 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000256 cache->setAuxProc(GlyphCacheAuxProc, scaler);
257 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000258
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000259 return scaler;
260}