2014-08-21 15:53:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkTextBlob_DEFINED
|
|
|
|
#define SkTextBlob_DEFINED
|
|
|
|
|
2016-02-17 21:13:44 +00:00
|
|
|
#include "../private/SkTemplates.h"
|
2017-03-13 20:45:28 +00:00
|
|
|
#include "../private/SkAtomics.h"
|
2014-08-21 15:53:26 +00:00
|
|
|
#include "SkPaint.h"
|
2016-08-30 18:58:33 +00:00
|
|
|
#include "SkString.h"
|
2014-08-21 15:53:26 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
2014-08-26 14:56:44 +00:00
|
|
|
class SkReadBuffer;
|
|
|
|
class SkWriteBuffer;
|
|
|
|
|
2017-12-08 18:20:01 +00:00
|
|
|
struct SkSerialProcs;
|
|
|
|
struct SkDeserialProcs;
|
|
|
|
|
2017-07-21 15:53:23 +00:00
|
|
|
typedef void (*SkTypefaceCatalogerProc)(SkTypeface*, void* ctx);
|
|
|
|
typedef sk_sp<SkTypeface> (*SkTypefaceResolverProc)(uint32_t id, void* ctx);
|
2017-07-11 14:27:40 +00:00
|
|
|
|
2014-08-21 15:53:26 +00:00
|
|
|
/** \class SkTextBlob
|
|
|
|
|
|
|
|
SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
|
|
|
|
*/
|
2016-08-09 19:20:04 +00:00
|
|
|
class SK_API SkTextBlob final : public SkNVRefCnt<SkTextBlob> {
|
2014-08-21 15:53:26 +00:00
|
|
|
public:
|
|
|
|
/**
|
2015-01-28 18:56:06 +00:00
|
|
|
* Returns a conservative blob bounding box.
|
2014-08-21 15:53:26 +00:00
|
|
|
*/
|
|
|
|
const SkRect& bounds() const { return fBounds; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a non-zero, unique value representing the text blob.
|
|
|
|
*/
|
2015-03-25 20:40:13 +00:00
|
|
|
uint32_t uniqueID() const { return fUniqueID; }
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-08-28 20:59:42 +00:00
|
|
|
/**
|
|
|
|
* Serialize to a buffer.
|
|
|
|
*/
|
|
|
|
void flatten(SkWriteBuffer&) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recreate an SkTextBlob that was serialized into a buffer.
|
|
|
|
*
|
|
|
|
* @param SkReadBuffer Serialized blob data.
|
|
|
|
* @return A new SkTextBlob representing the serialized data, or NULL if the buffer is
|
|
|
|
* invalid.
|
|
|
|
*/
|
2016-08-10 21:16:41 +00:00
|
|
|
static sk_sp<SkTextBlob> MakeFromBuffer(SkReadBuffer&);
|
|
|
|
|
2016-08-30 18:58:33 +00:00
|
|
|
enum GlyphPositioning : uint8_t {
|
2014-08-21 15:53:26 +00:00
|
|
|
kDefault_Positioning = 0, // Default glyph advances -- zero scalars per glyph.
|
|
|
|
kHorizontal_Positioning = 1, // Horizontal positioning -- one scalar per glyph.
|
|
|
|
kFull_Positioning = 2 // Point positioning -- two scalars per glyph.
|
|
|
|
};
|
|
|
|
|
2017-07-11 14:27:40 +00:00
|
|
|
/**
|
|
|
|
* Serialize the typeface into a data blob, storing type uniqueID of each referenced typeface.
|
|
|
|
* During this process, each time a typeface is encountered, it is passed to the catalog,
|
|
|
|
* allowing the caller to what typeface IDs will need to be resolved in Deserialize().
|
|
|
|
*/
|
2017-07-21 15:53:23 +00:00
|
|
|
sk_sp<SkData> serialize(SkTypefaceCatalogerProc, void* ctx) const;
|
2017-07-11 14:27:40 +00:00
|
|
|
|
2018-04-04 00:51:40 +00:00
|
|
|
/**
|
|
|
|
* Similar to serialize above, but writes directly into |memory|. Returns bytes written or 0u
|
|
|
|
* if serialization failed due to insufficient size.
|
|
|
|
*/
|
|
|
|
size_t serialize(const SkSerialProcs& procs, void* memory, size_t memory_size) const;
|
|
|
|
|
2017-07-11 14:27:40 +00:00
|
|
|
/**
|
|
|
|
* Re-create a text blob previously serialized. Since the serialized form records the uniqueIDs
|
|
|
|
* of its typefaces, deserialization requires that the caller provide the corresponding
|
|
|
|
* SkTypefaces for those IDs.
|
|
|
|
*/
|
2017-07-21 15:53:23 +00:00
|
|
|
static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
|
|
|
|
SkTypefaceResolverProc, void* ctx);
|
|
|
|
|
2017-12-08 18:20:01 +00:00
|
|
|
sk_sp<SkData> serialize(const SkSerialProcs&) const;
|
2017-12-19 22:12:13 +00:00
|
|
|
sk_sp<SkData> serialize() const;
|
2017-12-08 18:20:01 +00:00
|
|
|
static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size, const SkDeserialProcs&);
|
2017-12-19 22:12:13 +00:00
|
|
|
static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size);
|
2017-12-08 18:20:01 +00:00
|
|
|
|
2015-10-27 21:01:05 +00:00
|
|
|
private:
|
2016-08-09 19:20:04 +00:00
|
|
|
friend class SkNVRefCnt<SkTextBlob>;
|
2014-09-20 12:40:22 +00:00
|
|
|
class RunRecord;
|
|
|
|
|
2017-03-13 13:03:24 +00:00
|
|
|
explicit SkTextBlob(const SkRect& bounds);
|
2014-09-20 12:40:22 +00:00
|
|
|
|
2016-08-09 19:20:04 +00:00
|
|
|
~SkTextBlob();
|
2014-11-20 16:02:46 +00:00
|
|
|
|
|
|
|
// Memory for objects of this class is created with sk_malloc rather than operator new and must
|
|
|
|
// be freed with sk_free.
|
|
|
|
void operator delete(void* p) { sk_free(p); }
|
|
|
|
void* operator new(size_t) {
|
2017-08-16 14:53:04 +00:00
|
|
|
SK_ABORT("All blobs are created by placement new.");
|
2014-11-20 16:02:46 +00:00
|
|
|
return sk_malloc_throw(0);
|
|
|
|
}
|
|
|
|
void* operator new(size_t, void* p) { return p; }
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-08-26 14:56:44 +00:00
|
|
|
static unsigned ScalarsPerGlyph(GlyphPositioning pos);
|
|
|
|
|
2017-03-13 20:45:28 +00:00
|
|
|
// Call when this blob is part of the key to a cache entry. This allows the cache
|
|
|
|
// to know automatically those entries can be purged when this SkTextBlob is deleted.
|
2017-12-14 18:00:05 +00:00
|
|
|
void notifyAddedToCache(uint32_t cacheID) const {
|
|
|
|
fCacheID.store(cacheID);
|
2017-03-13 20:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
friend class GrTextBlobCache;
|
2014-08-21 15:53:26 +00:00
|
|
|
friend class SkTextBlobBuilder;
|
2015-10-27 21:01:05 +00:00
|
|
|
friend class SkTextBlobRunIterator;
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2017-12-14 18:00:05 +00:00
|
|
|
const SkRect fBounds;
|
|
|
|
const uint32_t fUniqueID;
|
|
|
|
mutable SkAtomic<uint32_t> fCacheID;
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
SkDEBUGCODE(size_t fStorageSize;)
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
// The actual payload resides in externally-managed storage, following the object.
|
|
|
|
// (see the .cpp for more details)
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
typedef SkRefCnt INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \class SkTextBlobBuilder
|
|
|
|
|
|
|
|
Helper class for constructing SkTextBlobs.
|
|
|
|
*/
|
2014-08-25 13:22:12 +00:00
|
|
|
class SK_API SkTextBlobBuilder {
|
2014-08-21 15:53:26 +00:00
|
|
|
public:
|
2014-09-20 12:40:22 +00:00
|
|
|
SkTextBlobBuilder();
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
~SkTextBlobBuilder();
|
|
|
|
|
|
|
|
/**
|
2017-03-13 13:03:24 +00:00
|
|
|
* Returns an immutable SkTextBlob for the current runs/glyphs,
|
|
|
|
* or nullptr if no runs were allocated.
|
|
|
|
*
|
|
|
|
* The builder is reset and can be reused.
|
2014-08-21 15:53:26 +00:00
|
|
|
*/
|
2016-08-10 21:16:41 +00:00
|
|
|
sk_sp<SkTextBlob> make();
|
|
|
|
|
2014-08-21 15:53:26 +00:00
|
|
|
/**
|
|
|
|
* Glyph and position buffers associated with a run.
|
|
|
|
*
|
2016-08-30 18:58:33 +00:00
|
|
|
* A run is a sequence of glyphs sharing the same font metrics
|
|
|
|
* and positioning mode.
|
|
|
|
*
|
|
|
|
* If textByteCount is 0, utf8text and clusters will be NULL (no
|
|
|
|
* character information will be associated with the glyphs).
|
|
|
|
*
|
|
|
|
* utf8text will point to a buffer of size textByteCount bytes.
|
|
|
|
*
|
|
|
|
* clusters (if not NULL) will point to an array of size count.
|
|
|
|
* For each glyph, give the byte-offset into the text for the
|
|
|
|
* first byte in the first character in that glyph's cluster.
|
|
|
|
* Each value in the array should be an integer less than
|
|
|
|
* textByteCount. Values in the array should either be
|
|
|
|
* monotonically increasing (left-to-right text) or monotonically
|
|
|
|
* decreasing (right-to-left text). This definiton is conviently
|
|
|
|
* the same as used by Harfbuzz's hb_glyph_info_t::cluster field,
|
|
|
|
* except that Harfbuzz interleaves glyphs and clusters.
|
2014-08-21 15:53:26 +00:00
|
|
|
*/
|
|
|
|
struct RunBuffer {
|
2016-07-25 14:18:12 +00:00
|
|
|
SkGlyphID* glyphs;
|
2014-08-21 15:53:26 +00:00
|
|
|
SkScalar* pos;
|
2016-08-30 18:58:33 +00:00
|
|
|
char* utf8text;
|
|
|
|
uint32_t* clusters;
|
2014-08-21 15:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a new default-positioned run and returns its writable glyph buffer
|
|
|
|
* for direct manipulation.
|
|
|
|
*
|
|
|
|
* @param font The font to be used for this run.
|
|
|
|
* @param count Number of glyphs.
|
|
|
|
* @param x,y Position within the blob.
|
2016-08-30 18:58:33 +00:00
|
|
|
* @param textByteCount length of the original UTF-8 text that
|
|
|
|
* corresponds to this sequence of glyphs. If 0,
|
|
|
|
* text will not be included in the textblob.
|
|
|
|
* @param lang Language code, currently unimplemented.
|
2014-08-21 15:53:26 +00:00
|
|
|
* @param bounds Optional run bounding box. If known in advance (!= NULL), it will
|
|
|
|
* be used when computing the blob bounds, to avoid re-measuring.
|
|
|
|
*
|
|
|
|
* @return A writable glyph buffer, valid until the next allocRun() or
|
|
|
|
* build() call. The buffer is guaranteed to hold @count@ glyphs.
|
|
|
|
*/
|
2016-08-30 18:58:33 +00:00
|
|
|
const RunBuffer& allocRunText(const SkPaint& font,
|
|
|
|
int count,
|
|
|
|
SkScalar x,
|
|
|
|
SkScalar y,
|
|
|
|
int textByteCount,
|
|
|
|
SkString lang,
|
2017-08-28 14:34:05 +00:00
|
|
|
const SkRect* bounds = nullptr);
|
2014-08-21 15:53:26 +00:00
|
|
|
const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
|
2017-08-28 14:34:05 +00:00
|
|
|
const SkRect* bounds = nullptr) {
|
2016-08-30 18:58:33 +00:00
|
|
|
return this->allocRunText(font, count, x, y, 0, SkString(), bounds);
|
|
|
|
}
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a new horizontally-positioned run and returns its writable glyph and position
|
|
|
|
* buffers for direct manipulation.
|
|
|
|
*
|
|
|
|
* @param font The font to be used for this run.
|
|
|
|
* @param count Number of glyphs.
|
|
|
|
* @param y Vertical offset within the blob.
|
2016-08-30 18:58:33 +00:00
|
|
|
* @param textByteCount length of the original UTF-8 text that
|
|
|
|
* corresponds to this sequence of glyphs. If 0,
|
|
|
|
* text will not be included in the textblob.
|
|
|
|
* @param lang Language code, currently unimplemented.
|
2014-08-21 15:53:26 +00:00
|
|
|
* @param bounds Optional run bounding box. If known in advance (!= NULL), it will
|
|
|
|
* be used when computing the blob bounds, to avoid re-measuring.
|
|
|
|
*
|
|
|
|
* @return Writable glyph and position buffers, valid until the next allocRun()
|
|
|
|
* or build() call. The buffers are guaranteed to hold @count@ elements.
|
|
|
|
*/
|
2016-08-30 18:58:33 +00:00
|
|
|
const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
|
|
|
|
int textByteCount, SkString lang,
|
2017-08-28 14:34:05 +00:00
|
|
|
const SkRect* bounds = nullptr);
|
2014-08-21 15:53:26 +00:00
|
|
|
const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
|
2017-08-28 14:34:05 +00:00
|
|
|
const SkRect* bounds = nullptr) {
|
2016-08-30 18:58:33 +00:00
|
|
|
return this->allocRunTextPosH(font, count, y, 0, SkString(), bounds);
|
|
|
|
}
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a new fully-positioned run and returns its writable glyph and position
|
|
|
|
* buffers for direct manipulation.
|
|
|
|
*
|
|
|
|
* @param font The font to be used for this run.
|
|
|
|
* @param count Number of glyphs.
|
2016-08-30 18:58:33 +00:00
|
|
|
* @param textByteCount length of the original UTF-8 text that
|
|
|
|
* corresponds to this sequence of glyphs. If 0,
|
|
|
|
* text will not be included in the textblob.
|
|
|
|
* @param lang Language code, currently unimplemented.
|
2014-08-21 15:53:26 +00:00
|
|
|
* @param bounds Optional run bounding box. If known in advance (!= NULL), it will
|
|
|
|
* be used when computing the blob bounds, to avoid re-measuring.
|
|
|
|
*
|
|
|
|
* @return Writable glyph and position buffers, valid until the next allocRun()
|
|
|
|
* or build() call. The glyph buffer and position buffer are
|
|
|
|
* guaranteed to hold @count@ and 2 * @count@ elements, respectively.
|
|
|
|
*/
|
2016-08-30 18:58:33 +00:00
|
|
|
const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
|
|
|
|
int textByteCount, SkString lang,
|
2017-08-28 14:34:05 +00:00
|
|
|
const SkRect* bounds = nullptr);
|
2016-08-30 18:58:33 +00:00
|
|
|
const RunBuffer& allocRunPos(const SkPaint& font, int count,
|
2017-08-28 14:34:05 +00:00
|
|
|
const SkRect* bounds = nullptr) {
|
2016-08-30 18:58:33 +00:00
|
|
|
return this->allocRunTextPos(font, count, 0, SkString(), bounds);
|
|
|
|
}
|
2014-08-21 15:53:26 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-20 12:40:22 +00:00
|
|
|
void reserve(size_t size);
|
2014-08-21 15:53:26 +00:00
|
|
|
void allocInternal(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
|
2016-08-30 18:58:33 +00:00
|
|
|
int count, int textBytes, SkPoint offset, const SkRect* bounds);
|
2014-09-20 12:40:22 +00:00
|
|
|
bool mergeRun(const SkPaint& font, SkTextBlob::GlyphPositioning positioning,
|
2017-11-22 15:11:12 +00:00
|
|
|
uint32_t count, SkPoint offset);
|
2014-08-21 15:53:26 +00:00
|
|
|
void updateDeferredBounds();
|
|
|
|
|
2015-01-28 18:56:06 +00:00
|
|
|
static SkRect ConservativeRunBounds(const SkTextBlob::RunRecord&);
|
|
|
|
static SkRect TightRunBounds(const SkTextBlob::RunRecord&);
|
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
SkAutoTMalloc<uint8_t> fStorage;
|
|
|
|
size_t fStorageSize;
|
|
|
|
size_t fStorageUsed;
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
SkRect fBounds;
|
|
|
|
int fRunCount;
|
|
|
|
bool fDeferredBounds;
|
|
|
|
size_t fLastRun; // index into fStorage
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
RunBuffer fCurrentRunBuffer;
|
2014-08-21 15:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SkTextBlob_DEFINED
|