blob: 40d5130907527943aa28f435753a8130c71e2ee0 [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
Mike Reed8e74cbc2017-12-08 13:20:01 -050017struct SkSerialProcs;
18struct SkDeserialProcs;
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
halcanary4f0a23a2016-08-30 11:58:33 -070039 enum GlyphPositioning : uint8_t {
fmalita00d5c2c2014-08-21 08:53:26 -070040 kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph.
41 kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph.
42 kFull_Positioning = 2 // Point positioning -- two scalars per glyph.
43 };
44
Mike Reedb99bedd2017-07-11 10:27:40 -040045 /**
46 * Serialize the typeface into a data blob, storing type uniqueID of each referenced typeface.
47 * During this process, each time a typeface is encountered, it is passed to the catalog,
48 * allowing the caller to what typeface IDs will need to be resolved in Deserialize().
49 */
Mike Reedaaa30562017-07-21 11:53:23 -040050 sk_sp<SkData> serialize(SkTypefaceCatalogerProc, void* ctx) const;
Mike Reedb99bedd2017-07-11 10:27:40 -040051
52 /**
Khushal42f8bc42018-04-03 17:51:40 -070053 * Similar to serialize above, but writes directly into |memory|. Returns bytes written or 0u
54 * if serialization failed due to insufficient size.
55 */
56 size_t serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const;
57
58 /**
Mike Reedb99bedd2017-07-11 10:27:40 -040059 * Re-create a text blob previously serialized. Since the serialized form records the uniqueIDs
60 * of its typefaces, deserialization requires that the caller provide the corresponding
61 * SkTypefaces for those IDs.
62 */
Mike Reedaaa30562017-07-21 11:53:23 -040063 static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
64 SkTypefaceResolverProc, void* ctx);
65
Mike Reed8e74cbc2017-12-08 13:20:01 -050066 sk_sp<SkData> serialize(const SkSerialProcs&) const;
67 static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size, const SkDeserialProcs&);
68
halcanary33779752015-10-27 14:01:05 -070069private:
mtkleinb47cd4b2016-08-09 12:20:04 -070070 friend class SkNVRefCnt<SkTextBlob>;
fmalita3c196de2014-09-20 05:40:22 -070071 class RunRecord;
72
Florin Malita3a9a7a32017-03-13 09:03:24 -040073 explicit SkTextBlob(const SkRect& bounds);
fmalita00d5c2c2014-08-21 08:53:26 -070074
mtkleinb47cd4b2016-08-09 12:20:04 -070075 ~SkTextBlob();
bsalomon07280312014-11-20 08:02:46 -080076
77 // Memory for objects of this class is created with sk_malloc rather than operator new and must
78 // be freed with sk_free.
Yong-Hwan Baek688a8e52018-07-09 14:14:26 +090079 void operator delete(void* p);
80 void* operator new(size_t);
81 void* operator new(size_t, void* p);
fmalita00d5c2c2014-08-21 08:53:26 -070082
fmalitab7425172014-08-26 07:56:44 -070083 static unsigned ScalarsPerGlyph(GlyphPositioning pos);
84
Florin Malita4a01ac92017-03-13 16:45:28 -040085 // Call when this blob is part of the key to a cache entry. This allows the cache
86 // to know automatically those entries can be purged when this SkTextBlob is deleted.
Jim Van Verth474d6872017-12-14 13:00:05 -050087 void notifyAddedToCache(uint32_t cacheID) const {
88 fCacheID.store(cacheID);
Florin Malita4a01ac92017-03-13 16:45:28 -040089 }
90
Herb Derby8a6348e2018-07-12 15:30:35 -040091 friend class SkGlyphRunList;
Florin Malita4a01ac92017-03-13 16:45:28 -040092 friend class GrTextBlobCache;
fmalita00d5c2c2014-08-21 08:53:26 -070093 friend class SkTextBlobBuilder;
Cary Clark53c87692018-07-17 08:59:34 -040094 friend class SkTextBlobPriv;
halcanary33779752015-10-27 14:01:05 -070095 friend class SkTextBlobRunIterator;
fmalita00d5c2c2014-08-21 08:53:26 -070096
Jim Van Verth474d6872017-12-14 13:00:05 -050097 const SkRect fBounds;
98 const uint32_t fUniqueID;
99 mutable SkAtomic<uint32_t> fCacheID;
fmalita00d5c2c2014-08-21 08:53:26 -0700100
fmalita3c196de2014-09-20 05:40:22 -0700101 SkDEBUGCODE(size_t fStorageSize;)
fmalita00d5c2c2014-08-21 08:53:26 -0700102
fmalita3c196de2014-09-20 05:40:22 -0700103 // The actual payload resides in externally-managed storage, following the object.
104 // (see the .cpp for more details)
fmalita00d5c2c2014-08-21 08:53:26 -0700105
106 typedef SkRefCnt INHERITED;
107};
108
109/** \class SkTextBlobBuilder
110
111 Helper class for constructing SkTextBlobs.
112 */
jbroman3053dfa2014-08-25 06:22:12 -0700113class SK_API SkTextBlobBuilder {
fmalita00d5c2c2014-08-21 08:53:26 -0700114public:
fmalita3c196de2014-09-20 05:40:22 -0700115 SkTextBlobBuilder();
fmalita00d5c2c2014-08-21 08:53:26 -0700116
117 ~SkTextBlobBuilder();
118
119 /**
Florin Malita3a9a7a32017-03-13 09:03:24 -0400120 * Returns an immutable SkTextBlob for the current runs/glyphs,
121 * or nullptr if no runs were allocated.
122 *
123 * The builder is reset and can be reused.
fmalita00d5c2c2014-08-21 08:53:26 -0700124 */
reed2ab90572016-08-10 14:16:41 -0700125 sk_sp<SkTextBlob> make();
126
fmalita00d5c2c2014-08-21 08:53:26 -0700127 /**
128 * Glyph and position buffers associated with a run.
129 *
halcanary4f0a23a2016-08-30 11:58:33 -0700130 * A run is a sequence of glyphs sharing the same font metrics
131 * and positioning mode.
132 *
133 * If textByteCount is 0, utf8text and clusters will be NULL (no
134 * character information will be associated with the glyphs).
135 *
136 * utf8text will point to a buffer of size textByteCount bytes.
137 *
138 * clusters (if not NULL) will point to an array of size count.
139 * For each glyph, give the byte-offset into the text for the
140 * first byte in the first character in that glyph's cluster.
141 * Each value in the array should be an integer less than
142 * textByteCount. Values in the array should either be
143 * monotonically increasing (left-to-right text) or monotonically
144 * decreasing (right-to-left text). This definiton is conviently
145 * the same as used by Harfbuzz's hb_glyph_info_t::cluster field,
146 * except that Harfbuzz interleaves glyphs and clusters.
fmalita00d5c2c2014-08-21 08:53:26 -0700147 */
148 struct RunBuffer {
halcanaryd0e95a52016-07-25 07:18:12 -0700149 SkGlyphID* glyphs;
fmalita00d5c2c2014-08-21 08:53:26 -0700150 SkScalar* pos;
halcanary4f0a23a2016-08-30 11:58:33 -0700151 char* utf8text;
152 uint32_t* clusters;
fmalita00d5c2c2014-08-21 08:53:26 -0700153 };
154
155 /**
156 * Allocates a new default-positioned run and returns its writable glyph buffer
157 * for direct manipulation.
158 *
159 * @param font The font to be used for this run.
160 * @param count Number of glyphs.
161 * @param x,y Position within the blob.
halcanary4f0a23a2016-08-30 11:58:33 -0700162 * @param textByteCount length of the original UTF-8 text that
163 * corresponds to this sequence of glyphs. If 0,
164 * text will not be included in the textblob.
165 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700166 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
167 * be used when computing the blob bounds, to avoid re-measuring.
168 *
169 * @return A writable glyph buffer, valid until the next allocRun() or
170 * build() call. The buffer is guaranteed to hold @count@ glyphs.
171 */
halcanary4f0a23a2016-08-30 11:58:33 -0700172 const RunBuffer& allocRunText(const SkPaint& font,
173 int count,
174 SkScalar x,
175 SkScalar y,
176 int textByteCount,
177 SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400178 const SkRect* bounds = nullptr);
fmalita00d5c2c2014-08-21 08:53:26 -0700179 const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400180 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700181 return this->allocRunText(font, count, x, y, 0, SkString(), bounds);
182 }
fmalita00d5c2c2014-08-21 08:53:26 -0700183
184 /**
185 * Allocates a new horizontally-positioned run and returns its writable glyph and position
186 * buffers for direct manipulation.
187 *
188 * @param font The font to be used for this run.
189 * @param count Number of glyphs.
190 * @param y Vertical offset within the blob.
halcanary4f0a23a2016-08-30 11:58:33 -0700191 * @param textByteCount length of the original UTF-8 text that
192 * corresponds to this sequence of glyphs. If 0,
193 * text will not be included in the textblob.
194 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700195 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
196 * be used when computing the blob bounds, to avoid re-measuring.
197 *
198 * @return Writable glyph and position buffers, valid until the next allocRun()
199 * or build() call. The buffers are guaranteed to hold @count@ elements.
200 */
halcanary4f0a23a2016-08-30 11:58:33 -0700201 const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
202 int textByteCount, SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400203 const SkRect* bounds = nullptr);
fmalita00d5c2c2014-08-21 08:53:26 -0700204 const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400205 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700206 return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds);
207 }
fmalita00d5c2c2014-08-21 08:53:26 -0700208
209 /**
210 * Allocates a new fully-positioned run and returns its writable glyph and position
211 * buffers for direct manipulation.
212 *
213 * @param font The font to be used for this run.
214 * @param count Number of glyphs.
halcanary4f0a23a2016-08-30 11:58:33 -0700215 * @param textByteCount length of the original UTF-8 text that
216 * corresponds to this sequence of glyphs. If 0,
217 * text will not be included in the textblob.
218 * @param lang Language code, currently unimplemented.
fmalita00d5c2c2014-08-21 08:53:26 -0700219 * @param bounds Optional run bounding box. If known in advance (!= NULL), it will
220 * be used when computing the blob bounds, to avoid re-measuring.
221 *
222 * @return Writable glyph and position buffers, valid until the next allocRun()
223 * or build() call. The glyph buffer and position buffer are
224 * guaranteed to hold @count@ and 2 * @count@ elements, respectively.
225 */
halcanary4f0a23a2016-08-30 11:58:33 -0700226 const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
227 int textByteCount, SkString lang,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400228 const SkRect* bounds = nullptr);
halcanary4f0a23a2016-08-30 11:58:33 -0700229 const RunBuffer& allocRunPos(const SkPaint& font, int count,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400230 const SkRect* bounds = nullptr) {
halcanary4f0a23a2016-08-30 11:58:33 -0700231 return this->allocRunTextPos(font, count, 0, SkString(), bounds);
232 }
fmalita00d5c2c2014-08-21 08:53:26 -0700233
234private:
fmalita3c196de2014-09-20 05:40:22 -0700235 void reserve(size_t size);
fmalita00d5c2c2014-08-21 08:53:26 -0700236 void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
halcanary4f0a23a2016-08-30 11:58:33 -0700237 int count, int textBytes, SkPoint offset, const SkRect* bounds);
fmalita3c196de2014-09-20 05:40:22 -0700238 bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
Florin Malitad923a712017-11-22 10:11:12 -0500239 uint32_t count, SkPoint offset);
fmalita00d5c2c2014-08-21 08:53:26 -0700240 void updateDeferredBounds();
241
fmalita3dc40ac2015-01-28 10:56:06 -0800242 static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
243 static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
244
fmalita3c196de2014-09-20 05:40:22 -0700245 SkAutoTMalloc<uint8_t> fStorage;
246 size_t fStorageSize;
247 size_t fStorageUsed;
fmalita00d5c2c2014-08-21 08:53:26 -0700248
fmalita3c196de2014-09-20 05:40:22 -0700249 SkRect fBounds;
250 int fRunCount;
251 bool fDeferredBounds;
252 size_t fLastRun; // index into fStorage
fmalita00d5c2c2014-08-21 08:53:26 -0700253
fmalita3c196de2014-09-20 05:40:22 -0700254 RunBuffer fCurrentRunBuffer;
fmalita00d5c2c2014-08-21 08:53:26 -0700255};
256
257#endif // SkTextBlob_DEFINED