| 1 |
// -*- related-file-name: "../../libefont/t1interp.cc" -*- |
| 2 |
#ifndef EFONT_T1INTERP_HH |
| 3 |
#define EFONT_T1INTERP_HH |
| 4 |
#include <efont/t1cs.hh> |
| 5 |
#include <lcdf/point.hh> |
| 6 |
#include <stdio.h> |
| 7 |
namespace Efont { |
| 8 |
|
| 9 |
class CharstringInterp { public: |
| 10 |
|
| 11 |
CharstringInterp(); |
| 12 |
CharstringInterp(const Vector<double> &weight_vec); |
| 13 |
virtual ~CharstringInterp() { } |
| 14 |
|
| 15 |
int error() const { return _error; } |
| 16 |
int error_data() const { return _error_data; } |
| 17 |
static String error_string(int error, int error_data); |
| 18 |
inline String error_string() const; |
| 19 |
|
| 20 |
bool careful() const { return _careful; } |
| 21 |
void set_careful(bool c) { _careful = c; } |
| 22 |
|
| 23 |
bool done() const { return _done; } |
| 24 |
void set_done() { _done = true; } |
| 25 |
|
| 26 |
int size() const { return _sp; } |
| 27 |
double &at(unsigned i) { return _s[i]; } |
| 28 |
double &top(unsigned i = 0) { return _s[_sp - i - 1]; } |
| 29 |
double pop(unsigned n = 1) { _sp -= n; return _s[_sp]; } |
| 30 |
inline void push(double); |
| 31 |
void clear() { _sp = 0; } |
| 32 |
|
| 33 |
int ps_size() const { return _ps_sp; } |
| 34 |
double ps_at(unsigned i) const { return _ps_s[i]; } |
| 35 |
double ps_pop() { return _ps_s[--_ps_sp]; } |
| 36 |
inline void ps_push(double); |
| 37 |
void ps_clear() { _ps_sp = 0; } |
| 38 |
|
| 39 |
int subr_depth() const { return _subr_depth; } |
| 40 |
|
| 41 |
inline double &vec(Vector<double> *, int); |
| 42 |
const Vector<double> &weight_vector() const { return _weight_vector; } |
| 43 |
Vector<double> *scratch_vector() { return &_scratch_vector; } |
| 44 |
|
| 45 |
const CharstringProgram *program() const { return _program; } |
| 46 |
inline Charstring *get_subr(int) const; |
| 47 |
inline Charstring *get_gsubr(int) const; |
| 48 |
inline Charstring *get_xsubr(bool g, int) const; |
| 49 |
inline Charstring *get_glyph(PermString) const; |
| 50 |
|
| 51 |
bool interpret(const CharstringProgram *, const Charstring *); |
| 52 |
inline bool interpret(const CharstringContext &); |
| 53 |
|
| 54 |
//virtual void init(const CharstringProgram *); |
| 55 |
bool error(int c) { return error(c, 0); } |
| 56 |
virtual bool error(int, int); |
| 57 |
virtual bool number(double); |
| 58 |
|
| 59 |
bool arith_command(int); |
| 60 |
bool vector_command(int); |
| 61 |
bool blend_command(); |
| 62 |
bool callsubr_command(); |
| 63 |
bool callgsubr_command(); |
| 64 |
inline bool callxsubr_command(bool g); |
| 65 |
bool mm_command(int, int); |
| 66 |
bool itc_command(int, int); |
| 67 |
|
| 68 |
const Point &left_sidebearing() const { return _lsb; } |
| 69 |
const Point ¤tpoint() const { return _cp; } |
| 70 |
void set_state_path() { _state = S_PATH; } |
| 71 |
|
| 72 |
virtual bool callothersubr_command(int, int); |
| 73 |
virtual bool type1_command(int); |
| 74 |
virtual bool type2_command(int, const uint8_t *, int *); |
| 75 |
|
| 76 |
virtual void act_sidebearing(int cmd, const Point &lsb); |
| 77 |
virtual void act_width(int cmd, const Point &width); |
| 78 |
virtual void act_default_width(int cmd); |
| 79 |
virtual void act_nominal_width_delta(int cmd, double delta); |
| 80 |
virtual void act_seac(int cmd, double asb, double adx, double ady, int bchar, int achar); |
| 81 |
|
| 82 |
virtual void act_line(int cmd, const Point &p0, const Point &p1); |
| 83 |
virtual void act_curve(int cmd, const Point &p0, const Point &p1, const Point &p2, const Point &p3); |
| 84 |
virtual void act_closepath(int cmd); |
| 85 |
|
| 86 |
virtual void act_flex(int cmd, const Point &p0, const Point &p1, const Point &p2, const Point &p3_4, const Point &p5, const Point &p6, const Point &p7, double flex_depth); |
| 87 |
|
| 88 |
virtual void act_hstem(int cmd, double y, double dy); |
| 89 |
virtual void act_vstem(int cmd, double x, double dx); |
| 90 |
virtual void act_hstem3(int cmd, double y0, double dy0, double y1, double dy1, double y2, double dy2); |
| 91 |
virtual void act_vstem3(int cmd, double x0, double dx0, double x1, double dx1, double x2, double dx2); |
| 92 |
virtual void act_hintmask(int cmd, const uint8_t *data, int nhints); |
| 93 |
|
| 94 |
typedef Charstring Cs; |
| 95 |
|
| 96 |
enum Errors { |
| 97 |
errOK = 0, |
| 98 |
errInternal = -1, |
| 99 |
errRunoff = -2, |
| 100 |
errUnimplemented = -3, |
| 101 |
errOverflow = -4, |
| 102 |
errUnderflow = -5, |
| 103 |
errVector = -6, |
| 104 |
errValue = -7, |
| 105 |
errSubr = -8, |
| 106 |
errGlyph = -9, |
| 107 |
errCurrentPoint = -10, |
| 108 |
errFlex = -11, |
| 109 |
errMultipleMaster = -12, |
| 110 |
errOpenStroke = -13, |
| 111 |
errLateSidebearing = -14, |
| 112 |
errOthersubr = -15, |
| 113 |
errOrdering = -16, |
| 114 |
errHintmask = -17, |
| 115 |
errSubrDepth = -18, |
| 116 |
errLastError = -18 |
| 117 |
}; |
| 118 |
|
| 119 |
enum { STACK_SIZE = 48, PS_STACK_SIZE = 24, MAX_SUBR_DEPTH = 10, |
| 120 |
SCRATCH_SIZE = 32 }; |
| 121 |
|
| 122 |
private: |
| 123 |
|
| 124 |
int _error; |
| 125 |
int _error_data; |
| 126 |
bool _done; |
| 127 |
bool _careful; |
| 128 |
|
| 129 |
double _s[STACK_SIZE]; |
| 130 |
int _sp; |
| 131 |
double _ps_s[PS_STACK_SIZE]; |
| 132 |
int _ps_sp; |
| 133 |
|
| 134 |
int _subr_depth; |
| 135 |
|
| 136 |
Vector<double> _weight_vector; |
| 137 |
Vector<double> _scratch_vector; |
| 138 |
|
| 139 |
Point _lsb; |
| 140 |
Point _cp; |
| 141 |
Point _seac_origin; |
| 142 |
|
| 143 |
const CharstringProgram *_program; |
| 144 |
|
| 145 |
enum State { |
| 146 |
S_INITIAL, S_SEAC, S_SBW, S_HSTEM, S_VSTEM, S_HINTMASK, S_IPATH, S_PATH |
| 147 |
}; |
| 148 |
State _state; |
| 149 |
bool _flex; |
| 150 |
|
| 151 |
// for processing Type 2 charstrings |
| 152 |
int _t2nhints; |
| 153 |
|
| 154 |
static double double_for_error; |
| 155 |
|
| 156 |
void initialize(); |
| 157 |
|
| 158 |
inline void ensure_weight_vector(); |
| 159 |
void fetch_weight_vector(); |
| 160 |
|
| 161 |
bool roll_command(); |
| 162 |
int type2_handle_width(int, bool); |
| 163 |
|
| 164 |
inline void actp_rmoveto(int, double, double); |
| 165 |
inline void actp_rlineto(int, double, double); |
| 166 |
void actp_rrcurveto(int, double, double, double, double, double, double); |
| 167 |
void actp_rrflex(int, double, double, double, double, double, double, double, double, double, double, double, double, double); |
| 168 |
|
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
inline String CharstringInterp::error_string() const |
| 173 |
{ |
| 174 |
return error_string(_error, _error_data); |
| 175 |
} |
| 176 |
|
| 177 |
inline void CharstringInterp::push(double d) |
| 178 |
{ |
| 179 |
if (_sp < STACK_SIZE) |
| 180 |
_s[_sp++] = d; |
| 181 |
else |
| 182 |
error(errOverflow); |
| 183 |
} |
| 184 |
|
| 185 |
inline void CharstringInterp::ps_push(double d) |
| 186 |
{ |
| 187 |
if (_ps_sp < PS_STACK_SIZE) |
| 188 |
_ps_s[_ps_sp++] = d; |
| 189 |
else |
| 190 |
error(errOverflow); |
| 191 |
} |
| 192 |
|
| 193 |
inline double &CharstringInterp::vec(Vector<double> *v, int i) |
| 194 |
{ |
| 195 |
if (i < 0 || i >= v->size()) { |
| 196 |
error(errVector); |
| 197 |
return double_for_error; |
| 198 |
} else |
| 199 |
return v->at_u(i); |
| 200 |
} |
| 201 |
|
| 202 |
inline Charstring *CharstringInterp::get_subr(int n) const |
| 203 |
{ |
| 204 |
return _program ? _program->subr(n) : 0; |
| 205 |
} |
| 206 |
|
| 207 |
inline Charstring *CharstringInterp::get_gsubr(int n) const |
| 208 |
{ |
| 209 |
return _program ? _program->gsubr(n) : 0; |
| 210 |
} |
| 211 |
|
| 212 |
inline Charstring *CharstringInterp::get_xsubr(bool g, int n) const |
| 213 |
{ |
| 214 |
return _program ? _program->xsubr(g, n) : 0; |
| 215 |
} |
| 216 |
|
| 217 |
inline Charstring *CharstringInterp::get_glyph(PermString n) const |
| 218 |
{ |
| 219 |
return _program ? _program->glyph(n) : 0; |
| 220 |
} |
| 221 |
|
| 222 |
inline void CharstringInterp::ensure_weight_vector() |
| 223 |
{ |
| 224 |
if (!_weight_vector.size()) |
| 225 |
fetch_weight_vector(); |
| 226 |
} |
| 227 |
|
| 228 |
inline bool CharstringInterp::callxsubr_command(bool g) |
| 229 |
{ |
| 230 |
return (g ? callgsubr_command() : callsubr_command()); |
| 231 |
} |
| 232 |
|
| 233 |
inline bool CharstringInterp::interpret(const CharstringContext &g) |
| 234 |
{ |
| 235 |
return interpret(g.program, g.cs); |
| 236 |
} |
| 237 |
|
| 238 |
} |
| 239 |
#endif |