From aa09dd51d1486eb74e72f2f86fb50be9dc81f249 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 17 Apr 2026 00:45:38 -0600 Subject: [PATCH] [vector] Replace hb-vector-utils with hb-vector-buf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move all buffer functionality into hb_buf_t methods in hb-vector-buf.hh. Remove all hb_buf_append_* free functions. hb_buf_t now owns: append_len, append_c, append_str, append_unsigned, append_hex_byte, append_num, append_svg_color, append_base64. Delete hb-vector-utils.hh. Rename hb-vector-utils.cc → hb-vector-buf.cc (keeps only hb_vector_decimal_point_get). Remove duplicate free functions from hb-vector-svg.hh (hb_buf_append_str, _unsigned, _hex_byte, _base64, _color). Assisted-by: Claude Opus 4.6 (1M context) --- CMakeLists.txt | 4 +- src/harfbuzz-world.cc | 2 +- src/{hb-vector-utils.cc => hb-vector-buf.cc} | 2 +- src/{hb-vector-utils.hh => hb-vector-buf.hh} | 161 ++++++++----------- src/hb-vector-draw.hh | 2 +- src/hb-vector-paint.hh | 2 +- src/hb-vector-path.cc | 2 +- src/hb-vector-path.hh | 2 +- src/hb-vector-svg.hh | 107 ------------ src/meson.build | 4 +- 10 files changed, 74 insertions(+), 214 deletions(-) rename src/{hb-vector-utils.cc => hb-vector-buf.cc} (98%) rename src/{hb-vector-utils.hh => hb-vector-buf.hh} (62%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e784610b..483a16a72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -263,8 +263,8 @@ set (vector_project_sources ${PROJECT_SOURCE_DIR}/src/hb-vector-paint-svg.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-paint-pdf.cc ${PROJECT_SOURCE_DIR}/src/hb-vector-path.hh - ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-utils.cc - ${PROJECT_SOURCE_DIR}/src/hb-vector-svg-utils.hh + ${PROJECT_SOURCE_DIR}/src/hb-vector-buf.cc + ${PROJECT_SOURCE_DIR}/src/hb-vector-buf.hh ${PROJECT_SOURCE_DIR}/src/hb-static.cc ) set (vector_project_headers diff --git a/src/harfbuzz-world.cc b/src/harfbuzz-world.cc index 870988979..22f06553c 100644 --- a/src/harfbuzz-world.cc +++ b/src/harfbuzz-world.cc @@ -153,12 +153,12 @@ #ifdef HB_HAS_VECTOR #include "hb-static.cc" +#include "hb-vector-buf.cc" #include "hb-vector-draw.cc" #include "hb-vector-paint-pdf.cc" #include "hb-vector-paint-svg.cc" #include "hb-vector-paint.cc" #include "hb-vector-path.cc" -#include "hb-vector-utils.cc" #include "hb-vector.cc" #include "hb-zlib.cc" #endif diff --git a/src/hb-vector-utils.cc b/src/hb-vector-buf.cc similarity index 98% rename from src/hb-vector-utils.cc rename to src/hb-vector-buf.cc index 00d2c784f..5e0ef4200 100644 --- a/src/hb-vector-utils.cc +++ b/src/hb-vector-buf.cc @@ -27,7 +27,7 @@ #include "hb.hh" #include "hb-machinery.hh" -#include "hb-vector-utils.hh" +#include "hb-vector-buf.hh" #include diff --git a/src/hb-vector-utils.hh b/src/hb-vector-buf.hh similarity index 62% rename from src/hb-vector-utils.hh rename to src/hb-vector-buf.hh index 629fab9b7..060f4259d 100644 --- a/src/hb-vector-utils.hh +++ b/src/hb-vector-buf.hh @@ -24,8 +24,8 @@ * Author(s): Behdad Esfahbod */ -#ifndef HB_VECTOR_UTILS_HH -#define HB_VECTOR_UTILS_HH +#ifndef HB_VECTOR_BUF_HH +#define HB_VECTOR_BUF_HH #include "hb.hh" #include "hb-vector.hh" @@ -36,90 +36,6 @@ HB_INTERNAL const char * hb_vector_decimal_point_get (void); -static inline bool -hb_buf_append_len (hb_vector_t *buf, - const char *s, - unsigned len) -{ - unsigned old_len = buf->length; - if (unlikely (!buf->resize_dirty ((int) (old_len + len)))) - return false; - hb_memcpy (buf->arrayZ + old_len, s, len); - return true; -} - -static inline bool -hb_buf_append_c (hb_vector_t *buf, char c) -{ - return buf->push_or_fail (c); -} - -static inline void -hb_buf_append_num (hb_vector_t *buf, - float v, - unsigned precision, - bool keep_nonzero = false) -{ - unsigned effective_precision = precision; - if (effective_precision > 12) - effective_precision = 12; - if (keep_nonzero && v != 0.f) - while (effective_precision < 12) - { - float rounded_zero_threshold = 0.5f; - for (unsigned i = 0; i < effective_precision; i++) - rounded_zero_threshold *= 0.1f; - if (fabsf (v) >= rounded_zero_threshold) - break; - effective_precision++; - } - - float rounded_zero_threshold = 0.5f; - for (unsigned i = 0; i < effective_precision; i++) - rounded_zero_threshold *= 0.1f; - if (fabsf (v) < rounded_zero_threshold) - v = 0.f; - - if (!(v == v) || !std::isfinite (v)) - { - hb_buf_append_c (buf, '0'); - return; - } - - static const char float_formats[13][6] = { - "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f", - "%.7f", "%.8f", "%.9f", "%.10f", "%.11f", "%.12f", - }; - char out[128]; - snprintf (out, sizeof (out), float_formats[effective_precision], (double) v); - - const char *decimal_point = hb_vector_decimal_point_get (); - - if (decimal_point[0] != '.' || decimal_point[1] != '\0') - { - char *p = strstr (out, decimal_point); - if (p) - { - unsigned dp_len = (unsigned) strlen (decimal_point); - unsigned tail_len = (unsigned) strlen (p + dp_len); - memmove (p + 1, p + dp_len, tail_len + 1); - *p = '.'; - } - } - - char *dot = strchr (out, '.'); - if (dot) - { - char *end = out + strlen (out) - 1; - while (end > dot && *end == '0') - *end-- = '\0'; - if (end == dot) - *end = '\0'; - } - - hb_buf_append_len (buf, out, (unsigned) strlen (out)); -} - static inline unsigned hb_vector_scale_precision (unsigned precision) { @@ -130,26 +46,26 @@ struct hb_buf_t : hb_vector_t { unsigned precision = 2; - void append_num (float v) - { hb_buf_append_num (this, v, precision); } - - void append_num (float v, unsigned p) - { hb_buf_append_num (this, v, p); } + bool append_len (const char *s, unsigned l) + { + unsigned old_len = length; + if (unlikely (!resize_dirty ((int) (old_len + l)))) + return false; + hb_memcpy (arrayZ + old_len, s, l); + return true; + } bool append_c (char ch) { return push_or_fail (ch); } bool append_str (const char *s) - { return hb_buf_append_len (this, s, (unsigned) strlen (s)); } - - bool append_len (const char *s, unsigned l) - { return hb_buf_append_len (this, s, l); } + { return append_len (s, (unsigned) strlen (s)); } bool append_unsigned (unsigned v) { char tmp[16]; snprintf (tmp, sizeof (tmp), "%u", v); - return hb_buf_append_len (this, tmp, (unsigned) strlen (tmp)); + return append_len (tmp, (unsigned) strlen (tmp)); } bool append_hex_byte (unsigned v) @@ -159,6 +75,57 @@ struct hb_buf_t : hb_vector_t return append_len (tmp, 2); } + void append_num (float v) + { append_num (v, precision); } + + void append_num (float v, unsigned p) + { + if (p > 12) p = 12; + + float rounded_zero_threshold = 0.5f; + for (unsigned i = 0; i < p; i++) + rounded_zero_threshold *= 0.1f; + if (fabsf (v) < rounded_zero_threshold) + v = 0.f; + + if (!(v == v) || !std::isfinite (v)) + { + append_c ('0'); + return; + } + + char fmt[6]; + snprintf (fmt, sizeof (fmt), "%%.%uf", p); + char out[128]; + snprintf (out, sizeof (out), fmt, (double) v); + + const char *decimal_point = hb_vector_decimal_point_get (); + + if (decimal_point[0] != '.' || decimal_point[1] != '\0') + { + char *dp = strstr (out, decimal_point); + if (dp) + { + unsigned dp_len = (unsigned) strlen (decimal_point); + unsigned tail_len = (unsigned) strlen (dp + dp_len); + memmove (dp + 1, dp + dp_len, tail_len + 1); + *dp = '.'; + } + } + + char *dot = strchr (out, '.'); + if (dot) + { + char *end = out + strlen (out) - 1; + while (end > dot && *end == '0') + *end-- = '\0'; + if (end == dot) + *end = '\0'; + } + + append_len (out, (unsigned) strlen (out)); + } + void append_svg_color (hb_color_t color, bool with_alpha) { unsigned r = hb_color_get_red (color); @@ -224,4 +191,4 @@ struct hb_buf_t : hb_vector_t } }; -#endif /* HB_VECTOR_UTILS_HH */ +#endif /* HB_VECTOR_BUF_HH */ diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index 405bfa5f2..db44e3b24 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -6,7 +6,7 @@ #include "hb-vector.h" #include "hb-geometry.hh" #include "hb-machinery.hh" -#include "hb-vector-utils.hh" +#include "hb-vector-buf.hh" #include "hb-vector-svg.hh" struct hb_vector_draw_t diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 26d1414e0..35ddf2e4f 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -8,7 +8,7 @@ #include "hb-machinery.hh" #include "hb-map.hh" #include "hb-vector-path.hh" -#include "hb-vector-utils.hh" +#include "hb-vector-buf.hh" #include "hb-vector-svg.hh" diff --git a/src/hb-vector-path.cc b/src/hb-vector-path.cc index 2e09e53fe..d24b4d0d5 100644 --- a/src/hb-vector-path.cc +++ b/src/hb-vector-path.cc @@ -29,7 +29,7 @@ #include "hb-geometry.hh" #include "hb-machinery.hh" #include "hb-vector-path.hh" -#include "hb-vector-utils.hh" +#include "hb-vector-buf.hh" #include "hb-vector-svg.hh" diff --git a/src/hb-vector-path.hh b/src/hb-vector-path.hh index b1588c81c..18fdcad64 100644 --- a/src/hb-vector-path.hh +++ b/src/hb-vector-path.hh @@ -31,7 +31,7 @@ #include "hb-vector.hh" #include "hb-vector.h" #include "hb-draw.h" -#include "hb-vector-utils.hh" +#include "hb-vector-buf.hh" /* Lightweight path sink: serializes hb_draw_* calls into an * external char buffer as either SVG path-data or PDF path diff --git a/src/hb-vector-svg.hh b/src/hb-vector-svg.hh index 0de96012a..09864b0a0 100644 --- a/src/hb-vector-svg.hh +++ b/src/hb-vector-svg.hh @@ -3,81 +3,6 @@ #include "hb-vector.h" -static inline bool -hb_buf_append_str (hb_buf_t *buf, const char *s) -{ - return buf->append_len (s, (unsigned) strlen (s)); -} - -static inline bool -hb_buf_append_unsigned (hb_buf_t *buf, unsigned v) -{ - char tmp[10]; - unsigned n = 0; - do { - tmp[n++] = (char) ('0' + (v % 10)); - v /= 10; - } while (v); - - unsigned old_len = buf->length; - if (unlikely (!buf->resize_dirty ((int) (old_len + n)))) - return false; - - for (unsigned i = 0; i < n; i++) - buf->arrayZ[old_len + i] = tmp[n - 1 - i]; - return true; -} - -static inline bool -hb_buf_append_hex_byte (hb_buf_t *buf, unsigned v) -{ - static const char hex[] = "0123456789ABCDEF"; - char tmp[2] = {hex[(v >> 4) & 15], hex[v & 15]}; - return buf->append_len (tmp, 2); -} - -static inline bool -hb_buf_append_base64 (hb_buf_t *buf, - const uint8_t *data, - unsigned len) -{ - static const char b64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - unsigned out_len = ((len + 2) / 3) * 4; - unsigned old_len = buf->length; - if (unlikely (!buf->resize_dirty ((int) (old_len + out_len)))) - return false; - - char *dst = buf->arrayZ + old_len; - unsigned di = 0; - unsigned i = 0; - while (i + 2 < len) - { - unsigned v = ((unsigned) data[i] << 16) | - ((unsigned) data[i + 1] << 8) | - ((unsigned) data[i + 2]); - dst[di++] = b64[(v >> 18) & 63]; - dst[di++] = b64[(v >> 12) & 63]; - dst[di++] = b64[(v >> 6) & 63]; - dst[di++] = b64[v & 63]; - i += 3; - } - - if (i < len) - { - unsigned v = (unsigned) data[i] << 16; - if (i + 1 < len) - v |= (unsigned) data[i + 1] << 8; - dst[di++] = b64[(v >> 18) & 63]; - dst[di++] = b64[(v >> 12) & 63]; - dst[di++] = (i + 1 < len) ? b64[(v >> 6) & 63] : '='; - dst[di++] = '='; - } - - return true; -} - struct hb_vector_blob_meta_t { char *data; @@ -213,38 +138,6 @@ hb_buf_recover_recycled (hb_blob_t *blob, meta->transferred = true; } -static inline void -hb_buf_append_color (hb_buf_t *buf, - hb_color_t color, - bool with_alpha) -{ - static const char hex[] = "0123456789ABCDEF"; - unsigned r = hb_color_get_red (color); - unsigned g = hb_color_get_green (color); - unsigned b = hb_color_get_blue (color); - unsigned a = hb_color_get_alpha (color); - buf->append_c ('#'); - if (((r >> 4) == (r & 0xF)) && - ((g >> 4) == (g & 0xF)) && - ((b >> 4) == (b & 0xF))) - { - buf->append_c (hex[r & 0xF]); - buf->append_c (hex[g & 0xF]); - buf->append_c (hex[b & 0xF]); - } - else - { - buf->append_hex_byte (r); - buf->append_hex_byte (g); - buf->append_hex_byte (b); - } - if (with_alpha && a != 255) - { - buf->append_str ("\" fill-opacity=\""); - buf->append_num (a / 255.f, 4); - } -} - static inline void hb_vector_transform_point (const hb_transform_t<> &t, float x_scale_factor, diff --git a/src/meson.build b/src/meson.build index 3dd588b72..214baa5cc 100644 --- a/src/meson.build +++ b/src/meson.build @@ -734,8 +734,8 @@ hb_vector_sources = files( 'hb-vector-paint-svg.cc', 'hb-vector-paint-pdf.cc', 'hb-vector-path.hh', - 'hb-vector-utils.cc', - 'hb-vector-utils.hh', + 'hb-vector-buf.cc', + 'hb-vector-buf.hh', 'hb-static.cc', )