-static gui::IGUIFont *select_font_by_line_height(double target_line_height)
-{
- // We don't get to directly select a font according to its
- // baseline-to-baseline height. Rather, we select by em size.
- // The ratio between these varies between fonts. The font
- // engine also takes its size parameter not specified in pixels,
- // as we want, but scaled by display density and gui_scaling,
- // so invert that scaling here. Use a binary search among
- // requested sizes to find the right font. Our starting bounds
- // are an em height of 1 (being careful not to request size 0,
- // which crashes the freetype system) and an em height of the
- // target baseline-to-baseline height.
- unsigned int loreq = ceil(1 / porting::getDisplayDensity()
- / g_settings->getFloat("gui_scaling"));
- unsigned int hireq = ceil(target_line_height
- / porting::getDisplayDensity()
- / g_settings->getFloat("gui_scaling"));
- unsigned int lohgt = font_line_height(g_fontengine->getFont(loreq));
- unsigned int hihgt = font_line_height(g_fontengine->getFont(hireq));
- while(hireq - loreq > 1 && lohgt != hihgt) {
- unsigned int nureq = (loreq + hireq) >> 1;
- unsigned int nuhgt = font_line_height(g_fontengine->getFont(nureq));
- if(nuhgt < target_line_height) {
- loreq = nureq;
- lohgt = nuhgt;
- } else {
- hireq = nureq;
- hihgt = nuhgt;
- }
- }
- return g_fontengine->getFont(target_line_height - lohgt < hihgt - target_line_height ? loreq : hireq);
-}
-