blob: bc116714dad02b1d2a5393518bc9bd918cb5e451 [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.orgcbbc4812014-01-30 22:05:47 +000015GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& properties) :
16 fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) {
17}
tomhudson@google.com375ff852012-06-29 18:37:57 +000018
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000019void GrTextContext::init(const GrPaint& grPaint, const SkPaint& skPaint) {
20 const GrClipData* clipData = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +000021
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,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000026 fContext->getRenderTarget()->width(),
27 fContext->getRenderTarget()->height(),
robertphillips@google.com641f8b12012-07-31 19:15:58 +000028 &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.orgcbbc4812014-01-30 22:05:47 +000032 fDrawTarget = fContext->getTextTarget();
33
34 fPaint = grPaint;
35 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000036}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000037
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;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000043
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000044 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000045
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000046 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);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000050
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000051 x += autokern.adjust(glyph) + glyph.fAdvanceX;
52 y += glyph.fAdvanceY;
53 }
54 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000055
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000056 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;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000067
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000068 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
69 scaler = (GrFontScaler*)auxData;
70 }
71 if (NULL == scaler) {
72 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
73 cache->setAuxProc(GlyphCacheAuxProc, scaler);
74 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000075
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000076 return scaler;
77}