• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  *
3  * ftsdfcommon.h
4  *
5  *   Auxiliary data for Signed Distance Field support (specification).
6  *
7  * Copyright (C) 2020-2021 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 
52 
53   /**************************************************************************
54    *
55    * common definitions (cannot be set individually for each renderer)
56    *
57    */
58 
59   /* If this macro is set to 1 the rasterizer uses squared distances for */
60   /* computation.  It can greatly improve the performance but there is a */
61   /* chance of overflow and artifacts.  You can safely use it up to a    */
62   /* pixel size of 128.                                                  */
63 #ifndef USE_SQUARED_DISTANCES
64 #define USE_SQUARED_DISTANCES  0
65 #endif
66 
67 
68   /**************************************************************************
69    *
70    * common macros
71    *
72    */
73 
74   /* convert int to 26.6 fixed-point   */
75 #define FT_INT_26D6( x )   ( x * 64 )
76   /* convert int to 16.16 fixed-point  */
77 #define FT_INT_16D16( x )  ( x * 65536 )
78   /* convert 26.6 to 16.16 fixed-point */
79 #define FT_26D6_16D16( x ) ( x * 1024 )
80 
81 
82   /* Convenience macro to call a function; it  */
83   /* jumps to label `Exit` if an error occurs. */
84 #define FT_CALL( x ) do                          \
85                      {                           \
86                        error = ( x );            \
87                        if ( error != FT_Err_Ok ) \
88                          goto Exit;              \
89                      } while ( 0 )
90 
91 
92   /*
93    * The macro `VECTOR_LENGTH_16D16` computes either squared distances or
94    * actual distances, depending on the value of `USE_SQUARED_DISTANCES`.
95    *
96    * By using squared distances the performance can be greatly improved but
97    * there is a risk of overflow.
98    */
99 #if USE_SQUARED_DISTANCES
100 #define VECTOR_LENGTH_16D16( v )  ( FT_MulFix( v.x, v.x ) + \
101                                     FT_MulFix( v.y, v.y ) )
102 #else
103 #define VECTOR_LENGTH_16D16( v )  FT_Vector_Length( &v )
104 #endif
105 
106 
107   /**************************************************************************
108    *
109    * common typedefs
110    *
111    */
112 
113   typedef FT_Vector FT_26D6_Vec;   /* with 26.6 fixed-point components  */
114   typedef FT_Vector FT_16D16_Vec;  /* with 16.16 fixed-point components */
115 
116   typedef FT_Fixed  FT_16D16;      /* 16.16 fixed-point representation  */
117   typedef FT_Fixed  FT_26D6;       /* 26.6 fixed-point representation   */
118   typedef FT_Byte   FT_SDFFormat;  /* format to represent SDF data      */
119 
120   typedef FT_BBox   FT_CBox;       /* control box of a curve            */
121 
122 
123   FT_LOCAL( FT_16D16 )
124   square_root( FT_16D16  val );
125 
126   FT_LOCAL( FT_SDFFormat )
127   map_fixed_to_sdf( FT_16D16  dist,
128                     FT_16D16  max_value );
129 
130   FT_LOCAL( FT_SDFFormat )
131   invert_sign( FT_SDFFormat  dist );
132 
133 
134 FT_END_HEADER
135 
136 #endif /* FTSDFCOMMON_H_ */
137 
138 
139 /* END */
140