blob: cef99c6c45de33479ddccfec1f2e30ae7eb8a218 [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"
joshualitt570d2f82015-02-25 13:19:48 -080010#include "GrDrawTarget.h"
11#include "GrFontScaler.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000012
13#include "SkAutoKern.h"
14#include "SkGlyphCache.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000016GrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& properties) :
jvanverth8c27a182014-10-14 08:45:50 -070017 fFallbackTextContext(NULL),
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000018 fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) {
19}
tomhudson@google.com375ff852012-06-29 18:37:57 +000020
jvanverth8c27a182014-10-14 08:45:50 -070021GrTextContext::~GrTextContext() {
22 SkDELETE(fFallbackTextContext);
23}
24
joshualitt570d2f82015-02-25 13:19:48 -080025void GrTextContext::init(GrRenderTarget* rt, const GrClip& clip, const GrPaint& grPaint,
26 const SkPaint& skPaint) {
27 fClip = clip;
robertphillips@google.combeb1af72012-07-26 18:52:16 +000028
joshualitt25d9c152015-02-18 12:29:52 -080029 fRenderTarget.reset(SkRef(rt));
30
joshualitt570d2f82015-02-25 13:19:48 -080031 fClip.getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000032
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000033 fDrawTarget = fContext->getTextTarget();
34
35 fPaint = grPaint;
36 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000037}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000038
joshualitt570d2f82015-02-25 13:19:48 -080039bool GrTextContext::drawText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
40 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070041 const char text[], size_t byteLength,
42 SkScalar x, SkScalar y) {
43
jvanverthaab626c2014-10-16 08:04:39 -070044 GrTextContext* textContext = this;
45 do {
joshualitt5531d512014-12-17 15:50:11 -080046 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt570d2f82015-02-25 13:19:48 -080047 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y);
jvanverthaab626c2014-10-16 08:04:39 -070048 return true;
49 }
50 textContext = textContext->fFallbackTextContext;
51 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070052
jvanverthaab626c2014-10-16 08:04:39 -070053 return false;
jvanverth8c27a182014-10-14 08:45:50 -070054}
55
joshualitt570d2f82015-02-25 13:19:48 -080056bool GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
57 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070058 const char text[], size_t byteLength,
59 const SkScalar pos[], int scalarsPerPosition,
60 const SkPoint& offset) {
61
62 GrTextContext* textContext = this;
63 do {
joshualitt5531d512014-12-17 15:50:11 -080064 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt570d2f82015-02-25 13:19:48 -080065 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, pos,
joshualitt5531d512014-12-17 15:50:11 -080066 scalarsPerPosition, offset);
jvanverth8c27a182014-10-14 08:45:50 -070067 return true;
68 }
69 textContext = textContext->fFallbackTextContext;
70 } while (textContext);
71
72 return false;
73}
74
75
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000076//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -070077int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000078 const char text[], size_t byteLength, SkVector* stopVector) {
79 SkFixed x = 0, y = 0;
80 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000081
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000082 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000083
jvanverth73f10532014-10-23 11:57:12 -070084 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000085 while (text < stop) {
86 // don't need x, y here, since all subpixel variants will have the
87 // same advance
88 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000089
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000090 x += autokern.adjust(glyph) + glyph.fAdvanceX;
91 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -070092 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000093 }
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);
jvanverth73f10532014-10-23 11:57:12 -070097
98 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000099}
100
101static void GlyphCacheAuxProc(void* data) {
102 GrFontScaler* scaler = (GrFontScaler*)data;
103 SkSafeUnref(scaler);
104}
105
106GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
107 void* auxData;
108 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000109
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000110 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
111 scaler = (GrFontScaler*)auxData;
112 }
113 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700114 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000115 cache->setAuxProc(GlyphCacheAuxProc, scaler);
116 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000117
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000118 return scaler;
119}