mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-26 09:21:06 +01:00
[vector] Share SVG path sink funcs and move number formatting to utils
This commit is contained in:
1 parent
5870854182
commit
591be6472a
6 files changed
+277
-402
No files matched your search
+1
-200
@@ -31,6 +31,7 @@
|
||||
#include "hb-geometry.hh"
|
||||
#include "hb-machinery.hh"
|
||||
#include "hb-map.hh"
|
||||
#include "hb-vector-svg-path.hh"
|
||||
#include "hb-vector-svg-subset.hh"
|
||||
#include "hb-vector-svg-utils.hh"
|
||||
|
||||
@@ -114,88 +115,6 @@ hb_svg_append_base64 (hb_vector_t<char> *buf,
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_append_num (hb_vector_t<char> *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) || !isfinite (v))
|
||||
{
|
||||
hb_svg_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 = ".";
|
||||
#ifndef HB_NO_SETLOCALE
|
||||
#if defined(HAVE_XLOCALE_H)
|
||||
lconv *lc = nullptr;
|
||||
hb_locale_t current_locale = hb_uselocale ((hb_locale_t) 0);
|
||||
if (current_locale)
|
||||
lc = localeconv_l (current_locale);
|
||||
if (lc && lc->decimal_point && lc->decimal_point[0])
|
||||
decimal_point = lc->decimal_point;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
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_svg_append_str (buf, out);
|
||||
}
|
||||
|
||||
static inline unsigned
|
||||
hb_svg_scale_precision (unsigned precision)
|
||||
{
|
||||
return precision < 7 ? 7 : precision;
|
||||
}
|
||||
|
||||
HB_UNUSED static inline bool
|
||||
hb_svg_buffer_contains (const hb_vector_t<char> &buf, const char *needle)
|
||||
{
|
||||
@@ -582,96 +501,7 @@ hb_vector_draw_close_path (hb_draw_funcs_t *,
|
||||
hb_svg_append_c (&d->path, 'Z');
|
||||
}
|
||||
|
||||
|
||||
struct hb_svg_path_sink_t
|
||||
{
|
||||
hb_vector_t<char> *path;
|
||||
unsigned precision;
|
||||
};
|
||||
|
||||
static void
|
||||
hb_svg_path_move_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'M');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_line_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'L');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_quadratic_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float cx, float cy,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'Q');
|
||||
hb_svg_append_num (s->path, cx, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, cy, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_cubic_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float c1x, float c1y,
|
||||
float c2x, float c2y,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'C');
|
||||
hb_svg_append_num (s->path, c1x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, c1y, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, c2x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, c2y, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_close_path (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'Z');
|
||||
}
|
||||
|
||||
static inline void free_static_vector_draw_funcs ();
|
||||
static inline void free_static_svg_path_draw_funcs ();
|
||||
|
||||
static struct hb_vector_draw_funcs_lazy_loader_t
|
||||
: hb_draw_funcs_lazy_loader_t<hb_vector_draw_funcs_lazy_loader_t>
|
||||
@@ -690,47 +520,18 @@ static struct hb_vector_draw_funcs_lazy_loader_t
|
||||
}
|
||||
} static_vector_draw_funcs;
|
||||
|
||||
static struct hb_svg_path_draw_funcs_lazy_loader_t
|
||||
: hb_draw_funcs_lazy_loader_t<hb_svg_path_draw_funcs_lazy_loader_t>
|
||||
{
|
||||
static hb_draw_funcs_t *create ()
|
||||
{
|
||||
hb_draw_funcs_t *funcs = hb_draw_funcs_create ();
|
||||
hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) hb_svg_path_move_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) hb_svg_path_line_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) hb_svg_path_quadratic_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) hb_svg_path_cubic_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) hb_svg_path_close_path, nullptr, nullptr);
|
||||
hb_draw_funcs_make_immutable (funcs);
|
||||
hb_atexit (free_static_svg_path_draw_funcs);
|
||||
return funcs;
|
||||
}
|
||||
} static_svg_path_draw_funcs;
|
||||
|
||||
static inline void
|
||||
free_static_vector_draw_funcs ()
|
||||
{
|
||||
static_vector_draw_funcs.free_instance ();
|
||||
}
|
||||
|
||||
static inline void
|
||||
free_static_svg_path_draw_funcs ()
|
||||
{
|
||||
static_svg_path_draw_funcs.free_instance ();
|
||||
}
|
||||
|
||||
static hb_draw_funcs_t *
|
||||
hb_vector_draw_funcs_get ()
|
||||
{
|
||||
return static_vector_draw_funcs.get_unconst ();
|
||||
}
|
||||
|
||||
static hb_draw_funcs_t *
|
||||
hb_svg_path_draw_funcs_get ()
|
||||
{
|
||||
return static_svg_path_draw_funcs.get_unconst ();
|
||||
}
|
||||
|
||||
hb_vector_draw_t *
|
||||
hb_vector_draw_create_or_fail (hb_vector_format_t format)
|
||||
{
|
||||
|
||||
+1
-202
@@ -31,6 +31,7 @@
|
||||
#include "hb-geometry.hh"
|
||||
#include "hb-machinery.hh"
|
||||
#include "hb-map.hh"
|
||||
#include "hb-vector-svg-path.hh"
|
||||
#include "hb-vector-svg-subset.hh"
|
||||
#include "hb-vector-svg-utils.hh"
|
||||
|
||||
@@ -114,88 +115,6 @@ hb_svg_append_base64 (hb_vector_t<char> *buf,
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_append_num (hb_vector_t<char> *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) || !isfinite (v))
|
||||
{
|
||||
hb_svg_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 = ".";
|
||||
#ifndef HB_NO_SETLOCALE
|
||||
#if defined(HAVE_XLOCALE_H)
|
||||
lconv *lc = nullptr;
|
||||
hb_locale_t current_locale = hb_uselocale ((hb_locale_t) 0);
|
||||
if (current_locale)
|
||||
lc = localeconv_l (current_locale);
|
||||
if (lc && lc->decimal_point && lc->decimal_point[0])
|
||||
decimal_point = lc->decimal_point;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
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_svg_append_str (buf, out);
|
||||
}
|
||||
|
||||
static inline unsigned
|
||||
hb_svg_scale_precision (unsigned precision)
|
||||
{
|
||||
return precision < 7 ? 7 : precision;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
hb_svg_buffer_contains (const hb_vector_t<char> &buf, const char *needle)
|
||||
{
|
||||
@@ -613,126 +532,6 @@ hb_svg_append_image_instance_translate (hb_vector_t<char> *out,
|
||||
hb_svg_append_c (out, ')');
|
||||
}
|
||||
|
||||
|
||||
struct hb_svg_path_sink_t
|
||||
{
|
||||
hb_vector_t<char> *path;
|
||||
unsigned precision;
|
||||
};
|
||||
|
||||
static void
|
||||
hb_svg_path_move_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'M');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_line_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'L');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_quadratic_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float cx, float cy,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'Q');
|
||||
hb_svg_append_num (s->path, cx, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, cy, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_cubic_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float c1x, float c1y,
|
||||
float c2x, float c2y,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'C');
|
||||
hb_svg_append_num (s->path, c1x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, c1y, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, c2x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, c2y, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_close_path (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'Z');
|
||||
}
|
||||
|
||||
static inline void free_static_svg_path_draw_funcs ();
|
||||
|
||||
static struct hb_svg_path_draw_funcs_lazy_loader_t
|
||||
: hb_draw_funcs_lazy_loader_t<hb_svg_path_draw_funcs_lazy_loader_t>
|
||||
{
|
||||
static hb_draw_funcs_t *create ()
|
||||
{
|
||||
hb_draw_funcs_t *funcs = hb_draw_funcs_create ();
|
||||
hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) hb_svg_path_move_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) hb_svg_path_line_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) hb_svg_path_quadratic_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) hb_svg_path_cubic_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) hb_svg_path_close_path, nullptr, nullptr);
|
||||
hb_draw_funcs_make_immutable (funcs);
|
||||
hb_atexit (free_static_svg_path_draw_funcs);
|
||||
return funcs;
|
||||
}
|
||||
} static_svg_path_draw_funcs;
|
||||
|
||||
static inline void
|
||||
free_static_svg_path_draw_funcs ()
|
||||
{
|
||||
static_svg_path_draw_funcs.free_instance ();
|
||||
}
|
||||
|
||||
static hb_draw_funcs_t *
|
||||
hb_svg_path_draw_funcs_get ()
|
||||
{
|
||||
return static_svg_path_draw_funcs.get_unconst ();
|
||||
}
|
||||
|
||||
|
||||
static hb_bool_t
|
||||
hb_svg_get_color_stops (hb_vector_paint_t *paint,
|
||||
hb_color_line_t *color_line,
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright © 2026 Behdad Esfahbod
|
||||
*
|
||||
* This is part of HarfBuzz, a text shaping library.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and without
|
||||
* license or royalty fees, to use, copy, modify, and distribute this
|
||||
* software and its documentation for any purpose, provided that the
|
||||
* above copyright notice and the following two paragraphs appear in
|
||||
* all copies of this software.
|
||||
*
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
||||
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
||||
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
||||
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
* DAMAGE.
|
||||
*
|
||||
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
||||
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
||||
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*
|
||||
* Author(s): Behdad Esfahbod
|
||||
*/
|
||||
|
||||
#include "hb.hh"
|
||||
|
||||
#include "hb-machinery.hh"
|
||||
#include "hb-vector-svg-path.hh"
|
||||
#include "hb-vector-svg-utils.hh"
|
||||
|
||||
static void
|
||||
hb_svg_path_move_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'M');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_line_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'L');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_quadratic_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float cx, float cy,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'Q');
|
||||
hb_svg_append_num (s->path, cx, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, cy, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_cubic_to (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
float c1x, float c1y,
|
||||
float c2x, float c2y,
|
||||
float to_x, float to_y,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'C');
|
||||
hb_svg_append_num (s->path, c1x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, c1y, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, c2x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, c2y, s->precision);
|
||||
hb_svg_append_c (s->path, ' ');
|
||||
hb_svg_append_num (s->path, to_x, s->precision);
|
||||
hb_svg_append_c (s->path, ',');
|
||||
hb_svg_append_num (s->path, to_y, s->precision);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_path_close_path (hb_draw_funcs_t *,
|
||||
void *draw_data,
|
||||
hb_draw_state_t *,
|
||||
void *)
|
||||
{
|
||||
auto *s = (hb_svg_path_sink_t *) draw_data;
|
||||
hb_svg_append_c (s->path, 'Z');
|
||||
}
|
||||
|
||||
static inline void
|
||||
free_static_svg_path_draw_funcs ();
|
||||
|
||||
static struct hb_svg_path_draw_funcs_lazy_loader_t
|
||||
: hb_draw_funcs_lazy_loader_t<hb_svg_path_draw_funcs_lazy_loader_t>
|
||||
{
|
||||
static hb_draw_funcs_t *create ()
|
||||
{
|
||||
hb_draw_funcs_t *funcs = hb_draw_funcs_create ();
|
||||
hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) hb_svg_path_move_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) hb_svg_path_line_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) hb_svg_path_quadratic_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) hb_svg_path_cubic_to, nullptr, nullptr);
|
||||
hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) hb_svg_path_close_path, nullptr, nullptr);
|
||||
hb_draw_funcs_make_immutable (funcs);
|
||||
hb_atexit (free_static_svg_path_draw_funcs);
|
||||
return funcs;
|
||||
}
|
||||
} static_svg_path_draw_funcs;
|
||||
|
||||
static inline void
|
||||
free_static_svg_path_draw_funcs ()
|
||||
{
|
||||
static_svg_path_draw_funcs.free_instance ();
|
||||
}
|
||||
|
||||
hb_draw_funcs_t *
|
||||
hb_svg_path_draw_funcs_get (void)
|
||||
{
|
||||
return static_svg_path_draw_funcs.get_unconst ();
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright © 2026 Behdad Esfahbod
|
||||
*
|
||||
* This is part of HarfBuzz, a text shaping library.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and without
|
||||
* license or royalty fees, to use, copy, modify, and distribute this
|
||||
* software and its documentation for any purpose, provided that the
|
||||
* above copyright notice and the following two paragraphs appear in
|
||||
* all copies of this software.
|
||||
*
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
||||
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
||||
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
||||
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
* DAMAGE.
|
||||
*
|
||||
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
||||
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
||||
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*
|
||||
* Author(s): Behdad Esfahbod
|
||||
*/
|
||||
|
||||
#ifndef HB_VECTOR_SVG_PATH_HH
|
||||
#define HB_VECTOR_SVG_PATH_HH
|
||||
|
||||
#include "hb.hh"
|
||||
#include "hb-vector.hh"
|
||||
#include "hb-draw.h"
|
||||
|
||||
struct hb_svg_path_sink_t
|
||||
{
|
||||
hb_vector_t<char> *path;
|
||||
unsigned precision;
|
||||
};
|
||||
|
||||
HB_INTERNAL hb_draw_funcs_t *
|
||||
hb_svg_path_draw_funcs_get (void);
|
||||
|
||||
#endif /* HB_VECTOR_SVG_PATH_HH */
|
||||
@@ -29,6 +29,9 @@
|
||||
|
||||
#include "hb.hh"
|
||||
#include "hb-vector.hh"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static inline bool
|
||||
hb_svg_append_len (hb_vector_t<char> *buf,
|
||||
@@ -48,4 +51,86 @@ hb_svg_append_c (hb_vector_t<char> *buf, char c)
|
||||
return !!buf->push (c);
|
||||
}
|
||||
|
||||
static inline void
|
||||
hb_svg_append_num (hb_vector_t<char> *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) || !isfinite (v))
|
||||
{
|
||||
hb_svg_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 = ".";
|
||||
#ifndef HB_NO_SETLOCALE
|
||||
#if defined(HAVE_XLOCALE_H)
|
||||
lconv *lc = nullptr;
|
||||
hb_locale_t current_locale = hb_uselocale ((hb_locale_t) 0);
|
||||
if (current_locale)
|
||||
lc = localeconv_l (current_locale);
|
||||
if (lc && lc->decimal_point && lc->decimal_point[0])
|
||||
decimal_point = lc->decimal_point;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
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_svg_append_len (buf, out, (unsigned) strlen (out));
|
||||
}
|
||||
|
||||
static inline unsigned
|
||||
hb_svg_scale_precision (unsigned precision)
|
||||
{
|
||||
return precision < 7 ? 7 : precision;
|
||||
}
|
||||
|
||||
#endif /* HB_VECTOR_SVG_UTILS_HH */
|
||||
@@ -716,9 +716,12 @@ hb_raster_headers = [
|
||||
hb_vector_sources = [
|
||||
'hb-vector.cc',
|
||||
'hb-vector-svg-draw.cc',
|
||||
'hb-vector-svg-path.cc',
|
||||
'hb-vector-svg-paint.cc',
|
||||
'hb-vector-svg-path.hh',
|
||||
'hb-vector-svg-subset.cc',
|
||||
'hb-vector-svg-subset.hh',
|
||||
'hb-vector-svg-utils.hh',
|
||||
'hb-static.cc',
|
||||
]
|
||||
|
||||
|
||||
Reference in new issue
Block a user