100d5c2c6523321d25b32905ff4822f083a4173eefmalita/* 200d5c2c6523321d25b32905ff4822f083a4173eefmalita * Copyright 2014 Google Inc. 300d5c2c6523321d25b32905ff4822f083a4173eefmalita * 400d5c2c6523321d25b32905ff4822f083a4173eefmalita * Use of this source code is governed by a BSD-style license that can be 500d5c2c6523321d25b32905ff4822f083a4173eefmalita * found in the LICENSE file. 600d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 700d5c2c6523321d25b32905ff4822f083a4173eefmalita 800d5c2c6523321d25b32905ff4822f083a4173eefmalita#ifndef SkTextBlob_DEFINED 900d5c2c6523321d25b32905ff4822f083a4173eefmalita#define SkTextBlob_DEFINED 1000d5c2c6523321d25b32905ff4822f083a4173eefmalita 1100d5c2c6523321d25b32905ff4822f083a4173eefmalita#include "SkPaint.h" 1200d5c2c6523321d25b32905ff4822f083a4173eefmalita#include "SkRefCnt.h" 1300d5c2c6523321d25b32905ff4822f083a4173eefmalita#include "SkTArray.h" 1400d5c2c6523321d25b32905ff4822f083a4173eefmalita#include "SkTDArray.h" 1500d5c2c6523321d25b32905ff4822f083a4173eefmalita 16b7425173f96e93b090787e2386ba5f022b6c2869fmalitaclass SkReadBuffer; 17b7425173f96e93b090787e2386ba5f022b6c2869fmalitaclass SkWriteBuffer; 18b7425173f96e93b090787e2386ba5f022b6c2869fmalita 1900d5c2c6523321d25b32905ff4822f083a4173eefmalita/** \class SkTextBlob 2000d5c2c6523321d25b32905ff4822f083a4173eefmalita 2100d5c2c6523321d25b32905ff4822f083a4173eefmalita SkTextBlob combines multiple text runs into an immutable, ref-counted structure. 2200d5c2c6523321d25b32905ff4822f083a4173eefmalita*/ 233053dfaefd12abd220d961e7e4c661e6eb8ba90ejbromanclass SK_API SkTextBlob : public SkRefCnt { 2400d5c2c6523321d25b32905ff4822f083a4173eefmalitapublic: 2500d5c2c6523321d25b32905ff4822f083a4173eefmalita /** 2600d5c2c6523321d25b32905ff4822f083a4173eefmalita * Returns the blob bounding box. 2700d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 2800d5c2c6523321d25b32905ff4822f083a4173eefmalita const SkRect& bounds() const { return fBounds; } 2900d5c2c6523321d25b32905ff4822f083a4173eefmalita 3000d5c2c6523321d25b32905ff4822f083a4173eefmalita /** 3100d5c2c6523321d25b32905ff4822f083a4173eefmalita * Return a non-zero, unique value representing the text blob. 3200d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 3300d5c2c6523321d25b32905ff4822f083a4173eefmalita uint32_t uniqueID() const; 3400d5c2c6523321d25b32905ff4822f083a4173eefmalita 35228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita /** 36228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita * Serialize to a buffer. 37228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita */ 38228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita void flatten(SkWriteBuffer&) const; 39228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita 40228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita /** 41228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita * Recreate an SkTextBlob that was serialized into a buffer. 42228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita * 43228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita * @param SkReadBuffer Serialized blob data. 44228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita * @return A new SkTextBlob representing the serialized data, or NULL if the buffer is 45228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita * invalid. 46228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita */ 47228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita static const SkTextBlob* CreateFromBuffer(SkReadBuffer&); 48228a6f275d7c5c17c48c20b57df971d9848f5dfffmalita 4900d5c2c6523321d25b32905ff4822f083a4173eefmalitaprivate: 5000d5c2c6523321d25b32905ff4822f083a4173eefmalita enum GlyphPositioning { 5100d5c2c6523321d25b32905ff4822f083a4173eefmalita kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph. 5200d5c2c6523321d25b32905ff4822f083a4173eefmalita kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph. 5300d5c2c6523321d25b32905ff4822f083a4173eefmalita kFull_Positioning = 2 // Point positioning -- two scalars per glyph. 5400d5c2c6523321d25b32905ff4822f083a4173eefmalita }; 5500d5c2c6523321d25b32905ff4822f083a4173eefmalita 5613645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita class RunRecord; 5713645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita 5800d5c2c6523321d25b32905ff4822f083a4173eefmalita class RunIterator { 5900d5c2c6523321d25b32905ff4822f083a4173eefmalita public: 6000d5c2c6523321d25b32905ff4822f083a4173eefmalita RunIterator(const SkTextBlob* blob); 6100d5c2c6523321d25b32905ff4822f083a4173eefmalita 6200d5c2c6523321d25b32905ff4822f083a4173eefmalita bool done() const; 6300d5c2c6523321d25b32905ff4822f083a4173eefmalita void next(); 6400d5c2c6523321d25b32905ff4822f083a4173eefmalita 6500d5c2c6523321d25b32905ff4822f083a4173eefmalita uint32_t glyphCount() const; 6600d5c2c6523321d25b32905ff4822f083a4173eefmalita const uint16_t* glyphs() const; 6700d5c2c6523321d25b32905ff4822f083a4173eefmalita const SkScalar* pos() const; 6800d5c2c6523321d25b32905ff4822f083a4173eefmalita const SkPoint& offset() const; 6937ecbaffd1f9f6841aa562aa31a824d109d52988fmalita void applyFontToPaint(SkPaint*) const; 7000d5c2c6523321d25b32905ff4822f083a4173eefmalita GlyphPositioning positioning() const; 7100d5c2c6523321d25b32905ff4822f083a4173eefmalita 7200d5c2c6523321d25b32905ff4822f083a4173eefmalita private: 7313645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita const RunRecord* fCurrentRun; 7413645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita int fRemainingRuns; 7500d5c2c6523321d25b32905ff4822f083a4173eefmalita 7613645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita SkDEBUGCODE(uint8_t* fStorageTop;) 7700d5c2c6523321d25b32905ff4822f083a4173eefmalita }; 7800d5c2c6523321d25b32905ff4822f083a4173eefmalita 7913645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita SkTextBlob(int runCount, const SkRect& bounds); 8013645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita 8113645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita virtual ~SkTextBlob(); 8213645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita virtual void internal_dispose() const SK_OVERRIDE; 8300d5c2c6523321d25b32905ff4822f083a4173eefmalita 84b7425173f96e93b090787e2386ba5f022b6c2869fmalita static unsigned ScalarsPerGlyph(GlyphPositioning pos); 85b7425173f96e93b090787e2386ba5f022b6c2869fmalita 86aa1b9120463fc69be7a5545eb83e6fbe6955a424fmalita friend class SkBaseDevice; 8700d5c2c6523321d25b32905ff4822f083a4173eefmalita friend class SkTextBlobBuilder; 88c6765d69e3aceaa316fe2d2ef00a7f0d138def2ffmalita friend class TextBlobTester; 8900d5c2c6523321d25b32905ff4822f083a4173eefmalita 9013645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita const int fRunCount; 9113645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita const SkRect fBounds; 9213645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita mutable uint32_t fUniqueID; 9300d5c2c6523321d25b32905ff4822f083a4173eefmalita 9413645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita SkDEBUGCODE(size_t fStorageSize;) 9500d5c2c6523321d25b32905ff4822f083a4173eefmalita 9613645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita // The actual payload resides in externally-managed storage, following the object. 9713645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita // (see the .cpp for more details) 9800d5c2c6523321d25b32905ff4822f083a4173eefmalita 9900d5c2c6523321d25b32905ff4822f083a4173eefmalita typedef SkRefCnt INHERITED; 10000d5c2c6523321d25b32905ff4822f083a4173eefmalita}; 10100d5c2c6523321d25b32905ff4822f083a4173eefmalita 10200d5c2c6523321d25b32905ff4822f083a4173eefmalita/** \class SkTextBlobBuilder 10300d5c2c6523321d25b32905ff4822f083a4173eefmalita 10400d5c2c6523321d25b32905ff4822f083a4173eefmalita Helper class for constructing SkTextBlobs. 10500d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 1063053dfaefd12abd220d961e7e4c661e6eb8ba90ejbromanclass SK_API SkTextBlobBuilder { 10700d5c2c6523321d25b32905ff4822f083a4173eefmalitapublic: 10813645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita SkTextBlobBuilder(); 10900d5c2c6523321d25b32905ff4822f083a4173eefmalita 11000d5c2c6523321d25b32905ff4822f083a4173eefmalita ~SkTextBlobBuilder(); 11100d5c2c6523321d25b32905ff4822f083a4173eefmalita 11200d5c2c6523321d25b32905ff4822f083a4173eefmalita /** 11300d5c2c6523321d25b32905ff4822f083a4173eefmalita * Returns an immutable SkTextBlob for the current runs/glyphs. The builder is reset and 11400d5c2c6523321d25b32905ff4822f083a4173eefmalita * can be reused. 11500d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 11600d5c2c6523321d25b32905ff4822f083a4173eefmalita const SkTextBlob* build(); 11700d5c2c6523321d25b32905ff4822f083a4173eefmalita 11800d5c2c6523321d25b32905ff4822f083a4173eefmalita /** 11900d5c2c6523321d25b32905ff4822f083a4173eefmalita * Glyph and position buffers associated with a run. 12000d5c2c6523321d25b32905ff4822f083a4173eefmalita * 12100d5c2c6523321d25b32905ff4822f083a4173eefmalita * A run is a sequence of glyphs sharing the same font metrics and positioning mode. 12200d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 12300d5c2c6523321d25b32905ff4822f083a4173eefmalita struct RunBuffer { 12400d5c2c6523321d25b32905ff4822f083a4173eefmalita uint16_t* glyphs; 12500d5c2c6523321d25b32905ff4822f083a4173eefmalita SkScalar* pos; 12600d5c2c6523321d25b32905ff4822f083a4173eefmalita }; 12700d5c2c6523321d25b32905ff4822f083a4173eefmalita 12800d5c2c6523321d25b32905ff4822f083a4173eefmalita /** 12900d5c2c6523321d25b32905ff4822f083a4173eefmalita * Allocates a new default-positioned run and returns its writable glyph buffer 13000d5c2c6523321d25b32905ff4822f083a4173eefmalita * for direct manipulation. 13100d5c2c6523321d25b32905ff4822f083a4173eefmalita * 13200d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param font The font to be used for this run. 13300d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param count Number of glyphs. 13400d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param x,y Position within the blob. 13500d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param bounds Optional run bounding box. If known in advance (!= NULL), it will 13600d5c2c6523321d25b32905ff4822f083a4173eefmalita * be used when computing the blob bounds, to avoid re-measuring. 13700d5c2c6523321d25b32905ff4822f083a4173eefmalita * 13800d5c2c6523321d25b32905ff4822f083a4173eefmalita * @return A writable glyph buffer, valid until the next allocRun() or 13900d5c2c6523321d25b32905ff4822f083a4173eefmalita * build() call. The buffer is guaranteed to hold @count@ glyphs. 14000d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 14100d5c2c6523321d25b32905ff4822f083a4173eefmalita const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y, 14200d5c2c6523321d25b32905ff4822f083a4173eefmalita const SkRect* bounds = NULL); 14300d5c2c6523321d25b32905ff4822f083a4173eefmalita 14400d5c2c6523321d25b32905ff4822f083a4173eefmalita /** 14500d5c2c6523321d25b32905ff4822f083a4173eefmalita * Allocates a new horizontally-positioned run and returns its writable glyph and position 14600d5c2c6523321d25b32905ff4822f083a4173eefmalita * buffers for direct manipulation. 14700d5c2c6523321d25b32905ff4822f083a4173eefmalita * 14800d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param font The font to be used for this run. 14900d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param count Number of glyphs. 15000d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param y Vertical offset within the blob. 15100d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param bounds Optional run bounding box. If known in advance (!= NULL), it will 15200d5c2c6523321d25b32905ff4822f083a4173eefmalita * be used when computing the blob bounds, to avoid re-measuring. 15300d5c2c6523321d25b32905ff4822f083a4173eefmalita * 15400d5c2c6523321d25b32905ff4822f083a4173eefmalita * @return Writable glyph and position buffers, valid until the next allocRun() 15500d5c2c6523321d25b32905ff4822f083a4173eefmalita * or build() call. The buffers are guaranteed to hold @count@ elements. 15600d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 15700d5c2c6523321d25b32905ff4822f083a4173eefmalita const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y, 15800d5c2c6523321d25b32905ff4822f083a4173eefmalita const SkRect* bounds = NULL); 15900d5c2c6523321d25b32905ff4822f083a4173eefmalita 16000d5c2c6523321d25b32905ff4822f083a4173eefmalita /** 16100d5c2c6523321d25b32905ff4822f083a4173eefmalita * Allocates a new fully-positioned run and returns its writable glyph and position 16200d5c2c6523321d25b32905ff4822f083a4173eefmalita * buffers for direct manipulation. 16300d5c2c6523321d25b32905ff4822f083a4173eefmalita * 16400d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param font The font to be used for this run. 16500d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param count Number of glyphs. 16600d5c2c6523321d25b32905ff4822f083a4173eefmalita * @param bounds Optional run bounding box. If known in advance (!= NULL), it will 16700d5c2c6523321d25b32905ff4822f083a4173eefmalita * be used when computing the blob bounds, to avoid re-measuring. 16800d5c2c6523321d25b32905ff4822f083a4173eefmalita * 16900d5c2c6523321d25b32905ff4822f083a4173eefmalita * @return Writable glyph and position buffers, valid until the next allocRun() 17000d5c2c6523321d25b32905ff4822f083a4173eefmalita * or build() call. The glyph buffer and position buffer are 17100d5c2c6523321d25b32905ff4822f083a4173eefmalita * guaranteed to hold @count@ and 2 * @count@ elements, respectively. 17200d5c2c6523321d25b32905ff4822f083a4173eefmalita */ 17300d5c2c6523321d25b32905ff4822f083a4173eefmalita const RunBuffer& allocRunPos(const SkPaint& font, int count, const SkRect* bounds = NULL); 17400d5c2c6523321d25b32905ff4822f083a4173eefmalita 17500d5c2c6523321d25b32905ff4822f083a4173eefmalitaprivate: 17613645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita void reserve(size_t size); 17700d5c2c6523321d25b32905ff4822f083a4173eefmalita void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning, 17800d5c2c6523321d25b32905ff4822f083a4173eefmalita int count, SkPoint offset, const SkRect* bounds); 17913645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning, 18013645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita int count, SkPoint offset); 18100d5c2c6523321d25b32905ff4822f083a4173eefmalita void updateDeferredBounds(); 18200d5c2c6523321d25b32905ff4822f083a4173eefmalita 18313645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita SkAutoTMalloc<uint8_t> fStorage; 18413645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita size_t fStorageSize; 18513645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita size_t fStorageUsed; 18600d5c2c6523321d25b32905ff4822f083a4173eefmalita 18713645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita SkRect fBounds; 18813645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita int fRunCount; 18913645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita bool fDeferredBounds; 19013645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita size_t fLastRun; // index into fStorage 19100d5c2c6523321d25b32905ff4822f083a4173eefmalita 19213645ea0ea87038ebd71be3bd6d53b313069a9e4fmalita RunBuffer fCurrentRunBuffer; 19300d5c2c6523321d25b32905ff4822f083a4173eefmalita}; 19400d5c2c6523321d25b32905ff4822f083a4173eefmalita 19500d5c2c6523321d25b32905ff4822f083a4173eefmalita#endif // SkTextBlob_DEFINED 196