[sdf] Add common elements for 'sdf' and 'bsdf' renderers.

* src/sdf/ftsdfrend.h (SDF_Rendere_Module, ft_sdf_renderer_class,
ft_bitmap_sdf_renderer_class): New structures.

* src/sdf/ftsdfcommon.h (DEFAULT_SPREAD, MIN_SPREAD_MAX_SPREAD,
USE_SQUARED_DISTANCES): New macros.
(FT_INT_26D6, FT_INT_16D16, FT_26D6_16D16): New macros.
(FT_CALL, VECTOR_LENGTH_16D16): New macros.
(FT_26D6_Vec, FT_16D16_Vec, FT_16D16, FT_26D6, FT_6D10, FT_CBox):
New typedefs.
(square_root): New macro.

* src/sdf/ftsdferrs.h: Add module error setup.
diff --git a/ChangeLog b/ChangeLog
index b77634f..ad0b9ab 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2020-08-17  Anuj Verma  <anujv@iitbhilai.ac.in>
+
+	[sdf] Add common elements for 'sdf' and 'bsdf' renderers.
+
+	* src/sdf/ftsdfrend.h (SDF_Rendere_Module, ft_sdf_renderer_class,
+	ft_bitmap_sdf_renderer_class): New structures.
+
+	* src/sdf/ftsdfcommon.h (DEFAULT_SPREAD, MIN_SPREAD_MAX_SPREAD,
+	USE_SQUARED_DISTANCES): New macros.
+	(FT_INT_26D6, FT_INT_16D16, FT_26D6_16D16): New macros.
+	(FT_CALL, VECTOR_LENGTH_16D16): New macros.
+	(FT_26D6_Vec, FT_16D16_Vec, FT_16D16, FT_26D6, FT_6D10, FT_CBox):
+	New typedefs.
+	(square_root): New macro.
+
+	* src/sdf/ftsdferrs.h: Add module error setup.
+
 2020-08-16  Anuj Verma  <anujv@iitbhilai.ac.in>
 
 	[sdf] Add files for new 'sdf' module.
diff --git a/src/sdf/ftsdfcommon.h b/src/sdf/ftsdfcommon.h
index b8284ac..22ec1d1 100644
--- a/src/sdf/ftsdfcommon.h
+++ b/src/sdf/ftsdfcommon.h
@@ -1,7 +1,150 @@
 
+  /****************************************************
+   *
+   * This file contains common functions and properties
+   * for both the 'sdf' and 'bsdf' renderers.
+   *
+   */
+
 #ifndef FTSDFCOMMON_H_
 #define FTSDFCOMMON_H_
 
+#include <ft2build.h>
+#include FT_CONFIG_CONFIG_H
+#include <freetype/freetype.h>
+
+
+FT_BEGIN_HEADER
+
+
+  /**************************************************************************
+   *
+   * default values (cannot be set individually for each renderer)
+   *
+   */
+
+  /* default spread value */
+#define DEFAULT_SPREAD  8
+  /* minimum spread supported by the renderer */
+#define MIN_SPREAD      2
+  /* maximum spread supported by the renderer */
+#define MAX_SPREAD      32
+
+
+  /**************************************************************************
+   *
+   * common definitions (cannot be set individually for each renderer)
+   *
+   */
+
+  /* If this macro is set to 1 the rasterizer uses squared distances for */
+  /* computation.  It can greatly improve the performance but there is a */
+  /* chance of overflow and artifacts.  You can safely use it up to a    */
+  /* pixel size of 128.                                                  */
+#ifndef USE_SQUARED_DISTANCES
+#define USE_SQUARED_DISTANCES  0
+#endif
+
+
+  /**************************************************************************
+   *
+   * common macros
+   *
+   */
+
+  /* convert int to 26.6 fixed-point   */
+#define FT_INT_26D6( x )   ( x * 64 )
+  /* convert int to 16.16 fixed-point  */
+#define FT_INT_16D16( x )  ( x * 65536 )
+  /* convert 26.6 to 16.16 fixed-point */
+#define FT_26D6_16D16( x ) ( x * 1024 )
+
+
+  /* Convenience macro to call a function; it  */
+  /* jumps to label `Exit` if an error occurs. */
+#define FT_CALL( x ) do                          \
+                     {                           \
+                       error = ( x );            \
+                       if ( error != FT_Err_Ok ) \
+                         goto Exit;              \
+                     } while ( 0 )
+
+
+  /*
+   * The macro `VECTOR_LENGTH_16D16` computes either squared distances or
+   * actual distances, depending on the value of `USE_SQUARED_DISTANCES`.
+   *
+   * By using squared distances the performance can be greatly improved but
+   * there is a risk of overflow.
+   */
+#if USE_SQUARED_DISTANCES
+#define VECTOR_LENGTH_16D16( v )  ( FT_MulFix( v.x, v.x ) + \
+                                    FT_MulFix( v.y, v.y ) )
+#else
+#define VECTOR_LENGTH_16D16( v )  FT_Vector_Length( &v )
+#endif
+
+
+  /**************************************************************************
+   *
+   * common typedefs
+   *
+   */
+
+  typedef FT_Vector FT_26D6_Vec;   /* with 26.6 fixed-point components  */
+  typedef FT_Vector FT_16D16_Vec;  /* with 16.16 fixed-point components */
+
+  typedef FT_Fixed  FT_16D16;      /* 16.16 fixed-point representation  */
+  typedef FT_Fixed  FT_26D6;       /* 26.6 fixed-point representation   */
+  typedef FT_Short  FT_6D10;       /* 6.10 fixed-point representation   */
+
+  typedef FT_BBox   FT_CBox;       /* control box of a curve            */
+
+
+  /**************************************************************************
+   *
+   * common functions
+   *
+   */
+
+  /*
+   * Original algorithm:
+   *
+   *   https://github.com/chmike/fpsqrt
+   *
+   * Use this to compute the square root of a 16.16 fixed point number.
+   */
+  static FT_16D16
+  square_root( FT_16D16  val )
+  {
+    FT_ULong  t, q, b, r;
+
+
+    r = val;
+    b = 0x40000000L;
+    q = 0;
+
+    while ( b > 0x40L )
+    {
+      t = q + b;
+
+      if ( r >= t )
+      {
+        r -= t;
+        q  = t + b;
+      }
+
+      r <<= 1;
+      b >>= 1;
+    }
+
+    q >>= 8;
+
+    return q;
+  }
+
+
+FT_END_HEADER
 
 #endif /* FTSDFCOMMON_H_ */
 
