Skip to content

Commit

Permalink
Add way to disable ligatures when shaping text
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Sep 23, 2024
1 parent 851bdbc commit f8c9221
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
5 changes: 4 additions & 1 deletion examples/text_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ struct TextEdit {
}

void makeBlob(FontMgrRef& fontMgr, FontRef& font) {
// Create a blob without ligatures (to edit char by char)
BoxBuilder handler;
blob = TextBlob::MakeWithShaper(fontMgr, font, text, &handler);
TextBlob::ShaperFeatures features;
features.ligatures = false;
blob = TextBlob::MakeWithShaper(fontMgr, font, text, &handler, features);
boxes = handler.boxes();
}
};
Expand Down
3 changes: 2 additions & 1 deletion text/skia_text_blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class SkiaTextBlob : public TextBlob {
const FontMgrRef& fontMgr,
const FontRef& font,
const std::string& text,
TextBlob::RunHandler* handler);
TextBlob::RunHandler* handler,
const TextBlob::ShaperFeatures features);

private:
sk_sp<SkTextBlob> m_skTextBlob;
Expand Down
11 changes: 10 additions & 1 deletion text/skia_text_blob_shaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "modules/skshaper/include/SkShaper.h"

#include <limits>
#include <vector>

namespace text {

Expand Down Expand Up @@ -125,7 +126,8 @@ TextBlobRef SkiaTextBlob::MakeWithShaper(
const FontMgrRef& fontMgr,
const FontRef& font,
const std::string& text,
TextBlob::RunHandler* handler)
TextBlob::RunHandler* handler,
const TextBlob::ShaperFeatures features)
{
ASSERT(font);
ASSERT(font->type() == FontType::Native);
Expand All @@ -150,12 +152,19 @@ TextBlobRef SkiaTextBlob::MakeWithShaper(
SkFontStyle::Normal(),
&*languageRun);

std::vector<SkShaper::Feature> ft;
if (!features.ligatures) {
ft.emplace_back(SkShaper::Feature{
SkSetFourByteTag('l', 'i', 'g', 'a'), 0, 0, text.size() });
}

shaper->shape(text.c_str(),
text.size(),
*fontRun,
*bidiRun,
*scriptRun,
*languageRun,
ft.data(), ft.size(),
std::numeric_limits<float>::max(),
&shaperHandler);

Expand Down
8 changes: 7 additions & 1 deletion text/text_blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ namespace text {
gfx::RectF getGlyphBounds(size_t i) const;
};

struct ShaperFeatures {
bool ligatures = true;
ShaperFeatures() { }
};

class RunHandler {
public:
virtual ~RunHandler() = default;
Expand Down Expand Up @@ -94,7 +99,8 @@ namespace text {
const FontMgrRef& fontMgr,
const FontRef& font,
const std::string& text,
RunHandler* handler = nullptr);
RunHandler* handler = nullptr,
const ShaperFeatures features = {});

private:
gfx::RectF m_bounds;
Expand Down
6 changes: 4 additions & 2 deletions text/text_blob_shaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ TextBlobRef TextBlob::MakeWithShaper(
const FontMgrRef& fontMgr,
const FontRef& font,
const std::string& text,
TextBlob::RunHandler* handler)
TextBlob::RunHandler* handler,
const TextBlob::ShaperFeatures features)
{
ASSERT(font);
switch (font->type()) {
Expand All @@ -37,7 +38,8 @@ TextBlobRef TextBlob::MakeWithShaper(

#if LAF_SKIA
case FontType::Native:
return SkiaTextBlob::MakeWithShaper(fontMgr, font, text, handler);
return SkiaTextBlob::MakeWithShaper(
fontMgr, font, text, handler, features);
#endif

default:
Expand Down

0 comments on commit f8c9221

Please sign in to comment.