1 static const char* glyphy_common_glsl =
3 " * Copyright 2012 Google, Inc. All Rights Reserved.\n"
5 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
6 " * you may not use this file except in compliance with the License.\n"
7 " * You may obtain a copy of the License at\n"
9 " * http://www.apache.org/licenses/LICENSE-2.0\n"
11 " * Unless required by applicable law or agreed to in writing, software\n"
12 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
13 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
14 " * See the License for the specific language governing permissions and\n"
15 " * limitations under the License.\n"
17 " * Google Author(s): Behdad Esfahbod, Maysum Panju\n"
21 "#ifndef GLYPHY_INFINITY\n"
22 "# define GLYPHY_INFINITY 1e9\n"
24 "#ifndef GLYPHY_EPSILON\n"
25 "# define GLYPHY_EPSILON 1e-5\n"
28 "#ifndef GLYPHY_RGBA\n"
29 "# ifdef GLYPHY_BGRA\n"
30 "# define GLYPHY_RGBA(v) glyphy_bgra (v)\n"
32 "# define GLYPHY_RGBA(v) glyphy_rgba (v)\n"
37 "glyphy_rgba (const vec4 v)\n"
43 "glyphy_bgra (const vec4 v)\n"
49 "struct glyphy_arc_t {\n"
55 "struct glyphy_arc_endpoint_t {\n"
56 " /* Second arc endpoint */\n"
58 " /* Infinity if this endpoint does not form an arc with the previous\n"
59 " * endpoint. Ie. a \"move_to\". Test with glyphy_isinf().\n"
60 " * Arc depth otherwise. */\n"
64 "struct glyphy_arc_list_t {\n"
65 " /* Number of endpoints in the list.\n"
66 " * Will be zero if we're far away inside or outside, in which case side is set.\n"
67 " * Will be -1 if this arc-list encodes a single line, in which case line_* are set. */\n"
68 " int num_endpoints;\n"
70 " /* If num_endpoints is zero, this specifies whether we are inside (-1)\n"
71 " * or outside (+1). Otherwise we're unsure (0). */\n"
73 " /* Offset to the arc-endpoints from the beginning of the glyph blob */\n"
76 " /* A single line is all we care about. It's right here. */\n"
77 " float line_angle;\n"
78 " float line_distance; /* From nominal glyph center */\n"
82 "glyphy_isinf (const float v)\n"
84 " return abs (v) >= GLYPHY_INFINITY * .5;\n"
88 "glyphy_iszero (const float v)\n"
90 " return abs (v) <= GLYPHY_EPSILON * 2.;\n"
94 "glyphy_ortho (const vec2 v)\n"
96 " return vec2 (-v.y, v.x);\n"
100 "glyphy_float_to_byte (const float v)\n"
102 " return int (v * (256. - GLYPHY_EPSILON));\n"
106 "glyphy_vec4_to_bytes (const vec4 v)\n"
108 " return ivec4 (v * (256. - GLYPHY_EPSILON));\n"
112 "glyphy_float_to_two_nimbles (const float v)\n"
114 " int f = glyphy_float_to_byte (v);\n"
115 " return ivec2 (f / 16, int(mod (float(f), 16.)));\n"
118 "/* returns tan (2 * atan (d)) */\n"
120 "glyphy_tan2atan (const float d)\n"
122 " return 2. * d / (1. - d * d);\n"
125 "glyphy_arc_endpoint_t\n"
126 "glyphy_arc_endpoint_decode (const vec4 v, const ivec2 nominal_size)\n"
128 " vec2 p = (vec2 (glyphy_float_to_two_nimbles (v.a)) + v.gb) / 16.;\n"
131 " d = GLYPHY_INFINITY;\n"
133 "#define GLYPHY_MAX_D .5\n"
134 " d = float(glyphy_float_to_byte (d) - 128) * GLYPHY_MAX_D / 127.;\n"
135 "#undef GLYPHY_MAX_D\n"
136 " return glyphy_arc_endpoint_t (p * vec2(nominal_size), d);\n"
140 "glyphy_arc_center (const glyphy_arc_t a)\n"
142 " return mix (a.p0, a.p1, .5) +\n"
143 " glyphy_ortho (a.p1 - a.p0) / (2. * glyphy_tan2atan (a.d));\n"
147 "glyphy_arc_wedge_contains (const glyphy_arc_t a, const vec2 p)\n"
149 " float d2 = glyphy_tan2atan (a.d);\n"
150 " return dot (p - a.p0, (a.p1 - a.p0) * mat2(1, d2, -d2, 1)) >= 0. &&\n"
151 " dot (p - a.p1, (a.p1 - a.p0) * mat2(1, -d2, d2, 1)) <= 0.;\n"
155 "glyphy_arc_wedge_signed_dist_shallow (const glyphy_arc_t a, const vec2 p)\n"
157 " vec2 v = normalize (a.p1 - a.p0);\n"
158 " float line_d = dot (p - a.p0, glyphy_ortho (v));\n"
162 " float d0 = dot ((p - a.p0), v);\n"
164 " return sign (line_d) * distance (p, a.p0);\n"
165 " float d1 = dot ((a.p1 - p), v);\n"
167 " return sign (line_d) * distance (p, a.p1);\n"
168 " float r = 2. * a.d * (d0 * d1) / (d0 + d1);\n"
169 " if (r * line_d > 0.)\n"
170 " return sign (line_d) * min (abs (line_d + r), min (distance (p, a.p0), distance (p, a.p1)));\n"
171 " return line_d + r;\n"
175 "glyphy_arc_wedge_signed_dist (const glyphy_arc_t a, const vec2 p)\n"
177 " if (abs (a.d) <= .03)\n"
178 " return glyphy_arc_wedge_signed_dist_shallow (a, p);\n"
179 " vec2 c = glyphy_arc_center (a);\n"
180 " return sign (a.d) * (distance (a.p0, c) - distance (p, c));\n"
184 "glyphy_arc_extended_dist (const glyphy_arc_t a, const vec2 p)\n"
186 " /* Note: this doesn't handle points inside the wedge. */\n"
187 " vec2 m = mix (a.p0, a.p1, .5);\n"
188 " float d2 = glyphy_tan2atan (a.d);\n"
189 " if (dot (p - m, a.p1 - m) < 0.)\n"
190 " return dot (p - a.p0, normalize ((a.p1 - a.p0) * mat2(+d2, -1, +1, +d2)));\n"
192 " return dot (p - a.p1, normalize ((a.p1 - a.p0) * mat2(-d2, -1, +1, -d2)));\n"
196 "glyphy_arc_list_offset (const vec2 p, const ivec2 nominal_size)\n"
198 " ivec2 cell = ivec2 (clamp (floor (p), vec2 (0.,0.), vec2(nominal_size - 1)));\n"
199 " return cell.y * nominal_size.x + cell.x;\n"
202 "glyphy_arc_list_t\n"
203 "glyphy_arc_list_decode (const vec4 v, const ivec2 nominal_size)\n"
205 " glyphy_arc_list_t l;\n"
206 " ivec4 iv = glyphy_vec4_to_bytes (v);\n"
207 " l.side = 0; /* unsure */\n"
208 " if (iv.r == 0) { /* arc-list encoded */\n"
209 " l.offset = (iv.g * 256) + iv.b;\n"
210 " l.num_endpoints = iv.a;\n"
211 " if (l.num_endpoints == 255) {\n"
212 " l.num_endpoints = 0;\n"
214 " } else if (l.num_endpoints == 0)\n"
216 " } else { /* single line encoded */\n"
217 " l.num_endpoints = -1;\n"
218 /*" l.line_distance = float(((iv.r - 128) * 256 + iv.g) - 0x4000) / float (0x1FFF)\n"
219 " * max (float (nominal_size.x), float (nominal_size.y));\n"
220 " l.line_angle = float(-((iv.b * 256 + iv.a) - 0x8000)) / float (0x7FFF) * 3.14159265358979;\n"*/
221 /*" l.line_distance = float(((iv.r - 128) * 256 + iv.g) - 16384) / 8191.0 \n"
222 " * max (float (nominal_size.x), float (nominal_size.y));\n"
223 " l.line_angle = float(-((iv.b * 256 + iv.a) - 32768)) / 32767. * 3.14159;\n"*/
224 " l.line_distance = ( float(iv.r)/32. + 0.01*float(iv.g)/82.0 - 6.) \n"
225 " * max (float (nominal_size.x), float (nominal_size.y));\n"
226 " l.line_angle = ( -float(iv.b)/40.74 - float( iv.a )*0.0001 )-3.142;\n"