diff --git a/src/sdf/ftsdferrs.h b/src/sdf/ftsdferrs.h
index d102e7f..b9f412f 100644
--- a/src/sdf/ftsdferrs.h
+++ b/src/sdf/ftsdferrs.h
@@ -2,6 +2,15 @@
 #ifndef FTSDFERRS_H_
 #define FTSDFERRS_H_
 
+#include <freetype/ftmoderr.h>
+
+#undef FTERRORS_H_
+
+#undef  FT_ERR_PREFIX
+#define FT_ERR_PREFIX  Sdf_Err_
+#define FT_ERR_BASE    FT_Mod_Err_Sdf
+
+#include <freetype/fterrors.h>
 
 #endif /* FTSDFERRS_H_ */
 
diff --git a/src/sdf/ftsdfrend.h b/src/sdf/ftsdfrend.h
index 08cfa4c..00f0238 100644
--- a/src/sdf/ftsdfrend.h
+++ b/src/sdf/ftsdfrend.h
@@ -2,6 +2,95 @@
 #ifndef FTSDFREND_H_
 #define FTSDFREND_H_
 
+#include <freetype/ftrender.h>
+#include <freetype/ftmodapi.h>
+#include <freetype/internal/ftobjs.h>
+
+FT_BEGIN_HEADER
+
+
+  /**************************************************************************
+   *
+   * @struct:
+   *   SDF_Renderer_Module
+   *
+   * @description:
+   *   This struct extends the native renderer struct `FT_RendererRec`.  It
+   *   is basically used to store various parameters required by the
+   *   renderer and some additional parameters that can be used to tweak the
+   *   output of the renderer.
+   *
+   * @fields:
+   *   root ::
+   *     The native rendere struct.
+   *
+   *   spread ::
+   *     This is an essential parameter/property required by the renderer.
+   *     `spread` defines the maximum unsigned value that is present in the
+   *     final SDF output.  For the default value check file
+   *     `ftsdfcommon.h`.
+   *
+   *   flip_sign ::
+   *     By default positive values indicate positions inside of contours,
+   *     i.e., filled by a contour.  If this property is true then that
+   *     output will be the opposite of the default, i.e., negative values
+   *     indicate positions inside of contours.
+   *
+   *   flip_y ::
+   *     Setting this parameter to true makes the output image flipped
+   *     along the y-axis.
+   *
+   *   overlaps ::
+   *     Set this to true to generate SDF for glyphs having overlapping
+   *     contours.  The overlapping support is limited to glyphs that do not
+   *     have self-intersecting contours.  Also, removing overlaps require a
+   *     considerable amount of extra memory; additionally, it will not work
+   *     if generating SDF from bitmap.
+   *
+   * @note:
+   *   All properties except `overlaps` are valid for both the 'sdf' and
+   *   'bsdf' renderers.
+   */
+  typedef struct  SDF_Renderer_Module_
+  {
+    FT_RendererRec  root;
+    FT_UInt         spread;
+    FT_Bool         flip_sign;
+    FT_Bool         flip_y;
+    FT_Bool         overlaps;
+
+  } SDF_Renderer_Module, *SDF_Renderer;
+
+
+  /**************************************************************************
+   *
+   * @renderer:
+   *   ft_sdf_renderer_class
+   *
+   * @description:
+   *   Renderer to convert @FT_Outline to signed distance fields.
+   *
+   */
+  FT_DECLARE_RENDERER( ft_sdf_renderer_class )
+
+
+  /**************************************************************************
+   *
+   * @renderer:
+   *   ft_bitmap_sdf_renderer_class
+   *
+   * @description:
+   *   This is not exactly a renderer; it is just a converter that
+   *   transforms bitmaps to signed distance fields.
+   *
+   * @note:
+   *   This is not a separate module, it is part of the 'sdf' module.
+   *
+   */
+  FT_DECLARE_RENDERER( ft_bitmap_sdf_renderer_class )
+
+
+FT_END_HEADER
 
 #endif /* FTSDFREND_H_ */