From 658091bfad6257b9e5a3fcfc9695a5621625d2c6 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Thu, 20 Aug 2020 13:55:05 +0200 Subject: [PATCH] ttf_font: add sanity check for invalid scale value The check prevents the Ttf_font from violating the bounding box in the presence of very small scale values. This can happen during the startup of Sculpt. Before the framebuffer driver is up, Sculpt bases its dynamic font-size setting on a screen resolution of 1x1. Issue #3812 --- repos/gems/include/gems/ttf_font.h | 1 + repos/gems/src/lib/ttf_font/ttf_font.cc | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/repos/gems/include/gems/ttf_font.h b/repos/gems/include/gems/ttf_font.h index e9d97ca18e..f62ba590a9 100644 --- a/repos/gems/include/gems/ttf_font.h +++ b/repos/gems/include/gems/ttf_font.h @@ -34,6 +34,7 @@ class Ttf_font : public Text_painter::Font Stbtt_font_info &_stbtt_font_info; + float const _px; float const _scale; unsigned const _baseline; unsigned const _height; diff --git a/repos/gems/src/lib/ttf_font/ttf_font.cc b/repos/gems/src/lib/ttf_font/ttf_font.cc index 62706a4693..17c7a4918e 100644 --- a/repos/gems/src/lib/ttf_font/ttf_font.cc +++ b/repos/gems/src/lib/ttf_font/ttf_font.cc @@ -250,6 +250,7 @@ Ttf_font::_create_stbtt_font_info(Allocator &alloc, void const *ttf) Ttf_font::Ttf_font(Allocator &alloc, void const *ttf, float px) : _stbtt_font_info(_create_stbtt_font_info(alloc, ttf)), + _px(px), _scale(stbtt_ScaleForPixelHeight(&_stbtt_font_info, px)), _baseline(obtain_baseline(_stbtt_font_info, _scale)), _height(px + 0.5 /* round to integer */), @@ -269,6 +270,16 @@ Ttf_font::~Ttf_font() void Ttf_font::_apply_glyph(Codepoint c, Apply_fn const &fn) const { + if (_px < 1.0) { + Glyph const glyph { .width = 0, + .height = 0, + .vpos = 0, + .advance = 0, + .values = nullptr }; + fn.apply(glyph); + return; + } + /* find vertical subpixel position that yields the sharpest glyph */ float best_shift_y = 0; {