[sdf] Formatting and improved comments.

This commit is contained in:
Werner Lemberg 2021-06-08 09:00:39 +02:00
parent 2b1d556269
commit 36ee71714f
4 changed files with 45 additions and 44 deletions

View File

@ -3,10 +3,10 @@
[sdf] Use 8 bits for final SDF output instead of 16bits. [sdf] Use 8 bits for final SDF output instead of 16bits.
Since 8-bits is enough to represent SDF data we no longer require Since 8-bits is enough to represent SDF data we no longer require
16-bits for this purpose. Also, we now normalize the output data 16-bits for this purpose. Also, we now normalize the output data
to use the entire 8-bit range efficiently. For example: if we use to use the entire 8-bit range efficiently. For example: if we use
3.5 format with a spread of 1 we basically only use the starting 3.5 format with a spread of 1 we basically only use the starting
5-bits. By normalizing we can use the entire 8-bit range. 5-bits. By normalizing we can use the entire 8-bit range.
* include/freetype/freetype.h (FT_Render_Mode): Updated description * include/freetype/freetype.h (FT_Render_Mode): Updated description
for `FT_RENDER_MODE_SDF` regarding this change. for `FT_RENDER_MODE_SDF` regarding this change.

View File

@ -3306,8 +3306,9 @@ FT_BEGIN_HEADER
* bitmaps. Each pixel in a SDF bitmap contains information about the * bitmaps. Each pixel in a SDF bitmap contains information about the
* nearest edge of the glyph outline. The distances are calculated * nearest edge of the glyph outline. The distances are calculated
* from the center of the pixel and are positive if they are filled by * from the center of the pixel and are positive if they are filled by
* the outline (i.e., inside the outline) and negative otherwise. Check * the outline (i.e., inside the outline) and negative otherwise.
* the note below on how to convert the output values to usable data. * Check the note below on how to convert the output values to usable
* data.
* *
* @note: * @note:
* The selected render mode only affects vector glyphs of a font. * The selected render mode only affects vector glyphs of a font.
@ -3315,30 +3316,31 @@ FT_BEGIN_HEADER
* @FT_PIXEL_MODE_MONO. You can use @FT_Bitmap_Convert to transform them * @FT_PIXEL_MODE_MONO. You can use @FT_Bitmap_Convert to transform them
* into 8-bit pixmaps. * into 8-bit pixmaps.
* *
* For @FT_RENDER_MODE_SDF output bitmap buffer contains normalized * For @FT_RENDER_MODE_SDF the output bitmap buffer contains normalized
* distance values that are packed into unsigned 8-bit buffer. To get * distances that are packed into unsigned 8-bit values. To get pixel
* pixel values in floating point representation use the following * values in floating point representation use the following pseudo-C
* conversion: * code for the conversion.
* *
* ``` * ```
* <load glyph and render using @FT_RENDER_MODE_SDF, then use the * // Load glyph and render using FT_RENDER_MODE_SDF,
* output buffer as follows> * // then use the output buffer as follows.
* *
* ... * ...
* FT_Byte buffer = glyph->bitmap->buffer; * FT_Byte buffer = glyph->bitmap->buffer;
*
* *
* for pixel in buffer * for pixel in buffer
* { * {
* <`sd` is the signed distance and spread is the current `spread`, * // `sd` is the signed distance and `spread` is the current spread;
* the default spread is 2 and can be changed> * // the default spread is 2 and can be changed.
* *
* float sd = (float)pixel - 128.0f; * float sd = (float)pixel - 128.0f;
* *
* <convert the to pixel values>
* *
* // Convert to pixel values.
* sd = ( sd / 128.0f ) * spread; * sd = ( sd / 128.0f ) * spread;
* *
* <store `sd` in a buffer or use as required> * // Store `sd` in a buffer or use as required.
* } * }
* *
* ``` * ```

View File

