blob: ab9ef85f1d1972d7e966948bec9d30755f2473bf [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com375ff852012-06-29 18:37:57 +00008#include "GrTextContext.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +00009#include "GrContext.h"
10
11#include "SkAutoKern.h"
12#include "SkGlyphCache.h"
13#include "SkGr.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +000015GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000016 const SkPaint& skPaint, const SkDeviceProperties& properties) :
17 fContext(context), fPaint(paint), fSkPaint(skPaint),
18 fDeviceProperties(properties) {
tomhudson@google.com375ff852012-06-29 18:37:57 +000019
robertphillips@google.combeb1af72012-07-26 18:52:16 +000020 const GrClipData* clipData = context->getClip();
21
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000022 SkRect devConservativeBound;
robertphillips@google.com641f8b12012-07-31 19:15:58 +000023 clipData->fClipStack->getConservativeBounds(
24 -clipData->fOrigin.fX,
25 -clipData->fOrigin.fY,
26 context->getRenderTarget()->width(),
27 context->getRenderTarget()->height(),
28 &devConservativeBound);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000029
robertphillips@google.com641f8b12012-07-31 19:15:58 +000030 devConservativeBound.roundOut(&fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000031
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000032 fDrawTarget = context->getTextTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +000033}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000034
35//*** change to output positions?
36void 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;
40
41 SkAutoKern autokern;
42
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);
47
48 x += autokern.adjust(glyph) + glyph.fAdvanceX;
49 y += glyph.fAdvanceY;
50 }
51 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
52
53 SkASSERT(text == stop);
54}
55
56static void GlyphCacheAuxProc(void* data) {
57 GrFontScaler* scaler = (GrFontScaler*)data;
58 SkSafeUnref(scaler);
59}
60
61GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
62 void* auxData;
63 GrFontScaler* scaler = NULL;
64
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 }
72
73 return scaler;
74}
75