Skip to content

Commit

Permalink
karm-media: Added kerning informations cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Apr 18, 2024
1 parent 6dc0d66 commit c25350e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/libs/karm-media/font-ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct TtfFontface : public Fontface {
Ttf::Font _ttf;
Map<Rune, Media::Glyph> _cachedEntries;
Map<Media::Glyph, f64> _cachedAdvances;
Map<Cons<Media::Glyph>, f64> _cachedKerns;

static Res<Strong<TtfFontface>> load(Sys::Mmap &&mmap) {
auto ttf = try$(Ttf::Font::load(mmap.bytes()));
Expand Down Expand Up @@ -53,8 +54,14 @@ struct TtfFontface : public Fontface {
return a;
}

f64 kern(Glyph prev, Glyph curr) const override {
return _ttf.glyphKern(prev, curr);
f64 kern(Glyph prev, Glyph curr) override {
auto kern = _cachedKerns.get({prev, curr});
if (kern.has())
return kern.unwrap();

auto k = _ttf.glyphKern(prev, curr);
_cachedKerns.put({prev, curr}, k);
return k;
}

void contour(Gfx::Context &g, Glyph glyph) const override {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-media/font-vga.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct VgaFontface : public Fontface {
return 8;
}

f64 kern(Glyph, Glyph) const override {
f64 kern(Glyph, Glyph) override {
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-media/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ f64 Font::advance(Glyph glyph) {
return fontface->advance(glyph) * scale();
}

f64 Font::kern(Glyph prev, Glyph curr) const {
f64 Font::kern(Glyph prev, Glyph curr) {
return fontface->kern(prev, curr) * scale();
}

Expand Down
4 changes: 2 additions & 2 deletions src/libs/karm-media/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct Fontface {

virtual f64 advance(Glyph glyph) = 0;

virtual f64 kern(Glyph prev, Glyph curr) const = 0;
virtual f64 kern(Glyph prev, Glyph curr) = 0;

virtual void contour(Gfx::Context &g, Glyph glyph) const = 0;

Expand All @@ -114,7 +114,7 @@ struct Font {

f64 advance(Glyph glyph);

f64 kern(Glyph prev, Glyph curr) const;
f64 kern(Glyph prev, Glyph curr);

FontMeasure measure(Glyph glyph);
};
Expand Down

0 comments on commit c25350e

Please sign in to comment.