GrTextContext.cpp revision e5d7015cde3b6f2a3929b8e378822e3d7be223d4
1/*
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.
6 */
7
8#include "GrTextContext.h"
9#include "GrContext.h"
10
11#include "SkAutoKern.h"
12#include "SkGlyphCache.h"
13#include "SkGr.h"
14
15GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint,
16                             const SkPaint& skPaint, const SkDeviceProperties& properties) :
17                            fContext(context), fPaint(paint), fSkPaint(skPaint),
18                            fDeviceProperties(properties) {
19
20    const GrClipData* clipData = context->getClip();
21
22    SkRect devConservativeBound;
23    clipData->fClipStack->getConservativeBounds(
24                                     -clipData->fOrigin.fX,
25                                     -clipData->fOrigin.fY,
26                                     context->getRenderTarget()->width(),
27                                     context->getRenderTarget()->height(),
28                                     &devConservativeBound);
29
30    devConservativeBound.roundOut(&fClipRect);
31
32    fDrawTarget = context->getTextTarget();
33}
34
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