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