Skia
2D Graphics Library
SkTextBlob.h
Go to the documentation of this file.
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 #ifndef SkTextBlob_DEFINED
9 #define SkTextBlob_DEFINED
10 
11 #include "include/core/SkFont.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkString.h"
15 #include "include/private/SkTemplates.h"
16 
17 #include <atomic>
18 
19 struct SkRSXform;
20 struct SkSerialProcs;
21 struct SkDeserialProcs;
22 
28 class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> {
29 private:
30  class RunRecord;
31 
32 public:
33 
40  const SkRect& bounds() const { return fBounds; }
41 
46  uint32_t uniqueID() const { return fUniqueID; }
47 
62  int getIntercepts(const SkScalar bounds[2], SkScalar intervals[],
63  const SkPaint* paint = nullptr) const;
64 
82  static sk_sp<SkTextBlob> MakeFromText(const void* text, size_t byteLength, const SkFont& font,
84 
102  static sk_sp<SkTextBlob> MakeFromString(const char* string, const SkFont& font,
104  if (!string) {
105  return nullptr;
106  }
107  return MakeFromText(string, strlen(string), font, encoding);
108  }
109 
122  static sk_sp<SkTextBlob> MakeFromPosTextH(const void* text, size_t byteLength,
123  const SkScalar xpos[], SkScalar constY, const SkFont& font,
125 
137  static sk_sp<SkTextBlob> MakeFromPosText(const void* text, size_t byteLength,
138  const SkPoint pos[], const SkFont& font,
140 
141  static sk_sp<SkTextBlob> MakeFromRSXform(const void* text, size_t byteLength,
142  const SkRSXform xform[], const SkFont& font,
144 
162  size_t serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const;
163 
177  sk_sp<SkData> serialize(const SkSerialProcs& procs) const;
178 
193  static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
194  const SkDeserialProcs& procs);
195 
196  class SK_API Iter {
197  public:
198  struct Run {
201  const uint16_t* fGlyphIndices;
202  };
203 
204  Iter(const SkTextBlob&);
205 
210  bool next(Run*);
211 
212  private:
213  const RunRecord* fRunRecord;
214  };
215 
216 private:
217  friend class SkNVRefCnt<SkTextBlob>;
218 
219  enum GlyphPositioning : uint8_t;
220 
221  explicit SkTextBlob(const SkRect& bounds);
222 
223  ~SkTextBlob();
224 
225  // Memory for objects of this class is created with sk_malloc rather than operator new and must
226  // be freed with sk_free.
227  void operator delete(void* p);
228  void* operator new(size_t);
229  void* operator new(size_t, void* p);
230 
231  static unsigned ScalarsPerGlyph(GlyphPositioning pos);
232 
233  // Call when this blob is part of the key to a cache entry. This allows the cache
234  // to know automatically those entries can be purged when this SkTextBlob is deleted.
235  void notifyAddedToCache(uint32_t cacheID) const {
236  fCacheID.store(cacheID);
237  }
238 
239  friend class SkGlyphRunList;
240  friend class GrTextBlobCache;
241  friend class SkTextBlobBuilder;
242  friend class SkTextBlobPriv;
243  friend class SkTextBlobRunIterator;
244 
245  const SkRect fBounds;
246  const uint32_t fUniqueID;
247  mutable std::atomic<uint32_t> fCacheID;
248 
249  SkDEBUGCODE(size_t fStorageSize;)
250 
251  // The actual payload resides in externally-managed storage, following the object.
252  // (see the .cpp for more details)
253 
254  using INHERITED = SkRefCnt;
255 };
256 
261 public:
262 
270 
274 
287 
298  struct RunBuffer {
301  char* utf8text;
302  uint32_t* clusters;
303 
304  // Helpers, since the "pos" field can be different types (always some number of floats).
305  SkPoint* points() const { return reinterpret_cast<SkPoint*>(pos); }
306  SkRSXform* xforms() const { return reinterpret_cast<SkRSXform*>(pos); }
307  };
308 
330  const RunBuffer& allocRun(const SkFont& font, int count, SkScalar x, SkScalar y,
331  const SkRect* bounds = nullptr);
332 
354  const RunBuffer& allocRunPosH(const SkFont& font, int count, SkScalar y,
355  const SkRect* bounds = nullptr);
356 
377  const RunBuffer& allocRunPos(const SkFont& font, int count,
378  const SkRect* bounds = nullptr);
379 
380  // RunBuffer.pos points to SkRSXform array
381  const RunBuffer& allocRunRSXform(const SkFont& font, int count);
382 
383 private:
384  const RunBuffer& allocRunText(const SkFont& font,
385  int count,
386  SkScalar x,
387  SkScalar y,
388  int textByteCount,
389  SkString lang,
390  const SkRect* bounds = nullptr);
391  const RunBuffer& allocRunTextPosH(const SkFont& font, int count, SkScalar y,
392  int textByteCount, SkString lang,
393  const SkRect* bounds = nullptr);
394  const RunBuffer& allocRunTextPos(const SkFont& font, int count,
395  int textByteCount, SkString lang,
396  const SkRect* bounds = nullptr);
397  const RunBuffer& allocRunRSXform(const SkFont& font, int count,
398  int textByteCount, SkString lang,
399  const SkRect* bounds = nullptr);
400 
401  void reserve(size_t size);
402  void allocInternal(const SkFont& font, SkTextBlob::GlyphPositioning positioning,
403  int count, int textBytes, SkPoint offset, const SkRect* bounds);
404  bool mergeRun(const SkFont& font, SkTextBlob::GlyphPositioning positioning,
405  uint32_t count, SkPoint offset);
406  void updateDeferredBounds();
407 
408  static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
409  static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
410 
411  friend class SkTextBlobPriv;
412  friend class SkTextBlobBuilderPriv;
413 
414  SkAutoTMalloc<uint8_t> fStorage;
415  size_t fStorageSize;
416  size_t fStorageUsed;
417 
418  SkRect fBounds;
419  int fRunCount;
420  bool fDeferredBounds;
421  size_t fLastRun; // index into fStorage
422 
423  RunBuffer fCurrentRunBuffer;
424 };
425 
426 #endif // SkTextBlob_DEFINED
SkGlyphID
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:556
SkTextBlob
Definition: SkTextBlob.h:28
SkRefCnt
Definition: SkRefCnt.h:118
SkTextBlobBuilder::~SkTextBlobBuilder
~SkTextBlobBuilder()
Deletes data allocated internally by SkTextBlobBuilder.
SkRSXform
A compressed form of a rotation+scale matrix.
Definition: SkRSXform.h:21
SkTextBlob::MakeFromText
static sk_sp< SkTextBlob > MakeFromText(const void *text, size_t byteLength, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Creates SkTextBlob with a single run.
SkTextBlob::bounds
const SkRect & bounds() const
Returns conservative bounding box.
Definition: SkTextBlob.h:40
SkNVRefCnt
Definition: SkRefCnt.h:159
SkTextBlob::MakeFromPosText
static sk_sp< SkTextBlob > MakeFromPosText(const void *text, size_t byteLength, const SkPoint pos[], const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Returns a textblob built from a single run of text with positions.
SkTextBlobBuilder::allocRunPos
const RunBuffer & allocRunPos(const SkFont &font, int count, const SkRect *bounds=nullptr)
Returns run with storage for glyphs and SkPoint positions.
SkTextBlobBuilder::allocRunPosH
const RunBuffer & allocRunPosH(const SkFont &font, int count, SkScalar y, const SkRect *bounds=nullptr)
Returns run with storage for glyphs and positions along baseline.
SkTextBlob::MakeFromPosTextH
static sk_sp< SkTextBlob > MakeFromPosTextH(const void *text, size_t byteLength, const SkScalar xpos[], SkScalar constY, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Returns a textblob built from a single run of text with x-positions and a single y value.
SkTypeface
Definition: SkTypeface.h:45
SkRefCnt.h
sk_sp< SkTextBlob >
SkTextBlobBuilder::allocRun
const RunBuffer & allocRun(const SkFont &font, int count, SkScalar x, SkScalar y, const SkRect *bounds=nullptr)
Returns run with storage for glyphs.
SkTextEncoding::kUTF8
@ kUTF8
uses bytes to represent UTF-8 or ASCII
SkTextBlob::getIntercepts
int getIntercepts(const SkScalar bounds[2], SkScalar intervals[], const SkPaint *paint=nullptr) const
Returns the number of intervals that intersect bounds.
SkTextBlobBuilder::RunBuffer::pos
SkScalar * pos
storage for positions in run
Definition: SkTextBlob.h:300
SkTextBlobBuilder::RunBuffer::points
SkPoint * points() const
Definition: SkTextBlob.h:305
SkPoint
Definition: SkPoint.h:160
SkTextBlobBuilder
Definition: SkTextBlob.h:260
SkDEBUGCODE
#define SkDEBUGCODE(...)
Definition: SkTypes.h:467
SkTextBlobBuilder::RunBuffer::xforms
SkRSXform * xforms() const
Definition: SkTextBlob.h:306
SkPaint
Definition: SkPaint.h:39
SkTextBlobBuilder::SkTextBlobBuilder
SkTextBlobBuilder()
Constructs empty SkTextBlobBuilder.
SK_API
#define SK_API
Definition: SkTypes.h:181
SkTextBlobBuilder::make
sk_sp< SkTextBlob > make()
Returns SkTextBlob built from runs of glyphs added by builder.
SkTextBlob::Iter::Run
Definition: SkTextBlob.h:198
SkTextBlob::Iter::next
bool next(Run *)
Returns true for each "run" inside the textblob, setting the Run fields (if not null).
SkPaint.h
SkTextBlob::Iter::Iter
Iter(const SkTextBlob &)
SkTextBlobBuilder::RunBuffer::utf8text
char * utf8text
reserved for future use
Definition: SkTextBlob.h:301
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkTextBlob::uniqueID
uint32_t uniqueID() const
Returns a non-zero value unique among all text blobs.
Definition: SkTextBlob.h:46
SkTextBlob::Iter::Run::fGlyphCount
int fGlyphCount
Definition: SkTextBlob.h:200
SkTextEncoding
SkTextEncoding
Definition: SkFontTypes.h:11
SkTextBlob::Iter::Run::fTypeface
SkTypeface * fTypeface
Definition: SkTextBlob.h:199
SkTextBlob::serialize
sk_sp< SkData > serialize(const SkSerialProcs &procs) const
Returns storage containing SkData describing SkTextBlob, using optional custom encoders.
SkTextBlob::MakeFromString
static sk_sp< SkTextBlob > MakeFromString(const char *string, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Creates SkTextBlob with a single run.
Definition: SkTextBlob.h:102
SkTextBlob::serialize
size_t serialize(const SkSerialProcs &procs, void *memory, size_t memory_size) const
Writes data to allow later reconstruction of SkTextBlob.
SkString.h
SkTextBlobBuilder::RunBuffer
Definition: SkTextBlob.h:298
SkTextBlobBuilder::RunBuffer::glyphs
SkGlyphID * glyphs
storage for glyphs in run
Definition: SkTextBlob.h:299
SkSerialProcs
Definition: SkSerialProcs.h:50
SkFont.h
SkTextBlob::Iter
Definition: SkTextBlob.h:196
SkString
Definition: SkString.h:116
SkFont
Definition: SkFont.h:23
SkRect
Definition: SkRect.h:572
SkDeserialProcs
Definition: SkSerialProcs.h:61
SkTextBlobBuilder::RunBuffer::clusters
uint32_t * clusters
reserved for future use
Definition: SkTextBlob.h:302
SkTextBlob::MakeFromRSXform
static sk_sp< SkTextBlob > MakeFromRSXform(const void *text, size_t byteLength, const SkRSXform xform[], const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
SkTextBlob::Iter::Run::fGlyphIndices
const uint16_t * fGlyphIndices
Definition: SkTextBlob.h:201
SkTextBlobBuilder::allocRunRSXform
const RunBuffer & allocRunRSXform(const SkFont &font, int count)