1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #ifndef SkDistanceFieldGen_DEFINED
8 #define SkDistanceFieldGen_DEFINED
9
10 #include "include/core/SkTypes.h"
11
12 #include <cstddef>
13
14 #if !defined(SK_DISABLE_SDF_TEXT)
15
16 // the max magnitude for the distance field
17 // distance values are limited to the range (-SK_DistanceFieldMagnitude, SK_DistanceFieldMagnitude]
18 #define SK_DistanceFieldMagnitude 4
19 // we need to pad around the original glyph to allow our maximum distance of
20 // SK_DistanceFieldMagnitude texels away from any edge
21 #define SK_DistanceFieldPad 4
22 // the rect we render with is inset from the distance field glyph size to allow for bilerp
23 #define SK_DistanceFieldInset 2
24
25 // For the fragment shader:
26 // The distance field is constructed as unsigned char values,
27 // so that the zero value is at 128, and the supported range of distances is [-4 * 127/128, 4].
28 // Hence our multiplier (width of the range) is 4 * 255/128 and zero threshold is 128/255.
29 #define SK_DistanceFieldMultiplier "7.96875"
30 #define SK_DistanceFieldThreshold "0.50196078431"
31
32 /** Given 8-bit mask data, generate the associated distance field
33
34 * @param distanceField The distance field to be generated. Should already be allocated
35 * by the client with the padding above.
36 * @param image 8-bit mask we're using to generate the distance field.
37 * @param w Width of the original image.
38 * @param h Height of the original image.
39 * @param rowBytes Size of each row in the image, in bytes
40 */
41 bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField,
42 const unsigned char* image,
43 int w, int h, size_t rowBytes);
44
45 /** Given LCD16 mask data (not a 16-bit image), generate the associated distance field
46
47 * @param distanceField The distance field to be generated. Should already be allocated
48 * by the client with the padding above.
49 * @param image 16-bit LCD data we're using to generate the distance field.
50 * @param w Width of the original image.
51 * @param h Height of the original image.
52 * @param rowBytes Size of each row in the image, in bytes
53 */
54 bool SkGenerateDistanceFieldFromLCD16Mask(unsigned char* distanceField,
55 const unsigned char* image,
56 int w, int h, size_t rowBytes);
57
58 /** Given 1-bit mask data, generate the associated distance field
59
60 * @param distanceField The distance field to be generated. Should already be allocated
61 * by the client with the padding above.
62 * @param image 1-bit mask we're using to generate the distance field.
63 * @param w Width of the original image.
64 * @param h Height of the original image.
65 * @param rowBytes Size of each row in the image, in bytes
66 */
67 bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField,
68 const unsigned char* image,
69 int w, int h, size_t rowBytes);
70
71 /** Given width and height of original image, return size (in bytes) of distance field
72 * @param w Width of the original image.
73 * @param h Height of the original image.
74 */
SkComputeDistanceFieldSize(int w,int h)75 inline size_t SkComputeDistanceFieldSize(int w, int h) {
76 return (w + 2*SK_DistanceFieldPad) * (h + 2*SK_DistanceFieldPad) * sizeof(unsigned char);
77 }
78
79 #endif // !defined(SK_DISABLE_SDF_TEXT)
80
81 #endif
82