blob: ad5e7c0aa5e1e21347641625d5fb982895adbbe9 [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) {
joshualitt5f5a8d72015-02-25 14:09:45 -080043 if (!fContext->getTextTarget()) {
44 return false;
45 }
jvanverth8c27a182014-10-14 08:45:50 -070046
jvanverthaab626c2014-10-16 08:04:39 -070047 GrTextContext* textContext = this;
48 do {
joshualitt5531d512014-12-17 15:50:11 -080049 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt570d2f82015-02-25 13:19:48 -080050 textContext->onDrawText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, x, y);
jvanverthaab626c2014-10-16 08:04:39 -070051 return true;
52 }
53 textContext = textContext->fFallbackTextContext;
54 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070055
jvanverthaab626c2014-10-16 08:04:39 -070056 return false;
jvanverth8c27a182014-10-14 08:45:50 -070057}
58
joshualitt570d2f82015-02-25 13:19:48 -080059bool GrTextContext::drawPosText(GrRenderTarget* rt, const GrClip& clip, const GrPaint& paint,
60 const SkPaint& skPaint, const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070061 const char text[], size_t byteLength,
62 const SkScalar pos[], int scalarsPerPosition,
63 const SkPoint& offset) {
joshualitt5f5a8d72015-02-25 14:09:45 -080064 if (!fContext->getTextTarget()) {
65 return false;
66 }
jvanverth8c27a182014-10-14 08:45:50 -070067
68 GrTextContext* textContext = this;
69 do {
joshualitt5531d512014-12-17 15:50:11 -080070 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt570d2f82015-02-25 13:19:48 -080071 textContext->onDrawPosText(rt, clip, paint, skPaint, viewMatrix, text, byteLength, pos,
joshualitt5531d512014-12-17 15:50:11 -080072 scalarsPerPosition, offset);
jvanverth8c27a182014-10-14 08:45:50 -070073 return true;
74 }
75 textContext = textContext->fFallbackTextContext;
76 } while (textContext);
77
78 return false;
79}
80
81
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000082//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -070083int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000084 const char text[], size_t byteLength, SkVector* stopVector) {
85 SkFixed x = 0, y = 0;
86 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000087
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000088 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000089
jvanverth73f10532014-10-23 11:57:12 -070090 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000091 while (text < stop) {
92 // don't need x, y here, since all subpixel variants will have the
93 // same advance
94 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000095
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000096 x += autokern.adjust(glyph) + glyph.fAdvanceX;
97 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -070098 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000099 }
100 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000101
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000102 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -0700103
104 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000105}
106
107static void GlyphCacheAuxProc(void* data) {
108 GrFontScaler* scaler = (GrFontScaler*)data;
109 SkSafeUnref(scaler);
110}
111
112GrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
113 void* auxData;
114 GrFontScaler* scaler = NULL;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000115
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000116 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
117 scaler = (GrFontScaler*)auxData;
118 }
119 if (NULL == scaler) {
jvanverth733f5f52014-07-11 19:45:16 -0700120 scaler = SkNEW_ARGS(GrFontScaler, (cache));
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000121 cache->setAuxProc(GlyphCacheAuxProc, scaler);
122 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000123
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000124 return scaler;
125}