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 "GrFontScaler.h"
14
15GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& properties) :
16                            fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) {
17}
18
19void GrTextContext::init(const GrPaint& grPaint, const SkPaint& skPaint) {
20    const GrClipData* clipData = fContext->getClip();
21
22    SkRect devConservativeBound;
23    clipData->fClipStack->getConservativeBounds(
24                                     -clipData->fOrigin.fX,
25                                     -clipData->fOrigin.fY,
26                                     fContext->getRenderTarget()->width(),
27                                     fContext->getRenderTarget()->height(),
28                                     &devConservativeBound);
29
30    devConservativeBound.roundOut(&fClipRect);
31
32    fDrawTarget = fContext->getTextTarget();
33
34    fPaint = grPaint;
35    fSkPaint = skPaint;
36}
37
38//*** change to output positions?
39void 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;
43
44    SkAutoKern  autokern;
45
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);
50
51        x += autokern.adjust(glyph) + glyph.fAdvanceX;
52        y += glyph.fAdvanceY;
53    }
54    stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
55
56    SkASSERT(text == stop);
57}
58
59static void GlyphCacheAuxProc(void* data) {
60    GrFontScaler* scaler = (GrFontScaler*)data;
61    SkSafeUnref(scaler);
62}
63
64GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
65    void* auxData;
66    GrFontScaler* scaler = NULL;
67
68    if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
69        scaler = (GrFontScaler*)auxData;
70    }
71    if (NULL == scaler) {
72        scaler = SkNEW_ARGS(GrFontScaler, (cache));
73        cache->setAuxProc(GlyphCacheAuxProc, scaler);
74    }
75
76    return scaler;
77}
78