gfx/skia/trunk/src/views/SkTextBox.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2006 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkTextBox.h"
michael@0 9 #include "SkUtils.h"
michael@0 10
michael@0 11 static inline int is_ws(int c)
michael@0 12 {
michael@0 13 return !((c - 1) >> 5);
michael@0 14 }
michael@0 15
michael@0 16 static size_t linebreak(const char text[], const char stop[],
michael@0 17 const SkPaint& paint, SkScalar margin,
michael@0 18 size_t* trailing = NULL)
michael@0 19 {
michael@0 20 size_t lengthBreak = paint.breakText(text, stop - text, margin);
michael@0 21
michael@0 22 //Check for white space or line breakers before the lengthBreak
michael@0 23 const char* start = text;
michael@0 24 const char* word_start = text;
michael@0 25 int prevWS = true;
michael@0 26 if (trailing) {
michael@0 27 *trailing = 0;
michael@0 28 }
michael@0 29
michael@0 30 while (text < stop) {
michael@0 31 const char* prevText = text;
michael@0 32 SkUnichar uni = SkUTF8_NextUnichar(&text);
michael@0 33 int currWS = is_ws(uni);
michael@0 34
michael@0 35 if (!currWS && prevWS) {
michael@0 36 word_start = prevText;
michael@0 37 }
michael@0 38 prevWS = currWS;
michael@0 39
michael@0 40 if (text > start + lengthBreak) {
michael@0 41 if (currWS) {
michael@0 42 // eat the rest of the whitespace
michael@0 43 while (text < stop && is_ws(SkUTF8_ToUnichar(text))) {
michael@0 44 text += SkUTF8_CountUTF8Bytes(text);
michael@0 45 }
michael@0 46 if (trailing) {
michael@0 47 *trailing = text - prevText;
michael@0 48 }
michael@0 49 } else {
michael@0 50 // backup until a whitespace (or 1 char)
michael@0 51 if (word_start == start) {
michael@0 52 if (prevText > start) {
michael@0 53 text = prevText;
michael@0 54 }
michael@0 55 } else {
michael@0 56 text = word_start;
michael@0 57 }
michael@0 58 }
michael@0 59 break;
michael@0 60 }
michael@0 61
michael@0 62 if ('\n' == uni) {
michael@0 63 size_t ret = text - start;
michael@0 64 size_t lineBreakSize = 1;
michael@0 65 if (text < stop) {
michael@0 66 uni = SkUTF8_NextUnichar(&text);
michael@0 67 if ('\r' == uni) {
michael@0 68 ret = text - start;
michael@0 69 ++lineBreakSize;
michael@0 70 }
michael@0 71 }
michael@0 72 if (trailing) {
michael@0 73 *trailing = lineBreakSize;
michael@0 74 }
michael@0 75 return ret;
michael@0 76 }
michael@0 77
michael@0 78 if ('\r' == uni) {
michael@0 79 size_t ret = text - start;
michael@0 80 size_t lineBreakSize = 1;
michael@0 81 if (text < stop) {
michael@0 82 uni = SkUTF8_NextUnichar(&text);
michael@0 83 if ('\n' == uni) {
michael@0 84 ret = text - start;
michael@0 85 ++lineBreakSize;
michael@0 86 }
michael@0 87 }
michael@0 88 if (trailing) {
michael@0 89 *trailing = lineBreakSize;
michael@0 90 }
michael@0 91 return ret;
michael@0 92 }
michael@0 93 }
michael@0 94
michael@0 95 return text - start;
michael@0 96 }
michael@0 97
michael@0 98 int SkTextLineBreaker::CountLines(const char text[], size_t len, const SkPaint& paint, SkScalar width)
michael@0 99 {
michael@0 100 const char* stop = text + len;
michael@0 101 int count = 0;
michael@0 102
michael@0 103 if (width > 0)
michael@0 104 {
michael@0 105 do {
michael@0 106 count += 1;
michael@0 107 text += linebreak(text, stop, paint, width);
michael@0 108 } while (text < stop);
michael@0 109 }
michael@0 110 return count;
michael@0 111 }
michael@0 112
michael@0 113 //////////////////////////////////////////////////////////////////////////////
michael@0 114
michael@0 115 SkTextBox::SkTextBox()
michael@0 116 {
michael@0 117 fBox.setEmpty();
michael@0 118 fSpacingMul = SK_Scalar1;
michael@0 119 fSpacingAdd = 0;
michael@0 120 fMode = kLineBreak_Mode;
michael@0 121 fSpacingAlign = kStart_SpacingAlign;
michael@0 122 }
michael@0 123
michael@0 124 void SkTextBox::setMode(Mode mode)
michael@0 125 {
michael@0 126 SkASSERT((unsigned)mode < kModeCount);
michael@0 127 fMode = SkToU8(mode);
michael@0 128 }
michael@0 129
michael@0 130 void SkTextBox::setSpacingAlign(SpacingAlign align)
michael@0 131 {
michael@0 132 SkASSERT((unsigned)align < kSpacingAlignCount);
michael@0 133 fSpacingAlign = SkToU8(align);
michael@0 134 }
michael@0 135
michael@0 136 void SkTextBox::getBox(SkRect* box) const
michael@0 137 {
michael@0 138 if (box)
michael@0 139 *box = fBox;
michael@0 140 }
michael@0 141
michael@0 142 void SkTextBox::setBox(const SkRect& box)
michael@0 143 {
michael@0 144 fBox = box;
michael@0 145 }
michael@0 146
michael@0 147 void SkTextBox::setBox(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom)
michael@0 148 {
michael@0 149 fBox.set(left, top, right, bottom);
michael@0 150 }
michael@0 151
michael@0 152 void SkTextBox::getSpacing(SkScalar* mul, SkScalar* add) const
michael@0 153 {
michael@0 154 if (mul)
michael@0 155 *mul = fSpacingMul;
michael@0 156 if (add)
michael@0 157 *add = fSpacingAdd;
michael@0 158 }
michael@0 159
michael@0 160 void SkTextBox::setSpacing(SkScalar mul, SkScalar add)
michael@0 161 {
michael@0 162 fSpacingMul = mul;
michael@0 163 fSpacingAdd = add;
michael@0 164 }
michael@0 165
michael@0 166 /////////////////////////////////////////////////////////////////////////////////////////////
michael@0 167
michael@0 168 void SkTextBox::draw(SkCanvas* canvas, const char text[], size_t len, const SkPaint& paint)
michael@0 169 {
michael@0 170 SkASSERT(canvas && &paint && (text || len == 0));
michael@0 171
michael@0 172 SkScalar marginWidth = fBox.width();
michael@0 173
michael@0 174 if (marginWidth <= 0 || len == 0)
michael@0 175 return;
michael@0 176
michael@0 177 const char* textStop = text + len;
michael@0 178
michael@0 179 SkScalar x, y, scaledSpacing, height, fontHeight;
michael@0 180 SkPaint::FontMetrics metrics;
michael@0 181
michael@0 182 switch (paint.getTextAlign()) {
michael@0 183 case SkPaint::kLeft_Align:
michael@0 184 x = 0;
michael@0 185 break;
michael@0 186 case SkPaint::kCenter_Align:
michael@0 187 x = SkScalarHalf(marginWidth);
michael@0 188 break;
michael@0 189 default:
michael@0 190 x = marginWidth;
michael@0 191 break;
michael@0 192 }
michael@0 193 x += fBox.fLeft;
michael@0 194
michael@0 195 fontHeight = paint.getFontMetrics(&metrics);
michael@0 196 scaledSpacing = SkScalarMul(fontHeight, fSpacingMul) + fSpacingAdd;
michael@0 197 height = fBox.height();
michael@0 198
michael@0 199 // compute Y position for first line
michael@0 200 {
michael@0 201 SkScalar textHeight = fontHeight;
michael@0 202
michael@0 203 if (fMode == kLineBreak_Mode && fSpacingAlign != kStart_SpacingAlign)
michael@0 204 {
michael@0 205 int count = SkTextLineBreaker::CountLines(text, textStop - text, paint, marginWidth);
michael@0 206 SkASSERT(count > 0);
michael@0 207 textHeight += scaledSpacing * (count - 1);
michael@0 208 }
michael@0 209
michael@0 210 switch (fSpacingAlign) {
michael@0 211 case kStart_SpacingAlign:
michael@0 212 y = 0;
michael@0 213 break;
michael@0 214 case kCenter_SpacingAlign:
michael@0 215 y = SkScalarHalf(height - textHeight);
michael@0 216 break;
michael@0 217 default:
michael@0 218 SkASSERT(fSpacingAlign == kEnd_SpacingAlign);
michael@0 219 y = height - textHeight;
michael@0 220 break;
michael@0 221 }
michael@0 222 y += fBox.fTop - metrics.fAscent;
michael@0 223 }
michael@0 224
michael@0 225 for (;;)
michael@0 226 {
michael@0 227 size_t trailing;
michael@0 228 len = linebreak(text, textStop, paint, marginWidth, &trailing);
michael@0 229 if (y + metrics.fDescent + metrics.fLeading > 0)
michael@0 230 canvas->drawText(text, len - trailing, x, y, paint);
michael@0 231 text += len;
michael@0 232 if (text >= textStop)
michael@0 233 break;
michael@0 234 y += scaledSpacing;
michael@0 235 if (y + metrics.fAscent >= fBox.fBottom)
michael@0 236 break;
michael@0 237 }
michael@0 238 }
michael@0 239
michael@0 240 ///////////////////////////////////////////////////////////////////////////////
michael@0 241
michael@0 242 void SkTextBox::setText(const char text[], size_t len, const SkPaint& paint) {
michael@0 243 fText = text;
michael@0 244 fLen = len;
michael@0 245 fPaint = &paint;
michael@0 246 }
michael@0 247
michael@0 248 void SkTextBox::draw(SkCanvas* canvas) {
michael@0 249 this->draw(canvas, fText, fLen, *fPaint);
michael@0 250 }
michael@0 251
michael@0 252 int SkTextBox::countLines() const {
michael@0 253 return SkTextLineBreaker::CountLines(fText, fLen, *fPaint, fBox.width());
michael@0 254 }
michael@0 255
michael@0 256 SkScalar SkTextBox::getTextHeight() const {
michael@0 257 SkScalar spacing = SkScalarMul(fPaint->getTextSize(), fSpacingMul) + fSpacingAdd;
michael@0 258 return this->countLines() * spacing;
michael@0 259 }

mercurial