From b398748d8bd61dbf61a3f8ec7e6aaf715bf57f65 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Wed, 4 Mar 2020 11:18:19 +0330 Subject: [PATCH] [algs] Add hb_clamp Similar to stl and glsl's clamp --- src/hb-algs.hh | 7 +++++++ src/hb-ot-var-fvar-table.hh | 2 +- src/hb-sanitize.hh | 6 +++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 8ae69af4b..681c447c5 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -360,6 +360,13 @@ struct (hb_forward (a) >= hb_forward (b) ? hb_forward (a) : hb_forward (b)) } HB_FUNCOBJ (hb_max); +struct +{ + template constexpr auto + operator () (T&& x, T2&& min, T3&& max) const HB_AUTO_RETURN + (hb_min (hb_max (hb_forward (x), hb_forward (min)), hb_forward (max))) +} +HB_FUNCOBJ (hb_clamp); /* diff --git a/src/hb-ot-var-fvar-table.hh b/src/hb-ot-var-fvar-table.hh index 7ce312381..9b8d169ed 100644 --- a/src/hb-ot-var-fvar-table.hh +++ b/src/hb-ot-var-fvar-table.hh @@ -229,7 +229,7 @@ struct fvar hb_ot_var_axis_info_t axis; get_axis_info (axis_index, &axis); - v = hb_max (hb_min (v, axis.max_value), axis.min_value); /* Clamp. */ + v = hb_clamp (v, axis.min_value, axis.max_value); if (v == axis.default_value) return 0; diff --git a/src/hb-sanitize.hh b/src/hb-sanitize.hh index 8f6453bf6..091b3010f 100644 --- a/src/hb-sanitize.hh +++ b/src/hb-sanitize.hh @@ -201,9 +201,9 @@ struct hb_sanitize_context_t : if (unlikely (hb_unsigned_mul_overflows (this->end - this->start, HB_SANITIZE_MAX_OPS_FACTOR))) this->max_ops = HB_SANITIZE_MAX_OPS_MAX; else - this->max_ops = hb_min (hb_max ((unsigned int) (this->end - this->start) * HB_SANITIZE_MAX_OPS_FACTOR, - (unsigned) HB_SANITIZE_MAX_OPS_MIN), - (unsigned) HB_SANITIZE_MAX_OPS_MAX); + this->max_ops = hb_clamp ((unsigned) (this->end - this->start) * HB_SANITIZE_MAX_OPS_FACTOR, + (unsigned) HB_SANITIZE_MAX_OPS_MIN, + (unsigned) HB_SANITIZE_MAX_OPS_MAX); this->edit_count = 0; this->debug_depth = 0;