1 static const char *glyphy_common_glsl = 2 "/*\n" 3 " * Copyright 2012 Google, Inc. All Rights Reserved.\n" 4 " *\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" 8 " *\n" 9 " * http://www.apache.org/licenses/LICENSE-2.0\n" 10 " *\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" 16 " *\n" 17 " * Google Author(s): Behdad Esfahbod, Maysum Panju\n" 18 " */\n" 19 "\n" 20 "\n" 21 "#ifndef GLYPHY_INFINITY\n" 22 "# define GLYPHY_INFINITY 1e9\n" 23 "#endif\n" 24 "#ifndef GLYPHY_EPSILON\n" 25 "# define GLYPHY_EPSILON 1e-5\n" 26 "#endif\n" 27 "\n" 28 "#ifndef GLYPHY_RGBA\n" 29 "# ifdef GLYPHY_BGRA\n" 30 "# define GLYPHY_RGBA(v) glyphy_bgra (v)\n" 31 "# else\n" 32 "# define GLYPHY_RGBA(v) glyphy_rgba (v)\n" 33 "# endif\n" 34 "#endif\n" 35 "\n" 36 "vec4\n" 37 "glyphy_rgba (const vec4 v)\n" 38 "{\n" 39 " return v.rgba;\n" 40 "}\n" 41 "\n" 42 "vec4\n" 43 "glyphy_bgra (const vec4 v)\n" 44 "{\n" 45 " return v.bgra;\n" 46 "}\n" 47 "\n" 48 "\n" 49 "struct glyphy_arc_t {\n" 50 " vec2 p0;\n" 51 " vec2 p1;\n" 52 " float d;\n" 53 "};\n" 54 "\n" 55 "struct glyphy_arc_endpoint_t {\n" 56 " /* Second arc endpoint */\n" 57 " vec2 p;\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" 61 " float d;\n" 62 "};\n" 63 "\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" 69 "\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" 72 " int side;\n" 73 " /* Offset to the arc-endpoints from the beginning of the glyph blob */\n" 74 " int offset;\n" 75 "\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" 79 "};\n" 80 "\n" 81 "bool\n" 82 "glyphy_isinf (const float v)\n" 83 "{\n" 84 " return abs (v) >= GLYPHY_INFINITY * .5;\n" 85 "}\n" 86 "\n" 87 "bool\n" 88 "glyphy_iszero (const float v)\n" 89 "{\n" 90 " return abs (v) <= GLYPHY_EPSILON * 2.;\n" 91 "}\n" 92 "\n" 93 "vec2\n" 94 "glyphy_ortho (const vec2 v)\n" 95 "{\n" 96 " return vec2 (-v.y, v.x);\n" 97 "}\n" 98 "\n" 99 "int\n" 100 "glyphy_float_to_byte (const float v)\n" 101 "{\n" 102 " return int (v * (256. - GLYPHY_EPSILON));\n" 103 "}\n" 104 "\n" 105 "ivec4\n" 106 "glyphy_vec4_to_bytes (const vec4 v)\n" 107 "{\n" 108 " return ivec4 (v * (256. - GLYPHY_EPSILON));\n" 109 "}\n" 110 "\n" 111 "ivec2\n" 112 "glyphy_float_to_two_nimbles (const float v)\n" 113 "{\n" 114 " int f = glyphy_float_to_byte (v);\n" 115 " return ivec2 (f / 16, int(mod (float(f), 16.)));\n" 116 "}\n" 117 "\n" 118 "/* returns tan (2 * atan (d)) */\n" 119 "float\n" 120 "glyphy_tan2atan (const float d)\n" 121 "{\n" 122 " return 2. * d / (1. - d * d);\n" 123 "}\n" 124 "\n" 125 "glyphy_arc_endpoint_t\n" 126 "glyphy_arc_endpoint_decode (const vec4 v, const ivec2 nominal_size)\n" 127 "{\n" 128 " vec2 p = (vec2 (glyphy_float_to_two_nimbles (v.a)) + v.gb) / 16.;\n" 129 " float d = v.r;\n" 130 " if (d == 0.)\n" 131 " d = GLYPHY_INFINITY;\n" 132 " else\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" 137 "}\n" 138 "\n" 139 "vec2\n" 140 "glyphy_arc_center (const glyphy_arc_t a)\n" 141 "{\n" 142 " return mix (a.p0, a.p1, .5) +\n" 143 " glyphy_ortho (a.p1 - a.p0) / (2. * glyphy_tan2atan (a.d));\n" 144 "}\n" 145 "\n" 146 "bool\n" 147 "glyphy_arc_wedge_contains (const glyphy_arc_t a, const vec2 p)\n" 148 "{\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" 152 "}\n" 153 "\n" 154 "float\n" 155 "glyphy_arc_wedge_signed_dist_shallow (const glyphy_arc_t a, const vec2 p)\n" 156 "{\n" 157 " vec2 v = normalize (a.p1 - a.p0);\n" 158 " float line_d = dot (p - a.p0, glyphy_ortho (v));\n" 159 " if (a.d == 0.)\n" 160 " return line_d;\n" 161 "\n" 162 " float d0 = dot ((p - a.p0), v);\n" 163 " if (d0 < 0.)\n" 164 " return sign (line_d) * distance (p, a.p0);\n" 165 " float d1 = dot ((a.p1 - p), v);\n" 166 " if (d1 < 0.)\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" 172 "}\n" 173 "\n" 174 "float\n" 175 "glyphy_arc_wedge_signed_dist (const glyphy_arc_t a, const vec2 p)\n" 176 "{\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" 181 "}\n" 182 "\n" 183 "float\n" 184 "glyphy_arc_extended_dist (const glyphy_arc_t a, const vec2 p)\n" 185 "{\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" 191 " else\n" 192 " return dot (p - a.p1, normalize ((a.p1 - a.p0) * mat2(-d2, -1, +1, -d2)));\n" 193 "}\n" 194 "\n" 195 "int\n" 196 "glyphy_arc_list_offset (const vec2 p, const ivec2 nominal_size)\n" 197 "{\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" 200 "}\n" 201 "\n" 202 "glyphy_arc_list_t\n" 203 "glyphy_arc_list_decode (const vec4 v, const ivec2 nominal_size)\n" 204 "{\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" 213 " l.side = -1;\n" 214 " } else if (l.num_endpoints == 0)\n" 215 " l.side = +1;\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 " }\n" 222 " return l;\n" 223 "}\n" 224 ; 225