Skip to content

Commit

Permalink
Implement capHeight metric in FontProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-spa committed Jul 2, 2024
1 parent e69f24f commit f7abef4
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sandbox/engraving/fontproviderstub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ qreal FontProviderStub::height(const Font&) const
return 0.0;
}

qreal FontProviderStub::capHeight(const Font &f) const
{
return 0.0;
}

qreal FontProviderStub::ascent(const Font&) const
{
return 0.0;
Expand Down
1 change: 1 addition & 0 deletions sandbox/engraving/fontproviderstub.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FontProviderStub : public IFontProvider
qreal lineSpacing(const Font& f) const override;
qreal xHeight(const Font& f) const override;
qreal height(const Font& f) const override;
qreal capHeight(const Font& f) const override;
qreal ascent(const Font& f) const override;
qreal descent(const Font& f) const override;

Expand Down
5 changes: 5 additions & 0 deletions src/framework/draw/fontmetrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ double FontMetrics::height() const
return fontProvider()->height(m_font);
}

double FontMetrics::capHeight() const
{
return fontProvider()->capHeight(m_font);
}

double FontMetrics::ascent() const
{
return fontProvider()->ascent(m_font);
Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/fontmetrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class FontMetrics
double lineSpacing() const;
double xHeight() const;
double height() const;
double capHeight() const;
double ascent() const;
double descent() const;

Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/ifontprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class IFontProvider : MODULE_EXPORT_INTERFACE
virtual double lineSpacing(const Font& f) const = 0;
virtual double xHeight(const Font& f) const = 0;
virtual double height(const Font& f) const = 0;
virtual double capHeight(const Font& f) const = 0;
virtual double ascent(const Font& f) const = 0;
virtual double descent(const Font& f) const = 0;

Expand Down
5 changes: 5 additions & 0 deletions src/framework/draw/internal/fontfacedu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ f26dot6_t FontFaceDU::xHeight() const
return m_origin->xHeight();
}

f26dot6_t FontFaceDU::capHeight() const
{
return m_origin->capHeight();
}

std::vector<GlyphPos> FontFaceDU::glyphs(const char32_t* text, int text_length) const
{
std::vector<GlyphPos> glyphs = m_origin->glyphs(text, text_length);
Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/internal/fontfacedu.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class FontFaceDU : public IFontFace
f26dot6_t ascent() const override;
f26dot6_t descent() const override;
f26dot6_t xHeight() const override;
f26dot6_t capHeight() const override;

std::vector<GlyphPos> glyphs(const char32_t* text, int text_length) const override;
glyph_idx_t glyphIndex(char32_t ucs4) const override;
Expand Down
16 changes: 16 additions & 0 deletions src/framework/draw/internal/fontfaceft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ f26dot6_t FontFaceFT::xHeight() const
return gm->bbox.height();
}

f26dot6_t FontFaceFT::capHeight() const
{
TT_OS2* os2 = (TT_OS2*)FT_Get_Sfnt_Table(m_data->face, ft_sfnt_os2);
if (os2 && os2->sCapHeight) {
f26dot6_t result = std::round(os2->sCapHeight * m_data->face->size->metrics.y_ppem * 64.0 / (double)m_data->face->units_per_EM);
return result;
}

const glyph_idx_t glyph = glyphIndex('x');
GlyphMetrics* gm = glyphMetrics(glyph);
IF_ASSERT_FAILED(gm) {
return 0;
}
return gm->bbox.height();
}

