blob: 28e10e398c1d0d6b978a241e6050ae7293e15dca [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"
joshualitt6e8cd962015-03-20 10:30:14 -070014#include "SkDrawProcs.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000015#include "SkGlyphCache.h"
joshualitt6e8cd962015-03-20 10:30:14 -070016#include "SkGpuDevice.h"
17#include "SkTextMapStateProc.h"
18#include "SkTextToPathIter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
joshualitt6e8cd962015-03-20 10:30:14 -070020GrTextContext::GrTextContext(GrContext* context, SkGpuDevice* gpuDevice,
21 const SkDeviceProperties& properties)
22 : fFallbackTextContext(NULL)
23 , fContext(context)
24 , fGpuDevice(gpuDevice)
25 , fDeviceProperties(properties)
26 , fDrawTarget(NULL) {
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000027}
tomhudson@google.com375ff852012-06-29 18:37:57 +000028
jvanverth8c27a182014-10-14 08:45:50 -070029GrTextContext::~GrTextContext() {
30 SkDELETE(fFallbackTextContext);
31}
32
joshualitt570d2f82015-02-25 13:19:48 -080033void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint,
joshualitt6e8cd962015-03-20 10:30:14 -070034 const SkPaint& skPaint, const SkIRect& regionClipBounds) {
joshualitt570d2f82015-02-25 13:19:48 -080035 fClip = clip;
robertphillips@google.combeb1af72012-07-26 18:52:16 +000036
joshualitt25d9c152015-02-18 12:29:52 -080037 fRenderTarget.reset(SkRef(rt));
38
joshualitt6e8cd962015-03-20 10:30:14 -070039 fRegionClipBounds = regionClipBounds;
joshualitt570d2f82015-02-25 13:19:48 -080040 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000041
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000042 fDrawTarget = fContext->getTextTarget();
43
44 fPaint = grPaint;
45 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000046}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000047
joshualitt6e8cd962015-03-20 10:30:14 -070048void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
joshualitt570d2f82015-02-25 13:19:48 -080049 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070050 const char text[], size_t byteLength,
joshualitt6e8cd962015-03-20 10:30:14 -070051 SkScalar x, SkScalar y, const SkIRect& clipBounds) {
joshualitt5f5a8d72015-02-25 14:09:45 -080052 if (!fContext->getTextTarget()) {
joshualitt6e8cd962015-03-20 10:30:14 -070053 return;
joshualitt5f5a8d72015-02-25 14:09:45 -080054 }
jvanverth8c27a182014-10-14 08:45:50 -070055
jvanverthaab626c2014-10-16 08:04:39 -070056 GrTextContext* textContext = this;
57 do {
joshualitt5531d512014-12-17 15:50:11 -080058 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt6e8cd962015-03-20 10:30:14 -070059 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y,
60 clipBounds);
61 return;
jvanverthaab626c2014-10-16 08:04:39 -070062 }
63 textContext = textContext->fFallbackTextContext;
64 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070065
joshualitt6e8cd962015-03-20 10:30:14 -070066 // fall back to drawing as a path
67 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) {
joshualitt5f5a8d72015-02-25 14:09:45 -080075 if (!fContext->getTextTarget()) {
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 {
joshualitt5531d512014-12-17 15:50:11 -080081 if (textContext->canDraw(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
90 this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsPerPosition, offset,
91 clipBounds);
jvanverth8c27a182014-10-14 08:45:50 -070092}
93
joshualitt6e8cd962015-03-20 10:30:14 -070094void GrTextContext::drawTextAsPath(const SkPaint& skPaint, const SkMatrix& viewMatrix,
95 const char text[], size_t byteLength, SkScalar x, SkScalar y,
96 const SkIRect& clipBounds) {
97 SkTextToPathIter iter(text, byteLength, skPaint, true);
98
99 SkMatrix matrix;
100 matrix.setScale(iter.getPathScale(), iter.getPathScale());
101 matrix.postTranslate(x, y);
102
103 const SkPath* iterPath;
104 SkScalar xpos, prevXPos = 0;
105
106 while (iter.next(&iterPath, &xpos)) {
107 matrix.postTranslate(xpos - prevXPos, 0);
108 if (iterPath) {
109 const SkPaint& pnt = iter.getPaint();
110 fGpuDevice->internalDrawPath(*iterPath, pnt, viewMatrix, &matrix, clipBounds, false);
111 }
112 prevXPos = xpos;
113 }
114}
115
116void GrTextContext::drawPosTextAsPath(const SkPaint& origPaint, const SkMatrix& viewMatrix,
117 const char text[], size_t byteLength,
118 const SkScalar pos[], int scalarsPerPosition,
119 const SkPoint& offset, const SkIRect& clipBounds) {
120 // setup our std paint, in hopes of getting hits in the cache
121 SkPaint paint(origPaint);
122 SkScalar matrixScale = paint.setupForAsPaths();
123
124 SkMatrix matrix;
125 matrix.setScale(matrixScale, matrixScale);
126
127 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
128 paint.setStyle(SkPaint::kFill_Style);
129 paint.setPathEffect(NULL);
130
131 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
132 SkAutoGlyphCache autoCache(paint, NULL, NULL);
133 SkGlyphCache* cache = autoCache.getCache();
134
135 const char* stop = text + byteLength;
136 SkTextAlignProc alignProc(paint.getTextAlign());
137 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
138
139 // Now restore the original settings, so we "draw" with whatever style/stroking.
140 paint.setStyle(origPaint.getStyle());
141 paint.setPathEffect(origPaint.getPathEffect());
142
143 while (text < stop) {
144 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
145 if (glyph.fWidth) {
146 const SkPath* path = cache->findPath(glyph);
147 if (path) {
148 SkPoint tmsLoc;
149 tmsProc(pos, &tmsLoc);
150 SkPoint loc;
151 alignProc(tmsLoc, glyph, &loc);
152
153 matrix[SkMatrix::kMTransX] = loc.fX;
154 matrix[SkMatrix::kMTransY] = loc.fY;
155 fGpuDevice->internalDrawPath(*path, paint, viewMatrix, &matrix, clipBounds, false);
156 }
157 }
158 pos += scalarsPerPosition;
159 }
160}
jvanverth8c27a182014-10-14 08:45:50 -0700161
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000162//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -0700163int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000164 const char text[], size_t byteLength, SkVector* stopVector) {
165 SkFixed x = 0, y = 0;
166 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000167
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000168 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000169
jvanverth73f10532014-10-23 11:57:12 -0700170 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000171 while (text < stop) {
172 // don't need x, y here, since all subpixel variants will have the
173 // same advance
174 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000175
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000176 x += autokern.adjust(glyph) + glyph.fAdvanceX;
177 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -0700178 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000179 }
180 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000181
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000182 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700183
184 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000185}
186
187static void GlyphCacheAuxProc(void* data) {
188 GrFontScaler* scaler = (GrFontScaler*)data;
189 SkSafeUnref(scaler);
190}
191
192GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
193 void* auxData;
194 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000195
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000196 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
197 scaler = (GrFontScaler*)auxData;
198 }
199 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700200 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000201 cache->setAuxProc(GlyphCacheAuxProc, scaler);
202 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000203
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000204 return scaler;
205}