blob: 32187109f359ac92b3291fa3009bb1bfa3e64723 [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
69 this->drawTextAsPath(skPaint, viewMatrix, text, byteLength, x, y, clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070070}
71
joshualitt6e8cd962015-03-20 10:30:14 -070072void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080073 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070074 const char text[], size_t byteLength,
75 const SkScalar pos[], int scalarsPerPosition,
joshualitt6e8cd962015-03-20 10:30:14 -070076 const SkPoint& offset, const SkIRect& clipBounds) {
joshualitt5f5a8d72015-02-25 14:09:45 -080077 if (!fContext->getTextTarget()) {
joshualitt6e8cd962015-03-20 10:30:14 -070078 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080079 }
jvanverth8c27a182014-10-14 08:45:50 -070080
81 GrTextContext* textContext = this;
82 do {
cdaltone68f7362015-03-25 14:02:37 -070083 if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) {
joshualitt570d2f82015-02-25 13:19:48 -080084 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, pos,
joshualitt6e8cd962015-03-20 10:30:14 -070085 scalarsPerPosition, offset, clipBounds);
86 return;
jvanverth8c27a182014-10-14 08:45:50 -070087 }
88 textContext = textContext->fFallbackTextContext;
89 } while (textContext);
90
joshualitt6e8cd962015-03-20 10:30:14 -070091 // fall back to drawing as a path
92 this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsPerPosition, offset,
93 clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070094}
95
joshualitt9c328182015-03-23 08:13:04 -070096void GrTextContext::drawTextBlob(GrRenderTarget* rt, const GrClip& clip, const SkPaint& skPaint,
97 const SkMatrix& viewMatrix, const SkTextBlob* blob,
98 SkScalar x, SkScalar y,
99 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
cdaltone68f7362015-03-25 14:02:37 -0700100 SkPaint runPaint = skPaint;
joshualitt9c328182015-03-23 08:13:04 -0700101
cdaltone68f7362015-03-25 14:02:37 -0700102 SkTextBlob::RunIterator it(blob);
103 for (;!it.done(); it.next()) {
104 size_t textLen = it.glyphCount() * sizeof(uint16_t);
105 const SkPoint& offset = it.offset();
106 // applyFontToPaint() always overwrites the exact same attributes,
107 // so it is safe to not re-seed the paint for this reason.
108 it.applyFontToPaint(&runPaint);
109
110 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
111 // A false return from filter() means we should abort the current draw.
112 runPaint = skPaint;
113 continue;
joshualitt9c328182015-03-23 08:13:04 -0700114 }
cdaltone68f7362015-03-25 14:02:37 -0700115
116 runPaint.setFlags(fGpuDevice->filterTextFlags(runPaint));
117
118 GrPaint grPaint;
119 SkPaint2GrPaintShader(fContext, fRenderTarget, runPaint, viewMatrix, true, &grPaint);
120
121 switch (it.positioning()) {
122 case SkTextBlob::kDefault_Positioning:
123 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
124 textLen, x + offset.x(), y + offset.y(), clipBounds);
125 break;
126 case SkTextBlob::kHorizontal_Positioning:
127 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
128 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
129 break;
130 case SkTextBlob::kFull_Positioning:
131 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
132 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
133 break;
134 default:
135 SkFAIL("unhandled positioning mode");
136 }
137
138 if (drawFilter) {
139 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
140 runPaint = skPaint;
141 }
142 }
joshualitt9c328182015-03-23 08:13:04 -0700143}
144
joshualitt6e8cd962015-03-20 10:30:14 -0700145void GrTextContext::drawTextAsPath(const SkPaint& skPaint, const SkMatrix& viewMatrix,
146 const char text[], size_t byteLength, SkScalar x, SkScalar y,
147 const SkIRect& clipBounds) {
148 SkTextToPathIter iter(text, byteLength, skPaint, true);
149
150 SkMatrix matrix;
151 matrix.setScale(iter.getPathScale(), iter.getPathScale());
152 matrix.postTranslate(x, y);
153
154 const SkPath* iterPath;
155 SkScalar xpos, prevXPos = 0;
156
157 while (iter.next(&iterPath, &xpos)) {
158 matrix.postTranslate(xpos - prevXPos, 0);
159 if (iterPath) {
160 const SkPaint& pnt = iter.getPaint();
161 fGpuDevice->internalDrawPath(*iterPath, pnt, viewMatrix, &matrix, clipBounds, false);
162 }
163 prevXPos = xpos;
164 }
165}
166
167void GrTextContext::drawPosTextAsPath(const SkPaint& origPaint, const SkMatrix& viewMatrix,
168 const char text[], size_t byteLength,
169 const SkScalar pos[], int scalarsPerPosition,
170 const SkPoint& offset, const SkIRect& clipBounds) {
171 // setup our std paint, in hopes of getting hits in the cache
172 SkPaint paint(origPaint);
173 SkScalar matrixScale = paint.setupForAsPaths();
174
175 SkMatrix matrix;
176 matrix.setScale(matrixScale, matrixScale);
177
178 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
179 paint.setStyle(SkPaint::kFill_Style);
180 paint.setPathEffect(NULL);
181
182 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
183 SkAutoGlyphCache autoCache(paint, NULL, NULL);
184 SkGlyphCache* cache = autoCache.getCache();
185
186 const char* stop = text + byteLength;
187 SkTextAlignProc alignProc(paint.getTextAlign());
188 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
189
190 // Now restore the original settings, so we "draw" with whatever style/stroking.
191 paint.setStyle(origPaint.getStyle());
192 paint.setPathEffect(origPaint.getPathEffect());
193
194 while (text < stop) {
195 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
196 if (glyph.fWidth) {
197 const SkPath* path = cache->findPath(glyph);
198 if (path) {
199 SkPoint tmsLoc;
200 tmsProc(pos, &tmsLoc);
201 SkPoint loc;
202 alignProc(tmsLoc, glyph, &loc);
203
204 matrix[SkMatrix::kMTransX] = loc.fX;
205 matrix[SkMatrix::kMTransY] = loc.fY;
206 fGpuDevice->internalDrawPath(*path, paint, viewMatrix, &matrix, clipBounds, false);
207 }
208 }
209 pos += scalarsPerPosition;
210 }
211}
jvanverth8c27a182014-10-14 08:45:50 -0700212
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000213//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700214int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000215 const char text[], size_t byteLength, SkVector* stopVector) {
216 SkFixed x = 0, y = 0;
217 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000218
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000219 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000220
jvanverth73f10532014-10-23 11:57:12 -0700221 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000222 while (text < stop) {
223 // don't need x, y here, since all subpixel variants will have the
224 // same advance
225 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000226
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000227 x += autokern.adjust(glyph) + glyph.fAdvanceX;
228 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700229 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000230 }
231 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000232
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000233 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700234
235 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000236}
237
238static void GlyphCacheAuxProc(void* data) {
239 GrFontScaler* scaler = (GrFontScaler*)data;
240 SkSafeUnref(scaler);
241}
242
243GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
244 void* auxData;
245 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000246
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000247 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
248 scaler = (GrFontScaler*)auxData;
249 }
250 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700251 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000252 cache->setAuxProc(GlyphCacheAuxProc, scaler);
253 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000254
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000255 return scaler;
256}