blob: 4b392fda0fd3079664731748f3504a17a64180d0 [file] [log] [blame]
/*
* Copyright 2018 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkGlyphRunInfo_DEFINED
#define SkGlyphRunInfo_DEFINED
#include <functional>
#include <memory>
#include <vector>
#include "SkDescriptor.h"
#include "SkMask.h"
#include "SkPath.h"
#include "SkPoint.h"
#include "SkTypes.h"
class SkBaseDevice;
template <typename T>
class SkSpan {
public:
SkSpan() : fPtr{nullptr}, fSize{0} {}
SkSpan(T* ptr, size_t size) : fPtr{ptr}, fSize{size} { }
template <typename U>
explicit SkSpan(std::vector<U>& v) : fPtr{v.data()}, fSize{v.size()} {}
SkSpan(const SkSpan<T>& o) = default;
SkSpan& operator=( const SkSpan& other ) = default;
T& operator [] (size_t i) const { return fPtr[i]; }
T* begin() const { return fPtr; }
T* end() const { return fPtr + fSize; }
const T* cbegin() const { return fPtr; }
const T* cend() const { return fPtr + fSize; }
T* data() const { return fPtr; }
size_t size() const { return fSize; }
bool empty() const { return fSize == 0; }
private:
T* fPtr;
size_t fSize;
};
class SkGlyphRun {
public:
SkGlyphRun() = default;
SkGlyphRun(SkPaint&& runPaint,
SkSpan<const uint16_t> denseIndices,
SkSpan<const SkPoint> positions,
SkSpan<const SkGlyphID> glyphIDs,
SkSpan<const SkGlyphID> uniqueGlyphIDs,
SkSpan<const char> text,
SkSpan<const uint32_t> clusters);
// The temporaryShunt calls are to allow inter-operating with existing code while glyph runs
// are developed.
void temporaryShuntToDrawPosText(SkBaseDevice* device);
using TemporaryShuntCallback = std::function<void(size_t, const char*, const SkScalar*)>;
void temporaryShuntToCallback(TemporaryShuntCallback callback);
size_t runSize() const { return fUniqueGlyphIDIndices.size(); }
SkSpan<const SkPoint> positions() const { return fPositions; }
const SkPaint& paint() const { return fRunPaint; }
private:
//
const SkSpan<const uint16_t> fUniqueGlyphIDIndices;
//
const SkSpan<const SkPoint> fPositions;
// This is temporary while converting from the old per glyph code to the bulk code.
const SkSpan<const SkGlyphID> fTemporaryShuntGlyphIDs;
// The unique glyphs from fTemporaryShuntGlyphIDs.
const SkSpan<const SkGlyphID> fUniqueGlyphIDs;
// Original text from SkTextBlob if present. Will be empty of not present.
const SkSpan<const char> fText;
// Original clusters from SkTextBlob if present. Will be empty if not present.
const SkSpan<const uint32_t> fClusters;
// Paint for this run modified to have glyph encoding and left alignment.
const SkPaint fRunPaint;
};
class SkGlyphRunList {
const SkPaint* fOriginalPaint{nullptr};
// The text blob is needed to hookup the call back that the SkTextBlob destructor calls. It
// should be used for nothing else
const SkTextBlob* fOriginalTextBlob{nullptr};
SkPoint fOrigin = {0, 0};
SkSpan<SkGlyphRun> fGlyphRuns;
public:
SkGlyphRunList() = default;
// Blob maybe null.
SkGlyphRunList(
const SkPaint& paint,
const SkTextBlob* blob,
SkPoint origin,
SkSpan<SkGlyphRun> glyphRunList);
uint64_t uniqueID() const;
bool anyRunsLCD() const;
void temporaryShuntBlobNotifyAddedToCache(uint32_t cacheID) const;
bool canCache() const { return fOriginalTextBlob != nullptr; }
size_t runCount() const { return fGlyphRuns.size(); }
size_t totalGlyphCount() const {
size_t glyphCount = 0;
for(const auto& run : fGlyphRuns) {
glyphCount += run.runSize();
}
return glyphCount;
}
SkPoint origin() const { return fOrigin; }
const SkPaint& paint() const { return *fOriginalPaint; }
auto begin() -> decltype(fGlyphRuns.begin()) { return fGlyphRuns.begin(); }
auto end() -> decltype(fGlyphRuns.end()) { return fGlyphRuns.end(); }
auto size() -> decltype(fGlyphRuns.size()) { return fGlyphRuns.size(); }
auto empty() -> decltype(fGlyphRuns.empty()) { return fGlyphRuns.empty(); }
auto operator [] (size_t i) -> decltype(fGlyphRuns[i]) { return fGlyphRuns[i]; }
void temporaryShuntToDrawPosText(SkBaseDevice* device) {
for (auto& run : fGlyphRuns) {
run.temporaryShuntToDrawPosText(device);
}
}
};
class SkGlyphIDSet {
public:
SkSpan<const SkGlyphID> uniquifyGlyphIDs(
uint32_t universeSize, SkSpan<const SkGlyphID> glyphIDs,
SkGlyphID* uniqueGlyphIDs, uint16_t* denseindices);
private:
size_t fUniverseToUniqueSize{0};
SkAutoTMalloc<uint16_t> fUniverseToUnique;
};
class SkGlyphRunBuilder {
public:
void drawText(
const SkPaint& paint, const void* bytes, size_t byteLength, SkPoint origin);
void drawPosTextH(
const SkPaint& paint, const void* bytes, size_t byteLength,
const SkScalar* xpos, SkScalar constY);
void drawPosText(
const SkPaint& paint, const void* bytes, size_t byteLength, const SkPoint* pos);
void drawTextBlob(const SkPaint& paint, const SkTextBlob& blob, SkPoint origin);
SkGlyphRun* useGlyphRun();
SkGlyphRunList* useGlyphRunList();
private:
void initialize(size_t totalRunSize);
SkSpan<const SkGlyphID> textToGlyphIDs(
const SkPaint& paint, const void* bytes, size_t byteLength);
// Returns the span of unique glyph IDs.
SkSpan<const SkGlyphID> addDenseAndUnique(
const SkPaint& paint,
SkSpan<const SkGlyphID> glyphIDs,
uint16_t* uniqueGlyphIDIndices,
SkGlyphID* uniqueGlyphIDs);
void makeGlyphRun(
const SkPaint& runPaint,
SkSpan<const SkGlyphID> glyphIDs,
SkSpan<const SkPoint> positions,
SkSpan<const uint16_t> uniqueGlyphIDIndices,
SkSpan<const SkGlyphID> uniqueGlyphIDs,
SkSpan<const char> text,
SkSpan<const uint32_t> clusters);
void makeGlyphRunList(const SkPaint& paint, const SkTextBlob* blob, SkPoint origin);
size_t simplifyDrawText(
const SkPaint& paint, SkSpan<const SkGlyphID> glyphIDs, SkPoint origin,
uint16_t* uniqueGlyphIDIndices, SkGlyphID* uniqueGlyphIDs, SkPoint* positions,
SkSpan<const char> text = SkSpan<const char>{},
SkSpan<const uint32_t> clusters = SkSpan<const uint32_t>{});
size_t simplifyDrawPosTextH(
const SkPaint& paint, SkSpan<const SkGlyphID> glyphIDs,
const SkScalar* xpos, SkScalar constY,
uint16_t* uniqueGlyphIDIndices, SkGlyphID* uniqueGlyphIDs, SkPoint* positions,
SkSpan<const char> text = SkSpan<const char>{},
SkSpan<const uint32_t> clusters = SkSpan<const uint32_t>{});
size_t simplifyDrawPosText(
const SkPaint& paint, SkSpan<const SkGlyphID> glyphIDs, const SkPoint* pos,
uint16_t* uniqueGlyphIDIndices, SkGlyphID* uniqueGlyphIDs,
SkSpan<const char> text = SkSpan<const char>{},
SkSpan<const uint32_t> clusters = SkSpan<const uint32_t>{});
size_t fMaxTotalRunSize{0};
SkAutoTMalloc<uint16_t> fUniqueGlyphIDIndices;
SkAutoTMalloc<SkPoint> fPositions;
SkAutoTMalloc<SkGlyphID> fUniqueGlyphIDs;
std::vector<SkGlyphRun> fGlyphRunListStorage;
SkGlyphRunList fGlyphRunList;
// Used as a temporary for preparing using utfN text. This implies that only one run of
// glyph ids will ever be needed because blobs are already glyph based.
std::vector<SkGlyphID> fScratchGlyphIDs;
// Used as temporary storage for calculating positions for drawText.
std::vector<SkPoint> fScratchAdvances;
// Used for collecting the set of unique glyphs.
SkGlyphIDSet fGlyphIDSet;
};
#endif // SkGlyphRunInfo_DEFINED