Git Product home page Git Product logo

Comments (2)

ocornut avatar ocornut commented on May 27, 2024 1

(1) Merged your fix as b475309.
(I also confirmed that the output for your specific case matches the alignment of when using imgui_freetype)

(2)

Bonus issue (related, but less important): a second inconvenience I ran into is merging with another font. Differences in ascent and descent to the merged into font will be visible unless compensated with a glyph offset. But that offset can be calculated only if the ascent of the other font is known in advance, since those metrics are not filled in before the atlas is done building. If the font is configurable by the user, this value is not known and I don't have a good solution yet. Building the atlas just to extract some metrics, discarding it again and then building it again with merged icons would work, but is far from a good solution. I need to think about this one a bit more or maybe discard custom font selection for users.

I haven't dug into this further but I wonder if merged fonts could be trying to align to e.g. descent line. I'll keep this issue in mind for reference. I think when we rewrite font building and packing we may reinvestigate this in more details.

Thank you for your investigation!

from imgui.

GamingMinds-DanielC avatar GamingMinds-DanielC commented on May 27, 2024

Made a pull request for the simple fix. Independent of that, I found a suitable solution for my second problem and put it into a wrapper function in my custom extensions:

#include <imgui.h>
#include <imgui_internal.h>
#include <imstb_truetype.h>

namespace ImGuiEx
{

	bool GetOrCalculateFontMetrics( const ImFont* font, float* size, float* ascent, float* descent, float* offsetY )
	{
		if ( font->FontSize != 0.0f )
		{
			if ( size != nullptr )
				*size = font->FontSize;
			if ( ascent != nullptr )
				*ascent = font->Ascent;
			if ( descent != nullptr )
				*descent = font->Descent;
		}
		else
		{
			if ( font->ConfigData == nullptr )
				return false;

			const ImFontConfig& config = *font->ConfigData;

			if ( size != nullptr )
				*size = config.SizePixels;

			if ( ( ascent != nullptr ) || ( descent != nullptr ) )
			{
				const unsigned char* fontData = (const unsigned char*)config.FontData;
				if ( fontData == nullptr )
					return false;

				const int fontOffset = stbtt_GetFontOffsetForIndex( fontData, config.FontNo );

				stbtt_fontinfo fontInfo;
				if ( !stbtt_InitFont( &fontInfo, (unsigned char*)font->ConfigData->FontData, fontOffset ) )
					return false;

				const float fontScale = stbtt_ScaleForPixelHeight( &fontInfo, config.SizePixels );
				int         unscaledAscent, unscaledDescent, unscaledLineGap;

				stbtt_GetFontVMetrics( &fontInfo, &unscaledAscent, &unscaledDescent, &unscaledLineGap );

#if 0
				// off by one calculation
				if ( ascent != nullptr )
					*ascent = ImTrunc( unscaledAscent * fontScale + ( ( unscaledAscent > 0.0f ) ? +1 : -1 ) );
				if ( descent != nullptr )
					*descent = ImTrunc( unscaledDescent * fontScale + ( ( unscaledDescent > 0.0f ) ? +1 : -1 ) );
#else
				// fixed calculation
				if ( ascent != nullptr )
					*ascent = ImCeil( unscaledAscent * fontScale );
				if ( descent != nullptr )
					*descent = ImFloor( unscaledDescent * fontScale );
#endif
			}
		}

		if ( offsetY != nullptr )
		{
			if ( font->ConfigData == nullptr )
				return false;
			*offsetY = font->ConfigData->GlyphOffset.y;
		}

		return true;
	}

} // namespace ImGuiEx

Doesn't replicate too much code, only parses a little of the font data and will get me the values I need to merge my icon font with perfect pixel precision regardless of font or font size it is merged into. There is just a single #if that must be set depending on whether the metrics fix has been applied or not. Maybe this is useful for someone else as well.

from imgui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.