Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkTextBlob.h
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 "../private/SkTemplates.h"
12 #include "../private/SkAtomics.h"
13 #include "SkPaint.h"
14 #include "SkString.h"
15 #include "SkRefCnt.h"
16 
17 class SkReadBuffer;
18 class SkWriteBuffer;
19 
24 class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> {
25 public:
29  const SkRect& bounds() const { return fBounds; }
30 
34  uint32_t uniqueID() const { return fUniqueID; }
35 
39  void flatten(SkWriteBuffer&) const;
40 
48  static sk_sp<SkTextBlob> MakeFromBuffer(SkReadBuffer&);
49 
50  static const SkTextBlob* CreateFromBuffer(SkReadBuffer& buffer) {
51  return MakeFromBuffer(buffer).release();
52  }
53 
54  enum GlyphPositioning : uint8_t {
55  kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph.
56  kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph.
57  kFull_Positioning = 2 // Point positioning -- two scalars per glyph.
58  };
59 
60 private:
61  friend class SkNVRefCnt<SkTextBlob>;
62  class RunRecord;
63 
64  explicit SkTextBlob(const SkRect& bounds);
65 
66  ~SkTextBlob();
67 
68  // Memory for objects of this class is created with sk_malloc rather than operator new and must
69  // be freed with sk_free.
70  void operator delete(void* p) { sk_free(p); }
71  void* operator new(size_t) {
72  SkFAIL("All blobs are created by placement new.");
73  return sk_malloc_throw(0);
74  }
75  void* operator new(size_t, void* p) { return p; }
76 
77  static unsigned ScalarsPerGlyph(GlyphPositioning pos);
78 
79  // Call when this blob is part of the key to a cache entry. This allows the cache
80  // to know automatically those entries can be purged when this SkTextBlob is deleted.
81  void notifyAddedToCache() const {
82  fAddedToCache.store(true);
83  }
84 
85  friend class GrTextBlobCache;
86  friend class SkTextBlobBuilder;
87  friend class SkTextBlobRunIterator;
88 
89  const SkRect fBounds;
90  const uint32_t fUniqueID;
91  mutable SkAtomic<bool> fAddedToCache;
92 
93  SkDEBUGCODE(size_t fStorageSize;)
94 
95  // The actual payload resides in externally-managed storage, following the object.
96  // (see the .cpp for more details)
97 
98  typedef SkRefCnt INHERITED;
99 };
100 
105 class SK_API SkTextBlobBuilder {
106 public:
108 
110 
117  sk_sp<SkTextBlob> make();
118 
140  struct RunBuffer {
141  SkGlyphID* glyphs;
142  SkScalar* pos;
143  char* utf8text;
144  uint32_t* clusters;
145  };
146 
164  const RunBuffer& allocRunText(const SkPaint& font,
165  int count,
166  SkScalar x,
167  SkScalar y,
168  int textByteCount,
169  SkString lang,
170  const SkRect* bounds = NULL);
171  const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
172  const SkRect* bounds = NULL) {
173  return this->allocRunText(font, count, x, y, 0, SkString(), bounds);
174  }
175 
193  const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
194  int textByteCount, SkString lang,
195  const SkRect* bounds = NULL);
196  const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
197  const SkRect* bounds = NULL) {
198  return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds);
199  }
200 
218  const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
219  int textByteCount, SkString lang,
220  const SkRect* bounds = NULL);
221  const RunBuffer& allocRunPos(const SkPaint& font, int count,
222  const SkRect* bounds = NULL) {
223  return this->allocRunTextPos(font, count, 0, SkString(), bounds);
224  }
225 
226 private:
227  void reserve(size_t size);
228  void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
229  int count, int textBytes, SkPoint offset, const SkRect* bounds);
230  bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
231  int count, SkPoint offset);
232  void updateDeferredBounds();
233 
234  static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
235  static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
236 
237  SkAutoTMalloc<uint8_t> fStorage;
238  size_t fStorageSize;
239  size_t fStorageUsed;
240 
241  SkRect fBounds;
242  int fRunCount;
243  bool fDeferredBounds;
244  size_t fLastRun; // index into fStorage
245 
246  RunBuffer fCurrentRunBuffer;
247 };
248 
249 #endif // SkTextBlob_DEFINED
SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
Definition: SkTextBlob.h:24
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:299
Definition: SkPoint.h:156
const SkRect & bounds() const
Returns a conservative blob bounding box.
Definition: SkTextBlob.h:29
Definition: SkRefCnt.h:125
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:45
Glyph and position buffers associated with a run.
Definition: SkTextBlob.h:140
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:246
Helper class for constructing SkTextBlobs.
Definition: SkTextBlob.h:105
Definition: SkWriteBuffer.h:27
Definition: SkRect.h:404
uint32_t uniqueID() const
Return a non-zero, unique value representing the text blob.
Definition: SkTextBlob.h:34
Definition: SkRefCnt.h:209
Light weight class for managing strings.
Definition: SkString.h:121