blob: 4b8bc68e339a602197ed17a0bd0f9b6c0e6a1054 [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)
robertphillipsea461502015-05-26 11:38:03 -070027 , fDeviceProperties(properties) {
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000028}
tomhudson@google.com375ff852012-06-29 18:37:57 +000029
jvanverth8c27a182014-10-14 08:45:50 -070030GrTextContext::~GrTextContext() {
31 SkDELETE(fFallbackTextContext);
32}
33
joshualitt570d2f82015-02-25 13:19:48 -080034void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint,
joshualitt6e8cd962015-03-20 10:30:14 -070035 const SkPaint& skPaint, const SkIRect& regionClipBounds) {
joshualitt570d2f82015-02-25 13:19:48 -080036 fClip = clip;
robertphillips@google.combeb1af72012-07-26 18:52:16 +000037
joshualitt25d9c152015-02-18 12:29:52 -080038 fRenderTarget.reset(SkRef(rt));
39
joshualitt6e8cd962015-03-20 10:30:14 -070040 fRegionClipBounds = regionClipBounds;
joshualitt570d2f82015-02-25 13:19:48 -080041 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000042
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000043 fPaint = grPaint;
44 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000045}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000046
joshualitt6e8cd962015-03-20 10:30:14 -070047void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080048 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070049 const char text[], size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070050 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
robertphillipsea461502015-05-26 11:38:03 -070051 if (fContext->abandoned()) {
joshualitt6e8cd962015-03-20 10:30:14 -070052 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080053 }
jvanverth8c27a182014-10-14 08:45:50 -070054
jvanverthaab626c2014-10-16 08:04:39 -070055 GrTextContext* textContext = this;
56 do {
cdaltone68f7362015-03-25 14:02:37 -070057 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
joshualitt6e8cd962015-03-20 10:30:14 -070058 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y,
59 clipBounds);
60 return;
jvanverthaab626c2014-10-16 08:04:39 -070061 }
62 textContext = textContext->fFallbackTextContext;
63 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070064
joshualitt6e8cd962015-03-20 10:30:14 -070065 // fall back to drawing as a path
joshualitt79dfb2b2015-05-11 08:58:08 -070066 SkASSERT(fGpuDevice);
joshualitt6e8cd962015-03-20 10:30:14 -070067 this->drawTextAsPath(skPaint, viewMatrix, text, byteLength, x, y, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070068}
69
joshualitt6e8cd962015-03-20 10:30:14 -070070void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080071 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070072 const char text[], size_t byteLength,
73 const SkScalar pos[], int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -070074 const SkPoint& offset, const SkIRect& clipBounds) {
robertphillipsea461502015-05-26 11:38:03 -070075 if (fContext->abandoned()) {
joshualitt6e8cd962015-03-20 10:30:14 -070076 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080077 }
jvanverth8c27a182014-10-14 08:45:50 -070078
79 GrTextContext* textContext = this;
80 do {
cdaltone68f7362015-03-25 14:02:37 -070081 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
joshualitt570d2f82015-02-25 13:19:48 -080082 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, pos,
joshualitt6e8cd962015-03-20 10:30:14 -070083 scalarsPerPosition, offset, clipBounds);
84 return;
jvanverth8c27a182014-10-14 08:45:50 -070085 }
86 textContext = textContext->fFallbackTextContext;
87 } while (textContext);
88
joshualitt6e8cd962015-03-20 10:30:14 -070089 // fall back to drawing as a path
joshualitt79dfb2b2015-05-11 08:58:08 -070090 SkASSERT(fGpuDevice);
joshualitt6e8cd962015-03-20 10:30:14 -070091 this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsPerPosition, offset,
92 clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070093}
94
joshualitt9c328182015-03-23 08:13:04 -070095void GrTextContext::drawTextBlob(GrRenderTarget* rt, const GrClip& clip, const SkPaint& skPaint,
96 const SkMatrix& viewMatrix, const SkTextBlob* blob,
97 SkScalar x, SkScalar y,
98 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -070099 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700100
cdaltone68f7362015-03-25 14:02:37 -0700101 SkTextBlob::RunIterator it(blob);
102 for (;!it.done(); it.next()) {
103 size_t textLen = it.glyphCount() * sizeof(uint16_t);
104 const SkPoint& offset = it.offset();
105 // applyFontToPaint() always overwrites the exact same attributes,
106 // so it is safe to not re-seed the paint for this reason.
107 it.applyFontToPaint(&runPaint);
108
109 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
110 // A false return from filter() means we should abort the current draw.
111 runPaint = skPaint;
112 continue;
joshualitt9c328182015-03-23 08:13:04 -0700113 }
cdaltone68f7362015-03-25 14:02:37 -0700114
115 runPaint.setFlags(fGpuDevice->filterTextFlags(runPaint));
116
117 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700118 if (!SkPaint2GrPaint(fContext, fRenderTarget, runPaint, viewMatrix, true, &grPaint)) {
119 return;
120 }
cdaltone68f7362015-03-25 14:02:37 -0700121
122 switch (it.positioning()) {
123 case SkTextBlob::kDefault_Positioning:
124 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
125 textLen, x + offset.x(), y + offset.y(), clipBounds);
126 break;
127 case SkTextBlob::kHorizontal_Positioning:
128 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
129 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
130 break;
131 case SkTextBlob::kFull_Positioning:
132 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
133 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
134 break;
135 default:
136 SkFAIL("unhandled positioning mode");
137 }
138
139 if (drawFilter) {
140 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
141 runPaint = skPaint;
142 }
143 }
joshualitt9c328182015-03-23 08:13:04 -0700144}
145
joshualitt6e8cd962015-03-20 10:30:14 -0700146void GrTextContext::drawTextAsPath(const SkPaint& skPaint, const SkMatrix& viewMatrix,
147 const char text[], size_t byteLength, SkScalar x, SkScalar y,
148 const SkIRect& clipBounds) {
149 SkTextToPathIter iter(text, byteLength, skPaint, true);
150
151 SkMatrix matrix;
152 matrix.setScale(iter.getPathScale(), iter.getPathScale());
153 matrix.postTranslate(x, y);
154
155 const SkPath* iterPath;
156 SkScalar xpos, prevXPos = 0;
157
158 while (iter.next(&iterPath, &xpos)) {
159 matrix.postTranslate(xpos - prevXPos, 0);
160 if (iterPath) {
161 const SkPaint& pnt = iter.getPaint();
162 fGpuDevice->internalDrawPath(*iterPath, pnt, viewMatrix, &matrix, clipBounds, false);
163 }
164 prevXPos = xpos;
165 }
166}
167
168void GrTextContext::drawPosTextAsPath(const SkPaint& origPaint, const SkMatrix& viewMatrix,
169 const char text[], size_t byteLength,
170 const SkScalar pos[], int scalarsPerPosition,
171 const SkPoint& offset, const SkIRect& clipBounds) {
172 // setup our std paint, in hopes of getting hits in the cache
173 SkPaint paint(origPaint);
174 SkScalar matrixScale = paint.setupForAsPaths();
175
176 SkMatrix matrix;
177 matrix.setScale(matrixScale, matrixScale);
178
179 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
180 paint.setStyle(SkPaint::kFill_Style);
181 paint.setPathEffect(NULL);
182
183 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
184 SkAutoGlyphCache autoCache(paint, NULL, NULL);
185 SkGlyphCache* cache = autoCache.getCache();
186
187 const char* stop = text + byteLength;
188 SkTextAlignProc alignProc(paint.getTextAlign());
189 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
190
191 // Now restore the original settings, so we "draw" with whatever style/stroking.
192 paint.setStyle(origPaint.getStyle());
193 paint.setPathEffect(origPaint.getPathEffect());
194
195 while (text < stop) {
196 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
197 if (glyph.fWidth) {
198 const SkPath* path = cache->findPath(glyph);
199 if (path) {
200 SkPoint tmsLoc;
201 tmsProc(pos, &tmsLoc);
202 SkPoint loc;
203 alignProc(tmsLoc, glyph, &loc);
204
205 matrix[SkMatrix::kMTransX] = loc.fX;
206 matrix[SkMatrix::kMTransY] = loc.fY;
207 fGpuDevice->internalDrawPath(*path, paint, viewMatrix, &matrix, clipBounds, false);
208 }
209 }
210 pos += scalarsPerPosition;
211 }
212}
jvanverth8c27a182014-10-14 08:45:50 -0700213
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000214//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700215int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000216 const char text[], size_t byteLength, SkVector* stopVector) {
217 SkFixed x = 0, y = 0;
218 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000219
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000220 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000221
jvanverth73f10532014-10-23 11:57:12 -0700222 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000223 while (text < stop) {
224 // don't need x, y here, since all subpixel variants will have the
225 // same advance
226 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000227
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000228 x += autokern.adjust(glyph) + glyph.fAdvanceX;
229 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700230 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000231 }
232 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000233
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000234 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700235
236 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000237}
238
239static void GlyphCacheAuxProc(void* data) {
240 GrFontScaler* scaler = (GrFontScaler*)data;
241 SkSafeUnref(scaler);
242}
243
244GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
245 void* auxData;
246 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000247
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000248 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
249 scaler = (GrFontScaler*)auxData;
250 }
251 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700252 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000253 cache->setAuxProc(GlyphCacheAuxProc, scaler);
254 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000255
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000256 return scaler;
257}