OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. |
| 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 |
| 8 #include "SkTextBlob.h" |
| 9 |
| 10 #include "SkDevice.h" |
| 11 |
| 12 const SkTextChunk* SkTextChunk::Create(const uint16_t* glyphs, size_t count, con
st SkPaint& paint, |
| 13 const SkRect* bounds) { |
| 14 if (NULL == glyphs || 0 == count) { |
| 15 return NULL; |
| 16 } |
| 17 |
| 18 return SkNEW_ARGS(SkTextChunk, (glyphs, count, NULL, 0, paint, bounds)); |
| 19 } |
| 20 |
| 21 const SkTextChunk* SkTextChunk::Create(const uint16_t* glyphs, size_t count, con
st SkScalar* pos, |
| 22 const SkPaint& paint, const SkRect* bounds) { |
| 23 if (NULL == glyphs || NULL == pos || 0 == count) { |
| 24 return NULL; |
| 25 } |
| 26 |
| 27 return SkNEW_ARGS(SkTextChunk, (glyphs, count, pos, 1, paint, bounds)); |
| 28 } |
| 29 |
| 30 const SkTextChunk* SkTextChunk::Create(const uint16_t* glyphs, size_t count, con
st SkPoint* pos, |
| 31 const SkPaint& paint, const SkRect* bounds) { |
| 32 if (NULL == glyphs || NULL == pos || 0 == count) { |
| 33 return NULL; |
| 34 } |
| 35 |
| 36 SK_COMPILE_ASSERT(sizeof(SkScalar) * 2 == sizeof(SkPoint), point_is_two_scal
ars); |
| 37 return SkNEW_ARGS(SkTextChunk, |
| 38 (glyphs, count, reinterpret_cast<const SkScalar*>(pos), 2,
paint, bounds)); |
| 39 } |
| 40 |
| 41 SkTextChunk::SkTextChunk(const uint16_t* glyphs, size_t count, const SkScalar* p
os, |
| 42 unsigned scalarsPerPos, const SkPaint& paint, const SkR
ect* bounds) |
| 43 : fGlyphCount(count) |
| 44 , fPos(NULL) |
| 45 , fFont(paint) |
| 46 , fScalarsPerPos(scalarsPerPos) { |
| 47 |
| 48 SkASSERT(glyphs); |
| 49 SkASSERT(count > 0); |
| 50 size_t glyphStorageSize = sizeof(uint16_t) * count; |
| 51 fGlyphs = reinterpret_cast<uint16_t*>(sk_malloc_throw(glyphStorageSize)); |
| 52 memcpy(fGlyphs, glyphs, glyphStorageSize); |
| 53 |
| 54 SkASSERT(scalarsPerPos <= 2); |
| 55 SkASSERT((NULL != pos) == (scalarsPerPos > 0)); |
| 56 if (NULL != pos) { |
| 57 size_t posStorageSize = sizeof(SkScalar) * fScalarsPerPos * count; |
| 58 fPos = reinterpret_cast<SkScalar*>(sk_malloc_throw(posStorageSize)); |
| 59 memcpy(fPos, pos, posStorageSize); |
| 60 } |
| 61 |
| 62 fBoundsDirty = (NULL == bounds); |
| 63 if (!fBoundsDirty) { |
| 64 fBounds = *bounds; |
| 65 } |
| 66 |
| 67 SkASSERT(SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()); |
| 68 } |
| 69 |
| 70 SkTextChunk::~SkTextChunk() { |
| 71 sk_free(fGlyphs); |
| 72 sk_free(fPos); |
| 73 } |
| 74 |
| 75 const SkRect& SkTextChunk::bounds() const { |
| 76 if (fBoundsDirty) { |
| 77 fFont.measureText(fGlyphs, fGlyphCount, &fBounds); |
| 78 fBoundsDirty = false; |
| 79 } |
| 80 |
| 81 return fBounds; |
| 82 } |
| 83 |
| 84 void SkTextChunk::draw(SkBaseDevice* device, const SkDraw& draw, const SkPaint&
paint) const { |
| 85 SkASSERT(NULL != device); |
| 86 SkASSERT(SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()); |
| 87 |
| 88 size_t length = sizeof(uint16_t) * fGlyphCount; |
| 89 if (NULL != fPos) { |
| 90 SkASSERT(1 == fScalarsPerPos || 2 == fScalarsPerPos); |
| 91 device->drawPosText(draw, fGlyphs, length, fPos, 0, fScalarsPerPos, pain
t); |
| 92 } else { |
| 93 SkASSERT(0 == fScalarsPerPos); |
| 94 device->drawText(draw, fGlyphs, length, 0, 0, paint); |
| 95 } |
| 96 } |
| 97 |
| 98 const SkTextBlob* SkTextBlob::Create(const SkTextChunk *chunk) { |
| 99 SkTDArray<const SkTextChunk*> chunks; |
| 100 chunks.setReserve(1); |
| 101 *chunks.append() = chunk; |
| 102 return SkNEW_ARGS(SkTextBlob, (chunks)); |
| 103 } |
| 104 |
| 105 const SkTextBlob* SkTextBlob::Create(const SkTDArray<const SkTextChunk*>& chunks
) { |
| 106 return SkNEW_ARGS(SkTextBlob, (chunks)); |
| 107 } |
| 108 |
| 109 SkTextBlob::SkTextBlob(const SkTDArray<const SkTextChunk*>& chunks) |
| 110 : fChunks(chunks) |
| 111 , fUniqueID(SK_InvalidGenID) { |
| 112 } |
| 113 |
| 114 SkTextBlob::~SkTextBlob() { |
| 115 Iter it(this); |
| 116 while (const SkTextChunk* chunk = it.next()) { |
| 117 SkDELETE(chunk); |
| 118 } |
| 119 } |
| 120 |
| 121 uint32_t SkTextBlob::uniqueID() const { |
| 122 static int32_t gTextBlobGenerationID; // = 0; |
| 123 |
| 124 // do a loop in case our global wraps around, as we never want to |
| 125 // return SK_InvalidGenID |
| 126 while (SK_InvalidGenID == fUniqueID) { |
| 127 fUniqueID = sk_atomic_inc(&gTextBlobGenerationID) + 1; |
| 128 } |
| 129 |
| 130 return fUniqueID; |
| 131 } |
| 132 |
| 133 SkTextBlobBuilder::SkTextBlobBuilder() { |
| 134 } |
| 135 |
| 136 SkTextBlobBuilder::~SkTextBlobBuilder() { |
| 137 // unused chunks. |
| 138 for (int i = 0; i < fChunks.count(); ++i) { |
| 139 SkDELETE(fChunks[i]); |
| 140 } |
| 141 } |
| 142 |
| 143 const SkTextBlob* SkTextBlobBuilder::build() { |
| 144 const SkTextBlob* blob = SkTextBlob::Create(fChunks); |
| 145 fChunks.rewind(); |
| 146 return blob; |
| 147 } |
| 148 |
| 149 void SkTextBlobBuilder::addChunk(const SkTextChunk* chunk) { |
| 150 *fChunks.append() = chunk; |
| 151 } |
| 152 |
| 153 |
OLD | NEW |