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;
|
|
|
|
|
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&);
|
|
|
|
|
|
|
|
static const SkTextBlob* CreateFromBuffer(SkReadBuffer& buffer) {
|
|
|
|
return MakeFromBuffer(buffer).release();
|
|
|
|
}
|
2014-08-28 20:59:42 +00:00
|
|
|
|
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.
|
|
|
|
};
|
|
|
|
|
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) {
|
|
|
|
SkFAIL("All blobs are created by placement new.");
|
|
|
|
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.
|
|
|
|
void notifyAddedToCache() const {
|
|
|
|
fAddedToCache.store(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-13 20:45:28 +00:00
|
|
|
const SkRect fBounds;
|
|
|
|
const uint32_t fUniqueID;
|
|
|
|
mutable SkAtomic<bool> fAddedToCache;
|
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,
|
|
|
|
const SkRect* bounds = NULL);
|
2014-08-21 15:53:26 +00:00
|
|
|
const RunBuffer& allocRun(const SkPaint& font, int count, SkScalar x, SkScalar y,
|
2016-08-30 18:58:33 +00:00
|
|
|
const SkRect* bounds = NULL) {
|
|
|
|
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,
|
|
|
|
const SkRect* bounds = NULL);
|
2014-08-21 15:53:26 +00:00
|
|
|
const RunBuffer& allocRunPosH(const SkPaint& font, int count, SkScalar y,
|
2016-08-30 18:58:33 +00:00
|
|
|
const SkRect* bounds = NULL) {
|
|
|
|
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,
|
|
|
|
const SkRect* bounds = NULL);
|
|
|
|
const RunBuffer& allocRunPos(const SkPaint& font, int count,
|
|
|
|
const SkRect* bounds = NULL) {
|
|
|
|
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,
|
|
|
|
int 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
|