Skip to content

Commit

Permalink
Implement font fallback functionality and refactor some code. (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
shlzxjp authored Nov 18, 2024
1 parent 110c3ee commit 4802e56
Show file tree
Hide file tree
Showing 7 changed files with 1,100 additions and 75 deletions.
71 changes: 70 additions & 1 deletion include/tgfx/layers/TextLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,75 @@ class TextLayer : public Layer {
TextAlign _textAlign = TextAlign::Left;
bool _autoWrap = false;

std::string preprocessNewLines(const std::string& text);
class GlyphInfo final {
public:
GlyphInfo(Unichar unichar, GlyphID glyphID, std::shared_ptr<Typeface> typeface)
: _unichar(unichar), _glyphID(glyphID), _typeface(std::move(typeface)) {
}

Unichar getUnichar() const {
return _unichar;
}

GlyphID getGlyphID() const {
return _glyphID;
}

std::shared_ptr<Typeface> getTypeface() const {
return _typeface;
}

private:
Unichar _unichar = 0;
GlyphID _glyphID = 0;
std::shared_ptr<Typeface> _typeface = nullptr;
};

class GlyphLine final {
public:
GlyphLine() = default;

void append(const std::shared_ptr<GlyphInfo>& glyphInfo, const float advance) {
_glyphInfosAndAdvance.emplace_back(std::move(glyphInfo), advance);
}

size_t getGlyphCount() const {
return _glyphInfosAndAdvance.size();
}

std::shared_ptr<GlyphInfo> getGlyphInfo(const size_t index) const {
return _glyphInfosAndAdvance[index].first;
}

float getAdvance(const size_t index) const {
return _glyphInfosAndAdvance[index].second;
}

float getLineWidth() const {
float lineWidth = 0.0f;
for (const auto& glyphInfoAndAdvance : _glyphInfosAndAdvance) {
lineWidth += glyphInfoAndAdvance.second;
}
return lineWidth;
}

private:
std::vector<std::pair<std::shared_ptr<GlyphInfo>, float>> _glyphInfosAndAdvance = {};
};

static std::string PreprocessNewLines(const std::string& text);
static std::vector<std::shared_ptr<GlyphInfo>> ShapeText(
const std::string& text, const std::shared_ptr<Typeface>& typeface);

float calcAdvance(const std::shared_ptr<GlyphInfo>& glyphInfo, float emptyAdvance) const;
float getLineHeight(const std::shared_ptr<GlyphLine>& glyphLine) const;
void TruncateGlyphLines(std::vector<std::shared_ptr<GlyphLine>>& glyphLines) const;
void resolveTextAlignment(const std::vector<std::shared_ptr<GlyphLine>>& glyphLines,
float emptyAdvance,
std::vector<std::shared_ptr<GlyphInfo>>& finalGlyphInfos,
std::vector<Point>& positions) const;
void buildGlyphRunList(const std::vector<std::shared_ptr<GlyphInfo>>& finalGlyphs,
const std::vector<Point>& positions,
std::vector<GlyphRun>& glyphRunList) const;
};
} // namespace tgfx
4 changes: 2 additions & 2 deletions src/core/GlyphRunList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ GlyphRunList::GlyphRunList(GlyphRun glyphRun) {

GlyphRunList::GlyphRunList(std::vector<GlyphRun> glyphRuns) : _glyphRuns(std::move(glyphRuns)) {
DEBUG_ASSERT(!_glyphRuns.empty());
DEBUG_ASSERT(std::all_of(glyphRuns.begin(), glyphRuns.end(),
[hasColor = glyphRuns[0].font.hasColor()](const GlyphRun& glyphRun) {
DEBUG_ASSERT(std::all_of(_glyphRuns.begin(), _glyphRuns.end(),
[hasColor = _glyphRuns[0].font.hasColor()](const GlyphRun& glyphRun) {
return !glyphRun.glyphs.empty() &&
glyphRun.glyphs.size() == glyphRun.positions.size() &&
glyphRun.font.hasColor() == hasColor;
Expand Down
4 changes: 2 additions & 2 deletions src/core/TextBlob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ std::shared_ptr<TextBlob> TextBlob::MakeFrom(const std::string& text, const Font
const char* textStop = textStart + text.size();
GlyphRun glyphRun = {};
glyphRun.font = font;
auto emptyGlyphID = font.getGlyphID(" ");
auto emptyAdvance = font.getAdvance(emptyGlyphID);
// Use half the font size as width for spaces
auto emptyAdvance = font.getSize() / 2.0f;
float xOffset = 0;
while (textStart < textStop) {
auto unichar = UTF::NextUTF8(&textStart, textStop);
Expand Down
Loading

0 comments on commit 4802e56

Please sign in to comment.