reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 8 | #include "GrTextContext.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 10 | #include "GrDrawTarget.h" |
| 11 | #include "GrFontScaler.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 12 | |
| 13 | #include "SkAutoKern.h" |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 14 | #include "SkDrawFilter.h" |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 15 | #include "SkDrawProcs.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 16 | #include "SkGlyphCache.h" |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 17 | #include "SkGpuDevice.h" |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 18 | #include "SkTextBlob.h" |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 19 | #include "SkTextMapStateProc.h" |
| 20 | #include "SkTextToPathIter.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 22 | GrTextContext::GrTextContext(GrContext* context, SkGpuDevice* gpuDevice, |
| 23 | const SkDeviceProperties& properties) |
| 24 | : fFallbackTextContext(NULL) |
| 25 | , fContext(context) |
| 26 | , fGpuDevice(gpuDevice) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame^] | 27 | , fDeviceProperties(properties) { |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 28 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 29 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 30 | GrTextContext::~GrTextContext() { |
| 31 | SkDELETE(fFallbackTextContext); |
| 32 | } |
| 33 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 34 | void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 35 | const SkPaint& skPaint, const SkIRect& regionClipBounds) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 36 | fClip = clip; |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 37 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 38 | fRenderTarget.reset(SkRef(rt)); |
| 39 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 40 | fRegionClipBounds = regionClipBounds; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 41 | fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 42 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 43 | fPaint = grPaint; |
| 44 | fSkPaint = skPaint; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | } |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 46 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 47 | void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 48 | const SkPaint& skPaint, const SkMatrix& viewMatrix, |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 49 | const char text[], size_t byteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 50 | SkScalar x, SkScalar y, const SkIRect& clipBounds) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame^] | 51 | if (fContext->abandoned()) { |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 52 | return; |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 53 | } |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 54 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 55 | GrTextContext* textContext = this; |
| 56 | do { |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 57 | if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 58 | textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y, |
| 59 | clipBounds); |
| 60 | return; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 61 | } |
| 62 | textContext = textContext->fFallbackTextContext; |
| 63 | } while (textContext); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 64 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 65 | // fall back to drawing as a path |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 66 | SkASSERT(fGpuDevice); |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 67 | this->drawTextAsPath(skPaint, viewMatrix, text, byteLength, x, y, clipBounds); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 68 | } |
| 69 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 70 | void GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 71 | const SkPaint& skPaint, const SkMatrix& viewMatrix, |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 72 | const char text[], size_t byteLength, |
| 73 | const SkScalar pos[], int scalarsPerPosition, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 74 | const SkPoint& offset, const SkIRect& clipBounds) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame^] | 75 | if (fContext->abandoned()) { |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 76 | return; |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 77 | } |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 78 | |
| 79 | GrTextContext* textContext = this; |
| 80 | do { |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 81 | if (textContext->canDraw(rt, clip, paint, skPaint, viewMatrix)) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 82 | textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, pos, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 83 | scalarsPerPosition, offset, clipBounds); |
| 84 | return; |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 85 | } |
| 86 | textContext = textContext->fFallbackTextContext; |
| 87 | } while (textContext); |
| 88 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 89 | // fall back to drawing as a path |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 90 | SkASSERT(fGpuDevice); |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 91 | this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsPerPosition, offset, |
| 92 | clipBounds); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 93 | } |
| 94 | |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 95 | void 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) { |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 99 | SkPaint runPaint = skPaint; |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 100 | |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 101 | 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; |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 113 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 114 | |
| 115 | runPaint.setFlags(fGpuDevice->filterTextFlags(runPaint)); |
| 116 | |
| 117 | GrPaint grPaint; |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 118 | if (!SkPaint2GrPaint(fContext, fRenderTarget, runPaint, viewMatrix, true, &grPaint)) { |
| 119 | return; |
| 120 | } |
cdalton | e68f736 | 2015-03-25 14:02:37 -0700 | [diff] [blame] | 121 | |
| 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 | } |
joshualitt | 9c32818 | 2015-03-23 08:13:04 -0700 | [diff] [blame] | 144 | } |
| 145 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 146 | void 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 | |
| 168 | void 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 | } |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 213 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 214 | //*** change to output positions? |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 215 | int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 216 | const char text[], size_t byteLength, SkVector* stopVector) { |
| 217 | SkFixed x = 0, y = 0; |
| 218 | const char* stop = text + byteLength; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 219 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 220 | SkAutoKern autokern; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 221 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 222 | int numGlyphs = 0; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 223 | 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.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 227 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 228 | x += autokern.adjust(glyph) + glyph.fAdvanceX; |
| 229 | y += glyph.fAdvanceY; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 230 | ++numGlyphs; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 231 | } |
| 232 | stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y)); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 233 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 234 | SkASSERT(text == stop); |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 235 | |
| 236 | return numGlyphs; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void GlyphCacheAuxProc(void* data) { |
| 240 | GrFontScaler* scaler = (GrFontScaler*)data; |
| 241 | SkSafeUnref(scaler); |
| 242 | } |
| 243 | |
| 244 | GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) { |
| 245 | void* auxData; |
| 246 | GrFontScaler* scaler = NULL; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 247 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 248 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 249 | scaler = (GrFontScaler*)auxData; |
| 250 | } |
| 251 | if (NULL == scaler) { |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 252 | scaler = SkNEW_ARGS(GrFontScaler, (cache)); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 253 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 254 | } |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 255 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 256 | return scaler; |
| 257 | } |