Imported Upstream version 2.13.2
[platform/upstream/freetype2.git] / src / sdf / ftsdfcommon.h
1 /****************************************************************************
2  *
3  * ftsdfcommon.h
4  *
5  *   Auxiliary data for Signed Distance Field support (specification).
6  *
7  * Copyright (C) 2020-2023 by
8  * David Turner, Robert Wilhelm, and Werner Lemberg.
9  *
10  * Written by Anuj Verma.
11  *
12  * This file is part of the FreeType project, and may only be used,
13  * modified, and distributed under the terms of the FreeType project
14  * license, LICENSE.TXT.  By continuing to use, modify, or distribute
15  * this file you indicate that you have read the license and
16  * understand and accept it fully.
17  *
18  */
19
20
21   /****************************************************
22    *
23    * This file contains common functions and properties
24    * for both the 'sdf' and 'bsdf' renderers.
25    *
26    */
27
28 #ifndef FTSDFCOMMON_H_
29 #define FTSDFCOMMON_H_
30
31 #include <ft2build.h>
32 #include FT_CONFIG_CONFIG_H
33 #include <freetype/internal/ftobjs.h>
34
35
36 FT_BEGIN_HEADER
37
38
39   /**************************************************************************
40    *
41    * default values (cannot be set individually for each renderer)
42    *
43    */
44
45   /* default spread value */
46 #define DEFAULT_SPREAD  8
47   /* minimum spread supported by the renderer */
48 #define MIN_SPREAD      2
49   /* maximum spread supported by the renderer */
50 #define MAX_SPREAD      32
51   /* pixel size in 26.6 */
52 #define ONE_PIXEL       ( 1 << 6 )
53
54
55   /**************************************************************************
56    *
57    * common definitions (cannot be set individually for each renderer)
58    *
59    */
60
61   /* If this macro is set to 1 the rasterizer uses squared distances for */
62   /* computation.  It can greatly improve the performance but there is a */
63   /* chance of overflow and artifacts.  You can safely use it up to a    */
64   /* pixel size of 128.                                                  */
65 #ifndef USE_SQUARED_DISTANCES
66 #define USE_SQUARED_DISTANCES  0
67 #endif
68
69
70   /**************************************************************************
71    *
72    * common macros
73    *
74    */
75
76   /* convert int to 26.6 fixed-point   */
77 #define FT_INT_26D6( x )   ( x * 64 )
78   /* convert int to 16.16 fixed-point  */
79 #define FT_INT_16D16( x )  ( x * 65536 )
80   /* convert 26.6 to 16.16 fixed-point */
81 #define FT_26D6_16D16( x ) ( x * 1024 )
82
83
84   /* Convenience macro to call a function; it  */
85   /* jumps to label `Exit` if an error occurs. */
86 #define FT_CALL( x ) do                          \
87                      {                           \
88                        error = ( x );            \
89                        if ( error != FT_Err_Ok ) \
90                          goto Exit;              \
91                      } while ( 0 )
92
93
94   /*
95    * The macro `VECTOR_LENGTH_16D16` computes either squared distances or
96    * actual distances, depending on the value of `USE_SQUARED_DISTANCES`.
97    *
98    * By using squared distances the performance can be greatly improved but
99    * there is a risk of overflow.
100    */
101 #if USE_SQUARED_DISTANCES
102 #define VECTOR_LENGTH_16D16( v )  ( FT_MulFix( v.x, v.x ) + \
103                                     FT_MulFix( v.y, v.y ) )
104 #else
105 #define VECTOR_LENGTH_16D16( v )  FT_Vector_Length( &v )
106 #endif
107
108
109   /**************************************************************************
110    *
111    * common typedefs
112    *
113    */
114
115   typedef FT_Vector FT_26D6_Vec;   /* with 26.6 fixed-point components  */
116   typedef FT_Vector FT_16D16_Vec;  /* with 16.16 fixed-point components */
117
118   typedef FT_Int32  FT_16D16;      /* 16.16 fixed-point representation  */
119   typedef FT_Int32  FT_26D6;       /* 26.6 fixed-point representation   */
120   typedef FT_Byte   FT_SDFFormat;  /* format to represent SDF data      */
121
122   typedef FT_BBox   FT_CBox;       /* control box of a curve            */
123
124
125   FT_LOCAL( FT_16D16 )
126   square_root( FT_16D16  val );
127
128   FT_LOCAL( FT_SDFFormat )
129   map_fixed_to_sdf( FT_16D16  dist,
130                     FT_16D16  max_value );
131
132   FT_LOCAL( FT_SDFFormat )
133   invert_sign( FT_SDFFormat  dist );
134
135
136 FT_END_HEADER
137
138 #endif /* FTSDFCOMMON_H_ */
139
140
141 /* END */