| 1 |
#ifndef OTFTOTFM_METRICS_HH |
| 2 |
#define OTFTOTFM_METRICS_HH |
| 3 |
#include <efont/otfgsub.hh> |
| 4 |
#include <efont/otfgpos.hh> |
| 5 |
#include "setting.hh" |
| 6 |
namespace Efont { class CharstringProgram; } |
| 7 |
class DvipsEncoding; |
| 8 |
class GlyphFilter; |
| 9 |
|
| 10 |
class Metrics { public: |
| 11 |
|
| 12 |
typedef int Code; |
| 13 |
typedef Efont::OpenType::Glyph Glyph; |
| 14 |
enum { VIRTUAL_GLYPH = 0x10000 }; |
| 15 |
|
| 16 |
typedef Efont::OpenType::Substitution Substitution; |
| 17 |
typedef Efont::OpenType::Positioning Positioning; |
| 18 |
|
| 19 |
Metrics(const Efont::CharstringProgram *, int nglyphs); |
| 20 |
~Metrics(); |
| 21 |
|
| 22 |
void check() const; |
| 23 |
|
| 24 |
Glyph boundary_glyph() const { return _boundary_glyph; } |
| 25 |
Glyph emptyslot_glyph() const { return _emptyslot_glyph; } |
| 26 |
|
| 27 |
String coding_scheme() const { return _coding_scheme; } |
| 28 |
void set_coding_scheme(const String &s) { _coding_scheme = s; } |
| 29 |
|
| 30 |
int design_units() const { return _design_units; } |
| 31 |
int units_per_em() const { return _units_per_em; } |
| 32 |
void set_design_units(int du) { _design_units = du; } |
| 33 |
|
| 34 |
int n_mapped_fonts() const { return _mapped_fonts.size();} |
| 35 |
const Efont::CharstringProgram *mapped_font(int i) const { return _mapped_fonts[i]; } |
| 36 |
const String &mapped_font_name(int i) const { return _mapped_font_names[i]; } |
| 37 |
int add_mapped_font(const Efont::CharstringProgram *, const String &); |
| 38 |
|
| 39 |
inline int encoding_size() const { return _encoding.size(); } |
| 40 |
inline bool valid_code(Code) const; |
| 41 |
inline bool nonvirtual_code(Code) const; |
| 42 |
PermString code_name(Code) const; |
| 43 |
inline const char *code_str(Code) const; |
| 44 |
|
| 45 |
inline Glyph glyph(Code code) const; |
| 46 |
inline uint32_t unicode(Code code) const; |
| 47 |
inline Code encoding(Glyph g, Code after) const; |
| 48 |
Code unicode_encoding(uint32_t uni) const; |
| 49 |
Code force_encoding(Glyph g, int lookup_source = -1); |
| 50 |
void encode(Code code, uint32_t uni, Glyph g); |
| 51 |
void encode_virtual(Code, PermString, uint32_t uni, const Vector<Setting> &, bool base_char); |
| 52 |
|
| 53 |
void add_altselector_code(Code, int altselector_type); |
| 54 |
bool altselectors() const { return _altselectors.size() > 0; } |
| 55 |
|
| 56 |
inline bool was_base_glyph(Code) const; |
| 57 |
inline Code base_code(Code) const; |
| 58 |
inline Glyph base_glyph(Code) const; |
| 59 |
bool base_glyphs(Vector<Glyph> &, int size) const; |
| 60 |
|
| 61 |
void add_ligature(Code in1, Code in2, Code out); |
| 62 |
Code pair_code(Code, Code, int lookup_source = -1); |
| 63 |
void add_kern(Code in1, Code in2, int kern); |
| 64 |
void set_kern(Code in1, Code in2, int kern); |
| 65 |
void add_single_positioning(Code, int pdx, int pdy, int adx); |
| 66 |
|
| 67 |
enum { CODE_ALL = 0x7FFFFFFF }; |
| 68 |
void remove_ligatures(Code in1, Code in2); |
| 69 |
int reencode_right_ligkern(Code old_in2, Code new_in2); |
| 70 |
|
| 71 |
int apply(const Vector<Substitution>&, bool allow_single, int lookup, const GlyphFilter&, const Vector<PermString>& glyph_names); |
| 72 |
void apply_alternates(const Vector<Substitution>&, int lookup, const GlyphFilter&, const Vector<PermString>& glyph_names); |
| 73 |
int apply(const Vector<Positioning>&); |
| 74 |
|
| 75 |
void apply_base_encoding(const String &font_name, const DvipsEncoding &, const Vector<int> &mapping); |
| 76 |
|
| 77 |
void cut_encoding(int size); |
| 78 |
void shrink_encoding(int size, const DvipsEncoding &, ErrorHandler *); |
| 79 |
void make_base(int size); |
| 80 |
|
| 81 |
bool need_virtual(int size) const; |
| 82 |
bool need_base(); |
| 83 |
enum SettingMode { SET_NONE = 0, SET_KEEP = 1, SET_INTERMEDIATE = 3 }; |
| 84 |
bool setting(Code, Vector<Setting> &, SettingMode = SET_NONE) const; |
| 85 |
int ligatures(Code in1, Vector<Code> &in2, Vector<Code> &out, Vector<int> &context) const; |
| 86 |
int kerns(Code in1, Vector<Code> &in2, Vector<int> &kern) const; |
| 87 |
int kern(Code in1, Code in2) const; |
| 88 |
|
| 89 |
void unparse() const; |
| 90 |
|
| 91 |
struct Ligature { |
| 92 |
Code in2; |
| 93 |
Code out; |
| 94 |
Ligature(Code in2_, Code out_) : in2(in2_), out(out_) { } |
| 95 |
}; |
| 96 |
|
| 97 |
struct Kern { |
| 98 |
Code in2; |
| 99 |
int kern; |
| 100 |
Kern(Code in2_, int kern_) : in2(in2_), kern(kern_) { } |
| 101 |
}; |
| 102 |
|
| 103 |
struct VirtualChar { |
| 104 |
PermString name; |
| 105 |
Vector<Setting> setting; |
| 106 |
}; |
| 107 |
|
| 108 |
struct Ligature3 { |
| 109 |
Code in1; |
| 110 |
Code in2; |
| 111 |
Code out; |
| 112 |
Ligature3(Code in1_, Code in2_, Code out_) : in1(in1_), in2(in2_), out(out_) { } |
| 113 |
String unparse(const Metrics& m) const; |
| 114 |
}; |
| 115 |
|
| 116 |
private: |
| 117 |
|
| 118 |
struct Char { |
| 119 |
Glyph glyph; |
| 120 |
Code base_code; |
| 121 |
uint32_t unicode; |
| 122 |
Vector<Ligature> ligatures; |
| 123 |
Vector<Kern> kerns; |
| 124 |
VirtualChar *virtual_char; |
| 125 |
int pdx; |
| 126 |
int pdy; |
| 127 |
int adx; |
| 128 |
Code built_in1; |
| 129 |
Code built_in2; |
| 130 |
int lookup_source; |
| 131 |
enum { BUILT = 1, INTERMEDIATE = 2, CONTEXT_ONLY = 4, LIVE = 8, |
| 132 |
BASE_LIVE = 16, BASE_REP = 32, IS_FF = 64 }; |
| 133 |
int flags; |
| 134 |
|
| 135 |
Char() : virtual_char(0) { clear(); } |
| 136 |
void clear(); |
| 137 |
void swap(Char &); |
| 138 |
bool visible() const { return glyph != 0; } |
| 139 |
bool visible_base() const { return glyph != 0 && glyph != VIRTUAL_GLYPH; } |
| 140 |
bool flag(int f) const { return (flags & f) != 0; } |
| 141 |
inline bool base_glyph() const; |
| 142 |
bool context_setting(Code in1, Code in2) const; |
| 143 |
}; |
| 144 |
|
| 145 |
Vector<Char> _encoding; |
| 146 |
mutable Vector<int> _emap; |
| 147 |
|
| 148 |
Glyph _boundary_glyph; |
| 149 |
Glyph _emptyslot_glyph; |
| 150 |
|
| 151 |
Vector<Kern> _altselectors; |
| 152 |
|
| 153 |
String _coding_scheme; |
| 154 |
int _design_units; |
| 155 |
int _units_per_em; |
| 156 |
|
| 157 |
bool _liveness_marked : 1; |
| 158 |
|
| 159 |
Vector<const Efont::CharstringProgram *> _mapped_fonts; |
| 160 |
Vector<String> _mapped_font_names; |
| 161 |
|
| 162 |
Metrics(const Metrics &); // does not exist |
| 163 |
Metrics &operator=(const Metrics &); // does not exist |
| 164 |
|
| 165 |
inline void assign_emap(Glyph, Code); |
| 166 |
Code hard_encoding(Glyph, Code) const; |
| 167 |
bool next_encoding(Vector<Code> &codes, const Vector<Glyph> &glyphs) const; |
| 168 |
|
| 169 |
Ligature *ligature_obj(Code, Code); |
| 170 |
Kern *kern_obj(Code, Code); |
| 171 |
inline void new_ligature(Code, Code, Code); |
| 172 |
inline void repoint_ligature(Code, Ligature *, Code); |
| 173 |
|
| 174 |
friend bool operator<(const Ligature3 &, const Ligature3 &); |
| 175 |
void all_ligatures(Vector<Ligature3> &) const; |
| 176 |
void mark_liveness(int size, const Vector<Ligature3> * = 0); |
| 177 |
void reencode(const Vector<Code> &); |
| 178 |
|
| 179 |
class ChangedContext; |
| 180 |
void apply_ligature(const Vector<Code> &, const Substitution *, int lookup); |
| 181 |
void apply_single(Code cin, const Substitution *s, int lookup, |
| 182 |
ChangedContext &ctx, const GlyphFilter &glyph_filter, |
| 183 |
const Vector<PermString> &glyph_names); |
| 184 |
void apply_simple_context_ligature(const Vector<Code> &codes, |
| 185 |
const Substitution *s, int lookup, ChangedContext &ctx, |
| 186 |
const GlyphFilter &glyph_filter, |
| 187 |
const Vector<PermString> &glyph_names); |
| 188 |
void apply_alternates_single(Code cin, const Substitution *s, int lookup, |
| 189 |
const GlyphFilter &glyph_filter, |
| 190 |
const Vector<PermString> &glyph_names); |
| 191 |
void apply_alternates_ligature(const Vector<Code> &codes, |
| 192 |
const Substitution *s, int lookup, |
| 193 |
const GlyphFilter &glyph_filter, |
| 194 |
const Vector<PermString> &glyph_names); |
| 195 |
|
| 196 |
void unparse(const Char *) const; |
| 197 |
|
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
inline bool |
| 202 |
Metrics::valid_code(Code code) const |
| 203 |
{ |
| 204 |
return code >= 0 && code < _encoding.size(); |
| 205 |
} |
| 206 |
|
| 207 |
inline bool |
| 208 |
Metrics::nonvirtual_code(Code code) const |
| 209 |
{ |
| 210 |
return code >= 0 && code < _encoding.size() && !_encoding[code].virtual_char; |
| 211 |
} |
| 212 |
|
| 213 |
inline Metrics::Glyph |
| 214 |
Metrics::glyph(Code code) const |
| 215 |
{ |
| 216 |
if (code < 0 || code >= _encoding.size()) |
| 217 |
return 0; |
| 218 |
else |
| 219 |
return _encoding[code].glyph; |
| 220 |
} |
| 221 |
|
| 222 |
inline uint32_t |
| 223 |
Metrics::unicode(Code code) const |
| 224 |
{ |
| 225 |
if (code < 0 || code >= _encoding.size()) |
| 226 |
return 0; |
| 227 |
else |
| 228 |
return _encoding[code].unicode; |
| 229 |
} |
| 230 |
|
| 231 |
inline Metrics::Glyph |
| 232 |
Metrics::base_glyph(Code code) const |
| 233 |
{ |
| 234 |
if (code < 0 || code >= _encoding.size() || _encoding[code].base_code < 0) |
| 235 |
return 0; |
| 236 |
else |
| 237 |
return _encoding[code].glyph; |
| 238 |
} |
| 239 |
|
| 240 |
inline Metrics::Code |
| 241 |
Metrics::base_code(Code code) const |
| 242 |
{ |
| 243 |
if (code < 0 || code >= _encoding.size()) |
| 244 |
return 0; |
| 245 |
else |
| 246 |
return _encoding[code].base_code; |
| 247 |
} |
| 248 |
|
| 249 |
inline Metrics::Code |
| 250 |
Metrics::encoding(Glyph g, Code after) const |
| 251 |
{ |
| 252 |
Code c; |
| 253 |
if (g >= 0 && g < _emap.size() && (c = _emap.at_u(g)) >= -1) |
| 254 |
return c < 0 || c >= after ? c : -1; |
| 255 |
else |
| 256 |
return hard_encoding(g, after); |
| 257 |
} |
| 258 |
|
| 259 |
inline void |
| 260 |
Metrics::assign_emap(Glyph g, Code code) |
| 261 |
{ |
| 262 |
if (g >= _emap.size()) |
| 263 |
_emap.resize(g + 1, -1); |
| 264 |
_emap[g] = (_emap[g] == -1 || _emap[g] == code ? code : -2); |
| 265 |
} |
| 266 |
|
| 267 |
inline const char * |
| 268 |
Metrics::code_str(Code code) const |
| 269 |
{ |
| 270 |
return code_name(code).c_str(); |
| 271 |
} |
| 272 |
|
| 273 |
inline bool |
| 274 |
Metrics::Char::base_glyph() const |
| 275 |
{ |
| 276 |
return glyph == VIRTUAL_GLYPH ? flags & BASE_REP : glyph != 0; |
| 277 |
} |
| 278 |
|
| 279 |
inline bool |
| 280 |
Metrics::was_base_glyph(Code code) const |
| 281 |
{ |
| 282 |
if (code < 0 || code >= _encoding.size()) |
| 283 |
return 0; |
| 284 |
else |
| 285 |
return _encoding[code].base_glyph(); |
| 286 |
} |
| 287 |
|
| 288 |
#endif |