@ -1092,12 +1092,13 @@
finalize_sdf( BSDF_Worker* worker, finalize_sdf( BSDF_Worker* worker,
const FT_Bitmap* target ) const FT_Bitmap* target )
{ {
FT_Error error = FT_Err_Ok; FT_Error error = FT_Err_Ok;
FT_Int w, r; FT_Int w, r;
FT_Int i, j; FT_Int i, j;
FT_SDFFormat* t_buffer;
FT_16D16 spread; FT_SDFFormat* t_buffer;
FT_16D16 spread;
if ( !worker || !target ) if ( !worker || !target )
@ -1128,10 +1129,10 @@
{ {
for ( i = 0; i < w; i++ ) for ( i = 0; i < w; i++ )
{ {
FT_Int index; FT_Int index;
FT_16D16 dist; FT_16D16 dist;
FT_SDFFormat final_dist; FT_SDFFormat final_dist;
FT_Char sign; FT_Char sign;
index = j * w + i; index = j * w + i;

View File

@ -2898,7 +2898,7 @@
#if 0 #if 0
#error "DO NOT USE THIS!" #error "DO NOT USE THIS!"
#error "The function still output 16-bit data which might cause memory" #error "The function still outputs 16-bit data, which might cause memory"
#error "corruption. If required I will add this later." #error "corruption. If required I will add this later."
/************************************************************************** /**************************************************************************
@ -3196,8 +3196,8 @@
FT_Int width, rows, i, j; FT_Int width, rows, i, j;
FT_Int sp_sq; /* max value to check */ FT_Int sp_sq; /* max value to check */
SDF_Contour* contours; /* list of all contours */ SDF_Contour* contours; /* list of all contours */
FT_SDFFormat* buffer; /* the bitmap buffer */ FT_SDFFormat* buffer; /* the bitmap buffer */
/* This buffer has the same size in indices as the */ /* This buffer has the same size in indices as the */
/* bitmap buffer. When we check a pixel position for */ /* bitmap buffer. When we check a pixel position for */
@ -3206,7 +3206,7 @@
/* and also determine the signs properly. */ /* and also determine the signs properly. */
SDF_Signed_Distance* dists = NULL; SDF_Signed_Distance* dists = NULL;
const FT_16D16 fixed_spread = FT_INT_16D16( spread ); const FT_16D16 fixed_spread = FT_INT_16D16( spread );
if ( !shape || !bitmap ) if ( !shape || !bitmap )
@ -3362,14 +3362,12 @@
dists[index].distance = fixed_spread; dists[index].distance = fixed_spread;
/* flip sign if required */ /* flip sign if required */
dists[index].distance *= internal_params.flip_sign ? dists[index].distance *= internal_params.flip_sign ? -current_sign
-current_sign : : current_sign;
current_sign;
/* concatenate to appropriate format */ /* concatenate to appropriate format */
buffer[index] = map_fixed_to_sdf( buffer[index] = map_fixed_to_sdf( dists[index].distance,
dists[index].distance, fixed_spread );
fixed_spread );
} }
} }
@ -3506,9 +3504,9 @@
SDF_Contour* head; /* head of the contour list */ SDF_Contour* head; /* head of the contour list */
SDF_Shape temp_shape; /* temporary shape */ SDF_Shape temp_shape; /* temporary shape */
FT_Memory memory; /* to allocate memory */ FT_Memory memory; /* to allocate memory */
FT_SDFFormat* t; /* target bitmap buffer */ FT_SDFFormat* t; /* target bitmap buffer */
FT_Bool flip_sign; /* filp sign? */ FT_Bool flip_sign; /* flip sign? */
/* orientation of all the separate contours */ /* orientation of all the separate contours */
SDF_Contour_Orientation* orientations; SDF_Contour_Orientation* orientations;
@ -3644,18 +3642,18 @@
{ {
for ( i = 0; i < width; i++ ) for ( i = 0; i < width; i++ )
{ {
FT_Int id = j * width + i; /* index of current pixel */ FT_Int id = j * width + i; /* index of current pixel */
FT_Int c; /* contour iterator */ FT_Int c; /* contour iterator */
FT_SDFFormat val_c = 0; /* max clockwise value */ FT_SDFFormat val_c = 0; /* max clockwise value */
FT_SDFFormat val_ac = UCHAR_MAX; /* min counter-clockwise val */ FT_SDFFormat val_ac = UCHAR_MAX; /* min counter-clockwise val */
/* iterate through all the contours */ /* iterate through all the contours */
for ( c = 0; c < num_contours; c++ ) for ( c = 0; c < num_contours; c++ )
{ {
/* current contour value */ /* current contour value */
FT_SDFFormat temp = ( (FT_SDFFormat*)bitmaps[c].buffer )[id]; FT_SDFFormat temp = ( (FT_SDFFormat*)bitmaps[c].buffer )[id];
if ( orientations[c] == SDF_ORIENTATION_CW ) if ( orientations[c] == SDF_ORIENTATION_CW )