blob: 9b26a9f00bba00c71963fe0a26d2ece4fc7c5fd8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkTextBox.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010
11static inline int is_ws(int c)
12{
13 return !((c - 1) >> 5);
14}
15
bungeman@google.com0d50bc12012-09-06 14:46:30 +000016static size_t linebreak(const char text[], const char stop[],
17 const SkPaint& paint, SkScalar margin,
halcanary96fcdcc2015-08-27 07:41:13 -070018 size_t* trailing = nullptr)
reed@android.com8a1c16f2008-12-17 15:59:43 +000019{
bungeman@google.com0d50bc12012-09-06 14:46:30 +000020 size_t lengthBreak = paint.breakText(text, stop - text, margin);
21
22 //Check for white space or line breakers before the lengthBreak
23 const char* start = text;
24 const char* word_start = text;
25 int prevWS = true;
26 if (trailing) {
27 *trailing = 0;
28 }
29
30 while (text < stop) {
31 const char* prevText = text;
32 SkUnichar uni = SkUTF8_NextUnichar(&text);
33 int currWS = is_ws(uni);
34
35 if (!currWS && prevWS) {
36 word_start = prevText;
37 }
38 prevWS = currWS;
39
40 if (text > start + lengthBreak) {
41 if (currWS) {
42 // eat the rest of the whitespace
43 while (text < stop && is_ws(SkUTF8_ToUnichar(text))) {
44 text += SkUTF8_CountUTF8Bytes(text);
45 }
46 if (trailing) {
47 *trailing = text - prevText;
48 }
49 } else {
50 // backup until a whitespace (or 1 char)
51 if (word_start == start) {
52 if (prevText > start) {
53 text = prevText;
54 }
55 } else {
56 text = word_start;
57 }
58 }
59 break;
60 }
61
62 if ('\n' == uni) {
63 size_t ret = text - start;
64 size_t lineBreakSize = 1;
65 if (text < stop) {
66 uni = SkUTF8_NextUnichar(&text);
67 if ('\r' == uni) {
68 ret = text - start;
69 ++lineBreakSize;
70 }
71 }
72 if (trailing) {
73 *trailing = lineBreakSize;
74 }
75 return ret;
76 }
77
78 if ('\r' == uni) {
79 size_t ret = text - start;
80 size_t lineBreakSize = 1;
81 if (text < stop) {
82 uni = SkUTF8_NextUnichar(&text);
83 if ('\n' == uni) {
84 ret = text - start;
85 ++lineBreakSize;
86 }
87 }
88 if (trailing) {
89 *trailing = lineBreakSize;
90 }
91 return ret;
92 }
93 }
94
95 return text - start;
reed@android.com8a1c16f2008-12-17 15:59:43 +000096}
97
98int SkTextLineBreaker::CountLines(const char text[], size_t len, const SkPaint& paint, SkScalar width)
99{
100 const char* stop = text + len;
101 int count = 0;
102
103 if (width > 0)
104 {
105 do {
106 count += 1;
107 text += linebreak(text, stop, paint, width);
108 } while (text < stop);
109 }
110 return count;
111}
112
113//////////////////////////////////////////////////////////////////////////////
114
115SkTextBox::SkTextBox()
116{
117 fBox.setEmpty();
118 fSpacingMul = SK_Scalar1;
119 fSpacingAdd = 0;
120 fMode = kLineBreak_Mode;
121 fSpacingAlign = kStart_SpacingAlign;
122}
123
124void SkTextBox::setMode(Mode mode)
125{
126 SkASSERT((unsigned)mode < kModeCount);
127 fMode = SkToU8(mode);
128}
129
130void SkTextBox::setSpacingAlign(SpacingAlign align)
131{
132 SkASSERT((unsigned)align < kSpacingAlignCount);
133 fSpacingAlign = SkToU8(align);
134}
135
136void SkTextBox::getBox(SkRect* box) const
137{
138 if (box)
139 *box = fBox;
140}
141
142void SkTextBox::setBox(const SkRect& box)
143{
144 fBox = box;
145}
146
147void SkTextBox::setBox(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom)
148{
149 fBox.set(left, top, right, bottom);
150}
151
152void SkTextBox::getSpacing(SkScalar* mul, SkScalar* add) const
153{
154 if (mul)
155 *mul = fSpacingMul;
156 if (add)
157 *add = fSpacingAdd;
158}
159
160void SkTextBox::setSpacing(SkScalar mul, SkScalar add)
161{
162 fSpacingMul = mul;
163 fSpacingAdd = add;
164}
165
166/////////////////////////////////////////////////////////////////////////////////////////////
167
reed1b6ab442014-11-03 19:55:41 -0800168SkScalar SkTextBox::visit(Visitor& visitor, const char text[], size_t len,
169 const SkPaint& paint) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 SkScalar marginWidth = fBox.width();
171
reed1b6ab442014-11-03 19:55:41 -0800172 if (marginWidth <= 0 || len == 0) {
173 return fBox.top();
174 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175
176 const char* textStop = text + len;
177
178 SkScalar x, y, scaledSpacing, height, fontHeight;
179 SkPaint::FontMetrics metrics;
180
181 switch (paint.getTextAlign()) {
182 case SkPaint::kLeft_Align:
183 x = 0;
184 break;
185 case SkPaint::kCenter_Align:
186 x = SkScalarHalf(marginWidth);
187 break;
188 default:
189 x = marginWidth;
190 break;
191 }
192 x += fBox.fLeft;
193
194 fontHeight = paint.getFontMetrics(&metrics);
Mike Reeda99b6ce2017-02-04 11:04:26 -0500195 scaledSpacing = fontHeight * fSpacingMul + fSpacingAdd;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 height = fBox.height();
197
198 // compute Y position for first line
199 {
200 SkScalar textHeight = fontHeight;
201
reed1b6ab442014-11-03 19:55:41 -0800202 if (fMode == kLineBreak_Mode && fSpacingAlign != kStart_SpacingAlign) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 int count = SkTextLineBreaker::CountLines(text, textStop - text, paint, marginWidth);
204 SkASSERT(count > 0);
205 textHeight += scaledSpacing * (count - 1);
206 }
207
208 switch (fSpacingAlign) {
209 case kStart_SpacingAlign:
210 y = 0;
211 break;
212 case kCenter_SpacingAlign:
213 y = SkScalarHalf(height - textHeight);
214 break;
215 default:
216 SkASSERT(fSpacingAlign == kEnd_SpacingAlign);
217 y = height - textHeight;
218 break;
219 }
220 y += fBox.fTop - metrics.fAscent;
221 }
222
reed1b6ab442014-11-03 19:55:41 -0800223 for (;;) {
bungeman@google.com0d50bc12012-09-06 14:46:30 +0000224 size_t trailing;
225 len = linebreak(text, textStop, paint, marginWidth, &trailing);
reed1b6ab442014-11-03 19:55:41 -0800226 if (y + metrics.fDescent + metrics.fLeading > 0) {
227 visitor(text, len - trailing, x, y, paint);
228 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 text += len;
reed1b6ab442014-11-03 19:55:41 -0800230 if (text >= textStop) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 break;
reed1b6ab442014-11-03 19:55:41 -0800232 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 y += scaledSpacing;
reed1b6ab442014-11-03 19:55:41 -0800234 if (y + metrics.fAscent >= fBox.fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 break;
reed1b6ab442014-11-03 19:55:41 -0800236 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000237 }
reed1b6ab442014-11-03 19:55:41 -0800238 return y + metrics.fDescent + metrics.fLeading;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239}
240
reed@android.com033e03c2010-05-18 21:17:43 +0000241///////////////////////////////////////////////////////////////////////////////
242
reed1b6ab442014-11-03 19:55:41 -0800243class CanvasVisitor : public SkTextBox::Visitor {
244 SkCanvas* fCanvas;
245public:
246 CanvasVisitor(SkCanvas* canvas) : fCanvas(canvas) {}
tfarina283b5872015-04-25 12:59:44 -0700247
248 void operator()(const char text[], size_t length, SkScalar x, SkScalar y,
249 const SkPaint& paint) override {
reed1b6ab442014-11-03 19:55:41 -0800250 fCanvas->drawText(text, length, x, y, paint);
251 }
252};
253
reed@android.com033e03c2010-05-18 21:17:43 +0000254void SkTextBox::setText(const char text[], size_t len, const SkPaint& paint) {
255 fText = text;
256 fLen = len;
257 fPaint = &paint;
258}
259
reed1b6ab442014-11-03 19:55:41 -0800260void SkTextBox::draw(SkCanvas* canvas, const char text[], size_t len, const SkPaint& paint) {
261 CanvasVisitor sink(canvas);
262 this->visit(sink, text, len, paint);
263}
264
reed@android.com033e03c2010-05-18 21:17:43 +0000265void SkTextBox::draw(SkCanvas* canvas) {
266 this->draw(canvas, fText, fLen, *fPaint);
267}
268
269int SkTextBox::countLines() const {
270 return SkTextLineBreaker::CountLines(fText, fLen, *fPaint, fBox.width());
271}
272
273SkScalar SkTextBox::getTextHeight() const {
Mike Reeda99b6ce2017-02-04 11:04:26 -0500274 SkScalar spacing = fPaint->getTextSize() * fSpacingMul + fSpacingAdd;
reed@android.com033e03c2010-05-18 21:17:43 +0000275 return this->countLines() * spacing;
276}
reed1b6ab442014-11-03 19:55:41 -0800277
278///////////////////////////////////////////////////////////////////////////////
279
280#include "SkTextBlob.h"
281
282class TextBlobVisitor : public SkTextBox::Visitor {
283public:
284 SkTextBlobBuilder fBuilder;
tfarina283b5872015-04-25 12:59:44 -0700285
286 void operator()(const char text[], size_t length, SkScalar x, SkScalar y,
287 const SkPaint& paint) override {
reed1b6ab442014-11-03 19:55:41 -0800288 SkPaint p(paint);
289 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
290 const int count = paint.countText(text, length);
291 paint.textToGlyphs(text, length, fBuilder.allocRun(p, count, x, y).glyphs);
292 }
293};
294
fmalita37283c22016-09-13 10:00:23 -0700295sk_sp<SkTextBlob> SkTextBox::snapshotTextBlob(SkScalar* computedBottom) const {
reed1b6ab442014-11-03 19:55:41 -0800296 TextBlobVisitor visitor;
297 SkScalar newB = this->visit(visitor, fText, fLen, *fPaint);
298 if (computedBottom) {
299 *computedBottom = newB;
300 }
fmalita37283c22016-09-13 10:00:23 -0700301 return visitor.fBuilder.make();
reed1b6ab442014-11-03 19:55:41 -0800302}