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, |
| 44 | const char text[], size_t byteLength, |
| 45 | SkScalar x, SkScalar y) { |
| 46 | |
| 47 | GrTextContext* textContext = this; |
| 48 | do { |
| 49 | if (textContext->canDraw(skPaint)) { |
| 50 | textContext->onDrawText(paint, skPaint, text, byteLength, x, y); |
| 51 | return true; |
| 52 | } |
| 53 | textContext = textContext->fFallbackTextContext; |
| 54 | } while (textContext); |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | bool GrTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint, |
| 60 | const char text[], size_t byteLength, |
| 61 | const SkScalar pos[], int scalarsPerPosition, |
| 62 | const SkPoint& offset) { |
| 63 | |
| 64 | GrTextContext* textContext = this; |
| 65 | do { |
| 66 | if (textContext->canDraw(skPaint)) { |
| 67 | textContext->onDrawPosText(paint, skPaint, text, byteLength, pos, scalarsPerPosition, |
| 68 | offset); |
| 69 | return true; |
| 70 | } |
| 71 | textContext = textContext->fFallbackTextContext; |
| 72 | } while (textContext); |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 78 | //*** change to output positions? |
| 79 | void GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, |
| 80 | const char text[], size_t byteLength, SkVector* stopVector) { |
| 81 | SkFixed x = 0, y = 0; |
| 82 | const char* stop = text + byteLength; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 84 | SkAutoKern autokern; |
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 | while (text < stop) { |
| 87 | // don't need x, y here, since all subpixel variants will have the |
| 88 | // same advance |
| 89 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 90 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 91 | x += autokern.adjust(glyph) + glyph.fAdvanceX; |
| 92 | y += glyph.fAdvanceY; |
| 93 | } |
| 94 | stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y)); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 95 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 96 | SkASSERT(text == stop); |
| 97 | } |
| 98 | |
| 99 | static void GlyphCacheAuxProc(void* data) { |
| 100 | GrFontScaler* scaler = (GrFontScaler*)data; |
| 101 | SkSafeUnref(scaler); |
| 102 | } |
| 103 | |
| 104 | GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) { |
| 105 | void* auxData; |
| 106 | GrFontScaler* scaler = NULL; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 108 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 109 | scaler = (GrFontScaler*)auxData; |
| 110 | } |
| 111 | if (NULL == scaler) { |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 112 | scaler = SkNEW_ARGS(GrFontScaler, (cache)); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 113 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 114 | } |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 115 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 116 | return scaler; |
| 117 | } |