blob: 672cc20c5ca058ff2687d3845d25905033205a40 [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 Reed8e74cbc2017-12-08 13:20:01 -050020struct SkSerialProcs;
21struct SkDeserialProcs;
22
Mike Reedaaa30562017-07-21 11:53:23 -040023typedef void (*SkTypefaceCatalogerProc)(SkTypeface*, void* ctx);
24typedef sk_sp<SkTypeface> (*SkTypefaceResolverProc)(uint32_t id, void* ctx);
Mike Reedb99bedd2017-07-11 10:27:40 -040025
fmalita00d5c2c2014-08-21 08:53:26 -070026/** \class SkTextBlob
27
28 SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
29*/
mtkleinb47cd4b2016-08-09 12:20:04 -070030class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> {
fmalita00d5c2c2014-08-21 08:53:26 -070031public:
32 /**
fmalita3dc40ac2015-01-28 10:56:06 -080033 * Returns a conservative blob bounding box.
fmalita00d5c2c2014-08-21 08:53:26 -070034 */
35 const SkRect& bounds() const { return fBounds; }
36
37 /**
38 * Return a non-zero, unique value representing the text blob.
39 */
joshualitt2af85832015-03-25 13:40:13 -070040 uint32_t uniqueID() const { return fUniqueID; }
fmalita00d5c2c2014-08-21 08:53:26 -070041
fmalita228a6f22014-08-28 13:59:42 -070042 /**
43 * Serialize to a buffer.
44 */
45 void flatten(SkWriteBuffer&) const;
46
47 /**
48 * Recreate an SkTextBlob that was serialized into a buffer.
49 *
50 * @param SkReadBuffer Serialized blob data.
51 * @return A new SkTextBlob representing the serialized data, or NULL if the buffer is
52 * invalid.
53 */
reed2ab90572016-08-10 14:16:41 -070054 static sk_sp<SkTextBlob> MakeFromBuffer(SkReadBuffer&);
55
halcanary4f0a23a2016-08-30 11:58:33 -070056 enum GlyphPositioning : uint8_t {
fmalita00d5c2c2014-08-21 08:53:26 -070057 kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph.
58 kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph.
59 kFull_Positioning = 2 // Point positioning -- two scalars per glyph.
60 };
61
Mike Reedb99bedd2017-07-11 10:27:40 -040062 /**
63 * Serialize the typeface into a data blob, storing type uniqueID of each referenced typeface.
64 * During this process, each time a typeface is encountered, it is passed to the catalog,
65 * allowing the caller to what typeface IDs will need to be resolved in Deserialize().
66 */
Mike Reedaaa30562017-07-21 11:53:23 -040067 sk_sp<SkData> serialize(SkTypefaceCatalogerProc, void* ctx) const;
Mike Reedb99bedd2017-07-11 10:27:40 -040068
69 /**
Khushal42f8bc42018-04-03 17:51:40 -070070 * Similar to serialize above, but writes directly into |memory|. Returns bytes written or 0u
71 * if serialization failed due to insufficient size.
72 */
73 size_t serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const;
74
75 /**
Mike Reedb99bedd2017-07-11 10:27:40 -040076 * Re-create a text blob previously serialized. Since the serialized form records the uniqueIDs
77 * of its typefaces, deserialization requires that the caller provide the corresponding
78 * SkTypefaces for those IDs.
79 */
Mike Reedaaa30562017-07-21 11:53:23 -040080 static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
81 SkTypefaceResolverProc, void* ctx);
82
Mike Reed8e74cbc2017-12-08 13:20:01 -050083 sk_sp<SkData> serialize(const SkSerialProcs&) const;
Mike Reedf6eb1f92017-12-19 17:12:13 -050084 sk_sp<SkData> serialize() const;
Mike Reed8e74cbc2017-12-08 13:20:01 -050085 static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size, const SkDeserialProcs&);
Mike Reedf6eb1f92017-12-19 17:12:13 -050086 static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size);
Mike Reed8e74cbc2017-12-08 13:20:01 -050087
halcanary33779752015-10-27 14:01:05 -070088private:
mtkleinb47cd4b2016-08-09 12:20:04 -070089 friend class SkNVRefCnt<SkTextBlob>;
fmalita3c196de2014-09-20 05:40:22 -070090 class RunRecord;
91
Florin Malita3a9a7a32017-03-13 09:03:24 -040092 explicit SkTextBlob(const SkRect& bounds);
fmalita00d5c2c2014-08-21 08:53:26 -070093
mtkleinb47cd4b2016-08-09 12:20:04 -070094 ~SkTextBlob();
bsalomon07280312014-11-20 08:02:46 -080095
96 // Memory for objects of this class is created with sk_malloc rather than operator new and must
97 // be freed with sk_free.
Yong-Hwan Baek688a8e52018-07-09 14:14:26 +090098 void operator delete(void* p);
99 void* operator new(size_t);
100 void* operator new(size_t, void* p);
fmalita00d5c2c2014-08-21 08:53:26 -0700101
fmalitab7425172014-08-26 07:56:44 -0700102 static unsigned ScalarsPerGlyph(GlyphPositioning pos);
103
Florin Malita4a01ac92017-03-13 16:45:28 -0400104 // Call when this blob is part of the key to a cache entry. This allows the cache
105 // to know automatically those entries can be purged when this SkTextBlob is deleted.
Jim Van Verth474d6872017-12-14 13:00:05 -0500106 void notifyAddedToCache(uint32_t cacheID) const {
107 fCacheID.store(cacheID);
Florin Malita4a01ac92017-03-13 16:45:28 -0400108 }
109
110 friend class GrTextBlobCache;
fmalita00d5c2c2014-08-21 08:53:26 -0700111 friend class SkTextBlobBuilder;
halcanary33779752015-10-27 14:01:05 -0700112 friend class SkTextBlobRunIterator;
fmalita00d5c2c2014-08-21 08:53:26 -0700113
Jim Van Verth474d6872017-12-14 13:00:05 -0500114 const SkRect fBounds;
115 const uint32_t fUniqueID;
116 mutable SkAtomic<uint32_t> fCacheID;
fmalita00d5c2c2014-08-21 08:53:26 -0700117
fmalita3c196de2014-09-20 05:40:22 -0700118 SkDEBUGCODE(size_t fStorageSize;)
fmalita00d5c2c2014-08-21 08:53:26 -0700119
fmalita3c196de2014-09-20 05:40:22 -0700120 // The actual payload resides in externally-managed storage, following the object.
121 // (see the .cpp for more details)
fmalita00d5c2c2014-08-21 08:53:26 -0700122
123 typedef SkRefCnt INHERITED;
124};
125
126/** \class SkTextBlobBuilder
127
128 Helper class for constructing SkTextBlobs.
129 */
jbroman3053dfa2014-08-25 06:22:12 -0700130class SK_API SkTextBlobBuilder {
fmalita00d5c2c2014-08-21 08:53:26 -0700131public:
fmalita3c196de2014-09-20 05:40:22 -0700132 SkTextBlobBuilder();
fmalita00d5c2c2014-08-21 08:53:26 -0700133
134 ~SkTextBlobBuilder();
135
136 /**
Florin Malita3a9a7a32017-03-13 09:03:24 -0400137 * Returns an immutable SkTextBlob for the current runs/glyphs,
138 * or nullptr if no runs were allocated.
139 *
140 * The builder is reset and can be reused.
fmalita00d5c2c2014-08-21 08:53:26 -0700141 */
reed2ab90572016-08-10 14:16:41 -0700142 sk_sp<SkTextBlob> make();
143
fmalita00d5c2c2014-08-21 08:53:26 -0700144 /**
145 * Glyph and position buffers associated with a run.
146 *
halcanary4f0a23a2016-08-30 11:58:33 -0700147 * A run is a sequence of glyphs sharing the same font metrics
148 * and positioning mode.
149 *
150 * If textByteCount is 0, utf8text and clusters will be NULL (no
151 * character information will be associated with the glyphs).
152 *
153 * utf8text will point to a buffer of size textByteCount bytes.
154 *
155 * clusters (if not NULL) will point to an array of size count.
156 * For each glyph, give the byte-offset into the text for the
157 * first byte in the first character in that glyph's cluster.
158 * Each value in the array should be an integer less than
159 * textByteCount. Values in the array should either be
160 * monotonically increasing (left-to-right text) or monotonically
161 * decreasing (right-to-left text). This definiton is conviently
162 * the same as used by Harfbuzz's hb_glyph_info_t::cluster field,
163 * except that Harfbuzz interleaves glyphs and clusters.
fmalita00d5c2c2014-08-21 08:53:26 -0700164 */
165 struct RunBuffer {
halcanaryd0e95a52016-07-25 07:18:12 -0700166 SkGlyphID* glyphs;
fmalita00d5c2c2014-08-21 08:53:26 -0700167 SkScalar* pos;
halcanary4f0a23a2016-08-30 11:58:33 -0700168 char* utf8text;
169 uint32_t* clusters;
fmalita00d5c2c2014-08-21 08:53:26 -0700170 };
171
172 /**
173 * Allocates a new default-positioned run and returns its writable glyph buffer
174 * for direct manipulation.
175 *
176 * @param font The font to be used for this run.
177 * @param count Number of glyphs.
178 * @param x,y Position within the blob.
halcanary4f0a23a2016-08-30 11:58:33 -0700179 * @param textByteCount length of the original UTF-8 text that
180 * corresponds to this sequence of glyphs. If 0,
181 * text will not be included in the textblob.
182 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700183 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
184 * be used when computing the blob bounds, to avoid re-measuring.
185 *
186 * @return A writable glyph buffer, valid until the next allocRun() or
187 * build() call. The buffer is guaranteed to hold @count@ glyphs.
188 */
halcanary4f0a23a2016-08-30 11:58:33 -0700189 const RunBuffer& allocRunText(const SkPaint& font,
190 int count,
191 SkScalar x,
192 SkScalar y,
193 int textByteCount,
194 SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400195 const SkRect* bounds = nullptr);
fmalita00d5c2c2014-08-21 08:53:26 -0700196 const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400197 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700198 return this->allocRunText(font, count, x, y, 0, SkString(), bounds);
199 }
fmalita00d5c2c2014-08-21 08:53:26 -0700200
201 /**
202 * Allocates a new horizontally-positioned run and returns its writable glyph and position
203 * buffers for direct manipulation.
204 *
205 * @param font The font to be used for this run.
206 * @param count Number of glyphs.
207 * @param y Vertical offset within the blob.
halcanary4f0a23a2016-08-30 11:58:33 -0700208 * @param textByteCount length of the original UTF-8 text that
209 * corresponds to this sequence of glyphs. If 0,
210 * text will not be included in the textblob.
211 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700212 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
213 * be used when computing the blob bounds, to avoid re-measuring.
214 *
215 * @return Writable glyph and position buffers, valid until the next allocRun()
216 * or build() call. The buffers are guaranteed to hold @count@ elements.
217 */
halcanary4f0a23a2016-08-30 11:58:33 -0700218 const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
219 int textByteCount, SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400220 const SkRect* bounds = nullptr);
fmalita00d5c2c2014-08-21 08:53:26 -0700221 const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400222 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700223 return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds);
224 }
fmalita00d5c2c2014-08-21 08:53:26 -0700225
226 /**
227 * Allocates a new fully-positioned run and returns its writable glyph and position
228 * buffers for direct manipulation.
229 *
230 * @param font The font to be used for this run.
231 * @param count Number of glyphs.
halcanary4f0a23a2016-08-30 11:58:33 -0700232 * @param textByteCount length of the original UTF-8 text that
233 * corresponds to this sequence of glyphs. If 0,
234 * text will not be included in the textblob.
235 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700236 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
237 * be used when computing the blob bounds, to avoid re-measuring.
238 *
239 * @return Writable glyph and position buffers, valid until the next allocRun()
240 * or build() call. The glyph buffer and position buffer are
241 * guaranteed to hold @count@ and 2 * @count@ elements, respectively.
242 */
halcanary4f0a23a2016-08-30 11:58:33 -0700243 const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
244 int textByteCount, SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400245 const SkRect* bounds = nullptr);
halcanary4f0a23a2016-08-30 11:58:33 -0700246 const RunBuffer& allocRunPos(const SkPaint& font, int count,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400247 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700248 return this->allocRunTextPos(font, count, 0, SkString(), bounds);
249 }
fmalita00d5c2c2014-08-21 08:53:26 -0700250
251private:
fmalita3c196de2014-09-20 05:40:22 -0700252 void reserve(size_t size);
fmalita00d5c2c2014-08-21 08:53:26 -0700253 void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
halcanary4f0a23a2016-08-30 11:58:33 -0700254 int count, int textBytes, SkPoint offset, const SkRect* bounds);
fmalita3c196de2014-09-20 05:40:22 -0700255 bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
Florin Malitad923a712017-11-22 10:11:12 -0500256 uint32_t count, SkPoint offset);
fmalita00d5c2c2014-08-21 08:53:26 -0700257 void updateDeferredBounds();
258
fmalita3dc40ac2015-01-28 10:56:06 -0800259 static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
260 static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
261
fmalita3c196de2014-09-20 05:40:22 -0700262 SkAutoTMalloc<uint8_t> fStorage;
263 size_t fStorageSize;
264 size_t fStorageUsed;
fmalita00d5c2c2014-08-21 08:53:26 -0700265
fmalita3c196de2014-09-20 05:40:22 -0700266 SkRect fBounds;
267 int fRunCount;
268 bool fDeferredBounds;
269 size_t fLastRun; // index into fStorage
fmalita00d5c2c2014-08-21 08:53:26 -0700270
fmalita3c196de2014-09-20 05:40:22 -0700271 RunBuffer fCurrentRunBuffer;
fmalita00d5c2c2014-08-21 08:53:26 -0700272};
273
274#endif // SkTextBlob_DEFINED