Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkTextBlob.h" | 8 #include "SkTextBlob.h" |
| 9 | 9 |
| 10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
| 11 #include "SkTypeface.h" | 11 #include "SkTypeface.h" |
| 12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 | 13 |
| 14 namespace { | |
| 15 | |
| 16 // TODO(fmalita): replace with SkFont. | |
| 17 class RunFont : SkNoncopyable { | |
|
mtklein
2015/04/09 09:44:19
Woah, if this adds size, let's not do that. Let's
f(malita)
2015/04/09 13:07:09
Done.
f(malita)
2015/04/09 13:18:31
OK, I see what's going on: this is triggered by th
| |
| 18 public: | |
| 19 RunFont(const SkPaint& paint) | |
| 20 : fTypeface(SkSafeRef(paint.getTypeface())) | |
| 21 , fSize(paint.getTextSize()) | |
| 22 , fScaleX(paint.getTextScaleX()) | |
| 23 , fSkewX(paint.getTextSkewX()) | |
| 24 , fHinting(paint.getHinting()) | |
| 25 , fFlags(paint.getFlags() & kFlagsMask) { } | |
| 26 | |
| 27 void applyToPaint(SkPaint* paint) const { | |
| 28 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
| 29 paint->setTypeface(fTypeface.get()); | |
| 30 paint->setTextSize(fSize); | |
| 31 paint->setTextScaleX(fScaleX); | |
| 32 paint->setTextSkewX(fSkewX); | |
| 33 paint->setHinting(static_cast<SkPaint::Hinting>(fHinting)); | |
| 34 | |
| 35 paint->setFlags((paint->getFlags() & ~kFlagsMask) | fFlags); | |
| 36 } | |
| 37 | |
| 38 bool operator==(const RunFont& other) const { | |
| 39 return fTypeface == other.fTypeface | |
| 40 && fSize == other.fSize | |
| 41 && fScaleX == other.fScaleX | |
| 42 && fSkewX == other.fSkewX | |
| 43 && fHinting == other.fHinting | |
| 44 && fFlags == other.fFlags; | |
| 45 } | |
| 46 | |
| 47 bool operator!=(const RunFont& other) const { | |
| 48 return !(*this == other); | |
| 49 } | |
| 50 private: | |
| 51 const static uint32_t kFlagsMask = | |
| 52 SkPaint::kAntiAlias_Flag | | |
| 53 SkPaint::kUnderlineText_Flag | | |
| 54 SkPaint::kStrikeThruText_Flag | | |
| 55 SkPaint::kFakeBoldText_Flag | | |
| 56 SkPaint::kLinearText_Flag | | |
| 57 SkPaint::kSubpixelText_Flag | | |
| 58 SkPaint::kDevKernText_Flag | | |
| 59 SkPaint::kLCDRenderText_Flag | | |
| 60 SkPaint::kEmbeddedBitmapText_Flag | | |
| 61 SkPaint::kAutoHinting_Flag | | |
| 62 SkPaint::kVerticalText_Flag | | |
| 63 SkPaint::kGenA8FromLCD_Flag | | |
| 64 SkPaint::kDistanceFieldTextTEMP_Flag; | |
| 65 | |
| 66 SkAutoTUnref<SkTypeface> fTypeface; | |
| 67 SkScalar fSize; | |
| 68 SkScalar fScaleX; | |
| 69 SkScalar fSkewX; | |
| 70 | |
| 71 SK_COMPILE_ASSERT(SkPaint::kFull_Hinting < 4, insufficient_hinting_bits); | |
| 72 uint32_t fHinting : 2; | |
| 73 SK_COMPILE_ASSERT((kFlagsMask & 0xffff) == kFlagsMask, insufficient_flags_bi ts); | |
| 74 uint32_t fFlags : 16; | |
| 75 | |
| 76 typedef SkNoncopyable INHERITED; | |
| 77 }; | |
| 78 | |
| 79 } // anonymous namespace | |
| 80 | |
| 14 // | 81 // |
| 15 // Textblob data is laid out into externally-managed storage as follows: | 82 // Textblob data is laid out into externally-managed storage as follows: |
| 16 // | 83 // |
| 17 // -------------------------------------------------------------------------- --- | 84 // -------------------------------------------------------------------------- --- |
| 18 // | SkTextBlob | RunRecord | Glyphs[] | Pos[] | RunRecord | Glyphs[] | Pos[] | ... | 85 // | SkTextBlob | RunRecord | Glyphs[] | Pos[] | RunRecord | Glyphs[] | Pos[] | ... |
| 19 // -------------------------------------------------------------------------- --- | 86 // -------------------------------------------------------------------------- --- |
| 20 // | 87 // |
| 21 // Each run record describes a text blob run, and can be used to determine the (implicit) | 88 // Each run record describes a text blob run, and can be used to determine the (implicit) |
| 22 // location of the following record. | 89 // location of the following record. |
| 23 | 90 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 34 } | 101 } |
| 35 | 102 |
| 36 uint32_t glyphCount() const { | 103 uint32_t glyphCount() const { |
| 37 return fCount; | 104 return fCount; |
| 38 } | 105 } |
| 39 | 106 |
| 40 const SkPoint& offset() const { | 107 const SkPoint& offset() const { |
| 41 return fOffset; | 108 return fOffset; |
| 42 } | 109 } |
| 43 | 110 |
| 44 const SkPaint& font() const { | 111 const RunFont& font() const { |
| 45 return fFont; | 112 return fFont; |
| 46 } | 113 } |
| 47 | 114 |
| 48 GlyphPositioning positioning() const { | 115 GlyphPositioning positioning() const { |
| 49 return fPositioning; | 116 return fPositioning; |
| 50 } | 117 } |
| 51 | 118 |
| 52 uint16_t* glyphBuffer() const { | 119 uint16_t* glyphBuffer() const { |
| 53 // Glyph are stored immediately following the record. | 120 // Glyph are stored immediately following the record. |
| 54 return reinterpret_cast<uint16_t*>(const_cast<RunRecord*>(this) + 1); | 121 return reinterpret_cast<uint16_t*>(const_cast<RunRecord*>(this) + 1); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 // Move the initial pos scalars to their new location. | 162 // Move the initial pos scalars to their new location. |
| 96 size_t copySize = initialCount * sizeof(SkScalar) * ScalarsPerGlyph(fPos itioning); | 163 size_t copySize = initialCount * sizeof(SkScalar) * ScalarsPerGlyph(fPos itioning); |
| 97 SkASSERT((uint8_t*)posBuffer() + copySize <= (uint8_t*)Next(this)); | 164 SkASSERT((uint8_t*)posBuffer() + copySize <= (uint8_t*)Next(this)); |
| 98 | 165 |
| 99 // memmove, as the buffers may overlap | 166 // memmove, as the buffers may overlap |
| 100 memmove(posBuffer(), initialPosBuffer, copySize); | 167 memmove(posBuffer(), initialPosBuffer, copySize); |
| 101 } | 168 } |
| 102 | 169 |
| 103 uint32_t fCount; | 170 uint32_t fCount; |
| 104 SkPoint fOffset; | 171 SkPoint fOffset; |
| 105 SkPaint fFont; | 172 RunFont fFont; |
| 106 GlyphPositioning fPositioning; | 173 GlyphPositioning fPositioning; |
| 107 | 174 |
| 108 SkDEBUGCODE(unsigned fMagic;) | 175 SkDEBUGCODE(unsigned fMagic;) |
| 109 }; | 176 }; |
| 110 | 177 |
| 111 static int32_t gNextID = 1; | 178 static int32_t gNextID = 1; |
| 112 static int32_t next_id() { | 179 static int32_t next_id() { |
| 113 int32_t id; | 180 int32_t id; |
| 114 do { | 181 do { |
| 115 id = sk_atomic_inc(&gNextID); | 182 id = sk_atomic_inc(&gNextID); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 } | 322 } |
| 256 | 323 |
| 257 SkTextBlob::GlyphPositioning SkTextBlob::RunIterator::positioning() const { | 324 SkTextBlob::GlyphPositioning SkTextBlob::RunIterator::positioning() const { |
| 258 SkASSERT(!this->done()); | 325 SkASSERT(!this->done()); |
| 259 return fCurrentRun->positioning(); | 326 return fCurrentRun->positioning(); |
| 260 } | 327 } |
| 261 | 328 |
| 262 void SkTextBlob::RunIterator::applyFontToPaint(SkPaint* paint) const { | 329 void SkTextBlob::RunIterator::applyFontToPaint(SkPaint* paint) const { |
| 263 SkASSERT(!this->done()); | 330 SkASSERT(!this->done()); |
| 264 | 331 |
| 265 const SkPaint& font = fCurrentRun->font(); | 332 fCurrentRun->font().applyToPaint(paint); |
| 266 | |
| 267 paint->setTypeface(font.getTypeface()); | |
| 268 paint->setTextEncoding(font.getTextEncoding()); | |
| 269 paint->setTextSize(font.getTextSize()); | |
| 270 paint->setTextScaleX(font.getTextScaleX()); | |
| 271 paint->setTextSkewX(font.getTextSkewX()); | |
| 272 paint->setHinting(font.getHinting()); | |
| 273 | |
| 274 uint32_t flagsMask = SkPaint::kAntiAlias_Flag | |
| 275 | SkPaint::kUnderlineText_Flag | |
| 276 | SkPaint::kStrikeThruText_Flag | |
| 277 | SkPaint::kFakeBoldText_Flag | |
| 278 | SkPaint::kLinearText_Flag | |
| 279 | SkPaint::kSubpixelText_Flag | |
| 280 | SkPaint::kDevKernText_Flag | |
| 281 | SkPaint::kLCDRenderText_Flag | |
| 282 | SkPaint::kEmbeddedBitmapText_Flag | |
| 283 | SkPaint::kAutoHinting_Flag | |
| 284 | SkPaint::kVerticalText_Flag | |
| 285 | SkPaint::kGenA8FromLCD_Flag | |
| 286 | SkPaint::kDistanceFieldTextTEMP_Flag; | |
| 287 paint->setFlags((paint->getFlags() & ~flagsMask) | (font.getFlags() & flagsM ask)); | |
| 288 } | 333 } |
| 289 | 334 |
| 290 SkTextBlobBuilder::SkTextBlobBuilder() | 335 SkTextBlobBuilder::SkTextBlobBuilder() |
| 291 : fStorageSize(0) | 336 : fStorageSize(0) |
| 292 , fStorageUsed(0) | 337 , fStorageUsed(0) |
| 293 , fRunCount(0) | 338 , fRunCount(0) |
| 294 , fDeferredBounds(false) | 339 , fDeferredBounds(false) |
| 295 , fLastRun(0) { | 340 , fLastRun(0) { |
| 296 fBounds.setEmpty(); | 341 fBounds.setEmpty(); |
| 297 } | 342 } |
| 298 | 343 |
| 299 SkTextBlobBuilder::~SkTextBlobBuilder() { | 344 SkTextBlobBuilder::~SkTextBlobBuilder() { |
| 300 if (NULL != fStorage.get()) { | 345 if (NULL != fStorage.get()) { |
| 301 // We are abandoning runs and must destruct the associated font data. | 346 // We are abandoning runs and must destruct the associated font data. |
| 302 // The easiest way to accomplish that is to use the blob destructor. | 347 // The easiest way to accomplish that is to use the blob destructor. |
| 303 build()->unref(); | 348 build()->unref(); |
| 304 } | 349 } |
| 305 } | 350 } |
| 306 | 351 |
| 307 SkRect SkTextBlobBuilder::TightRunBounds(const SkTextBlob::RunRecord& run) { | 352 SkRect SkTextBlobBuilder::TightRunBounds(const SkTextBlob::RunRecord& run) { |
| 308 SkASSERT(SkTextBlob::kDefault_Positioning == run.positioning()); | 353 SkASSERT(SkTextBlob::kDefault_Positioning == run.positioning()); |
| 309 | 354 |
| 310 SkRect bounds; | 355 SkRect bounds; |
| 311 run.font().measureText(run.glyphBuffer(), run.glyphCount() * sizeof(uint16_t ), &bounds); | 356 SkPaint paint; |
| 357 run.font().applyToPaint(&paint); | |
| 358 paint.measureText(run.glyphBuffer(), run.glyphCount() * sizeof(uint16_t), &b ounds); | |
| 312 | 359 |
| 313 return bounds.makeOffset(run.offset().x(), run.offset().y()); | 360 return bounds.makeOffset(run.offset().x(), run.offset().y()); |
| 314 } | 361 } |
| 315 | 362 |
| 316 SkRect SkTextBlobBuilder::ConservativeRunBounds(const SkTextBlob::RunRecord& run ) { | 363 SkRect SkTextBlobBuilder::ConservativeRunBounds(const SkTextBlob::RunRecord& run ) { |
| 317 SkASSERT(run.glyphCount() > 0); | 364 SkASSERT(run.glyphCount() > 0); |
| 318 SkASSERT(SkTextBlob::kFull_Positioning == run.positioning() || | 365 SkASSERT(SkTextBlob::kFull_Positioning == run.positioning() || |
| 319 SkTextBlob::kHorizontal_Positioning == run.positioning()); | 366 SkTextBlob::kHorizontal_Positioning == run.positioning()); |
| 320 | 367 |
| 321 // First, compute the glyph position bbox. | 368 // First, compute the glyph position bbox. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 339 const SkPoint* glyphPosPts = reinterpret_cast<const SkPoint*>(run.posBuf fer()); | 386 const SkPoint* glyphPosPts = reinterpret_cast<const SkPoint*>(run.posBuf fer()); |
| 340 SkASSERT((void*)(glyphPosPts + run.glyphCount()) <= SkTextBlob::RunRecor d::Next(&run)); | 387 SkASSERT((void*)(glyphPosPts + run.glyphCount()) <= SkTextBlob::RunRecor d::Next(&run)); |
| 341 | 388 |
| 342 bounds.setBounds(glyphPosPts, run.glyphCount()); | 389 bounds.setBounds(glyphPosPts, run.glyphCount()); |
| 343 } break; | 390 } break; |
| 344 default: | 391 default: |
| 345 SkFAIL("unsupported positioning mode"); | 392 SkFAIL("unsupported positioning mode"); |
| 346 } | 393 } |
| 347 | 394 |
| 348 // Expand by typeface glyph bounds. | 395 // Expand by typeface glyph bounds. |
| 349 const SkRect fontBounds = run.font().getFontBounds(); | 396 SkPaint paint; |
| 397 run.font().applyToPaint(&paint); | |
| 398 const SkRect fontBounds = paint.getFontBounds(); | |
| 350 bounds.fLeft += fontBounds.left(); | 399 bounds.fLeft += fontBounds.left(); |
| 351 bounds.fTop += fontBounds.top(); | 400 bounds.fTop += fontBounds.top(); |
| 352 bounds.fRight += fontBounds.right(); | 401 bounds.fRight += fontBounds.right(); |
| 353 bounds.fBottom += fontBounds.bottom(); | 402 bounds.fBottom += fontBounds.bottom(); |
| 354 | 403 |
| 355 // Offset by run position. | 404 // Offset by run position. |
| 356 return bounds.makeOffset(run.offset().x(), run.offset().y()); | 405 return bounds.makeOffset(run.offset().x(), run.offset().y()); |
| 357 } | 406 } |
| 358 | 407 |
| 359 void SkTextBlobBuilder::updateDeferredBounds() { | 408 void SkTextBlobBuilder::updateDeferredBounds() { |
| 360 SkASSERT(!fDeferredBounds || fRunCount > 0); | 409 SkASSERT(!fDeferredBounds || fRunCount > 0); |
| 361 | 410 |
| 362 if (!fDeferredBounds) { | 411 if (!fDeferredBounds) { |
| 363 return; | 412 return; |
| 364 } | 413 } |
| 365 | 414 |
| 366 SkASSERT(fLastRun >= sizeof(SkTextBlob)); | 415 SkASSERT(fLastRun >= sizeof(SkTextBlob)); |
| 367 SkTextBlob::RunRecord* run = reinterpret_cast<SkTextBlob::RunRecord*>(fStora ge.get() + | 416 SkTextBlob::RunRecord* run = reinterpret_cast<SkTextBlob::RunRecord*>(fStora ge.get() + |
| 368 fLastR un); | 417 fLastR un); |
| 369 SkASSERT(SkPaint::kGlyphID_TextEncoding == run->font().getTextEncoding()); | |
| 370 | 418 |
| 371 // FIXME: we should also use conservative bounds for kDefault_Positioning. | 419 // FIXME: we should also use conservative bounds for kDefault_Positioning. |
| 372 SkRect runBounds = SkTextBlob::kDefault_Positioning == run->positioning() ? | 420 SkRect runBounds = SkTextBlob::kDefault_Positioning == run->positioning() ? |
| 373 TightRunBounds(*run) : ConservativeRunBounds(*run); | 421 TightRunBounds(*run) : ConservativeRunBounds(*run); |
| 374 fBounds.join(runBounds); | 422 fBounds.join(runBounds); |
| 375 fDeferredBounds = false; | 423 fDeferredBounds = false; |
| 376 } | 424 } |
| 377 | 425 |
| 378 void SkTextBlobBuilder::reserve(size_t size) { | 426 void SkTextBlobBuilder::reserve(size_t size) { |
| 379 // We don't currently pre-allocate, but maybe someday... | 427 // We don't currently pre-allocate, but maybe someday... |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 | 583 |
| 536 fStorageUsed = 0; | 584 fStorageUsed = 0; |
| 537 fStorageSize = 0; | 585 fStorageSize = 0; |
| 538 fRunCount = 0; | 586 fRunCount = 0; |
| 539 fLastRun = 0; | 587 fLastRun = 0; |
| 540 fBounds.setEmpty(); | 588 fBounds.setEmpty(); |
| 541 | 589 |
| 542 return blob; | 590 return blob; |
| 543 } | 591 } |
| 544 | 592 |
| OLD | NEW |