Skip to content

Commit

Permalink
Cache glyphs
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Apr 23, 2023
1 parent 2f8502f commit 7943b35
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 126 deletions.
51 changes: 28 additions & 23 deletions lib/tlUI/FloatEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ namespace tl
int precision = 2;
FontRole fontRole = FontRole::Mono;

struct SizeCache
struct SizeData
{
math::Vector2i textSize;
math::Vector2i formatSize;
int margin = 0;
int border = 0;
imaging::FontMetrics fontMetrics;
math::Vector2i textSize;
math::Vector2i formatSize;
};
SizeCache sizeCache;
SizeData size;

struct DrawData
{
std::vector<std::shared_ptr<imaging::Glyph> > glyphs;
};
DrawData draw;

std::shared_ptr<observer::ValueObserver<float> > valueObserver;
std::shared_ptr<observer::ValueObserver<math::FloatRange> > rangeObserver;
Expand Down Expand Up @@ -123,27 +130,25 @@ namespace tl
IWidget::sizeEvent(event);
TLRENDER_P();

p.sizeCache.margin = event.style->getSizeRole(SizeRole::MarginInside) * event.contentScale;
p.sizeCache.border = event.style->getSizeRole(SizeRole::Border) * event.contentScale;
const auto fontInfo = event.getFontInfo(p.fontRole);
const auto fontMetrics = event.getFontMetrics(p.fontRole);
p.size.margin = event.style->getSizeRole(SizeRole::MarginInside) * event.contentScale;
p.size.border = event.style->getSizeRole(SizeRole::Border) * event.contentScale;
p.size.fontMetrics = event.getFontMetrics(p.fontRole);

p.sizeCache.textSize = event.fontSystem->measure(p.text, fontInfo);
p.sizeCache.formatSize = event.fontSystem->measure(p.format, fontInfo);
const auto fontInfo = event.getFontInfo(p.fontRole);
p.size.textSize = event.fontSystem->measure(p.text, fontInfo);
p.size.formatSize = event.fontSystem->measure(p.format, fontInfo);
p.draw.glyphs = event.fontSystem->getGlyphs(p.text, fontInfo);

_sizeHint.x = p.sizeCache.formatSize.x + p.sizeCache.margin * 2;
_sizeHint.y = fontMetrics.lineHeight + p.sizeCache.margin * 2;
_sizeHint.x = p.size.formatSize.x + p.size.margin * 2;
_sizeHint.y = p.size.fontMetrics.lineHeight + p.size.margin * 2;
}

