2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 static inline int is_ws(int c)
13 return !((c - 1) >> 5);
16 static size_t linebreak(const char text[], const char stop[],
17 const SkPaint& paint, SkScalar margin,
18 size_t* trailing = NULL)
20 size_t lengthBreak = paint.breakText(text, stop - text, margin);
22 //Check for white space or line breakers before the lengthBreak
23 const char* start = text;
24 const char* word_start = text;
31 const char* prevText = text;
32 SkUnichar uni = SkUTF8_NextUnichar(&text);
33 int currWS = is_ws(uni);
35 if (!currWS && prevWS) {
36 word_start = prevText;
40 if (text > start + lengthBreak) {
42 // eat the rest of the whitespace
43 while (text < stop && is_ws(SkUTF8_ToUnichar(text))) {
44 text += SkUTF8_CountUTF8Bytes(text);
47 *trailing = text - prevText;
50 // backup until a whitespace (or 1 char)
51 if (word_start == start) {
52 if (prevText > start) {
63 size_t ret = text - start;
64 size_t lineBreakSize = 1;
66 uni = SkUTF8_NextUnichar(&text);
73 *trailing = lineBreakSize;
79 size_t ret = text - start;
80 size_t lineBreakSize = 1;
82 uni = SkUTF8_NextUnichar(&text);
89 *trailing = lineBreakSize;
98 int SkTextLineBreaker::CountLines(const char text[], size_t len, const SkPaint& paint, SkScalar width)
100 const char* stop = text + len;
107 text += linebreak(text, stop, paint, width);
108 } while (text < stop);
113 //////////////////////////////////////////////////////////////////////////////
115 SkTextBox::SkTextBox()
118 fSpacingMul = SK_Scalar1;
120 fMode = kLineBreak_Mode;
121 fSpacingAlign = kStart_SpacingAlign;
124 void SkTextBox::setMode(Mode mode)
126 SkASSERT((unsigned)mode < kModeCount);
127 fMode = SkToU8(mode);
130 void SkTextBox::setSpacingAlign(SpacingAlign align)
132 SkASSERT((unsigned)align < kSpacingAlignCount);
133 fSpacingAlign = SkToU8(align);
136 void SkTextBox::getBox(SkRect* box) const
142 void SkTextBox::setBox(const SkRect& box)
147 void SkTextBox::setBox(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom)
149 fBox.set(left, top, right, bottom);
152 void SkTextBox::getSpacing(SkScalar* mul, SkScalar* add) const
160 void SkTextBox::setSpacing(SkScalar mul, SkScalar add)
166 /////////////////////////////////////////////////////////////////////////////////////////////
168 SkScalar SkTextBox::visit(Visitor& visitor, const char text[], size_t len,
169 const SkPaint& paint) const {
170 SkScalar marginWidth = fBox.width();
172 if (marginWidth <= 0 || len == 0) {
176 const char* textStop = text + len;
178 SkScalar x, y, scaledSpacing, height, fontHeight;
179 SkPaint::FontMetrics metrics;
181 switch (paint.getTextAlign()) {
182 case SkPaint::kLeft_Align:
185 case SkPaint::kCenter_Align:
186 x = SkScalarHalf(marginWidth);
194 fontHeight = paint.getFontMetrics(&metrics);
195 scaledSpacing = SkScalarMul(fontHeight, fSpacingMul) + fSpacingAdd;
196 height = fBox.height();
198 // compute Y position for first line
200 SkScalar textHeight = fontHeight;
202 if (fMode == kLineBreak_Mode && fSpacingAlign != kStart_SpacingAlign) {
203 int count = SkTextLineBreaker::CountLines(text, textStop - text, paint, marginWidth);
205 textHeight += scaledSpacing * (count - 1);
208 switch (fSpacingAlign) {
209 case kStart_SpacingAlign:
212 case kCenter_SpacingAlign:
213 y = SkScalarHalf(height - textHeight);
216 SkASSERT(fSpacingAlign == kEnd_SpacingAlign);
217 y = height - textHeight;
220 y += fBox.fTop - metrics.fAscent;
225 len = linebreak(text, textStop, paint, marginWidth, &trailing);
226 if (y + metrics.fDescent + metrics.fLeading > 0) {
227 visitor(text, len - trailing, x, y, paint);
230 if (text >= textStop) {
234 if (y + metrics.fAscent >= fBox.fBottom) {
238 return y + metrics.fDescent + metrics.fLeading;
241 ///////////////////////////////////////////////////////////////////////////////
243 class CanvasVisitor : public SkTextBox::Visitor {
246 CanvasVisitor(SkCanvas* canvas) : fCanvas(canvas) {}
248 virtual void operator()(const char text[], size_t length, SkScalar x, SkScalar y,
249 const SkPaint& paint) SK_OVERRIDE {
250 fCanvas->drawText(text, length, x, y, paint);
254 void SkTextBox::setText(const char text[], size_t len, const SkPaint& paint) {
260 void SkTextBox::draw(SkCanvas* canvas, const char text[], size_t len, const SkPaint& paint) {
261 CanvasVisitor sink(canvas);
262 this->visit(sink, text, len, paint);
265 void SkTextBox::draw(SkCanvas* canvas) {
266 this->draw(canvas, fText, fLen, *fPaint);
269 int SkTextBox::countLines() const {
270 return SkTextLineBreaker::CountLines(fText, fLen, *fPaint, fBox.width());
273 SkScalar SkTextBox::getTextHeight() const {
274 SkScalar spacing = SkScalarMul(fPaint->getTextSize(), fSpacingMul) + fSpacingAdd;
275 return this->countLines() * spacing;
278 ///////////////////////////////////////////////////////////////////////////////
280 #include "SkTextBlob.h"
282 class TextBlobVisitor : public SkTextBox::Visitor {
284 SkTextBlobBuilder fBuilder;
286 virtual void operator()(const char text[], size_t length, SkScalar x, SkScalar y,
287 const SkPaint& paint) SK_OVERRIDE {
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);
295 SkTextBlob* SkTextBox::snapshotTextBlob(SkScalar* computedBottom) const {
296 TextBlobVisitor visitor;
297 SkScalar newB = this->visit(visitor, fText, fLen, *fPaint);
298 if (computedBottom) {
299 *computedBottom = newB;
301 return (SkTextBlob*)visitor.fBuilder.build();