GlyphMetrics* FontFaceFT::glyphMetrics(glyph_idx_t idx) const
{
if (m_data->glyphsMetrics.find(idx) != m_data->glyphsMetrics.end()) {
Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/internal/fontfaceft.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class FontFaceFT : public IFontFace
f26dot6_t ascent() const override;
f26dot6_t descent() const override;
f26dot6_t xHeight() const override;
f26dot6_t capHeight() const override;

std::vector<GlyphPos> glyphs(const char32_t* text, int text_length) const override;
glyph_idx_t glyphIndex(char32_t ucs4) const override;
Expand Down
5 changes: 5 additions & 0 deletions src/framework/draw/internal/fontprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ double FontProvider::ascent(const muse::draw::Font& f) const
return fontsEngine()->ascent(f);
}

double FontProvider::capHeight(const muse::draw::Font& f) const
{
return fontsEngine()->capHeight(f);
}

double FontProvider::descent(const muse::draw::Font& f) const
{
return fontsEngine()->descent(f);
Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/internal/fontprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class FontProvider : public IFontProvider
double lineSpacing(const Font& f) const override;
double xHeight(const Font& f) const override;
double height(const Font& f) const override;
double capHeight(const Font& f) const override;
double ascent(const Font& f) const override;
double descent(const Font& f) const override;

Expand Down
10 changes: 10 additions & 0 deletions src/framework/draw/internal/fontsengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ double FontsEngine::height(const Font& f) const
return from_f26d6(rf->face->ascent() + rf->face->descent()) * rf->pixelScale();
}

double FontsEngine::capHeight(const Font &f) const
{
RequireFace* rf = fontFace(f);
IF_ASSERT_FAILED(rf && rf->face) {
return 0.0;
}

return from_f26d6(rf->face->capHeight()) * rf->pixelScale();
}

double FontsEngine::ascent(const Font& f) const
{
RequireFace* rf = fontFace(f);
Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/internal/fontsengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class FontsEngine : public IFontsEngine
double lineSpacing(const Font& f) const override;
double xHeight(const Font& f) const override;
double height(const Font& f) const override;
double capHeight(const Font& f) const override;
double ascent(const Font& f) const override;
double descent(const Font& f) const override;

Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/internal/ifontface.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class IFontFace
virtual f26dot6_t ascent() const = 0;
virtual f26dot6_t descent() const = 0;
virtual f26dot6_t xHeight() const = 0;
virtual f26dot6_t capHeight() const = 0;

virtual std::vector<GlyphPos> glyphs(const char32_t* text, int text_length) const = 0;
virtual glyph_idx_t glyphIndex(char32_t ucs4) const = 0;
Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/internal/ifontsengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class IFontsEngine : public modularity::IModuleExportInterface
virtual double lineSpacing(const Font& f) const = 0;
virtual double xHeight(const Font& f) const = 0;
virtual double height(const Font& f) const = 0;
virtual double capHeight(const Font& ff) const = 0;
virtual double ascent(const Font& f) const = 0;
virtual double descent(const Font& f) const = 0;

Expand Down
5 changes: 5 additions & 0 deletions src/framework/draw/internal/qfontprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ double QFontProvider::height(const Font& f) const
return QFontMetricsF(f.toQFont(), &device).height();
}

double QFontProvider::capHeight(const Font& f) const
{
return QFontMetrics(f.toQFont(), &device).capHeight();
}

double QFontProvider::ascent(const Font& f) const
{
return QFontMetricsF(f.toQFont(), &device).ascent();
Expand Down
1 change: 1 addition & 0 deletions src/framework/draw/internal/qfontprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class QFontProvider : public IFontProvider
double lineSpacing(const Font& f) const override;
double xHeight(const Font& f) const override;
double height(const Font& f) const override;
double capHeight(const Font& f) const override;
double ascent(const Font& f) const override;
double descent(const Font& f) const override;

Expand Down
5 changes: 5 additions & 0 deletions tools/check_build_without_qt/fontproviderstub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ double FontProviderStub::height(const Font&) const
return 0.0;
}

double FontProviderStub::capHeight(const Font &f) const
{
return 0.0;
}

double FontProviderStub::ascent(const Font&) const
{
return 0.0;
Expand Down
1 change: 1 addition & 0 deletions tools/check_build_without_qt/fontproviderstub.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FontProviderStub : public IFontProvider
double lineSpacing(const Font& f) const override;
double xHeight(const Font& f) const override;
double height(const Font& f) const override;
double capHeight(const Font& f) const override;
double ascent(const Font& f) const override;
double descent(const Font& f) const override;

Expand Down

0 comments on commit f7abef4

Please sign in to comment.