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
|
|
|
|
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkTArray.h"
|
|
|
|
#include "SkTDArray.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.
|
|
|
|
*/
|
2014-08-25 13:22:12 +00:00
|
|
|
class SK_API SkTextBlob : public SkRefCnt {
|
2014-08-21 15:53:26 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns the blob bounding box.
|
|
|
|
*/
|
|
|
|
const SkRect& bounds() const { return fBounds; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a non-zero, unique value representing the text blob.
|
|
|
|
*/
|
|
|
|
uint32_t uniqueID() const;
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static const SkTextBlob* CreateFromBuffer(SkReadBuffer&);
|
|
|
|
|
2014-08-21 15:53:26 +00:00
|
|
|
private:
|
|
|
|
enum GlyphPositioning {
|
|
|
|
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.
|
|
|
|
};
|
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
class RunRecord;
|
|
|
|
|
2014-08-21 15:53:26 +00:00
|
|
|
class RunIterator {
|
|
|
|
public:
|
|
|
|
RunIterator(const SkTextBlob* blob);
|
|
|
|
|
|
|
|
bool done() const;
|
|
|
|
void next();
|
|
|
|
|
|
|
|
uint32_t glyphCount() const;
|
|
|
|
const uint16_t* glyphs() const;
|
|
|
|
const SkScalar* pos() const;
|
|
|
|
const SkPoint& offset() const;
|
2014-08-22 16:01:19 +00:00
|
|
|
void applyFontToPaint(SkPaint*) const;
|
2014-08-21 15:53:26 +00:00
|
|
|
GlyphPositioning positioning() const;
|
|
|
|
|
|
|
|
private:
|
2014-09-20 12:40:22 +00:00
|
|
|
const RunRecord* fCurrentRun;
|
|
|
|
int fRemainingRuns;
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
SkDEBUGCODE(uint8_t* fStorageTop;)
|
2014-09-20 02:03:06 +00:00
|
|
|
};
|
2014-09-20 01:37:00 +00:00
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
SkTextBlob(int runCount, const SkRect& bounds);
|
|
|
|
|
|
|
|
virtual ~SkTextBlob();
|
|
|
|
virtual void internal_dispose() const SK_OVERRIDE;
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-08-26 14:56:44 +00:00
|
|
|
static unsigned ScalarsPerGlyph(GlyphPositioning pos);
|
|
|
|
|
2014-08-28 21:32:24 +00:00
|
|
|
friend class SkBaseDevice;
|
2014-08-21 15:53:26 +00:00
|
|
|
friend class SkTextBlobBuilder;
|
2014-08-21 22:03:04 +00:00
|
|
|
friend class TextBlobTester;
|
2014-08-21 15:53:26 +00:00
|
|
|
|
2014-09-20 12:40:22 +00:00
|
|
|
const int fRunCount;
|
|
|
|
const SkRect fBounds;
|
|
|
|
mutable uint32_t fUniqueID;
|
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();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an immutable SkTextBlob for the current runs/glyphs. The builder is reset and
|
|
|
|
* can be reused.
|
|
|
|
*/
|
|
|
|
const SkTextBlob* build();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Glyph and position buffers associated with a run.
|
|
|
|
*
|
|
|
|
* A run is a sequence of glyphs sharing the same font metrics and positioning mode.
|
|
|
|
*/
|
|
|
|
struct RunBuffer {
|
|
|
|
uint16_t* glyphs;
|
|
|
|
SkScalar* pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
const SkRect* bounds = NULL);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
const SkRect* bounds = NULL);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
const RunBuffer& allocRunPos(const SkPaint& font, int count, const SkRect* bounds = NULL);
|
|
|
|
|
|
|
|
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,
|
|
|
|
int count, 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();
|
|
|
|
|
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
|