From 5b50b07717a0adf353e866c1f5502f1c5b374d22 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 19 Feb 2023 20:30:38 -0700 Subject: [PATCH] [subset-cff] Make BCD writing locale-independent --- src/hb-common.cc | 26 -------------------------- src/hb-subset-cff-common.hh | 10 ++++++++-- src/hb.hh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/hb-common.cc b/src/hb-common.cc index e9f9cfeb5..c9a40295a 100644 --- a/src/hb-common.cc +++ b/src/hb-common.cc @@ -29,32 +29,6 @@ #include "hb.hh" #include "hb-machinery.hh" -#if !defined(HB_NO_SETLOCALE) && (!defined(HAVE_NEWLOCALE) || !defined(HAVE_USELOCALE)) -#define HB_NO_SETLOCALE 1 -#endif - -#ifndef HB_NO_SETLOCALE - -#include -#ifdef HAVE_XLOCALE_H -#include // Needed on BSD/OS X for uselocale -#endif - -#ifdef WIN32 -#define hb_locale_t _locale_t -#else -#define hb_locale_t locale_t -#endif -#define hb_setlocale setlocale -#define hb_uselocale uselocale - -#else - -#define hb_locale_t void * -#define hb_setlocale(Category, Locale) "C" -#define hb_uselocale(Locale) ((hb_locale_t) 0) - -#endif /** * SECTION:hb-common diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 6351720e6..ff50b0e51 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -125,7 +125,13 @@ struct str_encoder_t * * We use 8 here to match FontTools X-). */ - snprintf (buf, sizeof (buf), "%.8G", v); // XXX This is locale-sensitive; Ugh + + hb_locale_t clocale HB_UNUSED; + hb_locale_t oldlocale HB_UNUSED; + oldlocale = hb_uselocale (clocale = newlocale (LC_ALL_MASK, "C", NULL)); + snprintf (buf, sizeof (buf), "%.8G", v); + (void) hb_uselocale (((void) freelocale (clocale), oldlocale)); + char *s = buf; if (s[0] == '0' && s[1] == '.') s++; @@ -157,7 +163,7 @@ struct str_encoder_t continue; } - case '.': case ',': // Comma for some European locales!!! + case '.': case ',': // Comma for some European locales in case no uselocale available. nibbles.push (0x0A); // . continue; diff --git a/src/hb.hh b/src/hb.hh index aac43a295..857571b42 100644 --- a/src/hb.hh +++ b/src/hb.hh @@ -463,6 +463,37 @@ static int HB_UNUSED _hb_errno = 0; #endif #endif + +// Locale business + +#if !defined(HB_NO_SETLOCALE) && (!defined(HAVE_NEWLOCALE) || !defined(HAVE_USELOCALE)) +#define HB_NO_SETLOCALE 1 +#endif + +#ifndef HB_NO_SETLOCALE + +#include +#ifdef HAVE_XLOCALE_H +#include // Needed on BSD/OS X for uselocale +#endif + +#ifdef WIN32 +#define hb_locale_t _locale_t +#else +#define hb_locale_t locale_t +#endif +#define hb_setlocale setlocale +#define hb_uselocale uselocale + +#else + +#define hb_locale_t void * +#define hb_setlocale(Category, Locale) "C" +#define hb_uselocale(Locale) ((hb_locale_t) 0) + +#endif + + /* Lets assert int types. Saves trouble down the road. */ static_assert ((sizeof (hb_codepoint_t) == 4), ""); static_assert ((sizeof (hb_position_t) == 4), "");