epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkTextBox.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | |
| 11 | static inline int is_ws(int c) |
| 12 | { |
| 13 | return !((c - 1) >> 5); |
| 14 | } |
| 15 | |
bungeman@google.com | 0d50bc1 | 2012-09-06 14:46:30 +0000 | [diff] [blame] | 16 | static size_t linebreak(const char text[], const char stop[], |
| 17 | const SkPaint& paint, SkScalar margin, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 18 | size_t* trailing = nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | { |
bungeman@google.com | 0d50bc1 | 2012-09-06 14:46:30 +0000 | [diff] [blame] | 20 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | int 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 | |
| 115 | SkTextBox::SkTextBox() |
| 116 | { |
| 117 | fBox.setEmpty(); |
| 118 | fSpacingMul = SK_Scalar1; |
| 119 | fSpacingAdd = 0; |
| 120 | fMode = kLineBreak_Mode; |
| 121 | fSpacingAlign = kStart_SpacingAlign; |
| 122 | } |
| 123 | |
| 124 | void SkTextBox::setMode(Mode mode) |
| 125 | { |
| 126 | SkASSERT((unsigned)mode < kModeCount); |
| 127 | fMode = SkToU8(mode); |
| 128 | } |
| 129 | |
| 130 | void SkTextBox::setSpacingAlign(SpacingAlign align) |
| 131 | { |
| 132 | SkASSERT((unsigned)align < kSpacingAlignCount); |
| 133 | fSpacingAlign = SkToU8(align); |
| 134 | } |
| 135 | |
| 136 | void SkTextBox::getBox(SkRect* box) const |
| 137 | { |
| 138 | if (box) |
| 139 | *box = fBox; |
| 140 | } |
| 141 | |
| 142 | void SkTextBox::setBox(const SkRect& box) |
| 143 | { |
| 144 | fBox = box; |
| 145 | } |
| 146 | |
| 147 | void SkTextBox::setBox(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) |
| 148 | { |
| 149 | fBox.set(left, top, right, bottom); |
| 150 | } |
| 151 | |
| 152 | void SkTextBox::getSpacing(SkScalar* mul, SkScalar* add) const |
| 153 | { |
| 154 | if (mul) |
| 155 | *mul = fSpacingMul; |
| 156 | if (add) |
| 157 | *add = fSpacingAdd; |
| 158 | } |
| 159 | |
| 160 | void SkTextBox::setSpacing(SkScalar mul, SkScalar add) |
| 161 | { |
| 162 | fSpacingMul = mul; |
| 163 | fSpacingAdd = add; |
| 164 | } |
| 165 | |
| 166 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 167 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 168 | SkScalar SkTextBox::visit(Visitor& visitor, const char text[], size_t len, |
| 169 | const SkPaint& paint) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | SkScalar marginWidth = fBox.width(); |
| 171 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 172 | if (marginWidth <= 0 || len == 0) { |
| 173 | return fBox.top(); |
| 174 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | |
| 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 Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 195 | scaledSpacing = fontHeight * fSpacingMul + fSpacingAdd; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | height = fBox.height(); |
| 197 | |
| 198 | // compute Y position for first line |
| 199 | { |
| 200 | SkScalar textHeight = fontHeight; |
| 201 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 202 | if (fMode == kLineBreak_Mode && fSpacingAlign != kStart_SpacingAlign) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | 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 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 223 | for (;;) { |
bungeman@google.com | 0d50bc1 | 2012-09-06 14:46:30 +0000 | [diff] [blame] | 224 | size_t trailing; |
| 225 | len = linebreak(text, textStop, paint, marginWidth, &trailing); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 226 | if (y + metrics.fDescent + metrics.fLeading > 0) { |
| 227 | visitor(text, len - trailing, x, y, paint); |
| 228 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | text += len; |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 230 | if (text >= textStop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | break; |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 232 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | y += scaledSpacing; |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 234 | if (y + metrics.fAscent >= fBox.fBottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | break; |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 236 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 237 | } |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 238 | return y + metrics.fDescent + metrics.fLeading; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | } |
| 240 | |
reed@android.com | 033e03c | 2010-05-18 21:17:43 +0000 | [diff] [blame] | 241 | /////////////////////////////////////////////////////////////////////////////// |
| 242 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 243 | class CanvasVisitor : public SkTextBox::Visitor { |
| 244 | SkCanvas* fCanvas; |
| 245 | public: |
| 246 | CanvasVisitor(SkCanvas* canvas) : fCanvas(canvas) {} |
tfarina | 283b587 | 2015-04-25 12:59:44 -0700 | [diff] [blame] | 247 | |
| 248 | void operator()(const char text[], size_t length, SkScalar x, SkScalar y, |
| 249 | const SkPaint& paint) override { |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 250 | fCanvas->drawText(text, length, x, y, paint); |
| 251 | } |
| 252 | }; |
| 253 | |
reed@android.com | 033e03c | 2010-05-18 21:17:43 +0000 | [diff] [blame] | 254 | void SkTextBox::setText(const char text[], size_t len, const SkPaint& paint) { |
| 255 | fText = text; |
| 256 | fLen = len; |
| 257 | fPaint = &paint; |
| 258 | } |
| 259 | |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 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); |
| 263 | } |
| 264 | |
reed@android.com | 033e03c | 2010-05-18 21:17:43 +0000 | [diff] [blame] | 265 | void SkTextBox::draw(SkCanvas* canvas) { |
| 266 | this->draw(canvas, fText, fLen, *fPaint); |
| 267 | } |
| 268 | |
| 269 | int SkTextBox::countLines() const { |
| 270 | return SkTextLineBreaker::CountLines(fText, fLen, *fPaint, fBox.width()); |
| 271 | } |
| 272 | |
| 273 | SkScalar SkTextBox::getTextHeight() const { |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 274 | SkScalar spacing = fPaint->getTextSize() * fSpacingMul + fSpacingAdd; |
reed@android.com | 033e03c | 2010-05-18 21:17:43 +0000 | [diff] [blame] | 275 | return this->countLines() * spacing; |
| 276 | } |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 277 | |
| 278 | /////////////////////////////////////////////////////////////////////////////// |
| 279 | |
| 280 | #include "SkTextBlob.h" |
| 281 | |
| 282 | class TextBlobVisitor : public SkTextBox::Visitor { |
| 283 | public: |
| 284 | SkTextBlobBuilder fBuilder; |
tfarina | 283b587 | 2015-04-25 12:59:44 -0700 | [diff] [blame] | 285 | |
| 286 | void operator()(const char text[], size_t length, SkScalar x, SkScalar y, |
| 287 | const SkPaint& paint) override { |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 288 | 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 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 295 | sk_sp<SkTextBlob> SkTextBox::snapshotTextBlob(SkScalar* computedBottom) const { |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 296 | TextBlobVisitor visitor; |
| 297 | SkScalar newB = this->visit(visitor, fText, fLen, *fPaint); |
| 298 | if (computedBottom) { |
| 299 | *computedBottom = newB; |
| 300 | } |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 301 | return visitor.fBuilder.make(); |
reed | 1b6ab44 | 2014-11-03 19:55:41 -0800 | [diff] [blame] | 302 | } |