void FloatEdit::drawEvent(const DrawEvent& event)
{
IWidget::drawEvent(event);
TLRENDER_P();

const auto fontInfo = event.getFontInfo(p.fontRole);
const auto fontMetrics = event.getFontMetrics(p.fontRole);

math::BBox2i g = align(
const math::BBox2i g = align(
_geometry,
_sizeHint,
Stretch::Expanding,
Expand All @@ -152,21 +157,21 @@ namespace tl
_vAlign);

event.render->drawMesh(
border(g, p.sizeCache.border),
border(g, p.size.border),
math::Vector2i(),
event.style->getColorRole(ColorRole::Border));

event.render->drawRect(
g.margin(-p.sizeCache.border),
g.margin(-p.size.border),
event.style->getColorRole(ColorRole::Base));

math::BBox2i g2 = g.margin(-p.sizeCache.margin);
const math::BBox2i g2 = g.margin(-p.size.margin);
math::Vector2i pos(
g2.x() + g2.w() - p.sizeCache.textSize.x,
g2.y() + g2.h() / 2 - fontMetrics.lineHeight / 2 +
fontMetrics.ascender);
g2.x() + g2.w() - p.size.textSize.x,
g2.y() + g2.h() / 2 - p.size.fontMetrics.lineHeight / 2 +
p.size.fontMetrics.ascender);
event.render->drawText(
event.fontSystem->getGlyphs(p.text, fontInfo),
p.draw.glyphs,
pos,
event.style->getColorRole(ColorRole::Text));
}
Expand Down
23 changes: 11 additions & 12 deletions lib/tlUI/FloatSlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ namespace tl
{
std::shared_ptr<FloatModel> model;

struct Size
struct SizeData
{
imaging::FontInfo fontInfo;
imaging::FontMetrics fontMetrics;
int margin = 0;
int border = 0;
int handle = 0;
imaging::FontMetrics fontMetrics;
};
Size size;
SizeData size;

struct Mouse
struct MouseData
{
bool inside = false;
math::Vector2i pos;
bool pressed = false;
};
Mouse mouse;
MouseData mouse;

std::shared_ptr<observer::ValueObserver<float> > valueObserver;
std::shared_ptr<observer::ValueObserver<math::FloatRange> > rangeObserver;
Expand Down Expand Up @@ -100,9 +99,9 @@ namespace tl
p.size.border = event.style->getSizeRole(SizeRole::Border) * event.contentScale;
p.size.handle = event.style->getSizeRole(SizeRole::Handle) * event.contentScale;

p.size.fontInfo = imaging::FontInfo();
p.size.fontInfo.size *= event.contentScale;
p.size.fontMetrics = event.fontSystem->getMetrics(p.size.fontInfo);
auto fontInfo = imaging::FontInfo();
fontInfo.size *= event.contentScale;
p.size.fontMetrics = event.fontSystem->getMetrics(fontInfo);

_sizeHint.x = event.style->getSizeRole(SizeRole::ScrollArea) + p.size.margin * 2;
_sizeHint.y = p.size.fontMetrics.lineHeight + p.size.margin * 2;
Expand All @@ -113,7 +112,7 @@ namespace tl
IWidget::drawEvent(event);
TLRENDER_P();

math::BBox2i g = _geometry;
const math::BBox2i g = _geometry;

event.render->drawMesh(
border(g, p.size.border),
Expand All @@ -124,7 +123,7 @@ namespace tl
g.margin(-p.size.border),
event.style->getColorRole(ColorRole::Base));

math::BBox2i g2 = _getSliderGeometry();
const math::BBox2i g2 = _getSliderGeometry();
//event.render->drawRect(
// g2,
// imaging::Color4f(1.F, 0.F, 0.F, .5F));
Expand All @@ -133,7 +132,7 @@ namespace tl
{
pos = _valueToPos(p.model->getValue());
}
math::BBox2i g3(
const math::BBox2i g3(
pos - p.size.handle / 2,
g2.y(),
p.size.handle,
Expand Down
6 changes: 3 additions & 3 deletions lib/tlUI/GridLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ namespace tl
SizeRole marginRole = SizeRole::None;
SizeRole spacingRole = SizeRole::Spacing;

struct Size
struct SizeData
{
int margin = 0;
int spacing = 0;
};
Size size;
SizeData size;

GridPos getSize() const;

Expand Down Expand Up @@ -98,7 +98,7 @@ namespace tl
IWidget::setGeometry(value);
TLRENDER_P();

math::BBox2i g = _geometry.margin(-p.size.margin);
const math::BBox2i g = _geometry.margin(-p.size.margin);

// Get the child size hints.
std::vector<int> rowSizeHints;
Expand Down
32 changes: 21 additions & 11 deletions lib/tlUI/GroupBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ namespace tl
std::string text;
FontRole fontRole = FontRole::Label;

struct Size
struct SizeData
{
imaging::FontInfo fontInfo;
imaging::FontMetrics fontMetrics;
math::Vector2i textSize;
int margin = 0;
int spacing = 0;
int border = 0;
imaging::FontMetrics fontMetrics;
math::Vector2i textSize;
};
Size size;
SizeData size;

struct DrawData
{
std::vector<std::shared_ptr<imaging::Glyph> > glyphs;
};
DrawData draw;
};

void GroupBox::_init(
Expand Down Expand Up @@ -92,9 +97,10 @@ namespace tl
p.size.spacing = event.style->getSizeRole(SizeRole::SpacingSmall) * event.contentScale;
p.size.border = event.style->getSizeRole(SizeRole::Border) * event.contentScale;

p.size.fontInfo = event.getFontInfo(p.fontRole);
p.size.fontMetrics = event.getFontMetrics(p.fontRole);
p.size.textSize = event.fontSystem->measure(p.text, p.size.fontInfo);
const auto fontInfo = event.getFontInfo(p.fontRole);
p.size.textSize = event.fontSystem->measure(p.text, fontInfo);
p.draw.glyphs = event.fontSystem->getGlyphs(p.text, fontInfo);

_sizeHint = math::Vector2i();
for (const auto& child : _children)
Expand All @@ -114,16 +120,20 @@ namespace tl
IWidget::drawEvent(event);
TLRENDER_P();

math::BBox2i g = _geometry;
const math::BBox2i g = _geometry;

event.render->drawText(
event.fontSystem->getGlyphs(p.text, p.size.fontInfo),
p.draw.glyphs,
math::Vector2i(g.x(), g.y() + p.size.fontMetrics.ascender),
event.style->getColorRole(ColorRole::Text));

g.min.y += p.size.fontMetrics.lineHeight + p.size.spacing;
const math::BBox2i g2(
math::Vector2i(
g.min.x,
g.min.y + p.size.fontMetrics.lineHeight + p.size.spacing),
g.max);
event.render->drawMesh(
border(g, p.size.border, p.size.margin / 2),
border(g2, p.size.border, p.size.margin / 2),
math::Vector2i(),
event.style->getColorRole(ColorRole::Border));
}
Expand Down
32 changes: 19 additions & 13 deletions lib/tlUI/IntEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ namespace tl
int digits = 3;
FontRole fontRole = FontRole::Mono;

struct Size
struct SizeData
{
math::Vector2i textSize;
math::Vector2i formatSize;
int margin = 0;
int border = 0;
imaging::FontMetrics fontMetrics;
math::Vector2i textSize;
math::Vector2i formatSize;
};
Size size;
SizeData size;

struct DrawData
{
std::vector<std::shared_ptr<imaging::Glyph> > glyphs;
};
DrawData draw;

std::shared_ptr<observer::ValueObserver<int> > valueObserver;
std::shared_ptr<observer::ValueObserver<math::IntRange> > rangeObserver;
Expand Down Expand Up @@ -116,23 +123,22 @@ namespace tl
p.size.margin = event.style->getSizeRole(SizeRole::MarginInside) * event.contentScale;
p.size.border = event.style->getSizeRole(SizeRole::Border) * event.contentScale;

p.size.fontMetrics = event.getFontMetrics(p.fontRole);
const auto fontInfo = event.getFontInfo(p.fontRole);
const auto fontMetrics = event.getFontMetrics(p.fontRole);
p.size.textSize = event.fontSystem->measure(p.text, fontInfo);
p.size.formatSize = event.fontSystem->measure(p.format, fontInfo);
p.draw.glyphs = event.fontSystem->getGlyphs(p.text, fontInfo);

_sizeHint.x = p.size.formatSize.x + p.size.margin * 2;
_sizeHint.y = fontMetrics.lineHeight + p.size.margin * 2;
_sizeHint.y = p.size.fontMetrics.lineHeight + p.size.margin * 2;
}

void IntEdit::drawEvent(const DrawEvent& event)
{
IWidget::drawEvent(event);
TLRENDER_P();

const auto fontInfo = event.getFontInfo(p.fontRole);
const auto fontMetrics = event.getFontMetrics(p.fontRole);
math::BBox2i g = align(
const math::BBox2i g = align(
_geometry,
_sizeHint,
Stretch::Expanding,
Expand All @@ -149,13 +155,13 @@ namespace tl
g.margin(-p.size.border),
event.style->getColorRole(ColorRole::Base));

math::BBox2i g2 = g.margin(-p.size.margin);
const math::BBox2i g2 = g.margin(-p.size.margin);
math::Vector2i pos(
g2.x() + g2.w() - p.size.textSize.x,
g2.y() + g2.h() / 2 - fontMetrics.lineHeight / 2 +
fontMetrics.ascender);
g2.y() + g2.h() / 2 - p.size.fontMetrics.lineHeight / 2 +
p.size.fontMetrics.ascender);
event.render->drawText(
event.fontSystem->getGlyphs(p.text, fontInfo),
p.draw.glyphs,
pos,
event.style->getColorRole(ColorRole::Text));
}
Expand Down
23 changes: 11 additions & 12 deletions lib/tlUI/IntSlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ namespace tl
{
std::shared_ptr<IntModel> model;

struct Size
struct SizeData
{
imaging::FontInfo fontInfo;
imaging::FontMetrics fontMetrics;
int margin = 0;
int border = 0;
int handle = 0;
imaging::FontMetrics fontMetrics;
};
Size size;
SizeData size;

struct Mouse
struct MouseData
{
bool inside = false;
math::Vector2i pos;
bool pressed = false;
};
Mouse mouse;
MouseData mouse;

std::shared_ptr<observer::ValueObserver<int> > valueObserver;
std::shared_ptr<observer::ValueObserver<math::IntRange> > rangeObserver;
Expand Down Expand Up @@ -100,9 +99,9 @@ namespace tl
p.size.border = event.style->getSizeRole(SizeRole::Border) * event.contentScale;
p.size.handle = event.style->getSizeRole(SizeRole::Handle) * event.contentScale;

p.size.fontInfo = imaging::FontInfo();
p.size.fontInfo.size *= event.contentScale;
p.size.fontMetrics = event.fontSystem->getMetrics(p.size.fontInfo);
auto fontInfo = imaging::FontInfo();
fontInfo.size *= event.contentScale;
p.size.fontMetrics = event.fontSystem->getMetrics(fontInfo);

_sizeHint.x = event.style->getSizeRole(SizeRole::ScrollArea) + p.size.margin * 2;
_sizeHint.y = p.size.fontMetrics.lineHeight + p.size.margin * 2;
Expand All @@ -113,7 +112,7 @@ namespace tl
IWidget::drawEvent(event);
TLRENDER_P();

math::BBox2i g = _geometry;
const math::BBox2i g = _geometry;

event.render->drawMesh(
border(g, p.size.border),
Expand All @@ -124,7 +123,7 @@ namespace tl
g.margin(-p.size.border),
event.style->getColorRole(ColorRole::Base));

math::BBox2i g2 = _getSliderGeometry();
const math::BBox2i g2 = _getSliderGeometry();
//event.render->drawRect(
// g2,
// imaging::Color4f(1.F, 0.F, 0.F, .5F));
Expand All @@ -133,7 +132,7 @@ namespace tl
{
pos = _valueToPos(p.model->getValue());
}
math::BBox2i g3(
const math::BBox2i g3(
pos - p.size.handle / 2,
g2.y(),
p.size.handle,
Expand Down
Loading

0 comments on commit 7943b35

Please sign in to comment.