| 1 |
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. |
| 2 |
This software is licensed as OpenSource, under the Apache License, Version 2.0. |
| 3 |
This license is available at: http://opensource.org/licenses/Apache-2.0. */ |
| 4 |
|
| 5 |
/* |
| 6 |
* Glyph metrics support. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "absfont.h" |
| 10 |
|
| 11 |
#include <math.h> |
| 12 |
#include <float.h> |
| 13 |
|
| 14 |
/* Transform x and y coordinates by matrix. */ |
| 15 |
#define TX(x, y) (h->matrix[0] * x + h->matrix[2] * y + h->matrix[4]) |
| 16 |
#define TY(x, y) (h->matrix[1] * x + h->matrix[3] * y + h->matrix[5]) |
| 17 |
|
| 18 |
typedef struct /* Rectangle */ |
| 19 |
{ |
| 20 |
float left; |
| 21 |
float bottom; |
| 22 |
float right; |
| 23 |
float top; |
| 24 |
} Rect; |
| 25 |
|
| 26 |
/* Begin glyph path. */ |
| 27 |
static int glyphBeg(abfGlyphCallbacks *cb, abfGlyphInfo *info) { |
| 28 |
abfMetricsCtx h = (abfMetricsCtx)cb->direct_ctx; |
| 29 |
|
| 30 |
cb->info = info; |
| 31 |
h->err_code = abfSuccess; |
| 32 |
|
| 33 |
/* Initialize bounding box */ |
| 34 |
h->real_mtx.left = FLT_MAX; |
| 35 |
h->real_mtx.bottom = FLT_MAX; |
| 36 |
h->real_mtx.right = -FLT_MAX; |
| 37 |
h->real_mtx.top = -FLT_MAX; |
| 38 |
|
| 39 |
return ABF_CONT_RET; |
| 40 |
} |
| 41 |
|
| 42 |
/* Save glyph width. */ |
| 43 |
static void glyphWidth(abfGlyphCallbacks *cb, float hAdv) { |
| 44 |
abfMetricsCtx h = (abfMetricsCtx)cb->direct_ctx; |
| 45 |
h->real_mtx.hAdv = (h->flags & ABF_MTX_TRANSFORM) ? h->matrix[0] * hAdv : hAdv; |
| 46 |
} |
| 47 |
|
| 48 |
/* Add point to current bounds. */ |
| 49 |
static void boundPoint(abfMetricsCtx h, float x, float y) { |
| 50 |
if (h->real_mtx.left > x) |
| 51 |
h->real_mtx.left = x; |
| 52 |
if (h->real_mtx.right < x) |
| 53 |
h->real_mtx.right = x; |
| 54 |
if (h->real_mtx.bottom > y) |
| 55 |
h->real_mtx.bottom = y; |
| 56 |
if (h->real_mtx.top < y) |
| 57 |
h->real_mtx.top = y; |
| 58 |
} |
| 59 |
|
| 60 |
/* Add move to path. */ |
| 61 |
static void glyphMove(abfGlyphCallbacks *cb, float x0, float y0) { |
| 62 |
/* Note: do not call boundPoint() nor directly set h->real_mtx here as |
| 63 |
a moveTo may be used for an anchor point in UFO2. We will instead |
| 64 |
update the bounds before/after commands that actually draw. */ |
| 65 |
abfMetricsCtx h = (abfMetricsCtx)cb->direct_ctx; |
| 66 |
h->x = x0; |
| 67 |
h->y = y0; |
| 68 |
} |
| 69 |
|
| 70 |
/* Add line to path. */ |
| 71 |
static void glyphLine(abfGlyphCallbacks *cb, float x1, float y1) { |
| 72 |
abfMetricsCtx h = (abfMetricsCtx)cb->direct_ctx; |
| 73 |
|
| 74 |
if (h->flags & ABF_MTX_TRANSFORM) { |
| 75 |
boundPoint(h, TX(h->x, h->y), TY(h->x, h->y)); |
| 76 |
boundPoint(h, TX(x1, y1), TY(x1, y1)); |
| 77 |
} else { |
| 78 |
boundPoint(h, h->x, h->y); |
| 79 |
boundPoint(h, x1, y1); |
| 80 |
} |
| 81 |
h->x = x1; |
| 82 |
h->y = y1; |
| 83 |
} |
| 84 |
|
| 85 |
/* Set bounds on line from x0,y0 to x1,y1. */ |
| 86 |
static void setLineBounds(Rect *r, float x0, float y0, float x1, float y1) { |
| 87 |
if (x0 < x1) { |
| 88 |
r->left = x0; |
| 89 |
r->right = x1; |
| 90 |
} else { |
| 91 |
r->left = x1; |
| 92 |
r->right = x0; |
| 93 |
} |
| 94 |
if (y0 < y1) { |
| 95 |
r->bottom = y0; |
| 96 |
r->top = y1; |
| 97 |
} else { |
| 98 |
r->bottom = y1; |
| 99 |
r->top = y0; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/* Set horizontal or vertical limits (other than end points) on Bezier curve.*/ |
| 104 |
static void setBezLimits(float p0, float p1, float p2, float p3, |
| 105 |
float *lo, float *hi) { |
| 106 |
float t[2]; |
| 107 |
int i = 0; |
| 108 |
float a = p3 - 3 * (p2 - p1) - p0; |
| 109 |
float b = p2 - 2 * p1 + p0; |
| 110 |
float c = p1 - p0; |
| 111 |
if (a == 0) { |
| 112 |
if (b != 0) |
| 113 |
t[i++] = -c / (2 * b); |
| 114 |
} else { |
| 115 |
float r = b * b - a * c; |
| 116 |
if (r < 0) |
| 117 |
return; /* Equation has no solutions */ |
| 118 |
r = (float)sqrt(r); |
| 119 |
t[i++] = (-b + r) / a; |
| 120 |
t[i++] = (-b - r) / a; |
| 121 |
} |
| 122 |
while (i--) |
| 123 |
if (t[i] > 0 && t[i] < 1) { |
| 124 |
float limit = t[i] * (t[i] * (t[i] * a + 3 * b) + 3 * c) + p0; |
| 125 |
if (limit < *lo) |
| 126 |
*lo = limit; |
| 127 |
else if (limit > *hi) |
| 128 |
*hi = limit; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/* Add curve to path. */ |
| 133 |
static void glyphCurve(abfGlyphCallbacks *cb, |
| 134 |
float x1, float y1, |
| 135 |
float x2, float y2, |
| 136 |
float x3, float y3) { |
| 137 |
abfMetricsCtx h = (abfMetricsCtx)cb->direct_ctx; |
| 138 |
Rect ep; /* End-point bounds */ |
| 139 |
Rect cp; /* Control-point bounds */ |
| 140 |
float x0 = h->x; |
| 141 |
float y0 = h->y; |
| 142 |
|
| 143 |
if (h->flags & ABF_MTX_TRANSFORM) { |
| 144 |
/* Transform curve */ |
| 145 |
float xt; |
| 146 |
xt = x0; |
| 147 |
x0 = TX(xt, y0); |
| 148 |
y0 = TY(xt, y0); |
| 149 |
xt = x1; |
| 150 |
x1 = TX(xt, y1); |
| 151 |
y1 = TY(xt, y1); |
| 152 |
xt = x2; |
| 153 |
x2 = TX(xt, y2); |
| 154 |
y2 = TY(xt, y2); |
| 155 |
xt = x3; |
| 156 |
x3 = TX(xt, y3); |
| 157 |
y3 = TY(xt, y3); |
| 158 |
} |
| 159 |
|
| 160 |
setLineBounds(&ep, x0, y0, x3, y3); |
| 161 |
setLineBounds(&cp, x1, y1, x2, y2); |
| 162 |
|
| 163 |
if (ep.left < h->real_mtx.left || |
| 164 |
ep.bottom < h->real_mtx.bottom || |
| 165 |
ep.right > h->real_mtx.right || |
| 166 |
ep.top > h->real_mtx.top || |
| 167 |
cp.left < h->real_mtx.left || |
| 168 |
cp.bottom < h->real_mtx.bottom || |
| 169 |
cp.right > h->real_mtx.right || |
| 170 |
cp.top > h->real_mtx.top) { |
| 171 |
/* Curve may extend bounds */ |
| 172 |
if (cp.left < ep.left || cp.right > ep.right) |
| 173 |
/* Grow left and/or right bounds */ |
| 174 |
setBezLimits(x0, x1, x2, x3, &ep.left, &ep.right); |
| 175 |
|
| 176 |
if (cp.bottom < ep.bottom || cp.top > ep.top) |
| 177 |
/* Grow top and/or bottom bounds */ |
| 178 |
setBezLimits(y0, y1, y2, y3, &ep.bottom, &ep.top); |
| 179 |
|
| 180 |
boundPoint(h, ep.left, ep.bottom); |
| 181 |
boundPoint(h, ep.right, ep.top); |
| 182 |
} |
| 183 |
|
| 184 |
h->x = x3; |
| 185 |
h->y = y3; |
| 186 |
} |
| 187 |
|
| 188 |
/* Ignore stem operator. */ |
| 189 |
static void glyphStem(abfGlyphCallbacks *cb, |
| 190 |
int flags, float edge0, float edge1) { |
| 191 |
/* Nothing to do */ |
| 192 |
} |
| 193 |
|
| 194 |
/* Convert flex operator. */ |
| 195 |
static void glyphFlex(abfGlyphCallbacks *cb, float depth, |
| 196 |
float x1, float y1, |
| 197 |
float x2, float y2, |
| 198 |
float x3, float y3, |
| 199 |
float x4, float y4, |
| 200 |
float x5, float y5, |
| 201 |
float x6, float y6) { |
| 202 |
glyphCurve(cb, x1, y1, x2, y2, x3, y3); |
| 203 |
glyphCurve(cb, x4, y4, x5, y5, x6, y6); |
| 204 |
} |
| 205 |
|
| 206 |
/* Ignore general glyph operator. */ |
| 207 |
static void glyphGenop(abfGlyphCallbacks *cb, |
| 208 |
int cnt, float *args, int op) { |
| 209 |
/* Nothing to do */ |
| 210 |
} |
| 211 |
|
| 212 |
/* Handle seac operator. */ |
| 213 |
static void glyphSeac(abfGlyphCallbacks *cb, |
| 214 |
float adx, float ady, int bchar, int achar) { |
| 215 |
abfMetricsCtx h = (abfMetricsCtx)cb->direct_ctx; |
| 216 |
h->err_code = abfErrGlyphSeac; |
| 217 |
} |
| 218 |
|
| 219 |
/* End glyph path. */ |
| 220 |
static void glyphEnd(abfGlyphCallbacks *cb) { |
| 221 |
abfMetricsCtx h = (abfMetricsCtx)cb->direct_ctx; |
| 222 |
|
| 223 |
/* If we didn't see ink, then zero out the bounding box. */ |
| 224 |
if ((h->real_mtx.left == FLT_MAX) && |
| 225 |
(h->real_mtx.bottom == FLT_MAX) && |
| 226 |
(h->real_mtx.right == -FLT_MAX) && |
| 227 |
(h->real_mtx.top == -FLT_MAX)) { |
| 228 |
h->real_mtx.left = 0; |
| 229 |
h->real_mtx.bottom = 0; |
| 230 |
h->real_mtx.right = 0; |
| 231 |
h->real_mtx.top = 0; |
| 232 |
} |
| 233 |
|
| 234 |
/* Compute integer metrics */ |
| 235 |
h->int_mtx.left = (long)floor(h->real_mtx.left); |
| 236 |
h->int_mtx.bottom = (long)floor(h->real_mtx.bottom); |
| 237 |
h->int_mtx.right = (long)ceil(h->real_mtx.right); |
| 238 |
h->int_mtx.top = (long)ceil(h->real_mtx.top); |
| 239 |
|
| 240 |
/* Round advance away from zero. */ |
| 241 |
if (h->real_mtx.hAdv >= 0) { |
| 242 |
h->int_mtx.hAdv = (long)(h->real_mtx.hAdv + 0.5); |
| 243 |
} else { |
| 244 |
h->int_mtx.hAdv = (long)(h->real_mtx.hAdv - 0.5); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/* Glyph metrics callbacks template. */ |
| 249 |
const abfGlyphCallbacks abfGlyphMetricsCallbacks = |
| 250 |
{ |
| 251 |
NULL, |
| 252 |
NULL, |
| 253 |
NULL, |
| 254 |
glyphBeg, |
| 255 |
glyphWidth, |
| 256 |
glyphMove, |
| 257 |
glyphLine, |
| 258 |
glyphCurve, |
| 259 |
glyphStem, |
| 260 |
glyphFlex, |
| 261 |
glyphGenop, |
| 262 |
glyphSeac, |
| 263 |
glyphEnd, |
| 264 |
}; |