| 1 |
// -*- related-file-name: "../../libefont/t1cs.cc" -*- |
| 2 |
#ifndef EFONT_T1CS_HH |
| 3 |
#define EFONT_T1CS_HH |
| 4 |
#include <lcdf/permstr.hh> |
| 5 |
#include <lcdf/string.hh> |
| 6 |
#include <lcdf/vector.hh> |
| 7 |
#include <lcdf/inttypes.h> |
| 8 |
namespace Efont { |
| 9 |
|
| 10 |
// Allow unknown doubles to have some `fuzz' -- so if an unknown double |
| 11 |
// is a bit off from the canonical UNKDOUBLE value, we'll still recognize |
| 12 |
// it as unknown. (Useful for interpolation.) |
| 13 |
#define UNKDOUBLE -9.79797e97 |
| 14 |
#define MIN_KNOWN_DOUBLE -9.69696e97 |
| 15 |
#define KNOWN(d) ((d) >= MIN_KNOWN_DOUBLE) |
| 16 |
|
| 17 |
class CharstringProgram; |
| 18 |
class CharstringInterp; |
| 19 |
struct CharstringContext; |
| 20 |
class MultipleMasterSpace; |
| 21 |
class Type1Encoding; |
| 22 |
|
| 23 |
class Charstring { public: |
| 24 |
|
| 25 |
Charstring() { } |
| 26 |
virtual ~Charstring(); |
| 27 |
|
| 28 |
virtual bool process(CharstringInterp&) const = 0; |
| 29 |
|
| 30 |
enum Commands { |
| 31 |
cError = 0, |
| 32 |
cHstem = 1, |
| 33 |
cVstem = 3, |
| 34 |
cVmoveto = 4, |
| 35 |
cRlineto = 5, |
| 36 |
cHlineto = 6, |
| 37 |
cVlineto = 7, |
| 38 |
cRrcurveto = 8, |
| 39 |
cClosepath = 9, |
| 40 |
cCallsubr = 10, |
| 41 |
cReturn = 11, |
| 42 |
cEscape = 12, |
| 43 |
cHsbw = 13, |
| 44 |
cEndchar = 14, |
| 45 |
cBlend = 16, |
| 46 |
cHstemhm = 18, |
| 47 |
cHintmask = 19, |
| 48 |
cCntrmask = 20, |
| 49 |
cRmoveto = 21, |
| 50 |
cHmoveto = 22, |
| 51 |
cVstemhm = 23, |
| 52 |
cRcurveline = 24, |
| 53 |
cRlinecurve = 25, |
| 54 |
cVvcurveto = 26, |
| 55 |
cHhcurveto = 27, |
| 56 |
cShortint = 28, |
| 57 |
cCallgsubr = 29, |
| 58 |
cVhcurveto = 30, |
| 59 |
cHvcurveto = 31, |
| 60 |
|
| 61 |
cEscapeDelta = 32, |
| 62 |
cDotsection = 32 + 0, |
| 63 |
cVstem3 = 32 + 1, |
| 64 |
cHstem3 = 32 + 2, |
| 65 |
cAnd = 32 + 3, |
| 66 |
cOr = 32 + 4, |
| 67 |
cNot = 32 + 5, |
| 68 |
cSeac = 32 + 6, |
| 69 |
cSbw = 32 + 7, |
| 70 |
cStore = 32 + 8, |
| 71 |
cAbs = 32 + 9, |
| 72 |
cAdd = 32 + 10, |
| 73 |
cSub = 32 + 11, |
| 74 |
cDiv = 32 + 12, |
| 75 |
cLoad = 32 + 13, |
| 76 |
cNeg = 32 + 14, |
| 77 |
cEq = 32 + 15, |
| 78 |
cCallothersubr = 32 + 16, |
| 79 |
cPop = 32 + 17, |
| 80 |
cDrop = 32 + 18, |
| 81 |
cPut = 32 + 20, |
| 82 |
cGet = 32 + 21, |
| 83 |
cIfelse = 32 + 22, |
| 84 |
cRandom = 32 + 23, |
| 85 |
cMul = 32 + 24, |
| 86 |
cSqrt = 32 + 26, |
| 87 |
cDup = 32 + 27, |
| 88 |
cExch = 32 + 28, |
| 89 |
cIndex = 32 + 29, |
| 90 |
cRoll = 32 + 30, |
| 91 |
cSetcurrentpoint = 32 + 33, |
| 92 |
cHflex = 32 + 34, |
| 93 |
cFlex = 32 + 35, |
| 94 |
cHflex1 = 32 + 36, |
| 95 |
cFlex1 = 32 + 37, |
| 96 |
|
| 97 |
cLastCommand = cFlex1 |
| 98 |
}; |
| 99 |
|
| 100 |
enum OthersubrCommands { |
| 101 |
othcFlexend = 0, |
| 102 |
othcFlexbegin = 1, |
| 103 |
othcFlexmiddle = 2, |
| 104 |
othcReplacehints = 3, |
| 105 |
othcCountercontrolpart = 12, |
| 106 |
othcCountercontrol = 13, |
| 107 |
othcMM1 = 14, |
| 108 |
othcMM2 = 15, |
| 109 |
othcMM3 = 16, |
| 110 |
othcMM4 = 17, |
| 111 |
othcMM6 = 18, |
| 112 |
othcITC_load = 19, |
| 113 |
othcITC_add = 20, |
| 114 |
othcITC_sub = 21, |
| 115 |
othcITC_mul = 22, |
| 116 |
othcITC_div = 23, |
| 117 |
othcITC_put = 24, |
| 118 |
othcITC_get = 25, |
| 119 |
othcITC_unknown = 26, |
| 120 |
othcITC_ifelse = 27, |
| 121 |
othcITC_random = 28 |
| 122 |
}; |
| 123 |
|
| 124 |
static String command_name(int); |
| 125 |
static const char* const command_names[]; |
| 126 |
|
| 127 |
static const char* const standard_encoding[256]; |
| 128 |
|
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
class Type1Charstring : public Charstring { public: |
| 133 |
|
| 134 |
Type1Charstring() { } |
| 135 |
inline Type1Charstring(const String&); // unencrypted |
| 136 |
Type1Charstring(int lenIV, const String&); // encrypted |
| 137 |
// default copy constructor |
| 138 |
// default destructor |
| 139 |
// default assignment operator |
| 140 |
|
| 141 |
inline const uint8_t* data() const; |
| 142 |
int length() const { return _s.length(); } |
| 143 |
operator String::unspecified_bool_type() const { return _s; } |
| 144 |
|
| 145 |
inline const String& data_string() const; |
| 146 |
inline String substring(int pos, int len) const; |
| 147 |
int first_caret_after(int pos) const; |
| 148 |
|
| 149 |
inline void assign(const String&); |
| 150 |
void prepend(const Type1Charstring&); |
| 151 |
void assign_substring(int pos, int len, const String&); |
| 152 |
|
| 153 |
bool process(CharstringInterp&) const; |
| 154 |
|
| 155 |
private: |
| 156 |
|
| 157 |
mutable String _s; |
| 158 |
mutable int _key; |
| 159 |
|
| 160 |
void decrypt() const; |
| 161 |
|
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
class Type2Charstring : public Charstring { public: |
| 166 |
|
| 167 |
Type2Charstring() { } |
| 168 |
inline Type2Charstring(const String&); |
| 169 |
// default copy constructor |
| 170 |
// default destructor |
| 171 |
// default assignment operator |
| 172 |
|
| 173 |
inline const uint8_t* data() const; |
| 174 |
int length() const { return _s.length(); } |
| 175 |
|
| 176 |
bool process(CharstringInterp&) const; |
| 177 |
|
| 178 |
private: |
| 179 |
|
| 180 |
String _s; |
| 181 |
|
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
struct CharstringContext { |
| 186 |
|
| 187 |
CharstringContext(const CharstringProgram* program_, const Charstring* cs_) : program(program_), cs(cs_) { } |
| 188 |
|
| 189 |
operator String::unspecified_bool_type() const { |
| 190 |
return cs != 0 ? &String::length : 0; |
| 191 |
} |
| 192 |
|
| 193 |
const CharstringProgram* program; |
| 194 |
const Charstring* cs; |
| 195 |
|
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
class CharstringProgram { public: |
| 200 |
|
| 201 |
explicit CharstringProgram(unsigned units_per_em); |
| 202 |
virtual ~CharstringProgram() { } |
| 203 |
|
| 204 |
virtual PermString font_name() const { return PermString(); } |
| 205 |
virtual void font_matrix(double[6]) const; |
| 206 |
unsigned units_per_em() const { return _units_per_em; } |
| 207 |
|
| 208 |
inline const CharstringProgram* program(int) const; |
| 209 |
virtual const CharstringProgram* child_program(int) const; |
| 210 |
bool parent_program() const { return _parent_program; } |
| 211 |
void set_parent_program(bool pp) { _parent_program = pp; } |
| 212 |
|
| 213 |
virtual int nsubrs() const { return 0; } |
| 214 |
virtual Charstring* subr(int) const { return 0; } |
| 215 |
virtual int subr_bias() const { return 0; } |
| 216 |
|
| 217 |
virtual int ngsubrs() const { return 0; } |
| 218 |
virtual Charstring* gsubr(int) const { return 0; } |
| 219 |
virtual int gsubr_bias() const { return 0; } |
| 220 |
|
| 221 |
inline int nxsubrs(bool g) const; |
| 222 |
inline Charstring* xsubr(bool g, int) const; |
| 223 |
inline int xsubr_bias(bool g) const; |
| 224 |
|
| 225 |
virtual int nglyphs() const { return 0; } |
| 226 |
virtual PermString glyph_name(int) const { return PermString(); } |
| 227 |
virtual void glyph_names(Vector<PermString>&) const; |
| 228 |
virtual Charstring* glyph(int) const { return 0; } |
| 229 |
virtual Charstring* glyph(PermString) const { return 0; } |
| 230 |
|
| 231 |
inline CharstringContext glyph_context(int) const; |
| 232 |
inline CharstringContext glyph_context(PermString) const; |
| 233 |
|
| 234 |
virtual bool is_mm() const { return mmspace() != 0; } |
| 235 |
virtual MultipleMasterSpace* mmspace() const { return 0; } |
| 236 |
enum VectorType { VEC_WEIGHT = 0, VEC_NORM_DESIGN = 1, VEC_DESIGN = 2 }; |
| 237 |
virtual Vector<double>* mm_vector(VectorType, bool writable) const; |
| 238 |
|
| 239 |
virtual Type1Encoding* type1_encoding() const { return 0; } |
| 240 |
|
| 241 |
virtual double global_width_x(bool is_nominal) const; |
| 242 |
|
| 243 |
private: |
| 244 |
|
| 245 |
bool _parent_program; |
| 246 |
uint16_t _units_per_em; |
| 247 |
|
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
enum Type1Defs { |
| 252 |
t1Warmup_ee = 4, |
| 253 |
t1R_ee = 55665, |
| 254 |
t1R_cs = 4330, |
| 255 |
t1C1 = 52845, |
| 256 |
t1C2 = 22719 |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
inline Type1Charstring::Type1Charstring(const String& s) |
| 261 |
: Charstring(), _s(s), _key(-1) |
| 262 |
{ |
| 263 |
} |
| 264 |
|
| 265 |
inline void Type1Charstring::assign(const String& s) |
| 266 |
{ |
| 267 |
_s = s; |
| 268 |
_key = -1; |
| 269 |
} |
| 270 |
|
| 271 |
inline const uint8_t* Type1Charstring::data() const |
| 272 |
{ |
| 273 |
if (_key >= 0) |
| 274 |
decrypt(); |
| 275 |
return reinterpret_cast<const uint8_t*>(_s.data()); |
| 276 |
} |
| 277 |
|
| 278 |
inline const String& Type1Charstring::data_string() const |
| 279 |
{ |
| 280 |
if (_key >= 0) |
| 281 |
decrypt(); |
| 282 |
return _s; |
| 283 |
} |
| 284 |
|
| 285 |
inline String Type1Charstring::substring(int pos, int len) const |
| 286 |
{ |
| 287 |
if (_key >= 0) |
| 288 |
decrypt(); |
| 289 |
return _s.substring(pos, len); |
| 290 |
} |
| 291 |
|
| 292 |
inline bool operator==(const Type1Charstring& a, const Type1Charstring& b) |
| 293 |
{ |
| 294 |
return a.data_string() == b.data_string(); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
inline Type2Charstring::Type2Charstring(const String& s) |
| 299 |
: _s(s) |
| 300 |
{ |
| 301 |
} |
| 302 |
|
| 303 |
inline const uint8_t* Type2Charstring::data() const |
| 304 |
{ |
| 305 |
return reinterpret_cast<const uint8_t*>(_s.data()); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
inline int CharstringProgram::nxsubrs(bool g) const |
| 310 |
{ |
| 311 |
return (g ? ngsubrs() : nsubrs()); |
| 312 |
} |
| 313 |
|
| 314 |
inline Charstring* CharstringProgram::xsubr(bool g, int i) const |
| 315 |
{ |
| 316 |
return (g ? gsubr(i) : subr(i)); |
| 317 |
} |
| 318 |
|
| 319 |
inline int CharstringProgram::xsubr_bias(bool g) const |
| 320 |
{ |
| 321 |
return (g ? gsubr_bias() : subr_bias()); |
| 322 |
} |
| 323 |
|
| 324 |
inline const CharstringProgram* CharstringProgram::program(int gi) const |
| 325 |
{ |
| 326 |
return (_parent_program ? child_program(gi) : this); |
| 327 |
} |
| 328 |
|
| 329 |
inline CharstringContext CharstringProgram::glyph_context(int gi) const |
| 330 |
{ |
| 331 |
if (!_parent_program) |
| 332 |
return CharstringContext(this, glyph(gi)); |
| 333 |
else if (const CharstringProgram* p = child_program(gi)) |
| 334 |
return CharstringContext(p, p->glyph(gi)); |
| 335 |
else |
| 336 |
return CharstringContext(0, 0); |
| 337 |
} |
| 338 |
|
| 339 |
inline CharstringContext CharstringProgram::glyph_context(PermString gn) const |
| 340 |
{ |
| 341 |
return CharstringContext(this, glyph(gn)); |
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
#endif |