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"
|
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"
|
2018-08-09 16:23:19 +00:00
|
|
|
#include <atomic>
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2017-12-08 18:20:01 +00:00
|
|
|
struct SkSerialProcs;
|
|
|
|
struct SkDeserialProcs;
|
|
|
|
|
2014-08-21 15:53:26 +00:00
|
|
|
/** \class SkTextBlob
|
|
|
|
|
|
|
|
SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
|
|
|
|
*/
|
2018-08-10 12:41:57 +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
|
|
|
|
2018-07-27 20:28:01 +00:00
|
|
|
static sk_sp<SkTextBlob> MakeFromText(
|
2018-07-17 20:10:30 +00:00
|
|
|
const void* text, size_t byteLength, const SkPaint& paint);
|
|
|
|
|
2018-08-09 14:07:33 +00:00
|
|
|
static sk_sp<SkTextBlob> MakeFromString(const char* string, const SkPaint& paint) {
|
|
|
|
if (!string) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return MakeFromText(string, strlen(string), paint);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2018-07-19 14:07:01 +00:00
|
|
|
sk_sp<SkData> serialize(const SkSerialProcs& procs) const;
|
2017-07-21 15:53:23 +00:00
|
|
|
|
2018-07-19 14:07:01 +00:00
|
|
|
static sk_sp<SkTextBlob> Deserialize(const void* data, size_t size,
|
|
|
|
const SkDeserialProcs& procs);
|
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;
|
|
|
|
|
2018-07-27 13:47:15 +00:00
|
|
|
enum GlyphPositioning : uint8_t;
|
|
|
|
|
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.
|
2018-07-09 05:14:26 +00:00
|
|
|
void operator delete(void* p);
|
|
|
|
void* operator new(size_t);
|
|
|
|
void* operator new(size_t, void* 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
|
|
|
}
|
|
|
|
|
2018-07-12 19:30:35 +00:00
|
|
|
friend class SkGlyphRunList;
|
2017-03-13 20:45:28 +00:00
|
|
|
friend class GrTextBlobCache;
|
2014-08-21 15:53:26 +00:00
|
|
|
friend class SkTextBlobBuilder;
|
2018-07-17 12:59:34 +00:00
|
|
|
friend class SkTextBlobPriv;
|
2015-10-27 21:01:05 +00:00
|
|
|
friend class SkTextBlobRunIterator;
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2018-08-09 16:23:19 +00:00
|
|
|
const SkRect fBounds;
|
|
|
|
const uint32_t fUniqueID;
|
|
|
|
mutable std::atomic<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.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
* @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& 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:
|
2018-08-09 14:07:33 +00:00
|
|
|
const RunBuffer& allocRunText(const SkPaint& font,
|
|
|
|
int count,
|
|
|
|
SkScalar x,
|
|
|
|
SkScalar y,
|
|
|
|
int textByteCount,
|
|
|
|
SkString lang,
|
|
|
|
const SkRect* bounds = nullptr);
|
|
|
|
const RunBuffer& allocRunTextPosH(const SkPaint& font, int count, SkScalar y,
|
|
|
|
int textByteCount, SkString lang,
|
|
|
|
const SkRect* bounds = nullptr);
|
|
|
|
const RunBuffer& allocRunTextPos(const SkPaint& font, int count,
|
|
|
|
int textByteCount, SkString lang,
|
|
|
|
const SkRect* bounds = nullptr);
|
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&);
|
|
|
|
|
2018-08-09 14:07:33 +00:00
|
|
|
friend class SkTextBlobPriv;
|
|
|
|
friend class SkTextBlobBuilderPriv;
|
|
|
|
|
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
|