Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <campbell@blender.org>2022-04-05 13:10:55 +0300
committerCampbell Barton <campbell@blender.org>2022-04-13 04:58:42 +0300
commitae43872ad572eb3e6ad1ebfd02921fc2403059bc (patch)
treea223c292c2bc07b8f5b980b25f434e9fcc1b7163 /source/blender/blenfont/intern/blf_internal_types.h
parentd0a70adf8f2e8977fb27f8987488bded33e82106 (diff)
BLF: sub-pixel positioning support
Support sub-pixel kerning and hinting for future support for improved character placement. No user visible changes have been made. - Calculate sub-pixel offsets, using integer maths. - Use convenience functions to perform the conversions and hide the underlying values. - Use `ft_pix` type to distinguish values that use sub-pixel integer values from freetype and values rounded to pixels. This was originally based on D12999 by @harley with the user visible changes removed so they can be applied separately.
Diffstat (limited to 'source/blender/blenfont/intern/blf_internal_types.h')
-rw-r--r--source/blender/blenfont/intern/blf_internal_types.h87
1 files changed, 82 insertions, 5 deletions
diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h
index 23dc2fda38c..34fa82629b4 100644
--- a/source/blender/blenfont/intern/blf_internal_types.h
+++ b/source/blender/blenfont/intern/blf_internal_types.h
@@ -10,6 +10,79 @@
#include "GPU_texture.h"
#include "GPU_vertex_buffer.h"
+/* -------------------------------------------------------------------- */
+/** \name Sub-Pixel Offset & Utilities
+ *
+ * Free-type uses fixed point precision for sub-pixel offsets.
+ * Utility functions here avoid exposing the details in the BLF API.
+ * \{ */
+
+/**
+ * This is an internal type that represents sub-pixel positioning,
+ * users of this type are to use `ft_pix_*` functions to keep scaling/rounding in one place.
+ */
+typedef int32_t ft_pix;
+
+/* Macros copied from `include/freetype/internal/ftobjs.h`. */
+
+/* FIXME(@campbellbarton): Follow rounding from Blender 3.1x and older.
+ * This is what users will expect and changing this creates wider spaced text.
+ * Use this macro to communicate that rounding should be used, using floor is to avoid
+ * user visible changes, which can be reviewed and handled separately. */
+#define USE_LEGACY_SPACING
+
+#define FT_PIX_FLOOR(x) ((x) & ~63)
+#define FT_PIX_ROUND(x) FT_PIX_FLOOR((x) + 32)
+#define FT_PIX_CEIL(x) ((x) + 63)
+
+#ifdef USE_LEGACY_SPACING
+# define FT_PIX_DEFAULT_ROUNDING(x) FT_PIX_FLOOR(x)
+#else
+# define FT_PIX_DEFAULT_ROUNDING(x) FT_PIX_ROUND(x)
+#endif
+
+BLI_INLINE int ft_pix_to_int(ft_pix v)
+{
+#ifdef USE_LEGACY_SPACING
+ return (int)(v >> 6);
+#else
+ return (int)(FT_PIX_DEFAULT_ROUNDING(v) >> 6);
+#endif
+}
+
+BLI_INLINE int ft_pix_to_int_floor(ft_pix v)
+{
+ return (int)(v >> 6); /* No need for explicit floor as the bits are removed when shifting. */
+}
+
+BLI_INLINE int ft_pix_to_int_ceil(ft_pix v)
+{
+ return (int)(FT_PIX_CEIL(v) >> 6);
+}
+
+BLI_INLINE ft_pix ft_pix_from_int(int v)
+{
+ return v * 64;
+}
+
+BLI_INLINE ft_pix ft_pix_from_float(float v)
+{
+ return lroundf(v * 64.0f);
+}
+
+BLI_INLINE ft_pix ft_pix_round_advance(ft_pix v, ft_pix step)
+{
+ /* See #USE_LEGACY_SPACING, rounding logic could change here. */
+ return FT_PIX_DEFAULT_ROUNDING(v) + FT_PIX_DEFAULT_ROUNDING(step);
+}
+
+#undef FT_PIX_FLOOR
+#undef FT_PIX_ROUND
+#undef FT_PIX_CEIL
+#undef FT_PIX_DEFAULT_ROUNDING
+
+/** \} */
+
#define BLF_BATCH_DRAW_LEN_MAX 2048 /* in glyph */
/* Number of characters in GlyphCacheBLF.glyph_ascii_table. */
@@ -86,12 +159,16 @@ typedef struct GlyphBLF {
FT_UInt idx;
/* glyph box. */
- rctf box;
+ ft_pix box_xmin;
+ ft_pix box_xmax;
+ ft_pix box_ymin;
+ ft_pix box_ymax;
+
+ ft_pix advance_x;
- /* advance size. */
- float advance;
- /* avoid conversion to int while drawing */
- int advance_i;
+ /* The difference in bearings when hinting is active, zero otherwise. */
+ ft_pix lsb_delta;
+ ft_pix rsb_delta;
/* position inside the texture where this glyph is store. */
int offset;