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" |
| 10 | |
| 11 | #include "SkAutoKern.h" |
| 12 | #include "SkGlyphCache.h" |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 13 | #include "GrFontScaler.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 15 | GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& properties) : |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 16 | fFallbackTextContext(NULL), |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 17 | fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) { |
| 18 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 19 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 20 | GrTextContext::~GrTextContext() { |
| 21 | SkDELETE(fFallbackTextContext); |
| 22 | } |
| 23 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 24 | void GrTextContext::init(const GrPaint& grPaint, const SkPaint& skPaint) { |
| 25 | const GrClipData* clipData = fContext->getClip(); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 26 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 27 | SkRect devConservativeBound; |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 28 | clipData->fClipStack->getConservativeBounds( |
| 29 | -clipData->fOrigin.fX, |
| 30 | -clipData->fOrigin.fY, |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 31 | fContext->getRenderTarget()->width(), |
| 32 | fContext->getRenderTarget()->height(), |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 33 | &devConservativeBound); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 34 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 35 | devConservativeBound.roundOut(&fClipRect); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 37 | fDrawTarget = fContext->getTextTarget(); |
| 38 | |
| 39 | fPaint = grPaint; |
| 40 | fSkPaint = skPaint; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | } |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 42 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 43 | bool GrTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame^] | 44 | const SkMatrix& viewMatrix, |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 45 | const char text[], size_t byteLength, |
| 46 | SkScalar x, SkScalar y) { |
| 47 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 48 | GrTextContext* textContext = this; |
| 49 | do { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame^] | 50 | if (textContext->canDraw(skPaint, viewMatrix)) { |
| 51 | textContext->onDrawText(paint, skPaint, viewMatrix, text, byteLength, x, y); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | textContext = textContext->fFallbackTextContext; |
| 55 | } while (textContext); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 56 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 57 | return false; |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool GrTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame^] | 61 | const SkMatrix& viewMatrix, |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 62 | const char text[], size_t byteLength, |
| 63 | const SkScalar pos[], int scalarsPerPosition, |
| 64 | const SkPoint& offset) { |
| 65 | |
| 66 | GrTextContext* textContext = this; |
| 67 | do { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame^] | 68 | if (textContext->canDraw(skPaint, viewMatrix)) { |
| 69 | textContext->onDrawPosText(paint, skPaint, viewMatrix, text, byteLength, pos, |
| 70 | scalarsPerPosition, offset); |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | textContext = textContext->fFallbackTextContext; |
| 74 | } while (textContext); |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 80 | //*** change to output positions? |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 81 | int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 82 | const char text[], size_t byteLength, SkVector* stopVector) { |
| 83 | SkFixed x = 0, y = 0; |
| 84 | const char* stop = text + byteLength; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 85 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 86 | SkAutoKern autokern; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 87 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 88 | int numGlyphs = 0; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 89 | while (text < stop) { |
| 90 | // don't need x, y here, since all subpixel variants will have the |
| 91 | // same advance |
| 92 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 93 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 94 | x += autokern.adjust(glyph) + glyph.fAdvanceX; |
| 95 | y += glyph.fAdvanceY; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 96 | ++numGlyphs; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 97 | } |
| 98 | stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y)); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 99 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 100 | SkASSERT(text == stop); |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 101 | |
| 102 | return numGlyphs; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void GlyphCacheAuxProc(void* data) { |
| 106 | GrFontScaler* scaler = (GrFontScaler*)data; |
| 107 | SkSafeUnref(scaler); |
| 108 | } |
| 109 | |
| 110 | GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) { |
| 111 | void* auxData; |
| 112 | GrFontScaler* scaler = NULL; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 114 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 115 | scaler = (GrFontScaler*)auxData; |
| 116 | } |
| 117 | if (NULL == scaler) { |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 118 | scaler = SkNEW_ARGS(GrFontScaler, (cache)); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 119 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 120 | } |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 121 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 122 | return scaler; |
| 123 | } |