blob: a87c9a3d2e65dbd7a3b6889304975a76edd5d393 [file] [log] [blame]
fmalita00d5c2c2014-08-21 08:53:26 -07001/*
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#ifndef SkTextBlob_DEFINED
9#define SkTextBlob_DEFINED
10
bungemanbf521ff2016-02-17 13:13:44 -080011#include "../private/SkTemplates.h"
Florin Malita4a01ac92017-03-13 16:45:28 -040012#include "../private/SkAtomics.h"
fmalita00d5c2c2014-08-21 08:53:26 -070013#include "SkPaint.h"
halcanary4f0a23a2016-08-30 11:58:33 -070014#include "SkString.h"
fmalita00d5c2c2014-08-21 08:53:26 -070015#include "SkRefCnt.h"
fmalita00d5c2c2014-08-21 08:53:26 -070016
fmalitab7425172014-08-26 07:56:44 -070017class SkReadBuffer;
18class SkWriteBuffer;
19
Mike Reedaaa30562017-07-21 11:53:23 -040020typedef void (*SkTypefaceCatalogerProc)(SkTypeface*, void* ctx);
21typedef sk_sp<SkTypeface> (*SkTypefaceResolverProc)(uint32_t id, void* ctx);
Mike Reedb99bedd2017-07-11 10:27:40 -040022
fmalita00d5c2c2014-08-21 08:53:26 -070023/** \class SkTextBlob
24
25 SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
26*/
mtkleinb47cd4b2016-08-09 12:20:04 -070027class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> {
fmalita00d5c2c2014-08-21 08:53:26 -070028public:
29 /**
fmalita3dc40ac2015-01-28 10:56:06 -080030 * Returns a conservative blob bounding box.
fmalita00d5c2c2014-08-21 08:53:26 -070031 */
32 const SkRect& bounds() const { return fBounds; }
33
34 /**
35 * Return a non-zero, unique value representing the text blob.
36 */
joshualitt2af85832015-03-25 13:40:13 -070037 uint32_t uniqueID() const { return fUniqueID; }
fmalita00d5c2c2014-08-21 08:53:26 -070038
fmalita228a6f22014-08-28 13:59:42 -070039 /**
40 * Serialize to a buffer.
41 */
42 void flatten(SkWriteBuffer&) const;
43
44 /**
45 * Recreate an SkTextBlob that was serialized into a buffer.
46 *
47 * @param SkReadBuffer Serialized blob data.
48 * @return A new SkTextBlob representing the serialized data, or NULL if the buffer is
49 * invalid.
50 */
reed2ab90572016-08-10 14:16:41 -070051 static sk_sp<SkTextBlob> MakeFromBuffer(SkReadBuffer&);
52
halcanary4f0a23a2016-08-30 11:58:33 -070053 enum GlyphPositioning : uint8_t {
fmalita00d5c2c2014-08-21 08:53:26 -070054 kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph.
55 kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph.
56 kFull_Positioning = 2 // Point positioning -- two scalars per glyph.
57 };
58
Mike Reedb99bedd2017-07-11 10:27:40 -040059 /**
60 * Serialize the typeface into a data blob, storing type uniqueID of each referenced typeface.
61 * During this process, each time a typeface is encountered, it is passed to the catalog,
62 * allowing the caller to what typeface IDs will need to be resolved in Deserialize().
63 */
Mike Reedaaa30562017-07-21 11:53:23 -040064 sk_sp<SkData> serialize(SkTypefaceCatalogerProc, void* ctx) const;
Mike Reedb99bedd2017-07-11 10:27:40 -040065
66 /**
67 * Re-create a text blob previously serialized. Since the serialized form records the uniqueIDs
68 * of its typefaces, deserialization requires that the caller provide the corresponding
69 * SkTypefaces for those IDs.
70 */
Mike Reedaaa30562017-07-21 11:53:23 -040071 static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
72 SkTypefaceResolverProc, void* ctx);
73
halcanary33779752015-10-27 14:01:05 -070074private:
mtkleinb47cd4b2016-08-09 12:20:04 -070075 friend class SkNVRefCnt<SkTextBlob>;
fmalita3c196de2014-09-20 05:40:22 -070076 class RunRecord;
77
Florin Malita3a9a7a32017-03-13 09:03:24 -040078 explicit SkTextBlob(const SkRect& bounds);
fmalita00d5c2c2014-08-21 08:53:26 -070079
mtkleinb47cd4b2016-08-09 12:20:04 -070080 ~SkTextBlob();
bsalomon07280312014-11-20 08:02:46 -080081
82 // Memory for objects of this class is created with sk_malloc rather than operator new and must
83 // be freed with sk_free.
84 void operator delete(void* p) { sk_free(p); }
85 void* operator new(size_t) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040086 SK_ABORT("All blobs are created by placement new.");
bsalomon07280312014-11-20 08:02:46 -080087 return sk_malloc_throw(0);
88 }
89 void* operator new(size_t, void* p) { return p; }
fmalita00d5c2c2014-08-21 08:53:26 -070090
fmalitab7425172014-08-26 07:56:44 -070091 static unsigned ScalarsPerGlyph(GlyphPositioning pos);
92
Florin Malita4a01ac92017-03-13 16:45:28 -040093 // Call when this blob is part of the key to a cache entry. This allows the cache
94 // to know automatically those entries can be purged when this SkTextBlob is deleted.
95 void notifyAddedToCache() const {
96 fAddedToCache.store(true);
97 }
98
99 friend class GrTextBlobCache;
fmalita00d5c2c2014-08-21 08:53:26 -0700100 friend class SkTextBlobBuilder;
halcanary33779752015-10-27 14:01:05 -0700101 friend class SkTextBlobRunIterator;
fmalita00d5c2c2014-08-21 08:53:26 -0700102
Florin Malita4a01ac92017-03-13 16:45:28 -0400103 const SkRect fBounds;
104 const uint32_t fUniqueID;
105 mutable SkAtomic<bool> fAddedToCache;
fmalita00d5c2c2014-08-21 08:53:26 -0700106
fmalita3c196de2014-09-20 05:40:22 -0700107 SkDEBUGCODE(size_t fStorageSize;)
fmalita00d5c2c2014-08-21 08:53:26 -0700108
fmalita3c196de2014-09-20 05:40:22 -0700109 // The actual payload resides in externally-managed storage, following the object.
110 // (see the .cpp for more details)
fmalita00d5c2c2014-08-21 08:53:26 -0700111
112 typedef SkRefCnt INHERITED;
113};
114
115/** \class SkTextBlobBuilder
116
117 Helper class for constructing SkTextBlobs.
118 */
jbroman3053dfa2014-08-25 06:22:12 -0700119class SK_API SkTextBlobBuilder {
fmalita00d5c2c2014-08-21 08:53:26 -0700120public:
fmalita3c196de2014-09-20 05:40:22 -0700121 SkTextBlobBuilder();
fmalita00d5c2c2014-08-21 08:53:26 -0700122
123 ~SkTextBlobBuilder();
124
125 /**
Florin Malita3a9a7a32017-03-13 09:03:24 -0400126 * Returns an immutable SkTextBlob for the current runs/glyphs,
127 * or nullptr if no runs were allocated.
128 *
129 * The builder is reset and can be reused.
fmalita00d5c2c2014-08-21 08:53:26 -0700130 */
reed2ab90572016-08-10 14:16:41 -0700131 sk_sp<SkTextBlob> make();
132
fmalita00d5c2c2014-08-21 08:53:26 -0700133 /**
134 * Glyph and position buffers associated with a run.
135 *
halcanary4f0a23a2016-08-30 11:58:33 -0700136 * A run is a sequence of glyphs sharing the same font metrics
137 * and positioning mode.
138 *
139 * If textByteCount is 0, utf8text and clusters will be NULL (no
140 * character information will be associated with the glyphs).
141 *
142 * utf8text will point to a buffer of size textByteCount bytes.
143 *
144 * clusters (if not NULL) will point to an array of size count.
145 * For each glyph, give the byte-offset into the text for the
146 * first byte in the first character in that glyph's cluster.
147 * Each value in the array should be an integer less than
148 * textByteCount. Values in the array should either be
149 * monotonically increasing (left-to-right text) or monotonically
150 * decreasing (right-to-left text). This definiton is conviently
151 * the same as used by Harfbuzz's hb_glyph_info_t::cluster field,
152 * except that Harfbuzz interleaves glyphs and clusters.
fmalita00d5c2c2014-08-21 08:53:26 -0700153 */
154 struct RunBuffer {
halcanaryd0e95a52016-07-25 07:18:12 -0700155 SkGlyphID* glyphs;
fmalita00d5c2c2014-08-21 08:53:26 -0700156 SkScalar* pos;
halcanary4f0a23a2016-08-30 11:58:33 -0700157 char* utf8text;
158 uint32_t* clusters;
fmalita00d5c2c2014-08-21 08:53:26 -0700159 };
160
161 /**
162 * Allocates a new default-positioned run and returns its writable glyph buffer
163 * for direct manipulation.
164 *
165 * @param font The font to be used for this run.
166 * @param count Number of glyphs.
167 * @param x,y Position within the blob.
halcanary4f0a23a2016-08-30 11:58:33 -0700168 * @param textByteCount length of the original UTF-8 text that
169 * corresponds to this sequence of glyphs. If 0,
170 * text will not be included in the textblob.
171 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700172 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
173 * be used when computing the blob bounds, to avoid re-measuring.
174 *
175 * @return A writable glyph buffer, valid until the next allocRun() or
176 * build() call. The buffer is guaranteed to hold @count@ glyphs.
177 */
halcanary4f0a23a2016-08-30 11:58:33 -0700178 const RunBuffer& allocRunText(const SkPaint& font,
179 int count,
180 SkScalar x,
181 SkScalar y,
182 int textByteCount,
183 SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400184 const SkRect* bounds = nullptr);
fmalita00d5c2c2014-08-21 08:53:26 -0700185 const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400186 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700187 return this->allocRunText(font, count, x, y, 0, SkString(), bounds);
188 }
fmalita00d5c2c2014-08-21 08:53:26 -0700189
190 /**
191 * Allocates a new horizontally-positioned run and returns its writable glyph and position
192 * buffers for direct manipulation.
193 *
194 * @param font The font to be used for this run.
195 * @param count Number of glyphs.
196 * @param y Vertical offset within the blob.
halcanary4f0a23a2016-08-30 11:58:33 -0700197 * @param textByteCount length of the original UTF-8 text that
198 * corresponds to this sequence of glyphs. If 0,
199 * text will not be included in the textblob.
200 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700201 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
202 * be used when computing the blob bounds, to avoid re-measuring.
203 *
204 * @return Writable glyph and position buffers, valid until the next allocRun()
205 * or build() call. The buffers are guaranteed to hold @count@ elements.
206 */
halcanary4f0a23a2016-08-30 11:58:33 -0700207 const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
208 int textByteCount, SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400209 const SkRect* bounds = nullptr);
fmalita00d5c2c2014-08-21 08:53:26 -0700210 const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400211 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700212 return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds);
213 }
fmalita00d5c2c2014-08-21 08:53:26 -0700214
215 /**
216 * Allocates a new fully-positioned run and returns its writable glyph and position
217 * buffers for direct manipulation.
218 *
219 * @param font The font to be used for this run.
220 * @param count Number of glyphs.
halcanary4f0a23a2016-08-30 11:58:33 -0700221 * @param textByteCount length of the original UTF-8 text that
222 * corresponds to this sequence of glyphs. If 0,
223 * text will not be included in the textblob.
224 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700225 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
226 * be used when computing the blob bounds, to avoid re-measuring.
227 *
228 * @return Writable glyph and position buffers, valid until the next allocRun()
229 * or build() call. The glyph buffer and position buffer are
230 * guaranteed to hold @count@ and 2 * @count@ elements, respectively.
231 */
halcanary4f0a23a2016-08-30 11:58:33 -0700232 const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
233 int textByteCount, SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400234 const SkRect* bounds = nullptr);
halcanary4f0a23a2016-08-30 11:58:33 -0700235 const RunBuffer& allocRunPos(const SkPaint& font, int count,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400236 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700237 return this->allocRunTextPos(font, count, 0, SkString(), bounds);
238 }
fmalita00d5c2c2014-08-21 08:53:26 -0700239
240private:
fmalita3c196de2014-09-20 05:40:22 -0700241 void reserve(size_t size);
fmalita00d5c2c2014-08-21 08:53:26 -0700242 void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
halcanary4f0a23a2016-08-30 11:58:33 -0700243 int count, int textBytes, SkPoint offset, const SkRect* bounds);
fmalita3c196de2014-09-20 05:40:22 -0700244 bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
Florin Malitad923a712017-11-22 10:11:12 -0500245 uint32_t count, SkPoint offset);
fmalita00d5c2c2014-08-21 08:53:26 -0700246 void updateDeferredBounds();
247
fmalita3dc40ac2015-01-28 10:56:06 -0800248 static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
249 static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
250
fmalita3c196de2014-09-20 05:40:22 -0700251 SkAutoTMalloc<uint8_t> fStorage;
252 size_t fStorageSize;
253 size_t fStorageUsed;
fmalita00d5c2c2014-08-21 08:53:26 -0700254
fmalita3c196de2014-09-20 05:40:22 -0700255 SkRect fBounds;
256 int fRunCount;
257 bool fDeferredBounds;
258 size_t fLastRun; // index into fStorage
fmalita00d5c2c2014-08-21 08:53:26 -0700259
fmalita3c196de2014-09-20 05:40:22 -0700260 RunBuffer fCurrentRunBuffer;
fmalita00d5c2c2014-08-21 08:53:26 -0700261};
262
263#endif // SkTextBlob_DEFINED