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" |
| 13 | #include "SkGr.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) : |
| 16 | fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) { |
| 17 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 18 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame^] | 19 | void GrTextContext::init(const GrPaint& grPaint, const SkPaint& skPaint) { |
| 20 | const GrClipData* clipData = fContext->getClip(); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 21 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 22 | SkRect devConservativeBound; |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 23 | clipData->fClipStack->getConservativeBounds( |
| 24 | -clipData->fOrigin.fX, |
| 25 | -clipData->fOrigin.fY, |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame^] | 26 | fContext->getRenderTarget()->width(), |
| 27 | fContext->getRenderTarget()->height(), |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 28 | &devConservativeBound); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 29 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 30 | devConservativeBound.roundOut(&fClipRect); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame^] | 32 | fDrawTarget = fContext->getTextTarget(); |
| 33 | |
| 34 | fPaint = grPaint; |
| 35 | fSkPaint = skPaint; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | } |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 37 | |
| 38 | //*** change to output positions? |
| 39 | void GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, |
| 40 | const char text[], size_t byteLength, SkVector* stopVector) { |
| 41 | SkFixed x = 0, y = 0; |
| 42 | const char* stop = text + byteLength; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 43 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 44 | SkAutoKern autokern; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 45 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 46 | while (text < stop) { |
| 47 | // don't need x, y here, since all subpixel variants will have the |
| 48 | // same advance |
| 49 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 50 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 51 | x += autokern.adjust(glyph) + glyph.fAdvanceX; |
| 52 | y += glyph.fAdvanceY; |
| 53 | } |
| 54 | stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y)); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 56 | SkASSERT(text == stop); |
| 57 | } |
| 58 | |
| 59 | static void GlyphCacheAuxProc(void* data) { |
| 60 | GrFontScaler* scaler = (GrFontScaler*)data; |
| 61 | SkSafeUnref(scaler); |
| 62 | } |
| 63 | |
| 64 | GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) { |
| 65 | void* auxData; |
| 66 | GrFontScaler* scaler = NULL; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 67 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 68 | if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { |
| 69 | scaler = (GrFontScaler*)auxData; |
| 70 | } |
| 71 | if (NULL == scaler) { |
| 72 | scaler = SkNEW_ARGS(SkGrFontScaler, (cache)); |
| 73 | cache->setAuxProc(GlyphCacheAuxProc, scaler); |
| 74 | } |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 76 | return scaler; |
| 77 | } |