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 { | |
mtklein
2015/04/08 21:42:18
better : SkNoncopyable this.
f(malita)
2015/04/09 01:08:22
Aww, but that adds 8 bytes to the size (wiping out
| |
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(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 SkPaint::Hinting fHinting; | |
71 uint32_t fFlags; | |
mtklein
2015/04/08 21:42:18
Do we have two free bits in fFlags to store the Sk
f(malita)
2015/04/09 01:08:22
Yup, looks like SkPaint's flags fit in 16 bits jus
| |
72 }; | |
73 | |
74 } // anonymous namespace | |
75 | |
14 // | 76 // |
15 // Textblob data is laid out into externally-managed storage as follows: | 77 // Textblob data is laid out into externally-managed storage as follows: |
16 // | 78 // |
17 // -------------------------------------------------------------------------- --- | 79 // -------------------------------------------------------------------------- --- |
18 // | SkTextBlob | RunRecord | Glyphs[] | Pos[] | RunRecord | Glyphs[] | Pos[] | ... | 80 // | SkTextBlob | RunRecord | Glyphs[] | Pos[] | RunRecord | Glyphs[] | Pos[] | ... |
19 // -------------------------------------------------------------------------- --- | 81 // -------------------------------------------------------------------------- --- |
20 // | 82 // |
21 // Each run record describes a text blob run, and can be used to determine the (implicit) | 83 // Each run record describes a text blob run, and can be used to determine the (implicit) |
22 // location of the following record. | 84 // location of the following record. |
23 | 85 |
(...skipping 10 matching lines...) Expand all Loading... | |
34 } | 96 } |
35 | 97 |
36 uint32_t glyphCount() const { | 98 uint32_t glyphCount() const { |
37 return fCount; | 99 return fCount; |
38 } | 100 } |
39 | 101 |
40 const SkPoint& offset() const { | 102 const SkPoint& offset() const { |
41 return fOffset; | 103 return fOffset; |
42 } | 104 } |
43 | 105 |
44 const SkPaint& font() const { | 106 const RunFont& font() const { |
45 return fFont; | 107 return fFont; |
46 } | 108 } |
47 | 109 |
48 GlyphPositioning positioning() const { | 110 GlyphPositioning positioning() const { |
49 return fPositioning; | 111 return fPositioning; |
50 } | 112 } |
51 | 113 |
52 uint16_t* glyphBuffer() const { | 114 uint16_t* glyphBuffer() const { |
53 // Glyph are stored immediately following the record. | 115 // Glyph are stored immediately following the record. |
54 return reinterpret_cast<uint16_t*>(const_cast<RunRecord*>(this) + 1); | 116 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. | 157 // Move the initial pos scalars to their new location. |
96 size_t copySize = initialCount * sizeof(SkScalar) * ScalarsPerGlyph(fPos itioning); | 158 size_t copySize = initialCount * sizeof(SkScalar) * ScalarsPerGlyph(fPos itioning); |
97 SkASSERT((uint8_t*)posBuffer() + copySize <= (uint8_t*)Next(this)); | 159 SkASSERT((uint8_t*)posBuffer() + copySize <= (uint8_t*)Next(this)); |
98 | 160 |
99 // memmove, as the buffers may overlap | 161 // memmove, as the buffers may overlap |
100 memmove(posBuffer(), initialPosBuffer, copySize); | 162 memmove(posBuffer(), initialPosBuffer, copySize); |
101 } | 163 } |
102 | 164 |
103 uint32_t fCount; | 165 uint32_t fCount; |
104 SkPoint fOffset; | 166 SkPoint fOffset; |
105 SkPaint fFont; | 167 RunFont fFont; |
106 GlyphPositioning fPositioning; | 168 GlyphPositioning fPositioning; |
107 | 169 |
108 SkDEBUGCODE(unsigned fMagic;) | 170 SkDEBUGCODE(unsigned fMagic;) |
109 }; | 171 }; |
110 | 172 |
111 static int32_t gNextID = 1; | 173 static int32_t gNextID = 1; |
112 static int32_t next_id() { | 174 static int32_t next_id() { |
113 int32_t id; | 175 int32_t id; |
114 do { | 176 do { |
115 id = sk_atomic_inc(&gNextID); | 177 id = sk_atomic_inc(&gNextID); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 } | 317 } |
256 | 318 |
257 SkTextBlob::GlyphPositioning SkTextBlob::RunIterator::positioning() const { | 319 SkTextBlob::GlyphPositioning SkTextBlob::RunIterator::positioning() const { |
258 SkASSERT(!this->done()); | 320 SkASSERT(!this->done()); |
259 return fCurrentRun->positioning(); | 321 return fCurrentRun->positioning(); |
260 } | 322 } |
261 | 323 |
262 void SkTextBlob::RunIterator::applyFontToPaint(SkPaint* paint) const { | 324 void SkTextBlob::RunIterator::applyFontToPaint(SkPaint* paint) const { |
263 SkASSERT(!this->done()); | 325 SkASSERT(!this->done()); |
264 | 326 |
265 const SkPaint& font = fCurrentRun->font(); | 327 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 } | 328 } |
289 | 329 |
290 SkTextBlobBuilder::SkTextBlobBuilder() | 330 SkTextBlobBuilder::SkTextBlobBuilder() |
291 : fStorageSize(0) | 331 : fStorageSize(0) |
292 , fStorageUsed(0) | 332 , fStorageUsed(0) |
293 , fRunCount(0) | 333 , fRunCount(0) |
294 , fDeferredBounds(false) | 334 , fDeferredBounds(false) |
295 , fLastRun(0) { | 335 , fLastRun(0) { |
296 fBounds.setEmpty(); | 336 fBounds.setEmpty(); |
297 } | 337 } |
298 | 338 |
299 SkTextBlobBuilder::~SkTextBlobBuilder() { | 339 SkTextBlobBuilder::~SkTextBlobBuilder() { |
300 if (NULL != fStorage.get()) { | 340 if (NULL != fStorage.get()) { |
301 // We are abandoning runs and must destruct the associated font data. | 341 // We are abandoning runs and must destruct the associated font data. |
302 // The easiest way to accomplish that is to use the blob destructor. | 342 // The easiest way to accomplish that is to use the blob destructor. |
303 build()->unref(); | 343 build()->unref(); |
304 } | 344 } |
305 } | 345 } |
306 | 346 |
307 SkRect SkTextBlobBuilder::TightRunBounds(const SkTextBlob::RunRecord& run) { | 347 SkRect SkTextBlobBuilder::TightRunBounds(const SkTextBlob::RunRecord& run) { |
308 SkASSERT(SkTextBlob::kDefault_Positioning == run.positioning()); | 348 SkASSERT(SkTextBlob::kDefault_Positioning == run.positioning()); |
309 | 349 |
310 SkRect bounds; | 350 SkRect bounds; |
311 run.font().measureText(run.glyphBuffer(), run.glyphCount() * sizeof(uint16_t ), &bounds); | 351 SkPaint paint; |
352 run.font().applyToPaint(&paint); | |
353 paint.measureText(run.glyphBuffer(), run.glyphCount() * sizeof(uint16_t), &b ounds); | |
312 | 354 |
313 return bounds.makeOffset(run.offset().x(), run.offset().y()); | 355 return bounds.makeOffset(run.offset().x(), run.offset().y()); |
314 } | 356 } |
315 | 357 |
316 SkRect SkTextBlobBuilder::ConservativeRunBounds(const SkTextBlob::RunRecord& run ) { | 358 SkRect SkTextBlobBuilder::ConservativeRunBounds(const SkTextBlob::RunRecord& run ) { |
317 SkASSERT(run.glyphCount() > 0); | 359 SkASSERT(run.glyphCount() > 0); |
318 SkASSERT(SkTextBlob::kFull_Positioning == run.positioning() || | 360 SkASSERT(SkTextBlob::kFull_Positioning == run.positioning() || |
319 SkTextBlob::kHorizontal_Positioning == run.positioning()); | 361 SkTextBlob::kHorizontal_Positioning == run.positioning()); |
320 | 362 |
321 // First, compute the glyph position bbox. | 363 // First, compute the glyph position bbox. |
(...skipping 17 matching lines...) Expand all Loading... | |
339 const SkPoint* glyphPosPts = reinterpret_cast<const SkPoint*>(run.posBuf fer()); | 381 const SkPoint* glyphPosPts = reinterpret_cast<const SkPoint*>(run.posBuf fer()); |
340 SkASSERT((void*)(glyphPosPts + run.glyphCount()) <= SkTextBlob::RunRecor d::Next(&run)); | 382 SkASSERT((void*)(glyphPosPts + run.glyphCount()) <= SkTextBlob::RunRecor d::Next(&run)); |
341 | 383 |
342 bounds.setBounds(glyphPosPts, run.glyphCount()); | 384 bounds.setBounds(glyphPosPts, run.glyphCount()); |
343 } break; | 385 } break; |
344 default: | 386 default: |
345 SkFAIL("unsupported positioning mode"); | 387 SkFAIL("unsupported positioning mode"); |
346 } | 388 } |
347 | 389 |
348 // Expand by typeface glyph bounds. | 390 // Expand by typeface glyph bounds. |
349 const SkRect fontBounds = run.font().getFontBounds(); | 391 SkPaint paint; |
392 run.font().applyToPaint(&paint); | |
393 const SkRect fontBounds = paint.getFontBounds(); | |
350 bounds.fLeft += fontBounds.left(); | 394 bounds.fLeft += fontBounds.left(); |
351 bounds.fTop += fontBounds.top(); | 395 bounds.fTop += fontBounds.top(); |
352 bounds.fRight += fontBounds.right(); | 396 bounds.fRight += fontBounds.right(); |
353 bounds.fBottom += fontBounds.bottom(); | 397 bounds.fBottom += fontBounds.bottom(); |
354 | 398 |
355 // Offset by run position. | 399 // Offset by run position. |
356 return bounds.makeOffset(run.offset().x(), run.offset().y()); | 400 return bounds.makeOffset(run.offset().x(), run.offset().y()); |
357 } | 401 } |
358 | 402 |
359 void SkTextBlobBuilder::updateDeferredBounds() { | 403 void SkTextBlobBuilder::updateDeferredBounds() { |
360 SkASSERT(!fDeferredBounds || fRunCount > 0); | 404 SkASSERT(!fDeferredBounds || fRunCount > 0); |
361 | 405 |
362 if (!fDeferredBounds) { | 406 if (!fDeferredBounds) { |
363 return; | 407 return; |
364 } | 408 } |
365 | 409 |
366 SkASSERT(fLastRun >= sizeof(SkTextBlob)); | 410 SkASSERT(fLastRun >= sizeof(SkTextBlob)); |
367 SkTextBlob::RunRecord* run = reinterpret_cast<SkTextBlob::RunRecord*>(fStora ge.get() + | 411 SkTextBlob::RunRecord* run = reinterpret_cast<SkTextBlob::RunRecord*>(fStora ge.get() + |
368 fLastR un); | 412 fLastR un); |
369 SkASSERT(SkPaint::kGlyphID_TextEncoding == run->font().getTextEncoding()); | |
370 | 413 |
371 // FIXME: we should also use conservative bounds for kDefault_Positioning. | 414 // FIXME: we should also use conservative bounds for kDefault_Positioning. |
372 SkRect runBounds = SkTextBlob::kDefault_Positioning == run->positioning() ? | 415 SkRect runBounds = SkTextBlob::kDefault_Positioning == run->positioning() ? |
373 TightRunBounds(*run) : ConservativeRunBounds(*run); | 416 TightRunBounds(*run) : ConservativeRunBounds(*run); |
374 fBounds.join(runBounds); | 417 fBounds.join(runBounds); |
375 fDeferredBounds = false; | 418 fDeferredBounds = false; |
376 } | 419 } |
377 | 420 |
378 void SkTextBlobBuilder::reserve(size_t size) { | 421 void SkTextBlobBuilder::reserve(size_t size) { |
379 // We don't currently pre-allocate, but maybe someday... | 422 // We don't currently pre-allocate, but maybe someday... |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
535 | 578 |
536 fStorageUsed = 0; | 579 fStorageUsed = 0; |
537 fStorageSize = 0; | 580 fStorageSize = 0; |
538 fRunCount = 0; | 581 fRunCount = 0; |
539 fLastRun = 0; | 582 fLastRun = 0; |
540 fBounds.setEmpty(); | 583 fBounds.setEmpty(); |
541 | 584 |
542 return blob; | 585 return blob; |
543 } | 586 } |
544 | 587 |
OLD | NEW |