blob: 6153f3b05de56cf741d63afabfd7be8b170ffd8b [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;
bsalomonbed83a62015-04-15 14:18:34 -0700119 if (!SkPaint2GrPaint(fContext, fRenderTarget, runPaint, viewMatrix, true, &grPaint)) {
120 return;
121 }
cdaltone68f7362015-03-25 14:02:37 -0700122
123 switch (it.positioning()) {
124 case SkTextBlob::kDefault_Positioning:
125 this->drawText(rt, clip, grPaint, runPaint, viewMatrix, (const char *)it.glyphs(),
126 textLen, x + offset.x(), y + offset.y(), clipBounds);
127 break;
128 case SkTextBlob::kHorizontal_Positioning:
129 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
130 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), clipBounds);
131 break;
132 case SkTextBlob::kFull_Positioning:
133 this->drawPosText(rt, clip, grPaint, runPaint, viewMatrix, (const char*)it.glyphs(),
134 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
135 break;
136 default:
137 SkFAIL("unhandled positioning mode");
138 }
139
140 if (drawFilter) {
141 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
142 runPaint = skPaint;
143 }
144 }
joshualitt9c328182015-03-23 08:13:04 -0700145}
146
joshualitt6e8cd962015-03-20 10:30:14 -0700147void GrTextContext::drawTextAsPath(const SkPaint& skPaint, const SkMatrix& viewMatrix,
148 const char text[], size_t byteLength, SkScalar x, SkScalar y,
149 const SkIRect& clipBounds) {
150 SkTextToPathIter iter(text, byteLength, skPaint, true);
151
152 SkMatrix matrix;
153 matrix.setScale(iter.getPathScale(), iter.getPathScale());
154 matrix.postTranslate(x, y);
155
156 const SkPath* iterPath;
157 SkScalar xpos, prevXPos = 0;
158
159 while (iter.next(&iterPath, &xpos)) {
160 matrix.postTranslate(xpos - prevXPos, 0);
161 if (iterPath) {
162 const SkPaint& pnt = iter.getPaint();
163 fGpuDevice->internalDrawPath(*iterPath, pnt, viewMatrix, &matrix, clipBounds, false);
164 }
165 prevXPos = xpos;
166 }
167}
168
169void GrTextContext::drawPosTextAsPath(const SkPaint& origPaint, const SkMatrix& viewMatrix,
170 const char text[], size_t byteLength,
171 const SkScalar pos[], int scalarsPerPosition,
172 const SkPoint& offset, const SkIRect& clipBounds) {
173 // setup our std paint, in hopes of getting hits in the cache
174 SkPaint paint(origPaint);
175 SkScalar matrixScale = paint.setupForAsPaths();
176
177 SkMatrix matrix;
178 matrix.setScale(matrixScale, matrixScale);
179
180 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
181 paint.setStyle(SkPaint::kFill_Style);
182 paint.setPathEffect(NULL);
183
184 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
185 SkAutoGlyphCache autoCache(paint, NULL, NULL);
186 SkGlyphCache* cache = autoCache.getCache();
187
188 const char* stop = text + byteLength;
189 SkTextAlignProc alignProc(paint.getTextAlign());
190 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
191
192 // Now restore the original settings, so we "draw" with whatever style/stroking.
193 paint.setStyle(origPaint.getStyle());
194 paint.setPathEffect(origPaint.getPathEffect());
195
196 while (text < stop) {
197 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
198 if (glyph.fWidth) {
199 const SkPath* path = cache->findPath(glyph);
200 if (path) {
201 SkPoint tmsLoc;
202 tmsProc(pos, &tmsLoc);
203 SkPoint loc;
204 alignProc(tmsLoc, glyph, &loc);
205
206 matrix[SkMatrix::kMTransX] = loc.fX;
207 matrix[SkMatrix::kMTransY] = loc.fY;
208 fGpuDevice->internalDrawPath(*path, paint, viewMatrix, &matrix, clipBounds, false);
209 }
210 }
211 pos += scalarsPerPosition;
212 }
213}
jvanverth8c27a182014-10-14 08:45:50 -0700214
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000215//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700216int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000217 const char text[], size_t byteLength, SkVector* stopVector) {
218 SkFixed x = 0, y = 0;
219 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000220
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000221 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000222
jvanverth73f10532014-10-23 11:57:12 -0700223 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000224 while (text < stop) {
225 // don't need x, y here, since all subpixel variants will have the
226 // same advance
227 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000228
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000229 x += autokern.adjust(glyph) + glyph.fAdvanceX;
230 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700231 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000232 }
233 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000234
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000235 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700236
237 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000238}
239
240static void GlyphCacheAuxProc(void* data) {
241 GrFontScaler* scaler = (GrFontScaler*)data;
242 SkSafeUnref(scaler);
243}
244
245GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
246 void* auxData;
247 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000248
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000249 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
250 scaler = (GrFontScaler*)auxData;
251 }
252 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700253 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000254 cache->setAuxProc(GlyphCacheAuxProc, scaler);
255 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000256
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000257 return scaler;
258}