Skip to content

Commit

Permalink
Fixed|libgui: Font metrics and rasterization bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 99d2e2f commit c22035c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions doomsday/libs/gui/include/de/libgui.h
Expand Up @@ -46,9 +46,9 @@
# define LIBGUI_PUBLIC DE_PUBLIC
#endif

#if defined(WIN32) || defined(MACOSX)
# define LIBGUI_ACCURATE_TEXT_BOUNDS
#endif
//#if defined(WIN32) || defined(MACOSX)
#define LIBGUI_ACCURATE_TEXT_BOUNDS
//#endif

// Assertion specific to GL errors.
#if 0 //|| defined(DE_X11) || defined(WIN32)
Expand Down
4 changes: 3 additions & 1 deletion doomsday/libs/gui/src/text/font.cpp
Expand Up @@ -394,6 +394,7 @@ Image Font::rasterize(RichFormatRef const &format,

const String part = iter.range();

/*
#ifdef WIN32
// Kludge: No light-weight fonts available, so reduce opacity to give the
// illusion of thinness.
Expand All @@ -407,6 +408,7 @@ Image Font::rasterize(RichFormatRef const &format,
fg.w *= .925f;
}
#endif
*/

Image fragment = font->rasterize(part, fg, bg);
Rectanglei const bounds = font->measure(part);
Expand Down Expand Up @@ -439,7 +441,7 @@ Rule const &Font::lineSpacing() const

bool Font::load(const Block &data) // static
{
DE_ASSERT_FAIL("Font::load not implemented");
return SdlNativeFont::load(data);
}

//------------------------------------------------------------------------------------------------
Expand Down
32 changes: 16 additions & 16 deletions doomsday/libs/gui/src/text/sdlnativefont.cpp
Expand Up @@ -17,7 +17,7 @@
*/

#include "sdlnativefont.h"
#include "de/GuiApp"
#include <de/GuiApp>
#include <de/Map>
#include <de/String>
#include <de/ThreadLocal>
Expand Down Expand Up @@ -74,11 +74,11 @@ struct FontDatabase

bool addSource(const Block &source)
{
const int size = 16;
if (TTF_Font *font =
TTF_OpenFontRW(SDL_RWFromConstMem(source.data(), int(source.size())), 0, size))
TTF_OpenFontRW(SDL_RWFromConstMem(source.data(), int(source.size())), 0, 16))
{
sourceData[fontName(font)] = source;
TTF_CloseFont(font);
return true;
}
return false;
Expand All @@ -91,12 +91,12 @@ struct FontCache // thread-local
{
Map<FontSpec, std::unique_ptr<TTF_Font, FontDeleter>> fonts; // loaded fonts

void insertFont(TTF_Font *font, int size)
{
const String name = fontName(font);
debug("[SdlNativeFont] inserting font %p '%s' size:%d", font, name.c_str(), size);
fonts[{name, size}].reset(font);
}
// void insertFont(TTF_Font *font, int size)
// {
// const String name = fontName(font);
// debug("[SdlNativeFont] inserting font %p '%s' size:%d", font, name.c_str(), size);
// fonts[{name, size}].reset(font);
// }

TTF_Font *load(const String &name, int size)
{
Expand Down Expand Up @@ -172,12 +172,10 @@ DE_PIMPL(SdlNativeFont)
self().style());
if (font)
{
const float scale = DE_GUI_APP->devicePixelRatio();

height = TTF_FontHeight(font) * scale;
ascent = TTF_FontAscent(font) * scale;
descent = TTF_FontDescent(font) * scale;
lineHeight = TTF_FontLineSkip(font) * scale;
height = TTF_FontHeight(font);
ascent = TTF_FontAscent(font);
descent = -TTF_FontDescent(font);
lineHeight = TTF_FontLineSkip(font);
}
else
{
Expand Down Expand Up @@ -232,7 +230,9 @@ Rectanglei SdlNativeFont::nativeFontMeasure(const String &text) const
{
Vec2i size;
TTF_SizeUTF8(d->font, text.c_str(), &size.x, &size.y);
return {{}, size};
Rectanglei rect{{}, size};
rect.move({0, -d->ascent});
return rect;
}
return {};
}
Expand Down

0 comments on commit c22035c

Please sign in to comment.