blob: 94c05a712439a8c8498c899a0059574df4c9e187 [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"
jvanverth733f5f52014-07-11 19:45:16 -070013#include "GrFontScaler.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) :
jvanverth8c27a182014-10-14 08:45:50 -070016 fFallbackTextContext(NULL),
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000017 fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) {
18}
tomhudson@google.com375ff852012-06-29 18:37:57 +000019
jvanverth8c27a182014-10-14 08:45:50 -070020GrTextContext::~GrTextContext() {
21 SkDELETE(fFallbackTextContext);
22}
23
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000024void GrTextContext::init(const GrPaint& grPaint, const SkPaint& skPaint) {
25 const GrClipData* clipData = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +000026
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000027 SkRect devConservativeBound;
robertphillips@google.com641f8b12012-07-31 19:15:58 +000028 clipData->fClipStack->getConservativeBounds(
29 -clipData->fOrigin.fX,
30 -clipData->fOrigin.fY,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000031 fContext->getRenderTarget()->width(),
32 fContext->getRenderTarget()->height(),
robertphillips@google.com641f8b12012-07-31 19:15:58 +000033 &devConservativeBound);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000034
robertphillips@google.com641f8b12012-07-31 19:15:58 +000035 devConservativeBound.roundOut(&fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000036
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000037 fDrawTarget = fContext->getTextTarget();
38
39 fPaint = grPaint;
40 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000041}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000042
jvanverth8c27a182014-10-14 08:45:50 -070043bool GrTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
44 const char text[], size_t byteLength,
45 SkScalar x, SkScalar y) {
46
47 GrTextContext* textContext = this;
48 do {
49 if (textContext->canDraw(skPaint)) {
50 textContext->onDrawText(paint, skPaint, text, byteLength, x, y);
51 return true;
52 }
53 textContext = textContext->fFallbackTextContext;
54 } while (textContext);
55
56 return false;
57}
58
59bool 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) {
63
64 GrTextContext* textContext = this;
65 do {
66 if (textContext->canDraw(skPaint)) {
67 textContext->onDrawPosText(paint, skPaint, text, byteLength, pos, scalarsPerPosition,
68 offset);
69 return true;
70 }
71 textContext = textContext->fFallbackTextContext;
72 } while (textContext);
73
74 return false;
75}
76
77
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000078//*** change to output positions?
79void GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
80 const char text[], size_t byteLength, SkVector* stopVector) {
81 SkFixed x = 0, y = 0;
82 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000083
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000084 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000085
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000086 while (text < stop) {
87 // don't need x, y here, since all subpixel variants will have the
88 // same advance
89 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000090
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000091 x += autokern.adjust(glyph) + glyph.fAdvanceX;
92 y += glyph.fAdvanceY;
93 }
94 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000095
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000096 SkASSERT(text == stop);
97}
98
99static void GlyphCacheAuxProc(void* data) {
100 GrFontScaler* scaler = (GrFontScaler*)data;
101 SkSafeUnref(scaler);
102}
103
104GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
105 void* auxData;
106 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000107
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000108 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
109 scaler = (GrFontScaler*)auxData;
110 }
111 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700112 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000113 cache->setAuxProc(GlyphCacheAuxProc, scaler);
114 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000115
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000116 return scaler;
117}