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/Quartz/Fl_Quartz_Graphics_Driver_font.cxx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/drivers/Quartz') diff --git a/src/drivers/Quartz/Fl_Quartz_Graphics_Driver_font.cxx b/src/drivers/Quartz/Fl_Quartz_Graphics_Driver_font.cxx index 7b2085c23..a277010e3 100644 --- a/src/drivers/Quartz/Fl_Quartz_Graphics_Driver_font.cxx +++ b/src/drivers/Quartz/Fl_Quartz_Graphics_Driver_font.cxx @@ -1,7 +1,7 @@ // // MacOS font selection routines for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2018 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 @@ -300,6 +300,21 @@ void Fl_Quartz_Graphics_Driver::rtl_draw(const char* c, int n, int x, int y) { } double Fl_Quartz_Graphics_Driver::width(const char* txt, int n) { + if (n == 0) return 0; + int len1 = fl_utf8len1(*txt); + if (len1 > 0 && n > len1) { // a text with several codepoints: compute its typographical width + CFStringRef str = CFStringCreateWithBytes(NULL, (const UInt8*)txt, n, kCFStringEncodingUTF8, false); + if (str) { + CFDictionarySetValue(attributes, kCTFontAttributeName, valid_font_descriptor()->fontref); + CFAttributedStringRef mastr = CFAttributedStringCreate(kCFAllocatorDefault, str, attributes); + CFRelease(str); + CTLineRef ctline = CTLineCreateWithAttributedString(mastr); + CFRelease(mastr); + double d = CTLineGetTypographicBounds(ctline, NULL, NULL, NULL); + CFRelease(ctline); + return d; + } + } int wc_len = n; UniChar *uniStr = mac_Utf8_to_Utf16(txt, n, &wc_len); return width(uniStr, wc_len); -- cgit v1.2.3