From 36ee71714f5f84876cd3e9b479dbbd72a5124412 Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Tue, 8 Jun 2021 09:00:39 +0200 Subject: [PATCH] [sdf] Formatting and improved comments. --- ChangeLog | 6 +++--- include/freetype/freetype.h | 30 ++++++++++++++++-------------- src/sdf/ftbsdf.c | 19 ++++++++++--------- src/sdf/ftsdf.c | 34 ++++++++++++++++------------------ 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index 09f468fa0..84d0088b9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,10 +3,10 @@ [sdf] Use 8 bits for final SDF output instead of 16bits. 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 - to use the entire 8-bit range efficiently. For example: if we use + 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 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 for `FT_RENDER_MODE_SDF` regarding this change. diff --git a/include/freetype/freetype.h b/include/freetype/freetype.h index ebb129f71..a66d45a27 100644 --- a/include/freetype/freetype.h +++ b/include/freetype/freetype.h @@ -3306,8 +3306,9 @@ FT_BEGIN_HEADER * bitmaps. Each pixel in a SDF bitmap contains information about the * nearest edge of the glyph outline. The distances are calculated * 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 note below on how to convert the output values to usable data. + * the outline (i.e., inside the outline) and negative otherwise. + * Check the note below on how to convert the output values to usable + * data. * * @note: * 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 * into 8-bit pixmaps. * - * For @FT_RENDER_MODE_SDF output bitmap buffer contains normalized - * distance values that are packed into unsigned 8-bit buffer. To get - * pixel values in floating point representation use the following - * conversion: + * For @FT_RENDER_MODE_SDF the output bitmap buffer contains normalized + * distances that are packed into unsigned 8-bit values. To get pixel + * values in floating point representation use the following pseudo-C + * code for the conversion. * * ``` - * + * // Load glyph and render using FT_RENDER_MODE_SDF, + * // then use the output buffer as follows. * * ... - * FT_Byte buffer = glyph->bitmap->buffer; + * FT_Byte buffer = glyph->bitmap->buffer; + * * * for pixel in buffer * { - * <`sd` is the signed distance and spread is the current `spread`, - * the default spread is 2 and can be changed> + * // `sd` is the signed distance and `spread` is the current spread; + * // the default spread is 2 and can be changed. * - * float sd = (float)pixel - 128.0f; + * float sd = (float)pixel - 128.0f; * - * * + * // Convert to pixel values. * sd = ( sd / 128.0f ) * spread; * - * + * // Store `sd` in a buffer or use as required. * } * * ``` diff --git a/src/sdf/ftbsdf.c b/src/sdf/ftbsdf.c index 659e0de89..db4a2dd11 100644 --- a/src/sdf/ftbsdf.c +++ b/src/sdf/ftbsdf.c @@ -1092,12 +1092,13 @@ finalize_sdf( BSDF_Worker* worker, const FT_Bitmap* target ) { - FT_Error error = FT_Err_Ok; + FT_Error error = FT_Err_Ok; - FT_Int w, r; - FT_Int i, j; - FT_SDFFormat* t_buffer; - FT_16D16 spread; + FT_Int w, r; + FT_Int i, j; + + FT_SDFFormat* t_buffer; + FT_16D16 spread; if ( !worker || !target ) @@ -1128,10 +1129,10 @@ { for ( i = 0; i < w; i++ ) { - FT_Int index; - FT_16D16 dist; - FT_SDFFormat final_dist; - FT_Char sign; + FT_Int index; + FT_16D16 dist; + FT_SDFFormat final_dist; + FT_Char sign; index = j * w + i; diff --git a/src/sdf/ftsdf.c b/src/sdf/ftsdf.c index 335b800e5..a2f38be53 100644 --- a/src/sdf/ftsdf.c +++ b/src/sdf/ftsdf.c @@ -2898,7 +2898,7 @@ #if 0 #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." /************************************************************************** @@ -3196,8 +3196,8 @@ FT_Int width, rows, i, j; FT_Int sp_sq; /* max value to check */ - SDF_Contour* contours; /* list of all contours */ - FT_SDFFormat* buffer; /* the bitmap buffer */ + SDF_Contour* contours; /* list of all contours */ + FT_SDFFormat* buffer; /* the bitmap buffer */ /* This buffer has the same size in indices as the */ /* bitmap buffer. When we check a pixel position for */ @@ -3206,7 +3206,7 @@ /* and also determine the signs properly. */ 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 ) @@ -3362,14 +3362,12 @@ dists[index].distance = fixed_spread; /* flip sign if required */ - dists[index].distance *= internal_params.flip_sign ? - -current_sign : - current_sign; + dists[index].distance *= internal_params.flip_sign ? -current_sign + : current_sign; /* concatenate to appropriate format */ - buffer[index] = map_fixed_to_sdf( - dists[index].distance, - fixed_spread ); + buffer[index] = map_fixed_to_sdf( dists[index].distance, + fixed_spread ); } } @@ -3506,9 +3504,9 @@ SDF_Contour* head; /* head of the contour list */ SDF_Shape temp_shape; /* temporary shape */ - FT_Memory memory; /* to allocate memory */ - FT_SDFFormat* t; /* target bitmap buffer */ - FT_Bool flip_sign; /* filp sign? */ + FT_Memory memory; /* to allocate memory */ + FT_SDFFormat* t; /* target bitmap buffer */ + FT_Bool flip_sign; /* flip sign? */ /* orientation of all the separate contours */ SDF_Contour_Orientation* orientations; @@ -3644,18 +3642,18 @@ { for ( i = 0; i < width; i++ ) { - FT_Int id = j * width + i; /* index of current pixel */ - FT_Int c; /* contour iterator */ + FT_Int id = j * width + i; /* index of current pixel */ + FT_Int c; /* contour iterator */ - FT_SDFFormat val_c = 0; /* max clockwise value */ - FT_SDFFormat val_ac = UCHAR_MAX; /* min counter-clockwise val */ + FT_SDFFormat val_c = 0; /* max clockwise value */ + FT_SDFFormat val_ac = UCHAR_MAX; /* min counter-clockwise val */ /* iterate through all the contours */ for ( c = 0; c < num_contours; c++ ) { /* 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 )