| 1 |
/* secondary.{cc,hh} -- code for generating fake glyphs |
| 2 |
* |
| 3 |
* Copyright (c) 2003-2023 Eddie Kohler |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the Free |
| 7 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 8 |
* any later version. This program is distributed in the hope that it will be |
| 9 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 11 |
* Public License for more details. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <config.h> |
| 15 |
#include "secondary.hh" |
| 16 |
#include "metrics.hh" |
| 17 |
#include "automatic.hh" |
| 18 |
#include "otftotfm.hh" |
| 19 |
#include "util.hh" |
| 20 |
#include <efont/t1bounds.hh> |
| 21 |
#include <efont/t1font.hh> |
| 22 |
#include <efont/t1rw.hh> |
| 23 |
#include <efont/otfname.hh> |
| 24 |
#include <efont/otfos2.hh> |
| 25 |
#include <efont/otfpost.hh> |
| 26 |
#include <efont/t1csgen.hh> |
| 27 |
#include <efont/t1unparser.hh> |
| 28 |
#include <efont/ttfcs.hh> |
| 29 |
#include <lcdf/straccum.hh> |
| 30 |
#include <stdarg.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <limits.h> |
| 33 |
#include <algorithm> |
| 34 |
|
| 35 |
enum { |
| 36 |
U_EXCLAMDOWN = 0x00A1, // U+00A1 INVERTED EXCLAMATION MARK |
| 37 |
U_DEGREE = 0x00B0, // U+00B0 DEGREE SIGN |
| 38 |
U_QUESTIONDOWN = 0x00BF, // U+00BF INVERTED QUESTION MARK |
| 39 |
U_IJ = 0x0132, // U+0132 LATIN CAPITAL LIGATURE IJ |
| 40 |
U_ij = 0x0133, // U+0133 LATIN SMALL LIGATURE IJ |
| 41 |
U_DOTLESSJ = 0x0237, // U+0237 LATIN SMALL LETTER DOTLESS J |
| 42 |
U_RINGABOVE = 0x02DA, // U+02DA RING ABOVE |
| 43 |
U_COMBININGRINGABOVE = 0x030A, // U+030A COMBINING RING ABOVE |
| 44 |
U_CWM = 0x200C, // U+200C ZERO WIDTH NON-JOINER |
| 45 |
U_ENDASH = 0x2013, // U+2013 EN DASH |
| 46 |
U_PERTENTHOUSAND = 0x2031, // U+2031 PER TEN THOUSAND SIGN |
| 47 |
U_INTERROBANG = 0x203D, // U+203D INTERROBANG |
| 48 |
U_FRACTION = 0x2044, // U+2044 FRACTION SLASH |
| 49 |
U_CENTIGRADE = 0x2103, // U+2103 DEGREE CELSIUS |
| 50 |
U_ASTERISKMATH = 0x2217, // U+2217 ASTERISK OPERATOR |
| 51 |
U_BARDBL = 0x2225, // U+2225 PARALLEL TO |
| 52 |
U_VISIBLESPACE = 0x2423, // U+2423 OPEN BOX |
| 53 |
U_DBLBRACKETLEFT = 0x27E6, // U+27E6 MATHEMATICAL LEFT WHITE SQUARE BRACKET |
| 54 |
U_DBLBRACKETRIGHT = 0x27E7, // U+27E7 MATHEMATICAL RIGHT WHITE SQUARE BRACKET |
| 55 |
U_INTERROBANGDOWN = 0x2E18, // U+2E18 INVERTED INTERROBANG |
| 56 |
U_ALTSELECTOR = 0xD802, // invalid Unicode |
| 57 |
U_CAPITALCWM = 0xD809, // invalid Unicode |
| 58 |
U_ASCENDERCWM = 0xD80A, // invalid Unicode |
| 59 |
U_TWELVEUDASH = 0xD80C, // invalid Unicode |
| 60 |
U_RINGFITTED = 0xD80D, // invalid Unicode |
| 61 |
|
| 62 |
// BEGIN BACKWARDS COMPATIBILITY -- newer texglyphlist.txt does not include |
| 63 |
// these code points |
| 64 |
U_SS = 0xD800, // invalid Unicode |
| 65 |
U_SSSMALL = 0xD803, // invalid Unicode |
| 66 |
U_FFSMALL = 0xD804, // invalid Unicode |
| 67 |
U_FISMALL = 0xD805, // invalid Unicode |
| 68 |
U_FLSMALL = 0xD806, // invalid Unicode |
| 69 |
U_FFISMALL = 0xD807, // invalid Unicode |
| 70 |
U_FFLSMALL = 0xD808, // invalid Unicode |
| 71 |
// END BACKWARDS COMPATIBILITY |
| 72 |
|
| 73 |
U_VS1 = 0xFE00, |
| 74 |
U_VS16 = 0xFE0F, |
| 75 |
U_VS17 = 0xE0100, |
| 76 |
U_VS256 = 0xE01FF, |
| 77 |
U_DOTLESSJ_2 = 0xF6BE, |
| 78 |
U_THREEQUARTERSEMDASH = 0xF6DE, |
| 79 |
U_FSMALL = 0xF766, |
| 80 |
U_ISMALL = 0xF769, |
| 81 |
U_LSMALL = 0xF76C, |
| 82 |
U_SSMALL = 0xF773, |
| 83 |
U_MATHDOTLESSJ = 0x1D6A5 // U+1D6A5 MATHEMATICAL ITALIC SMALL DOTLESS J |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
FontInfo::FontInfo(const Efont::OpenType::Font *otf_, ErrorHandler *errh) |
| 88 |
: otf(otf_), cmap(0), cff_file(0), cff(0), post(0), name(0), _nglyphs(-1), |
| 89 |
_got_glyph_names(false), _ttb_program(0), _override_is_fixed_pitch(false), |
| 90 |
_override_italic_angle(false), _override_x_height(x_height_auto) |
| 91 |
{ |
| 92 |
cmap = new Efont::OpenType::Cmap(otf->table("cmap"), errh); |
| 93 |
assert(cmap->ok()); |
| 94 |
|
| 95 |
if (String cff_string = otf->table("CFF")) { |
| 96 |
cff_file = new Efont::Cff(cff_string, otf->units_per_em(), errh); |
| 97 |
if (!cff_file->ok()) |
| 98 |
return; |
| 99 |
Efont::Cff::FontParent *fp = cff_file->font(PermString(), errh); |
| 100 |
if (!fp || !fp->ok()) |
| 101 |
return; |
| 102 |
if (!(cff = dynamic_cast<Efont::Cff::Font *>(fp))) { |
| 103 |
errh->error("CID-keyed fonts not supported"); |
| 104 |
return; |
| 105 |
} |
| 106 |
_nglyphs = cff->nglyphs(); |
| 107 |
} |
| 108 |
|
| 109 |
if (!cff) { |
| 110 |
post = new Efont::OpenType::Post(otf->table("post"), errh); |
| 111 |
// read number of glyphs from 'maxp' -- should probably be elsewhere |
| 112 |
if (Efont::OpenType::Data maxp = otf->table("maxp")) |
| 113 |
if (maxp.length() >= 6) |
| 114 |
_nglyphs = maxp.u16(4); |
| 115 |
if (_nglyphs < 0 && post->ok()) |
| 116 |
_nglyphs = post->nglyphs(); |
| 117 |
} |
| 118 |
|
| 119 |
name = new Efont::OpenType::Name(otf->table("name"), errh); |
| 120 |
} |
| 121 |
|
| 122 |
FontInfo::~FontInfo() |
| 123 |
{ |
| 124 |
delete cmap; |
| 125 |
delete cff_file; |
| 126 |
delete post; |
| 127 |
delete name; |
| 128 |
delete _ttb_program; |
| 129 |
} |
| 130 |
|
| 131 |
bool |
| 132 |
FontInfo::ok() const |
| 133 |
{ |
| 134 |
if (cff) |
| 135 |
return cmap->ok() && cff->ok(); |
| 136 |
else |
| 137 |
return post && post->ok() && name && name->ok(); |
| 138 |
} |
| 139 |
|
| 140 |
bool |
| 141 |
FontInfo::glyph_names(Vector<PermString> &glyph_names) const |
| 142 |
{ |
| 143 |
program()->glyph_names(glyph_names); |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
int |
| 148 |
FontInfo::glyphid(PermString name) const |
| 149 |
{ |
| 150 |
if (cff) |
| 151 |
return cff->glyphid(name); |
| 152 |
else { |
| 153 |
if (!_got_glyph_names) { |
| 154 |
glyph_names(_glyph_names); |
| 155 |
_got_glyph_names = true; |
| 156 |
} |
| 157 |
PermString *found = std::find(_glyph_names.begin(), _glyph_names.end(), name); |
| 158 |
if (found == _glyph_names.end()) |
| 159 |
return 0; |
| 160 |
return found - _glyph_names.begin(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
String |
| 165 |
FontInfo::family_name() const |
| 166 |
{ |
| 167 |
if (cff) |
| 168 |
return cff->dict_string(Efont::Cff::oFamilyName); |
| 169 |
else |
| 170 |
return name->english_name(Efont::OpenType::Name::N_FAMILY); |
| 171 |
} |
| 172 |
|
| 173 |
String |
| 174 |
FontInfo::postscript_name() const |
| 175 |
{ |
| 176 |
if (cff) |
| 177 |
return cff->font_name(); |
| 178 |
else |
| 179 |
return name->english_name(Efont::OpenType::Name::N_POSTSCRIPT); |
| 180 |
} |
| 181 |
|
| 182 |
const Efont::CharstringProgram * |
| 183 |
FontInfo::program() const |
| 184 |
{ |
| 185 |
if (cff) |
| 186 |
return cff; |
| 187 |
else { |
| 188 |
if (!_ttb_program) |
| 189 |
_ttb_program = new Efont::TrueTypeBoundsCharstringProgram(otf); |
| 190 |
return _ttb_program; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
bool |
| 195 |
FontInfo::is_fixed_pitch() const |
| 196 |
{ |
| 197 |
if (_override_is_fixed_pitch) |
| 198 |
return _is_fixed_pitch; |
| 199 |
else if (cff) { |
| 200 |
double d; |
| 201 |
return (cff->dict_value(Efont::Cff::oIsFixedPitch, &d) && d); |
| 202 |
} else |
| 203 |
return post->is_fixed_pitch(); |
| 204 |
} |
| 205 |
|
| 206 |
double |
| 207 |
FontInfo::italic_angle() const |
| 208 |
{ |
| 209 |
if (_override_italic_angle) |
| 210 |
return _italic_angle; |
| 211 |
else if (cff) { |
| 212 |
double d; |
| 213 |
(void) cff->dict_value(Efont::Cff::oItalicAngle, &d); |
| 214 |
return d; |
| 215 |
} else |
| 216 |
return post->italic_angle(); |
| 217 |
} |
| 218 |
|
| 219 |
double FontInfo::x_height(const Transform& font_xform) const { |
| 220 |
if (_override_x_height == x_height_explicit) |
| 221 |
return _x_height; |
| 222 |
double x1 = -1, x2 = -1; |
| 223 |
if (_override_x_height != x_height_os2) |
| 224 |
// XXX what if 'x', 'm', 'z' were subject to substitution? |
| 225 |
x1 = char_one_bound(*this, font_xform, 3, false, units_per_em(), |
| 226 |
(int) 'x', (int) 'm', (int) 'z', 0); |
| 227 |
if (_override_x_height != x_height_x) |
| 228 |
try { |
| 229 |
Efont::OpenType::Os2 os2(otf->table("OS/2")); |
| 230 |
x2 = (Point(0, os2.x_height()) * font_xform).y; |
| 231 |
} catch (Efont::OpenType::Bounds) { |
| 232 |
} |
| 233 |
static bool warned = false; |
| 234 |
if (_override_x_height == x_height_auto |
| 235 |
&& x1 >= 0 |
| 236 |
&& x2 >= 0 |
| 237 |
&& fabs(x1 - x2) > units_per_em() / 50.) { |
| 238 |
if (!warned) { |
| 239 |
ErrorHandler* errh = ErrorHandler::default_handler(); |
| 240 |
errh->warning("font x-height and height of %<x%> differ by %d%%", (int) (fabs(x1 - x2) * 100 / units_per_em())); |
| 241 |
errh->message("(The height of %<x%> is usually more reliable than the x-height, so I%,m\nusing that. Or try `--x-height=font` or `--x-height=x`.)\n"); |
| 242 |
warned = true; |
| 243 |
} |
| 244 |
return x1; |
| 245 |
} else |
| 246 |
return x2 >= 0 ? x2 : x1; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
/* */ |
| 251 |
|
| 252 |
Secondary::~Secondary() |
| 253 |
{ |
| 254 |
} |
| 255 |
|
| 256 |
bool |
| 257 |
Secondary::encode_uni(int code, PermString name, |
| 258 |
const uint32_t* uni_begin, const uint32_t* uni_end, |
| 259 |
Metrics &metrics, ErrorHandler *errh) |
| 260 |
{ |
| 261 |
uint32_t uni = 0; |
| 262 |
if (uni_begin + 1 == uni_end) |
| 263 |
uni = *uni_begin; |
| 264 |
|
| 265 |
SettingSet set(this, metrics); |
| 266 |
int max_s = 0; |
| 267 |
while (uni_begin != uni_end) { |
| 268 |
int s = setting(*uni_begin, set, errh); |
| 269 |
if (s == 0) |
| 270 |
return false; |
| 271 |
max_s = (max_s > s ? max_s : s); |
| 272 |
++uni_begin; |
| 273 |
set.checkpoint(); |
| 274 |
} |
| 275 |
|
| 276 |
if (uni == U_ALTSELECTOR |
| 277 |
|| (uni >= U_VS1 && uni <= U_VS16) |
| 278 |
|| (uni >= U_VS17 && uni <= U_VS256)) { |
| 279 |
int selector = 0; |
| 280 |
if (uni >= U_VS1 && uni <= U_VS16) |
| 281 |
selector = uni - U_VS1 + 1; |
| 282 |
else if (uni >= U_VS17 && uni <= U_VS256) |
| 283 |
selector = uni - U_VS17 + 17; |
| 284 |
metrics.add_altselector_code(code, selector); |
| 285 |
name = selector ? permprintf("<vs%d>", selector) : PermString("<altselector>"); |
| 286 |
} |
| 287 |
|
| 288 |
metrics.encode_virtual(code, name, 0, set.settings(), max_s > 1); |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
T1Secondary::T1Secondary(const FontInfo &finfo, const String &font_name, |
| 293 |
const String &otf_file_name) |
| 294 |
: Secondary(finfo), _font_name(font_name), _otf_file_name(otf_file_name), |
| 295 |
_units_per_em(finfo.units_per_em()), |
| 296 |
_xheight((int) ceil(finfo.x_height(Transform()))), |
| 297 |
_spacewidth(_units_per_em) |
| 298 |
{ |
| 299 |
double bounds[4], width; |
| 300 |
if (char_bounds(bounds, width, finfo, Transform(), ' ')) |
| 301 |
_spacewidth = (int) ceil(width); |
| 302 |
} |
| 303 |
|
| 304 |
int |
| 305 |
Secondary::setting(uint32_t uni, SettingSet& set, ErrorHandler *errh) |
| 306 |
{ |
| 307 |
if (_next) |
| 308 |
return _next->setting(uni, set, errh); |
| 309 |
else |
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
SettingSet& SettingSet::show(int uni) { |
| 314 |
if (!ok_) |
| 315 |
return *this; |
| 316 |
int code = metrics_.unicode_encoding(uni); |
| 317 |
if (code < 0) { |
| 318 |
Glyph glyph = s_->_finfo.cmap->map_uni(uni); |
| 319 |
if (glyph != 0) |
| 320 |
code = metrics_.force_encoding(glyph); |
| 321 |
} |
| 322 |
if (code < 0) { |
| 323 |
ok_ = false; |
| 324 |
while (v_.size() > original_size_) |
| 325 |
v_.pop_back(); |
| 326 |
} else { |
| 327 |
if (!v_.empty() |
| 328 |
&& v_.back().op == Setting::SHOW |
| 329 |
&& kern_type_) |
| 330 |
v_.push_back(Setting(kern_type_)); |
| 331 |
v_.push_back(Setting(Setting::SHOW, code, metrics_.base_glyph(code))); |
| 332 |
} |
| 333 |
return *this; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
static String dotlessj_file_name; |
| 338 |
|
| 339 |
static String |
| 340 |
dotlessj_dvips_include(const String &, const FontInfo &, ErrorHandler *) |
| 341 |
{ |
| 342 |
return "<" + pathname_filename(dotlessj_file_name); |
| 343 |
} |
| 344 |
|
| 345 |
int |
| 346 |
T1Secondary::dotlessj_font(Metrics &metrics, ErrorHandler *errh, Glyph &dj_glyph) |
| 347 |
{ |
| 348 |
if (!_font_name || !_finfo.otf || !_finfo.cff) |
| 349 |
return -1; |
| 350 |
|
| 351 |
String dj_name; |
| 352 |
bool install_metrics; |
| 353 |
// XXX make sure dotlessj is for the main font? |
| 354 |
if ((dj_name = installed_metrics_font_name(_font_name, "dotlessj"))) |
| 355 |
install_metrics = false; |
| 356 |
else { |
| 357 |
dj_name = suffix_font_name(_font_name, "--lcdfj"); |
| 358 |
install_metrics = true; |
| 359 |
} |
| 360 |
|
| 361 |
// is dotlessj already mapped? |
| 362 |
for (int i = 0; i < metrics.n_mapped_fonts(); i++) |
| 363 |
if (metrics.mapped_font_name(i) == dj_name) |
| 364 |
return i; |
| 365 |
|
| 366 |
if (String filename = installed_type1_dotlessj(_otf_file_name, _finfo.cff->font_name(), (output_flags & G_DOTLESSJ), errh)) { |
| 367 |
|
| 368 |
// check for special case: "\0" means the font's "j" is already |
| 369 |
// dotless |
| 370 |
if (filename == String("\0", 1)) |
| 371 |
return J_NODOT; |
| 372 |
|
| 373 |
// open dotless-j font file |
| 374 |
FILE *f = fopen(filename.c_str(), "rb"); |
| 375 |
if (!f) { |
| 376 |
errh->error("%s: %s", filename.c_str(), strerror(errno)); |
| 377 |
return -1; |
| 378 |
} |
| 379 |
|
| 380 |
// read font |
| 381 |
Efont::Type1Reader *reader; |
| 382 |
int c = getc(f); |
| 383 |
ungetc(c, f); |
| 384 |
if (c == 128) |
| 385 |
reader = new Efont::Type1PFBReader(f); |
| 386 |
else |
| 387 |
reader = new Efont::Type1PFAReader(f); |
| 388 |
Efont::Type1Font *font = new Efont::Type1Font(*reader); |
| 389 |
delete reader; |
| 390 |
|
| 391 |
if (!font->ok()) { |
| 392 |
errh->error("%s: no glyphs in dotless-J font", filename.c_str()); |
| 393 |
delete font; |
| 394 |
return -1; |
| 395 |
} |
| 396 |
|
| 397 |
// find dotless-J character |
| 398 |
Vector<PermString> glyph_names; |
| 399 |
font->glyph_names(glyph_names); |
| 400 |
Vector<PermString>::iterator g = std::find(glyph_names.begin(), glyph_names.end(), "uni0237"); |
| 401 |
if (g == glyph_names.end()) { |
| 402 |
errh->error("%s: dotless-J font has no %<uni0237%> glyph", filename.c_str()); |
| 403 |
delete font; |
| 404 |
return -1; |
| 405 |
} |
| 406 |
dj_glyph = g - glyph_names.begin(); |
| 407 |
|
| 408 |
// create metrics for dotless-J |
| 409 |
if (install_metrics) { |
| 410 |
Metrics dj_metrics(font, 256); |
| 411 |
dj_metrics.encode('j', U_DOTLESSJ, dj_glyph); |
| 412 |
::dotlessj_file_name = filename; |
| 413 |
output_metrics(dj_metrics, font->font_name(), -1, _finfo, String(), String(), dj_name, dotlessj_dvips_include, errh); |
| 414 |
} else if (verbose) |
| 415 |
errh->message("using %<%s%> for dotless-J font metrics", dj_name.c_str()); |
| 416 |
|
| 417 |
// add font to metrics |
| 418 |
return metrics.add_mapped_font(font, dj_name); |
| 419 |
|
| 420 |
} else |
| 421 |
return -1; |
| 422 |
} |
| 423 |
|
| 424 |
int |
| 425 |
T1Secondary::setting(uint32_t uni, SettingSet& set, ErrorHandler *errh) |
| 426 |
{ |
| 427 |
Transform xform; |
| 428 |
extern int letterspace; |
| 429 |
|
| 430 |
if (set.show(uni).check()) |
| 431 |
return 1; |
| 432 |
|
| 433 |
switch (uni) { |
| 434 |
|
| 435 |
case U_CWM: |
| 436 |
case U_ALTSELECTOR: |
| 437 |
set.push_back(Setting::RULE, 0, _xheight); |
| 438 |
return 1; |
| 439 |
|
| 440 |
case U_CAPITALCWM: |
| 441 |
set.push_back(Setting::RULE, 0, font_cap_height(_finfo, xform)); |
| 442 |
return 1; |
| 443 |
|
| 444 |
case U_ASCENDERCWM: |
| 445 |
set.push_back(Setting::RULE, 0, font_ascender(_finfo, xform)); |
| 446 |
return 1; |
| 447 |
|
| 448 |
case U_VISIBLESPACE: { |
| 449 |
int sb = (int) (0.050 * _units_per_em), h = (int) (0.150 * _units_per_em), |
| 450 |
lw = (int) (0.040 * _units_per_em); |
| 451 |
set.move(sb, -h); |
| 452 |
set.push_back(Setting::RULE, lw, h); |
| 453 |
set.push_back(Setting::RULE, _spacewidth, lw); |
| 454 |
set.push_back(Setting::RULE, lw, h); |
| 455 |
set.move(sb, h); |
| 456 |
return 2; |
| 457 |
} |
| 458 |
|
| 459 |
case U_SS: |
| 460 |
if (set.show('S').show('S').check()) |
| 461 |
return 1; |
| 462 |
break; |
| 463 |
|
| 464 |
case U_SSSMALL: |
| 465 |
if (set.show(U_SSMALL).show(U_SSMALL).check() |
| 466 |
|| set.show('s').show('s').check()) |
| 467 |
return 1; |
| 468 |
break; |
| 469 |
|
| 470 |
case U_FFSMALL: |
| 471 |
if (set.show(U_FSMALL).show(U_FSMALL).check() |
| 472 |
|| set.show('f').show('f').check()) |
| 473 |
return 1; |
| 474 |
break; |
| 475 |
|
| 476 |
case U_FISMALL: |
| 477 |
if (set.show(U_FSMALL).show(U_ISMALL).check() |
| 478 |
|| set.show('f').show('i').check()) |
| 479 |
return 1; |
| 480 |
break; |
| 481 |
|
| 482 |
case U_FLSMALL: |
| 483 |
if (set.show(U_FSMALL).show(U_LSMALL).check() |
| 484 |
|| set.show('f').show('l').check()) |
| 485 |
return 1; |
| 486 |
break; |
| 487 |
|
| 488 |
case U_FFISMALL: |
| 489 |
if (set.show(U_FSMALL).show(U_FSMALL).show(U_ISMALL).check() |
| 490 |
|| set.show('f').show('f').show('i').check()) |
| 491 |
return 1; |
| 492 |
break; |
| 493 |
|
| 494 |
case U_FFLSMALL: |
| 495 |
if (set.show(U_FSMALL).show(U_FSMALL).show(U_LSMALL).check() |
| 496 |
|| set.show('f').show('f').show('l').check()) |
| 497 |
return 1; |
| 498 |
break; |
| 499 |
|
| 500 |
case U_IJ: |
| 501 |
if (set.show('I').show('J').check()) |
| 502 |
return 1; |
| 503 |
break; |
| 504 |
|
| 505 |
case U_ij: |
| 506 |
if (set.show('i').show('j').check()) |
| 507 |
return 1; |
| 508 |
break; |
| 509 |
|
| 510 |
case U_DOTLESSJ: |
| 511 |
case U_DOTLESSJ_2: |
| 512 |
case U_MATHDOTLESSJ: { |
| 513 |
Glyph dj_glyph; |
| 514 |
int which = dotlessj_font(set.metrics(), errh, dj_glyph); |
| 515 |
if (which >= 0) { |
| 516 |
set.push_back(Setting::FONT, which); |
| 517 |
set.push_back(Setting::SHOW, 'j', dj_glyph); |
| 518 |
return 2; |
| 519 |
} else if (which == J_NODOT && set.show('j').check()) |
| 520 |
return 1; |
| 521 |
break; |
| 522 |
} |
| 523 |
|
| 524 |
case U_DBLBRACKETLEFT: |
| 525 |
if (set.show('[').check()) { |
| 526 |
if (!_finfo.is_fixed_pitch()) { |
| 527 |
double d = char_one_bound(_finfo, xform, 4, true, 0, '[', 0); |
| 528 |
set.move((int) (-0.666 * d - letterspace)); |
| 529 |
} |
| 530 |
set.show('['); |
| 531 |
return 1; |
| 532 |
} |
| 533 |
break; |
| 534 |
|
| 535 |
case U_DBLBRACKETRIGHT: |
| 536 |
if (set.show(']').check()) { |
| 537 |
if (!_finfo.is_fixed_pitch()) { |
| 538 |
double d = char_one_bound(_finfo, xform, 4, true, 0, ']', 0); |
| 539 |
set.move((int) (-0.666 * d - letterspace)); |
| 540 |
} |
| 541 |
set.show(']'); |
| 542 |
return 1; |
| 543 |
} |
| 544 |
break; |
| 545 |
|
| 546 |
case U_BARDBL: |
| 547 |
if (set.show('|').check()) { |
| 548 |
if (!_finfo.is_fixed_pitch()) { |
| 549 |
double d = char_one_bound(_finfo, Transform(), 4, true, 0, '|', 0); |
| 550 |
set.move((int) (-0.333 * d - letterspace)); |
| 551 |
} |
| 552 |
set.show('|'); |
| 553 |
return 1; |
| 554 |
} |
| 555 |
break; |
| 556 |
|
| 557 |
case U_ASTERISKMATH: { |
| 558 |
double bounds[5]; |
| 559 |
double dropdown = 0; |
| 560 |
if (char_bounds(bounds, bounds[4], _finfo, xform, '*')) |
| 561 |
dropdown += std::max(bounds[3], 0.) + std::min(bounds[1], 0.); |
| 562 |
if (char_bounds(bounds, bounds[4], _finfo, xform, '(')) |
| 563 |
dropdown -= std::max(bounds[3], 0.) + std::min(bounds[1], 0.); |
| 564 |
int dy = (int) (-dropdown / 2); |
| 565 |
if (set.move(0, dy).show('*').move(0, -dy).check()) |
| 566 |
return 1; |
| 567 |
break; |
| 568 |
} |
| 569 |
|
| 570 |
case U_TWELVEUDASH: |
| 571 |
if (set.show(U_ENDASH).check()) { |
| 572 |
if (!_finfo.is_fixed_pitch()) { |
| 573 |
double d = char_one_bound(_finfo, xform, 4, true, 0, U_ENDASH, 0); |
| 574 |
set.move((int) (_units_per_em * 0.667 - 2 * d - letterspace)); |
| 575 |
} |
| 576 |
set.show(U_ENDASH); |
| 577 |
return 1; |
| 578 |
} |
| 579 |
break; |
| 580 |
|
| 581 |
case U_THREEQUARTERSEMDASH: |
| 582 |
if (set.show(U_ENDASH).check()) { |
| 583 |
if (!_finfo.is_fixed_pitch()) { |
| 584 |
double d = char_one_bound(_finfo, xform, 4, true, 0, U_ENDASH, 0); |
| 585 |
set.move((int) (_units_per_em * 0.750 - 2 * d - letterspace)); |
| 586 |
} |
| 587 |
set.show(U_ENDASH); |
| 588 |
return 1; |
| 589 |
} |
| 590 |
break; |
| 591 |
|
| 592 |
case U_CENTIGRADE: |
| 593 |
// TODO: set italic correction to that of a 'C' |
| 594 |
if (set.show(U_DEGREE).kernx(true).show('C').kernx(false).check()) |
| 595 |
return 1; |
| 596 |
break; |
| 597 |
|
| 598 |
case U_INTERROBANG: { |
| 599 |
double exclam_offset = |
| 600 |
(char_one_bound(_finfo, xform, 4, true, 0, '?', 0) |
| 601 |
- char_one_bound(_finfo, xform, 4, true, 0, '!', 0)) * 0.5 |
| 602 |
+ 0.050 * _units_per_em; |
| 603 |
if (set.push_back(Setting::PUSH).move((int) exclam_offset) |
| 604 |
.show('!').push_back(Setting::POP).show('?').check()) |
| 605 |
return 1; |
| 606 |
break; |
| 607 |
} |
| 608 |
|
| 609 |
case U_INTERROBANGDOWN: { |
| 610 |
double exclam_offset = |
| 611 |
(char_one_bound(_finfo, xform, 4, true, 0, U_QUESTIONDOWN, 0) |
| 612 |
- char_one_bound(_finfo, xform, 4, true, 0, U_EXCLAMDOWN, 0)) * 0.5 |
| 613 |
+ 0.050 * _units_per_em; |
| 614 |
if (set.push_back(Setting::PUSH).move((int) exclam_offset) |
| 615 |
.show(U_EXCLAMDOWN).push_back(Setting::POP).show(U_QUESTIONDOWN).check()) |
| 616 |
return 1; |
| 617 |
break; |
| 618 |
} |
| 619 |
|
| 620 |
case U_PERTENTHOUSAND: |
| 621 |
if (set.show(0xF661).kernx(true).show(U_FRACTION) |
| 622 |
.show(0xF655).show(0xF655).show(0xF655).kernx(false).check()) |
| 623 |
return 1; |
| 624 |
break; |
| 625 |
|
| 626 |
case U_RINGFITTED: { |
| 627 |
int A_width = char_one_bound(_finfo, xform, 4, true, -_units_per_em, 'A', 0); |
| 628 |
uint32_t ring_char = U_RINGABOVE; |
| 629 |
int ring_width = char_one_bound(_finfo, xform, 4, true, -_units_per_em, ring_char, 0); |
| 630 |
if (ring_width <= -_units_per_em) { |
| 631 |
ring_char = U_COMBININGRINGABOVE; |
| 632 |
ring_width = char_one_bound(_finfo, xform, 4, true, -_units_per_em, ring_char, 0); |
| 633 |
} |
| 634 |
if (A_width > -_units_per_em && ring_width > -_units_per_em) { |
| 635 |
int offset = (A_width - ring_width) / 2; |
| 636 |
if (set.move(offset).show(ring_char).move(A_width - ring_width - offset).check()) { |
| 637 |
return 1; |
| 638 |
} |
| 639 |
} |
| 640 |
break; |
| 641 |
} |
| 642 |
|
| 643 |
} |
| 644 |
|
| 645 |
// variant selectors get the same setting as ALTSELECTOR |
| 646 |
if ((uni >= U_VS1 && uni <= U_VS16) || (uni >= U_VS17 && uni <= U_VS256)) |
| 647 |
return setting(U_ALTSELECTOR, set, errh); |
| 648 |
|
| 649 |
// otherwise, try other secondaries |
| 650 |
return Secondary::setting(uni, set, errh); |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
bool |
| 655 |
char_bounds(double bounds[4], double& width, const FontInfo &finfo, |
| 656 |
const Transform &transform, uint32_t uni) |
| 657 |
{ |
| 658 |
if (Efont::OpenType::Glyph g = finfo.cmap->map_uni(uni)) |
| 659 |
return Efont::CharstringBounds::bounds(transform, finfo.program()->glyph_context(g), bounds, width); |
| 660 |
else |
| 661 |
return false; |
| 662 |
} |
| 663 |
|
| 664 |
double |
| 665 |
char_one_bound(const FontInfo &finfo, const Transform &transform, |
| 666 |
int dimen, bool max, double best, int uni, ...) |
| 667 |
{ |
| 668 |
double bounds[5]; |
| 669 |
va_list val; |
| 670 |
va_start(val, uni); |
| 671 |
while (uni != 0) { |
| 672 |
if (char_bounds(bounds, bounds[4], finfo, transform, uni)) |
| 673 |
if (max ? bounds[dimen] > best : bounds[dimen] < best) |
| 674 |
best = bounds[dimen]; |
| 675 |
uni = va_arg(val, int); |
| 676 |
} |
| 677 |
va_end(val); |
| 678 |
return best; |
| 679 |
} |
| 680 |
|
| 681 |
double |
| 682 |
font_cap_height(const FontInfo &finfo, const Transform &font_xform) |
| 683 |
{ |
| 684 |
try { |
| 685 |
Efont::OpenType::Os2 os2(finfo.otf->table("OS/2")); |
| 686 |
return os2.cap_height(); |
| 687 |
} catch (Efont::OpenType::Bounds) { |
| 688 |
// XXX what if 'H', 'O', 'B' were subject to substitution? |
| 689 |
return char_one_bound(finfo, font_xform, 3, false, finfo.units_per_em(), |
| 690 |
(int) 'H', (int) 'O', (int) 'B', 0); |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
double |
| 695 |
font_ascender(const FontInfo &finfo, const Transform &font_xform) |
| 696 |
{ |
| 697 |
try { |
| 698 |
Efont::OpenType::Os2 os2(finfo.otf->table("OS/2")); |
| 699 |
return os2.typo_ascender(); |
| 700 |
} catch (Efont::OpenType::Bounds) { |
| 701 |
// XXX what if 'd', 'l' were subject to substitution? |
| 702 |
return char_one_bound(finfo, font_xform, 3, true, |
| 703 |
finfo.x_height(font_xform), |
| 704 |
(int) 'd', (int) 'l', 0); |
| 705 |
} |
| 706 |
} |