From 14a5f705c8e3385a637be3377f0800b30c38e589 Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Wed, 21 Jan 2026 14:00:26 +0100 Subject: Improve handling of text containing context-dependent unicode points. This commit makes platforms Windows and macOS compute string widths with the same mechanism as what is in place for platforms Wayland/X11: - the width of a string containing a single codepoint is computed and memorized in the table of character widths; - the width of a string containing several codepoints is computed as such rather than as the sum of the widths of its composing characters. The result is that FLTK text widgets input and draw correctly also complex emojis encoded with context-dependent codepoints. Function fl_utf8_remove_context_dependent() is no longer necessary. --- src/drivers/GDI/Fl_GDI_Graphics_Driver_font.cxx | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/drivers/GDI') diff --git a/src/drivers/GDI/Fl_GDI_Graphics_Driver_font.cxx b/src/drivers/GDI/Fl_GDI_Graphics_Driver_font.cxx index e901db971..49111f10e 100644 --- a/src/drivers/GDI/Fl_GDI_Graphics_Driver_font.cxx +++ b/src/drivers/GDI/Fl_GDI_Graphics_Driver_font.cxx @@ -1,7 +1,7 @@ // // Windows font utilities for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2025 by Bill Spitzak and others. +// Copyright 1998-2026 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -366,6 +366,27 @@ Fl_Fontsize Fl_GDI_Graphics_Driver::size_unscaled() { } double Fl_GDI_Graphics_Driver::width_unscaled(const char* c, int n) { + if (n == 0) return 0; + int len1 = fl_utf8len1(*c); + if (n > len1 && len1 > 0) { // a text with several codepoints: compute its typographical width + int wn = fl_utf8toUtf16(c, n, wstr, wstr_len); + if (wn >= wstr_len) { + wstr = (unsigned short*) realloc(wstr, sizeof(unsigned short) * (wn + 1)); + wstr_len = wn + 1; + wn = fl_utf8toUtf16(c, n, wstr, wstr_len); + } + HDC gc2 = gc_; + HWND hWnd; + if (!gc2) { + hWnd = Fl::first_window() ? fl_xid(Fl::first_window()) : NULL; + gc2 = GetDC(hWnd); + } + SelectObject(gc2, ((Fl_GDI_Font_Descriptor*)font_descriptor())->fid); + SIZE s; + GetTextExtentPoint32W(gc2, (WCHAR*)wstr, wn, &s); + if (gc2 && gc2 != gc_) ReleaseDC(hWnd, gc2); + return (double)s.cx; + } int i = 0; if (!font_descriptor()) return -1.0; double w = 0.0; -- cgit v1.2.3