2 * Copyright 2010 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrTextContext.h"
11 #include "SkAutoKern.h"
12 #include "SkGlyphCache.h"
13 #include "GrFontScaler.h"
15 GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& properties) :
16 fFallbackTextContext(NULL),
17 fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) {
20 GrTextContext::~GrTextContext() {
21 SkDELETE(fFallbackTextContext);
24 void GrTextContext::init(const GrPaint& grPaint, const SkPaint& skPaint) {
25 const GrClipData* clipData = fContext->getClip();
27 SkRect devConservativeBound;
28 clipData->fClipStack->getConservativeBounds(
29 -clipData->fOrigin.fX,
30 -clipData->fOrigin.fY,
31 fContext->getRenderTarget()->width(),
32 fContext->getRenderTarget()->height(),
33 &devConservativeBound);
35 devConservativeBound.roundOut(&fClipRect);
37 fDrawTarget = fContext->getTextTarget();
43 bool GrTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
44 const char text[], size_t byteLength,
45 SkScalar x, SkScalar y) {
47 GrTextContext* textContext = this;
49 if (textContext->canDraw(skPaint)) {
50 textContext->onDrawText(paint, skPaint, text, byteLength, x, y);
53 textContext = textContext->fFallbackTextContext;
54 } while (textContext);
59 bool GrTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
60 const char text[], size_t byteLength,
61 const SkScalar pos[], int scalarsPerPosition,
62 const SkPoint& offset) {
64 GrTextContext* textContext = this;
66 if (textContext->canDraw(skPaint)) {
67 textContext->onDrawPosText(paint, skPaint, text, byteLength, pos, scalarsPerPosition,
71 textContext = textContext->fFallbackTextContext;
72 } while (textContext);
78 //*** change to output positions?
79 int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
80 const char text[], size_t byteLength, SkVector* stopVector) {
82 const char* stop = text + byteLength;
88 // don't need x, y here, since all subpixel variants will have the
90 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
92 x += autokern.adjust(glyph) + glyph.fAdvanceX;
96 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
98 SkASSERT(text == stop);
103 static void GlyphCacheAuxProc(void* data) {
104 GrFontScaler* scaler = (GrFontScaler*)data;
108 GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
110 GrFontScaler* scaler = NULL;
112 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
113 scaler = (GrFontScaler*)auxData;
115 if (NULL == scaler) {
116 scaler = SkNEW_ARGS(GrFontScaler, (cache));
117 cache->setAuxProc(GlyphCacheAuxProc, scaler);