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 | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 14 | #include "SkDrawProcs.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 15 | #include "SkGlyphCache.h" |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 16 | #include "SkGpuDevice.h" |
| 17 | #include "SkTextMapStateProc.h" |
| 18 | #include "SkTextToPathIter.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 20 | GrTextContext::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.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 27 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 28 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 29 | GrTextContext::~GrTextContext() { |
| 30 | SkDELETE(fFallbackTextContext); |
| 31 | } |
| 32 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 33 | void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 34 | const SkPaint& skPaint, const SkIRect& regionClipBounds) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 35 | fClip = clip; |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 36 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 37 | fRenderTarget.reset(SkRef(rt)); |
| 38 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 39 | fRegionClipBounds = regionClipBounds; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 40 | fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 41 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 42 | fDrawTarget = fContext->getTextTarget(); |
| 43 | |
| 44 | fPaint = grPaint; |
| 45 | fSkPaint = skPaint; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | } |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 47 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 48 | void GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 49 | const SkPaint& skPaint, const SkMatrix& viewMatrix, |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 50 | const char text[], size_t byteLength, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 51 | SkScalar x, SkScalar y, const SkIRect& clipBounds) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 52 | if (!fContext->getTextTarget()) { |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 53 | return; |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 54 | } |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 55 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 56 | GrTextContext* textContext = this; |
| 57 | do { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 58 | if (textContext->canDraw(skPaint, viewMatrix)) { |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 59 | textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y, |
| 60 | clipBounds); |
| 61 | return; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 62 | } |
| 63 | textContext = textContext->fFallbackTextContext; |
| 64 | } while (textContext); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 65 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 66 | // fall back to drawing as a path |
| 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) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 75 | if (!fContext->getTextTarget()) { |
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 { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 81 | if (textContext->canDraw(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 |
| 90 | this->drawPosTextAsPath(skPaint, viewMatrix, text, byteLength, pos, scalarsPerPosition, offset, |
| 91 | clipBounds); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame^] | 94 | void 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 | |
| 116 | void 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 | } |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 161 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 162 | //*** change to output positions? |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 163 | int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 164 | const char text[], size_t byteLength, SkVector* stopVector) { |
| 165 | SkFixed x = 0, y = 0; |
| 166 | const char* stop = text + byteLength; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 167 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 168 | SkAutoKern autokern; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 169 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 170 | int numGlyphs = 0; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 171 | 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.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 175 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 176 | x += autokern.adjust(glyph) + glyph.fAdvanceX; |
| 177 | y += glyph.fAdvanceY; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 178 | ++numGlyphs; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 179 | } |
| 180 | stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y)); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 181 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 182 | SkASSERT(text == stop); |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 183 | |
| 184 | return numGlyphs; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void GlyphCacheAuxProc(void* data) { |
| 188 | GrFontScaler* scaler = (GrFontScaler*)data; |
| 189 | SkSafeUnref(scaler); |
| 190 | } |
| 191 | |
| 192 | GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) { |
| 193 | void* auxData; |
| 194 | GrFontScaler* scaler = NULL; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 195 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 196 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 197 | scaler = (GrFontScaler*)auxData; |
| 198 | } |
| 199 | if (NULL == scaler) { |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 200 | scaler = SkNEW_ARGS(GrFontScaler, (cache)); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 201 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 202 | } |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 203 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 204 | return scaler; |
| 205 | } |