Skip to content

Commit

Permalink
Add SpriteTextBlob to improve the rendering of SpriteSheetFont with n…
Browse files Browse the repository at this point in the history
…on-existent chars

To render text with a SpriteSheetFont we now create a SpriteTextBlob.
This blob can render non-existent code points using a default fallback
native font (SkiaFont) and create a chain of runs for each TextBlob
that use a different font.

In this way we don't change the whole appearance of the rendered text
with an antialiased font when a missing code point appears. We just
use the fallback font to render that specific code point/char (or
sequence of chars in one run).

This problem was reported several times and we can find examples here:
aseprite/aseprite#2890 (comment)

As a side note, all code/static functions that use a text shaper
(HarfBuzz, skshaper module, etc.) is separated in its own file
(i.e. translation unit) to make binary executable files smaller when
the shaper is not needed (e.g. in English-only apps).
  • Loading branch information
dacap committed May 3, 2024
1 parent 16e811c commit d590eec
Show file tree
Hide file tree
Showing 17 changed files with 831 additions and 471 deletions.
13 changes: 5 additions & 8 deletions examples/text_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ struct TextEdit {

// TextBlob::RunHandler impl
void commitRunBuffer(TextBlob::RunInfo& info) override {
if (info.clusters &&
info.glyphCount > 0) {
Box box;
for (int i=0; i<info.glyphCount; ++i) {
box.utf8Range = info.getGlyphUtf8Range(i);
box.bounds = info.getGlyphBounds(i);
m_boxes.push_back(box);
}
Box box;
for (int i=0; i<info.glyphCount; ++i) {
box.utf8Range = info.getGlyphUtf8Range(i);
box.bounds = info.getGlyphBounds(i);
m_boxes.push_back(box);
}
}

Expand Down
9 changes: 9 additions & 0 deletions os/skia/skia_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "gfx/color.h"
#include "gfx/point.h"
#include "gfx/rect.h"
#include "os/paint.h"
#include "os/sampling.h"
Expand All @@ -30,6 +31,10 @@ inline SkColor4f to_skia4f(gfx::Color c) {
return SkColor4f::FromColor(to_skia(c));
}

inline SkPoint to_skia(const gfx::PointF& pt) {
return SkPoint::Make(pt.x, pt.y);
}

inline SkIRect to_skia(const gfx::Rect& rc) {
return SkIRect::MakeXYWH(rc.x, rc.y, rc.w, rc.h);
}
Expand All @@ -38,6 +43,10 @@ inline SkRect to_skia(const gfx::RectF& rc) {
return SkRect::MakeXYWH(SkScalar(rc.x), SkScalar(rc.y), SkScalar(rc.w), SkScalar(rc.h));
}

inline gfx::PointF from_skia(const SkPoint& pt) {
return gfx::PointF(pt.x(), pt.y());
}

inline gfx::RectF from_skia(const SkRect& rc) {
return gfx::RectF(rc.x(), rc.y(), rc.width(), rc.height());
}
Expand Down
9 changes: 6 additions & 3 deletions text/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

add_library(laf-text
draw_text.cpp
draw_text_shaper.cpp
font_mgr.cpp
text_blob.cpp)
sprite_text_blob.cpp
sprite_text_blob_shaper.cpp
text_blob.cpp
text_blob_shaper.cpp)

target_link_libraries(laf-text laf-os laf-gfx laf-base)

Expand All @@ -18,11 +22,10 @@ endif()

if(LAF_BACKEND STREQUAL "skia")
target_sources(laf-text PRIVATE
skia_draw_text.cpp
skia_font.cpp
skia_font_mgr.cpp
skia_text_blob.cpp
skia_with_shaper.cpp)
skia_text_blob_shaper.cpp)
target_link_libraries(laf-text
skia skshaper)
else()
Expand Down
Loading

0 comments on commit d590eec

Please sign in to comment.