Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering U+0305 #7362

Closed
evgenydrainov opened this issue Mar 3, 2024 · 3 comments
Closed

Rendering U+0305 #7362

evgenydrainov opened this issue Mar 3, 2024 · 3 comments

Comments

@evgenydrainov
Copy link

Version/Branch of Dear ImGui:

Version 1.90.5 WIP, Branch: docking

Back-ends:

imgui_impl_win32.cpp + imgui_impl_dx9.cpp

Compiler, OS:

Windows 11 + MSVC 2022

Full config/build information:

Dear ImGui 1.90.5 WIP (19042)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS
define: IMGUI_DISABLE_OBSOLETE_KEYIO
define: _WIN32
define: _WIN64
define: _MSC_VER=1939
define: _MSVC_LANG=201402
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx9
io.ConfigFlags: 0x00000003
 NavEnableKeyboard
 NavEnableGamepad
io.ConfigViewportsNoDecoration
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C0E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 3 fonts, Flags: 0x00000000, TexSize: 512,1024
io.DisplaySize: 1920.00,1133.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

Rendering U+0305

Is it possible to render U+0305 (Combining Overline) with imgui? My string is written like this:

std::string result_unicode = "(x\xCC\x85 \xE2\x88\xA7 y\xCC\x85 \xE2\x88\xA7 z\xCC\x85)";

And I render it like this:

ImGui::PushTextWrapPos(0);
ImGui::PushFont(fnt_math);
ImGui::TextUnformatted(result_unicode.c_str());
ImGui::PopFont();
ImGui::PopTextWrapPos();

I've tried several fonts: C:\Windows\Fonts\segoeui.ttf, C:\Windows\Fonts\cambria.ttc, C:\Windows\Fonts\cour.ttf, DroidSans from misc/fonts.
I'm loading 0x3000, 0x036F Combining Diacritical Marks range and 0x2000, 0x206F General Punctuation range.

Screenshots/Video:

With cambria.ttc:
image

Minimal, Complete and Verifiable Example code:

My full font loading code:

ImGuiIO& io = ImGui::GetIO();

{
	ImVector<ImWchar> fnt_main_ranges;
	{
		ImWchar _ranges[] = {
			0x2000, 0x206F, // General Punctuation (for Overline)
			0x2070, 0x209F, // Superscripts and Subscripts
			0x2200, 0x22FF, // Mathematical Operators (for logical and, or)
			0x3000, 0x036F, // Combining Diacritical Marks (for Combining Overline)
			0,
		};

		ImFontGlyphRangesBuilder builder;
		builder.AddRanges(io.Fonts->GetGlyphRangesDefault());
		builder.AddRanges(io.Fonts->GetGlyphRangesCyrillic());
		builder.AddRanges(_ranges);

		builder.BuildRanges(&fnt_main_ranges);
	}

	fnt_main = AddFontFromFileTTF(io.Fonts, "C:\\Windows\\Fonts\\segoeui.ttf", 24, nullptr, fnt_main_ranges.Data);
	if (!fnt_main) {
		ImFontConfig config;
		config.SizePixels = 20;
		fnt_main = io.Fonts->AddFontDefault(&config);
	}

	fnt_mono = AddFontFromFileTTF(io.Fonts, "C:\\Windows\\Fonts\\cour.ttf", 24);
	if (!fnt_mono) {
		fnt_mono = fnt_main;
	}

	ImVector<ImWchar> fnt_math_ranges;
	{
		ImWchar _ranges[] = {
			0x2000, 0x206F, // General Punctuation (for Overline)
			0x2200, 0x22FF, // Mathematical Operators (for logical and, or)
			0x3000, 0x036F, // Combining Diacritical Marks (for Combining Overline)
			0,
		};

		ImFontGlyphRangesBuilder builder;
		builder.AddRanges(io.Fonts->GetGlyphRangesDefault());
		builder.AddRanges(_ranges);

		builder.BuildRanges(&fnt_math_ranges);
	}

	fnt_math = AddFontFromFileTTF(io.Fonts, "C:\\Windows\\Fonts\\cambria.ttc", 24, nullptr, fnt_math_ranges.Data);
	if (!fnt_math) {
		fnt_math = fnt_main;
	}

	// fnt_math = fnt_main;

	io.Fonts->Build();
}

And rendering:

if (ImGui::Begin("mcve")) {
	std::string result_unicode = "(x\xCC\x85 \xE2\x88\xA7 y\xCC\x85 \xE2\x88\xA7 z\xCC\x85)";

	ImGui::PushTextWrapPos(0);
	ImGui::PushFont(fnt_math);
	ImGui::TextUnformatted(result_unicode.c_str());
	ImGui::PopFont();
	ImGui::PopTextWrapPos();
}
ImGui::End();
@evgenydrainov
Copy link
Author

Forgot to tell, AddFontFromFileTTF is a version of ImFontAtlas::AddFontFromFileTTF that doesn't assert on failure:

// 
// Don't assert on fail
// 
static ImFont* AddFontFromFileTTF(ImFontAtlas* atlas, const char* filename, float size_pixels, const ImFontConfig* font_cfg_template = nullptr, const ImWchar* glyph_ranges = nullptr)
{
	size_t data_size = 0;
	void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0);
	if (!data)
	{
		return NULL;
	}
	ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
	if (font_cfg.Name[0] == '\0')
	{
		// Store a short copy of filename into into the font name for convenience
		const char* p;
		for (p = filename + strlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {}
		ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", p, size_pixels);
	}
	return atlas->AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);
}

@PathogenDavid
Copy link
Contributor

Unfortunately Dear ImGui doesn't support combining characters.

Non-trivial text shaping doesn't mesh well with the architecture or philosophy of the library so this is unlikely to improve.

(See also #4922 #6036 #4227 #1228 #4943)

@evgenydrainov
Copy link
Author

Ok got it thanks. I'll try to look for a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants