From 998e8dda938cfef0146f1bfc4e8973a0e12d7d35 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Mon, 2 Nov 2015 16:55:29 +0800 Subject: [PATCH 1/4] util: Fix build on Visual Studio Use the fallback implementation for lround() only on pre-2013 Visual Studio, and ensure we are clear about the types of the parameters for lround() and scalbnf(), since Visual Studio can be quite picky on ambiguous parameter types. Also, use g_ascii_strcasecmp() rather than strcasecmp() as we are already using GLib for this code and we are assured that g_ascii_strcasemp() is available. For scalbnf() on pre-2013 Visaul Studio, a fallback implementation is needed, but use another forced-included header for those compilers, which will be added later. Also use (char)27 on Visual Studio builds as '\e' is not a recognized escape sequence, which will do the same thing. --- util/ansi-print.cc | 16 +++++++++------- util/helper-cairo.cc | 20 ++++++++++---------- util/options.cc | 2 +- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/util/ansi-print.cc b/util/ansi-print.cc index e9060af56..e0ce7b356 100644 --- a/util/ansi-print.cc +++ b/util/ansi-print.cc @@ -41,7 +41,7 @@ #include /* for isatty() */ #endif -#ifdef _MSC_VER +#if defined (_MSC_VER) && (_MSC_VER < 1800) static inline long int lround (double x) { @@ -52,6 +52,8 @@ lround (double x) } #endif +#define ESC_E (char)27 + #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define CELL_W 8 @@ -298,7 +300,7 @@ block_best (const biimage_t &bi, bool *inverse) } if (best_s < score) { static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"}; - unsigned int which = lround (((best_i + 1) * 8) / bi.height); + unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.height); if (1 <= which && which <= 7) { score = best_s; *inverse = best_inv; @@ -330,7 +332,7 @@ block_best (const biimage_t &bi, bool *inverse) } if (best_s < score) { static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"}; - unsigned int which = lround (((best_i + 1) * 8) / bi.width); + unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.width); if (1 <= which && which <= 7) { score = best_s; *inverse = best_inv; @@ -395,7 +397,7 @@ ansi_print_image_rgb24 (const uint32_t *data, bi.set (cell); if (bi.unicolor) { if (last_bg != bi.bg) { - printf ("\e[%dm", 40 + bi.bg); + printf ("%c[%dm", ESC_E, 40 + bi.bg); last_bg = bi.bg; } printf (" "); @@ -405,13 +407,13 @@ ansi_print_image_rgb24 (const uint32_t *data, const char *c = block_best (bi, &inverse); if (inverse) { if (last_bg != bi.fg || last_fg != bi.bg) { - printf ("\e[%d;%dm", 30 + bi.bg, 40 + bi.fg); + printf ("%c[%d;%dm", ESC_E, 30 + bi.bg, 40 + bi.fg); last_bg = bi.fg; last_fg = bi.bg; } } else { if (last_bg != bi.bg || last_fg != bi.fg) { - printf ("\e[%d;%dm", 40 + bi.bg, 30 + bi.fg); + printf ("%c[%d;%dm", ESC_E, 40 + bi.bg, 30 + bi.fg); last_bg = bi.bg; last_fg = bi.fg; } @@ -419,7 +421,7 @@ ansi_print_image_rgb24 (const uint32_t *data, printf ("%s", c); } } - printf ("\e[0m\n"); /* Reset */ + printf ("%c[0m\n", ESC_E); /* Reset */ last_bg = last_fg = -1; } } diff --git a/util/helper-cairo.cc b/util/helper-cairo.cc index 50e22ab0e..450e5cf95 100644 --- a/util/helper-cairo.cc +++ b/util/helper-cairo.cc @@ -341,25 +341,25 @@ helper_cairo_create_context (double w, double h, } if (0) ; - else if (0 == strcasecmp (extension, "ansi")) + else if (0 == g_ascii_strcasecmp (extension, "ansi")) constructor2 = _cairo_ansi_surface_create_for_stream; #ifdef CAIRO_HAS_PNG_FUNCTIONS - else if (0 == strcasecmp (extension, "png")) + else if (0 == g_ascii_strcasecmp (extension, "png")) constructor2 = _cairo_png_surface_create_for_stream; #endif #ifdef CAIRO_HAS_SVG_SURFACE - else if (0 == strcasecmp (extension, "svg")) + else if (0 == g_ascii_strcasecmp (extension, "svg")) constructor = cairo_svg_surface_create_for_stream; #endif #ifdef CAIRO_HAS_PDF_SURFACE - else if (0 == strcasecmp (extension, "pdf")) + else if (0 == g_ascii_strcasecmp (extension, "pdf")) constructor = cairo_pdf_surface_create_for_stream; #endif #ifdef CAIRO_HAS_PS_SURFACE - else if (0 == strcasecmp (extension, "ps")) + else if (0 == g_ascii_strcasecmp (extension, "ps")) constructor = cairo_ps_surface_create_for_stream; #ifdef HAS_EPS - else if (0 == strcasecmp (extension, "eps")) + else if (0 == g_ascii_strcasecmp (extension, "eps")) constructor = _cairo_eps_surface_create_for_stream; #endif #endif @@ -478,16 +478,16 @@ helper_cairo_line_from_buffer (helper_cairo_line_t *l, for (i = 0; i < (int) l->num_glyphs; i++) { l->glyphs[i].index = hb_glyph[i].codepoint; - l->glyphs[i].x = scalbn ( hb_position->x_offset + x, scale_bits); - l->glyphs[i].y = scalbn (-hb_position->y_offset + y, scale_bits); + l->glyphs[i].x = scalbn ((double) hb_position->x_offset + x, scale_bits); + l->glyphs[i].y = scalbn ((double) -hb_position->y_offset + y, scale_bits); x += hb_position->x_advance; y += -hb_position->y_advance; hb_position++; } l->glyphs[i].index = -1; - l->glyphs[i].x = scalbn (x, scale_bits); - l->glyphs[i].y = scalbn (y, scale_bits); + l->glyphs[i].x = scalbn ((double) x, scale_bits); + l->glyphs[i].y = scalbn ((double) y, scale_bits); if (l->num_clusters) { memset ((void *) l->clusters, 0, l->num_clusters * sizeof (l->clusters[0])); diff --git a/util/options.cc b/util/options.cc index ca8f90669..882e06072 100644 --- a/util/options.cc +++ b/util/options.cc @@ -569,7 +569,7 @@ font_options_t::get_font (void) const else { for (unsigned int i = 0; i < ARRAY_LENGTH (supported_font_funcs); i++) - if (0 == strcasecmp (font_funcs, supported_font_funcs[i].name)) + if (0 == g_ascii_strcasecmp (font_funcs, supported_font_funcs[i].name)) { set_font_funcs = supported_font_funcs[i].func; break; From a49e7b7e40127beba25f39ef3c10d7a77e2bb0f0 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Tue, 3 Nov 2015 18:49:34 +0800 Subject: [PATCH 2/4] MSVC builds: Add fallback implementation for pre-2013 MSVC Pre-2013 MSVC does not have scalbn() and scalbnf(), which are used in the utility programs. Add fallback implementations for these, which can be used when necessary. --- util/options.hh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/util/options.hh b/util/options.hh index 1ef4131ee..919e4f8b2 100644 --- a/util/options.hh +++ b/util/options.hh @@ -474,5 +474,22 @@ struct format_options_t : option_group_t hb_bool_t show_extents; }; +/* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */ +#if defined (_MSC_VER) && (_MSC_VER < 1800) + +#ifndef FLT_RADIX +#define FLT_RADIX 2 +#endif + +__inline long double scalbn (long double x, int exp) +{ + return x * (pow ((long double) FLT_RADIX, exp)); +} + +__inline float scalbnf (float x, int exp) +{ + return x * (pow ((float) FLT_RADIX, exp)); +} +#endif #endif From 4d27bb87468a1b84387e7ce084e3d92c0fc8f065 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Fri, 6 Nov 2015 14:28:30 +0800 Subject: [PATCH 3/4] hb-ot-shape-complex-arabic.cc: Fix build on Visual Studio Visual Studio does not like declaring a enum variable within a for statement, so fix the build by declaring the enum before doing the for loop. --- src/hb-ot-shape-complex-arabic.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hb-ot-shape-complex-arabic.cc b/src/hb-ot-shape-complex-arabic.cc index 575ab6100..507547714 100644 --- a/src/hb-ot-shape-complex-arabic.cc +++ b/src/hb-ot-shape-complex-arabic.cc @@ -469,8 +469,9 @@ apply_stch (const hb_ot_shape_plan_t *plan, DEBUG_MSG (ARABIC, NULL, "overlap for stretching is %d", overlap); int sign = font->x_scale < 0 ? -1 : +1; unsigned int extra_glyphs_needed = 0; // Set during MEASURE, used during CUT + typedef enum { MEASURE, CUT } step_t; - for (enum step_t { MEASURE, CUT } step = MEASURE; step <= CUT; step = (step_t) (step + 1)) + for (step_t step = MEASURE; step <= CUT; step = (step_t) (step + 1)) { unsigned int count = buffer->len; hb_glyph_info_t *info = buffer->info; From 167c3271778cd1a8c4433b9d2230901ce17c099e Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Mon, 9 Nov 2015 17:17:56 +0800 Subject: [PATCH 4/4] Fix build on MSVC >= 2012 Use the DEFINE_ENUM_FLAG_OPERATORS macro in winnt.h on Visual Studio, which defines the bitwise operators for the enumerations that we want to mark as hb_mark_as_flags_t, which will take care of the situation on newer Visual Studio (>= 2012), where the build breaks with C2057 errors as the underlying types of the enumerations is not clear to the compiler when we do a bitwise op within the declaration of the enumerations themselves. Also disable the C4200 (nonstandard extension used : zero-sized array in struct/union) and C4800 ('type' : forcing value to bool 'true' or 'false' (performance warning)) warnings as the C4200 is the intended scenario and C4800 is harmless but is so far an unavoidable side effect of using DEFINE_ENUM_FLAG_OPERATORS. --- src/hb-buffer-private.hh | 6 +++--- src/hb-ot-layout-common-private.hh | 2 +- src/hb-ot-layout-private.hh | 4 ++-- src/hb-ot-map-private.hh | 2 +- src/hb-private.hh | 11 +++++++++++ 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/hb-buffer-private.hh b/src/hb-buffer-private.hh index 67c431d39..ceccecdce 100644 --- a/src/hb-buffer-private.hh +++ b/src/hb-buffer-private.hh @@ -48,8 +48,8 @@ ASSERT_STATIC (sizeof (hb_glyph_info_t) == 20); ASSERT_STATIC (sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t)); -template <> class hb_mark_as_flags_t {}; -template <> class hb_mark_as_flags_t {}; +HB_MARK_AS_FLAG_T (hb_buffer_flags_t); +HB_MARK_AS_FLAG_T (hb_buffer_serialize_flags_t); enum hb_buffer_scratch_flags_t { HB_BUFFER_SCRATCH_FLAG_DEFAULT = 0x00000000u, @@ -64,7 +64,7 @@ enum hb_buffer_scratch_flags_t { HB_BUFFER_SCRATCH_FLAG_COMPLEX2 = 0x04000000u, HB_BUFFER_SCRATCH_FLAG_COMPLEX3 = 0x08000000u, }; -template <> class hb_mark_as_flags_t {}; +HB_MARK_AS_FLAG_T (hb_buffer_scratch_flags_t); /* diff --git a/src/hb-ot-layout-common-private.hh b/src/hb-ot-layout-common-private.hh index 04958a816..5b21f1427 100644 --- a/src/hb-ot-layout-common-private.hh +++ b/src/hb-ot-layout-common-private.hh @@ -581,7 +581,7 @@ struct LookupFlag : USHORT } /* namespace OT */ /* This has to be outside the namespace. */ -template <> class hb_mark_as_flags_t {}; +HB_MARK_AS_FLAG_T (OT::LookupFlag::Flags); namespace OT { struct Lookup diff --git a/src/hb-ot-layout-private.hh b/src/hb-ot-layout-private.hh index 049b232de..45897ed5a 100644 --- a/src/hb-ot-layout-private.hh +++ b/src/hb-ot-layout-private.hh @@ -65,7 +65,7 @@ enum hb_ot_layout_glyph_props_flags_t HB_OT_LAYOUT_GLYPH_PROPS_LIGATED | HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED }; -template <> class hb_mark_as_flags_t {}; +HB_MARK_AS_FLAG_T (hb_ot_layout_glyph_props_flags_t); /* @@ -237,7 +237,7 @@ enum hb_unicode_props_flags_t { UPROPS_MASK_IGNORABLE = 0x80u, UPROPS_MASK_GEN_CAT = 0x1Fu }; -template <> class hb_mark_as_flags_t {}; +HB_MARK_AS_FLAG_T (hb_unicode_props_flags_t); static inline void _hb_glyph_info_set_unicode_props (hb_glyph_info_t *info, hb_buffer_t *buffer) diff --git a/src/hb-ot-map-private.hh b/src/hb-ot-map-private.hh index 1e3ff67e3..6f62c77de 100644 --- a/src/hb-ot-map-private.hh +++ b/src/hb-ot-map-private.hh @@ -159,7 +159,7 @@ enum hb_ot_map_feature_flags_t { F_MANUAL_ZWJ = 0x0004u, /* Don't skip over ZWJ when matching. */ F_GLOBAL_SEARCH = 0x0008u /* If feature not found in LangSys, look for it in global feature list and pick one. */ }; -template <> class hb_mark_as_flags_t {}; +HB_MARK_AS_FLAG_T (hb_ot_map_feature_flags_t); /* Macro version for where const is desired. */ #define F_COMBINE(l,r) (hb_ot_map_feature_flags_t ((unsigned int) (l) | (unsigned int) (r))) diff --git a/src/hb-private.hh b/src/hb-private.hh index 5ed7ff3b2..32f8d5e37 100644 --- a/src/hb-private.hh +++ b/src/hb-private.hh @@ -119,6 +119,15 @@ extern "C" void hb_free_impl(void *ptr); #define HB_FUNC __func__ #endif +/* Use templates for bitwise ops on enums or MSVC's DEFINE_ENUM_FLAG_OPERATORS */ +#ifdef _MSC_VER +# pragma warning(disable:4200) +# pragma warning(disable:4800) +# define HB_MARK_AS_FLAG_T(flags_t) DEFINE_ENUM_FLAG_OPERATORS (##flags_t##); +#else +# define HB_MARK_AS_FLAG_T(flags_t) template <> class hb_mark_as_flags_t {}; +#endif + /* * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch @@ -895,6 +904,7 @@ hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3) /* To my surprise, looks like the function resolver is happy to silently cast * one enum to another... So this doesn't provide the type-checking that I * originally had in mind... :( */ +#ifndef _MSC_VER template class hb_mark_as_flags_t; template static inline T operator | (T l, T r) { hb_mark_as_flags_t unused HB_UNUSED; return T ((unsigned int) l | (unsigned int) r); } @@ -906,6 +916,7 @@ template static inline T& operator |= (T &l, T r) { hb_mark_as_flags_t unused HB_UNUSED; l = l | r; return l; } template static inline T& operator &= (T& l, T r) { hb_mark_as_flags_t unused HB_UNUSED; l = l & r; return l; } +#endif /* Useful for set-operations on small enums.