blob: 895e97752b81fc1e322fbba4349cbc15e6ad6149 [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"
joshualitt29070592015-02-25 13:04:43 -080013#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
joshualitt29070592015-02-25 13:04:43 -080024void GrTextContext::init(GrRenderTarget* rt, const GrPaint& grPaint, const SkPaint& skPaint) {
25 fClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +000026
joshualitt25d9c152015-02-18 12:29:52 -080027 fRenderTarget.reset(SkRef(rt));
28
joshualitt29070592015-02-25 13:04:43 -080029 fClip->getConservativeBounds(fRenderTarget->width(), fRenderTarget->height(), &fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000030
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000031 fDrawTarget = fContext->getTextTarget();
32
33 fPaint = grPaint;
34 fSkPaint = skPaint;
reed@google.comac10a2d2010-12-22 21:39:39 +000035}
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000036
joshualitt29070592015-02-25 13:04:43 -080037bool GrTextContext::drawText(GrRenderTarget* rt, const GrPaint& paint, const SkPaint& skPaint,
38 const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070039 const char text[], size_t byteLength,
40 SkScalar x, SkScalar y) {
41
jvanverthaab626c2014-10-16 08:04:39 -070042 GrTextContext* textContext = this;
43 do {
joshualitt5531d512014-12-17 15:50:11 -080044 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt29070592015-02-25 13:04:43 -080045 textContext->onDrawText(rt, paint, skPaint, viewMatrix, text, byteLength, x, y);
jvanverthaab626c2014-10-16 08:04:39 -070046 return true;
47 }
48 textContext = textContext->fFallbackTextContext;
49 } while (textContext);
jvanverth8c27a182014-10-14 08:45:50 -070050
jvanverthaab626c2014-10-16 08:04:39 -070051 return false;
jvanverth8c27a182014-10-14 08:45:50 -070052}
53
joshualitt29070592015-02-25 13:04:43 -080054bool GrTextContext::drawPosText(GrRenderTarget* rt, const GrPaint& paint, const SkPaint& skPaint,
55 const SkMatrix& viewMatrix,
jvanverth8c27a182014-10-14 08:45:50 -070056 const char text[], size_t byteLength,
57 const SkScalar pos[], int scalarsPerPosition,
58 const SkPoint& offset) {
59
60 GrTextContext* textContext = this;
61 do {
joshualitt5531d512014-12-17 15:50:11 -080062 if (textContext->canDraw(skPaint, viewMatrix)) {
joshualitt29070592015-02-25 13:04:43 -080063 textContext->onDrawPosText(rt, paint, skPaint, viewMatrix, text, byteLength, pos,
joshualitt5531d512014-12-17 15:50:11 -080064 scalarsPerPosition, offset);
jvanverth8c27a182014-10-14 08:45:50 -070065 return true;
66 }
67 textContext = textContext->fFallbackTextContext;
68 } while (textContext);
69
70 return false;
71}
72
73
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000074//*** change to output positions?
jvanverth73f10532014-10-23 11:57:12 -070075int GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000076 const char text[], size_t byteLength, SkVector* stopVector) {
77 SkFixed x = 0, y = 0;
78 const char* stop = text + byteLength;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000079
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000080 SkAutoKern autokern;
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000081
jvanverth73f10532014-10-23 11:57:12 -070082 int numGlyphs = 0;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000083 while (text < stop) {
84 // don't need x, y here, since all subpixel variants will have the
85 // same advance
86 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000087
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000088 x += autokern.adjust(glyph) + glyph.fAdvanceX;
89 y += glyph.fAdvanceY;
jvanverth73f10532014-10-23 11:57:12 -070090 ++numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000091 }
92 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000093
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000094 SkASSERT(text == stop);
jvanverth73f10532014-10-23 11:57:12 -070095
96 return numGlyphs;
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000097}
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}