Skia
2DGraphicsLibrary
 All Classes 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 "SkPaint.h"
13 #include "SkRefCnt.h"
14 
15 class SkReadBuffer;
16 class SkWriteBuffer;
17 
22 class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> {
23 public:
27  const SkRect& bounds() const { return fBounds; }
28 
32  uint32_t uniqueID() const { return fUniqueID; }
33 
37  void flatten(SkWriteBuffer&) const;
38 
46  static sk_sp<SkTextBlob> MakeFromBuffer(SkReadBuffer&);
47 
48  static const SkTextBlob* CreateFromBuffer(SkReadBuffer& buffer) {
49  return MakeFromBuffer(buffer).release();
50  }
51 
52  enum GlyphPositioning {
53  kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph.
54  kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph.
55  kFull_Positioning = 2 // Point positioning -- two scalars per glyph.
56  };
57 
58 private:
59  friend class SkNVRefCnt<SkTextBlob>;
60  class RunRecord;
61 
62  SkTextBlob(int runCount, const SkRect& bounds);
63 
64  ~SkTextBlob();
65 
66  // Memory for objects of this class is created with sk_malloc rather than operator new and must
67  // be freed with sk_free.
68  void operator delete(void* p) { sk_free(p); }
69  void* operator new(size_t) {
70  SkFAIL("All blobs are created by placement new.");
71  return sk_malloc_throw(0);
72  }
73  void* operator new(size_t, void* p) { return p; }
74 
75  static unsigned ScalarsPerGlyph(GlyphPositioning pos);
76 
77  friend class SkTextBlobBuilder;
78  friend class SkTextBlobRunIterator;
79 
80  const int fRunCount;
81  const SkRect fBounds;
82  const uint32_t fUniqueID;
83 
84  SkDEBUGCODE(size_t fStorageSize;)
85 
86  // The actual payload resides in externally-managed storage, following the object.
87  // (see the .cpp for more details)
88 
89  typedef SkRefCnt INHERITED;
90 };
91 
96 class SK_API SkTextBlobBuilder {
97 public:
99 
101 
106  sk_sp<SkTextBlob> make();
107 
108  const SkTextBlob* build() {
109  return this->make().release();
110  }
111 
117  struct RunBuffer {
118  SkGlyphID* glyphs;
119  SkScalar* pos;
120  };
121 
135  const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
136  const SkRect* bounds = NULL);
137 
151  const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
152  const SkRect* bounds = NULL);
153 
167  const RunBuffer& allocRunPos(const SkPaint& font, int count, const SkRect* bounds = NULL);
168 
169 private:
170  void reserve(size_t size);
171  void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
172  int count, SkPoint offset, const SkRect* bounds);
173  bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
174  int count, SkPoint offset);
175  void updateDeferredBounds();
176 
177  static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
178  static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
179 
180  SkAutoTMalloc<uint8_t> fStorage;
181  size_t fStorageSize;
182  size_t fStorageUsed;
183 
184  SkRect fBounds;
185  int fRunCount;
186  bool fDeferredBounds;
187  size_t fLastRun; // index into fStorage
188 
189  RunBuffer fCurrentRunBuffer;
190 };
191 
192 #endif // SkTextBlob_DEFINED
T *SK_WARN_UNUSED_RESULT release()
Return the bare pointer, and set the internal object pointer to nullptr.
Definition: SkRefCnt.h:363
SK_API void * sk_malloc_throw(size_t size)
Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag.
SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
Definition: SkTextBlob.h:22
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:359
Definition: SkPoint.h:156
const SkRect & bounds() const
Returns a conservative blob bounding box.
Definition: SkTextBlob.h:27
Definition: SkRefCnt.h:135
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:46
Glyph and position buffers associated with a run.
Definition: SkTextBlob.h:117
SK_API void sk_free(void *)
Free memory returned by sk_malloc().
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:258
Helper class for constructing SkTextBlobs.
Definition: SkTextBlob.h:96
Definition: SkWriteBuffer.h:26
Definition: SkRect.h:390
uint32_t uniqueID() const
Return a non-zero, unique value representing the text blob.
Definition: SkTextBlob.h:32
Definition: SkRefCnt.h:221