michael@0: /* michael@0: * Copyright 2010 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrTextContext.h" michael@0: #include "GrContext.h" michael@0: michael@0: #include "SkAutoKern.h" michael@0: #include "SkGlyphCache.h" michael@0: #include "SkGr.h" michael@0: michael@0: GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& properties) : michael@0: fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) { michael@0: } michael@0: michael@0: void GrTextContext::init(const GrPaint& grPaint, const SkPaint& skPaint) { michael@0: const GrClipData* clipData = fContext->getClip(); michael@0: michael@0: SkRect devConservativeBound; michael@0: clipData->fClipStack->getConservativeBounds( michael@0: -clipData->fOrigin.fX, michael@0: -clipData->fOrigin.fY, michael@0: fContext->getRenderTarget()->width(), michael@0: fContext->getRenderTarget()->height(), michael@0: &devConservativeBound); michael@0: michael@0: devConservativeBound.roundOut(&fClipRect); michael@0: michael@0: fDrawTarget = fContext->getTextTarget(); michael@0: michael@0: fPaint = grPaint; michael@0: fSkPaint = skPaint; michael@0: } michael@0: michael@0: //*** change to output positions? michael@0: void GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, michael@0: const char text[], size_t byteLength, SkVector* stopVector) { michael@0: SkFixed x = 0, y = 0; michael@0: const char* stop = text + byteLength; michael@0: michael@0: SkAutoKern autokern; michael@0: michael@0: while (text < stop) { michael@0: // don't need x, y here, since all subpixel variants will have the michael@0: // same advance michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); michael@0: michael@0: x += autokern.adjust(glyph) + glyph.fAdvanceX; michael@0: y += glyph.fAdvanceY; michael@0: } michael@0: stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y)); michael@0: michael@0: SkASSERT(text == stop); michael@0: } michael@0: michael@0: static void GlyphCacheAuxProc(void* data) { michael@0: GrFontScaler* scaler = (GrFontScaler*)data; michael@0: SkSafeUnref(scaler); michael@0: } michael@0: michael@0: GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) { michael@0: void* auxData; michael@0: GrFontScaler* scaler = NULL; michael@0: michael@0: if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) { michael@0: scaler = (GrFontScaler*)auxData; michael@0: } michael@0: if (NULL == scaler) { michael@0: scaler = SkNEW_ARGS(SkGrFontScaler, (cache)); michael@0: cache->setAuxProc(GlyphCacheAuxProc, scaler); michael@0: } michael@0: michael@0: return scaler; michael@0: }