| 1 |
/* t1fontskel.cc -- Type 1 font skeleton |
| 2 |
* |
| 3 |
* Copyright (c) 1998-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 |
#ifdef HAVE_CONFIG_H |
| 15 |
# include <config.h> |
| 16 |
#endif |
| 17 |
#include <efont/t1font.hh> |
| 18 |
#include <efont/t1item.hh> |
| 19 |
#include <efont/t1rw.hh> |
| 20 |
#include <efont/t1mm.hh> |
| 21 |
#include <lcdf/error.hh> |
| 22 |
#include <string.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <ctype.h> |
| 25 |
namespace Efont { |
| 26 |
|
| 27 |
#if HAVE_ADOBE_CODE |
| 28 |
|
| 29 |
static const char* othersubrs_code = |
| 30 |
// This version of the OtherSubrs code for old PostScript interpreters comes from |
| 31 |
// https://github.com/adobe-type-tools/afdko:c/shared/source/t1write/t1write_flexothers.h |
| 32 |
// (commit 26761e9). The Adobe Font Development Kit is licensed by the Apache |
| 33 |
// License, 2.0, which permits this embedding. |
| 34 |
"% Copyright 2014 Adobe Systems Incorporated. All rights reserved.\n" |
| 35 |
"/OtherSubrs[systemdict/internaldict known{1183615869 systemdict/internaldict\n" |
| 36 |
"get exec/FlxProc known{save true}{false}ifelse}{userdict/internaldict known\n" |
| 37 |
"not{userdict/internaldict{count 0 eq{/internaldict errordict/invalidaccess get\n" |
| 38 |
"exec}if dup type/integertype ne{/internaldict errordict/invalidaccess get exec\n" |
| 39 |
"}if dup 1183615869 eq{pop 0}{/internaldict errordict/invalidaccess get exec}\n" |
| 40 |
"ifelse}dup 14 get 1 25 dict put bind executeonly put}if 1183615869 userdict\n" |
| 41 |
"/internaldict get exec/FlxProc known{save true}{false}ifelse}ifelse[systemdict\n" |
| 42 |
"/internaldict known not{100 dict/begin cvx/mtx matrix/def cvx}if systemdict\n" |
| 43 |
"/currentpacking known{currentpacking true setpacking}if{systemdict\n" |
| 44 |
"/internaldict known{1183615869 systemdict/internaldict get exec dup/$FlxDict\n" |
| 45 |
"known not{dup dup length exch maxlength eq{pop userdict dup/$FlxDict known not\n" |
| 46 |
"{100 dict begin/mtx matrix def dup/$FlxDict currentdict put end}if}{100 dict\n" |
| 47 |
"begin/mtx matrix def dup/$FlxDict currentdict put end}ifelse}if/$FlxDict get\n" |
| 48 |
"begin}if grestore/exdef{exch def}def/dmin exch abs 100 div def/epX exdef/epY\n" |
| 49 |
"exdef/c4y2 exdef/c4x2 exdef/c4y1 exdef/c4x1 exdef/c4y0 exdef/c4x0 exdef/c3y2\n" |
| 50 |
"exdef/c3x2 exdef/c3y1 exdef/c3x1 exdef/c3y0 exdef/c3x0 exdef/c1y2 exdef/c1x2\n" |
| 51 |
"exdef/c2x2 c4x2 def/c2y2 c4y2 def/yflag c1y2 c3y2 sub abs c1x2 c3x2 sub abs gt\n" |
| 52 |
"def/PickCoords{{c1x0 c1y0 c1x1 c1y1 c1x2 c1y2 c2x0 c2y0 c2x1 c2y1 c2x2 c2y2}{\n" |
| 53 |
"c3x0 c3y0 c3x1 c3y1 c3x2 c3y2 c4x0 c4y0 c4x1 c4y1 c4x2 c4y2}ifelse/y5 exdef/x5\n" |
| 54 |
"exdef/y4 exdef/x4 exdef/y3 exdef/x3 exdef/y2 exdef/x2 exdef/y1 exdef/x1 exdef\n" |
| 55 |
"/y0 exdef/x0 exdef}def mtx currentmatrix pop mtx 0 get abs 1e-05 lt mtx 3 get\n" |
| 56 |
"abs 1e-05 lt or{/flipXY -1 def}{mtx 1 get abs 1e-05 lt mtx 2 get abs 1e-05 lt\n" |
| 57 |
"or{/flipXY 1 def}{/flipXY 0 def}ifelse}ifelse/erosion 1 def systemdict\n" |
| 58 |
"/internaldict known{1183615869 systemdict/internaldict get exec dup/erosion\n" |
| 59 |
"known{/erosion get/erosion exch def}{pop}ifelse}if yflag{flipXY 0 eq c3y2 c4y2\n" |
| 60 |
"eq or{false PickCoords}{/shrink c3y2 c4y2 eq{0}{c1y2 c4y2 sub c3y2 c4y2 sub\n" |
| 61 |
"div abs}ifelse def/yshrink{c4y2 sub shrink mul c4y2 add}def/c1y0 c3y0 yshrink\n" |
| 62 |
"def/c1y1 c3y1 yshrink def/c2y0 c4y0 yshrink def/c2y1 c4y1 yshrink def/c1x0\n" |
| 63 |
"c3x0 def/c1x1 c3x1 def/c2x0 c4x0 def/c2x1 c4x1 def/dY 0 c3y2 c1y2 sub round\n" |
| 64 |
"dtransform flipXY 1 eq{exch}if pop abs def dY dmin lt PickCoords y2 c1y2 sub\n" |
| 65 |
"abs .001 gt{c1x2 c1y2 transform flipXY 1 eq{exch}if/cx exch def/cy exch def/dY\n" |
| 66 |
"0 y2 c1y2 sub round dtransform flipXY 1 eq{exch}if pop def dY round dup 0 ne{\n" |
| 67 |
"/dY exdef}{pop dY 0 lt{-1}{1}ifelse/dY exdef}ifelse/erode PaintType 2 ne\n" |
| 68 |
"erosion .5 ge and def erode{/cy cy .5 sub def}if/ey cy dY add def/ey ey\n" |
| 69 |
"ceiling ey sub ey floor add def erode{/ey ey .5 add def}if ey cx flipXY 1 eq{\n" |
| 70 |
"exch}if itransform exch pop y2 sub/eShift exch def/y1 y1 eShift add def/y2 y2\n" |
| 71 |
"eShift add def/y3 y3 eShift add def}if}ifelse}{flipXY 0 eq c3x2 c4x2 eq or{\n" |
| 72 |
"false PickCoords}{/shrink c3x2 c4x2 eq{0}{c1x2 c4x2 sub c3x2 c4x2 sub div abs}\n" |
| 73 |
"ifelse def/xshrink{c4x2 sub shrink mul c4x2 add}def/c1x0 c3x0 xshrink def/c1x1\n" |
| 74 |
"c3x1 xshrink def/c2x0 c4x0 xshrink def/c2x1 c4x1 xshrink def/c1y0 c3y0 def\n" |
| 75 |
"/c1y1 c3y1 def/c2y0 c4y0 def/c2y1 c4y1 def/dX c3x2 c1x2 sub round 0 dtransform\n" |
| 76 |
"flipXY -1 eq{exch}if pop abs def dX dmin lt PickCoords x2 c1x2 sub abs .001 gt\n" |
| 77 |
"{c1x2 c1y2 transform flipXY -1 eq{exch}if/cy exch def/cx exch def/dX x2 c1x2\n" |
| 78 |
"sub round 0 dtransform flipXY -1 eq{exch}if pop def dX round dup 0 ne{/dX\n" |
| 79 |
"exdef}{pop dX 0 lt{-1}{1}ifelse/dX exdef}ifelse/erode PaintType 2 ne erosion\n" |
| 80 |
".5 ge and def erode{/cx cx .5 sub def}if/ex cx dX add def/ex ex ceiling ex sub\n" |
| 81 |
"ex floor add def erode{/ex ex .5 add def}if ex cy flipXY -1 eq{exch}if\n" |
| 82 |
"itransform pop x2 sub/eShift exch def/x1 x1 eShift add def/x2 x2 eShift add\n" |
| 83 |
"def/x3 x3 eShift add def}if}ifelse}ifelse x2 x5 eq y2 y5 eq or{x5 y5 lineto}{\n" |
| 84 |
"x0 y0 x1 y1 x2 y2 curveto x3 y3 x4 y4 x5 y5 curveto}ifelse epY epX}systemdict\n" |
| 85 |
"/currentpacking known{exch setpacking}if/exec cvx/end cvx]cvx executeonly exch\n" |
| 86 |
"{pop true exch restore}{systemdict/internaldict known not{1183615869 userdict\n" |
| 87 |
"/internaldict get exec exch/FlxProc exch put true}{1183615869 systemdict\n" |
| 88 |
"/internaldict get exec dup length exch maxlength eq{false}{1183615869\n" |
| 89 |
"systemdict/internaldict get exec exch/FlxProc exch put true}ifelse}ifelse}\n" |
| 90 |
"ifelse{systemdict/internaldict known{{1183615869 systemdict/internaldict get\n" |
| 91 |
"exec/FlxProc get exec}}{{1183615869 userdict/internaldict get exec/FlxProc get\n" |
| 92 |
"exec}}ifelse executeonly}if{gsave currentpoint newpath moveto}executeonly{\n" |
| 93 |
"currentpoint grestore gsave currentpoint newpath moveto}executeonly{systemdict\n" |
| 94 |
"/internaldict known not{pop 3}{1183615869 systemdict/internaldict get exec dup\n" |
| 95 |
"/startlock known{/startlock get exec}{dup/strtlck known{/strtlck get exec}{pop\n" |
| 96 |
"3}ifelse}ifelse}ifelse}executeonly]def"; |
| 97 |
|
| 98 |
#else |
| 99 |
|
| 100 |
static const char* othersubrs_code = "/OtherSubrs\n" |
| 101 |
"[\n" |
| 102 |
"{pop pop pop grestore 12 6 roll curveto curveto pop pop} executeonly\n" |
| 103 |
"{gsave currentpoint newpath moveto} executeonly\n" |
| 104 |
"{currentpoint currentpoint newpath moveto} executeonly\n" |
| 105 |
"{} executeonly\n" |
| 106 |
"] noaccess def"; |
| 107 |
|
| 108 |
#endif |
| 109 |
|
| 110 |
|
| 111 |
Type1Font * |
| 112 |
Type1Font::skeleton_make(PermString font_name, const String &version) |
| 113 |
{ |
| 114 |
Type1Font *output = new Type1Font(font_name); |
| 115 |
|
| 116 |
// %!PS-AdobeFont comment |
| 117 |
StringAccum sa; |
| 118 |
sa << "%!FontType1-1.1: " << font_name; |
| 119 |
if (version) { |
| 120 |
sa << ' ' << version; |
| 121 |
} |
| 122 |
output->add_item(new Type1CopyItem(sa.take_string())); |
| 123 |
// XXX %%BeginResource: font FontName |
| 124 |
|
| 125 |
output->_dict_deltas[dF] = 4; // Private, FontInfo, Encoding, FID |
| 126 |
output->_dict_deltas[dP] = 3; // OtherSubrs, Subrs, CharStrings |
| 127 |
|
| 128 |
return output; |
| 129 |
} |
| 130 |
|
| 131 |
void |
| 132 |
Type1Font::skeleton_fontinfo_end() |
| 133 |
{ |
| 134 |
if (first_dict_item(Type1Font::dFI) >= 0) |
| 135 |
add_item(new Type1CopyItem("end readonly def")); |
| 136 |
else |
| 137 |
add_item(new Type1CopyItem("% no FontInfo dict")); |
| 138 |
} |
| 139 |
|
| 140 |
void |
| 141 |
Type1Font::skeleton_fontdict_end() |
| 142 |
{ |
| 143 |
// switch to eexec |
| 144 |
add_item(new Type1CopyItem("end")); |
| 145 |
add_item(new Type1EexecItem(true)); |
| 146 |
|
| 147 |
// Private dictionary |
| 148 |
add_definition(Type1Font::dP, Type1Definition::make_literal("-|", "{string currentfile exch readstring pop}", "executeonly def")); |
| 149 |
set_charstring_definer(" -| "); |
| 150 |
add_definition(Type1Font::dP, Type1Definition::make_literal("|-", "{noaccess def}", "executeonly def")); |
| 151 |
add_definition(Type1Font::dP, Type1Definition::make_literal("|", "{noaccess put}", "executeonly def")); |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
Type1Font::skeleton_private_end() |
| 156 |
{ |
| 157 |
add_item(new Type1CopyItem(othersubrs_code)); |
| 158 |
|
| 159 |
// Subrs |
| 160 |
add_item(new Type1SubrGroupItem(this, true, "/Subrs 0 array")); |
| 161 |
add_item(new Type1CopyItem("|-")); |
| 162 |
|
| 163 |
// CharStrings |
| 164 |
add_item(new Type1SubrGroupItem(this, false, "2 index /CharStrings 0 dict dup begin")); |
| 165 |
|
| 166 |
// completion |
| 167 |
add_item(new Type1CopyItem("end\n\ |
| 168 |
end\n\ |
| 169 |
readonly put\n\ |
| 170 |
noaccess put\n\ |
| 171 |
dup /FontName get exch definefont pop\n\ |
| 172 |
mark currentfile closefile")); |
| 173 |
add_item(new Type1EexecItem(false)); |
| 174 |
add_item(new Type1CopyItem("\ |
| 175 |
0000000000000000000000000000000000000000000000000000000000000000\n\ |
| 176 |
0000000000000000000000000000000000000000000000000000000000000000\n\ |
| 177 |
0000000000000000000000000000000000000000000000000000000000000000\n\ |
| 178 |
0000000000000000000000000000000000000000000000000000000000000000\n\ |
| 179 |
0000000000000000000000000000000000000000000000000000000000000000\n\ |
| 180 |
0000000000000000000000000000000000000000000000000000000000000000\n\ |
| 181 |
0000000000000000000000000000000000000000000000000000000000000000\n\ |
| 182 |
0000000000000000000000000000000000000000000000000000000000000000\n\ |
| 183 |
cleartomark")); |
| 184 |
} |
| 185 |
|
| 186 |
void |
| 187 |
Type1Font::skeleton_common_subrs() |
| 188 |
{ |
| 189 |
// - first four Subrs have fixed definitions |
| 190 |
// - 0: "3 0 callothersubr pop pop setcurrentpoint return" |
| 191 |
set_subr(0, Type1Charstring(String::make_stable("\216\213\014\020\014\021\014\021\014\041\013", 11)), " |"); |
| 192 |
// - 1: "0 1 callothersubr return" |
| 193 |
set_subr(1, Type1Charstring(String::make_stable("\213\214\014\020\013", 5)), " |"); |
| 194 |
// - 2: "0 2 callothersubr return" |
| 195 |
set_subr(2, Type1Charstring(String::make_stable("\213\215\014\020\013", 5)), " |"); |
| 196 |
// - 3: "return" |
| 197 |
set_subr(3, Type1Charstring(String::make_stable("\013", 1)), " |"); |
| 198 |
// - 4: "1 3 callothersubr pop callsubr return" |
| 199 |
set_subr(4, Type1Charstring(String::make_stable("\214\216\014\020\014\021\012\013", 8)), " |"); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
static void |
| 204 |
add_number_def(Type1Font *output, int dict, PermString name, const Type1Font *font) |
| 205 |
{ |
| 206 |
double v; |
| 207 |
if (Type1Definition *t1d = font->dict(dict, name)) |
| 208 |
if (t1d->value_num(v)) |
| 209 |
output->add_definition(dict, Type1Definition::make(name, v, "def")); |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
add_copy_def(Type1Font *output, int dict, PermString name, const Type1Font *font, const char *definer = "def") |
| 214 |
{ |
| 215 |
if (Type1Definition *t1d = font->dict(dict, name)) |
| 216 |
output->add_definition(dict, Type1Definition::make_literal(name, t1d->value(), definer)); |
| 217 |
} |
| 218 |
|
| 219 |
static String |
| 220 |
font_dict_string(const Type1Font *font, int dict, PermString name) |
| 221 |
{ |
| 222 |
String s; |
| 223 |
if (Type1Definition *d = font->dict(dict, name)) |
| 224 |
if (d->value_string(s)) |
| 225 |
return s; |
| 226 |
return String(); |
| 227 |
} |
| 228 |
|
| 229 |
Type1Font * |
| 230 |
Type1Font::skeleton_make_copy(const Type1Font *font, PermString font_name, const Vector<double> *xuid_extension) |
| 231 |
{ |
| 232 |
String version = font_dict_string(font, dFI, "version"); |
| 233 |
Type1Font *output = skeleton_make(font_name, version); |
| 234 |
|
| 235 |
// other comments from font header |
| 236 |
for (int i = 0; i < font->nitems(); i++) { |
| 237 |
if (Type1CopyItem *c = font->item(i)->cast_copy()) { |
| 238 |
if (c->length() > 1 && c->value()[0] == '%') { |
| 239 |
if (c->value()[1] != '!') |
| 240 |
output->add_item(new Type1CopyItem(c->value())); |
| 241 |
} else |
| 242 |
break; |
| 243 |
} else |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
add_number_def(output, dF, "FontType", font); |
| 248 |
output->add_definition(dF, new Type1Definition("FontName", "/" + String(font_name), "def")); |
| 249 |
add_number_def(output, dF, "PaintType", font); |
| 250 |
add_copy_def(output, dF, "FontMatrix", font, "readonly def"); |
| 251 |
|
| 252 |
// FontInfo dictionary |
| 253 |
if (version) |
| 254 |
output->add_definition(dFI, Type1Definition::make_string("version", version, "readonly def")); |
| 255 |
if (String s = font_dict_string(font, dFI, "Notice")) |
| 256 |
output->add_definition(dFI, Type1Definition::make_string("Notice", s, "readonly def")); |
| 257 |
if (String s = font_dict_string(font, dFI, "Copyright")) |
| 258 |
output->add_definition(dFI, Type1Definition::make_string("Copyright", s, "readonly def")); |
| 259 |
if (String s = font_dict_string(font, dFI, "FullName")) |
| 260 |
output->add_definition(dFI, Type1Definition::make_string("FullName", s, "readonly def")); |
| 261 |
if (String s = font_dict_string(font, dFI, "FamilyName")) |
| 262 |
output->add_definition(dFI, Type1Definition::make_string("FamilyName", s, "readonly def")); |
| 263 |
if (String s = font_dict_string(font, dFI, "Weight")) |
| 264 |
output->add_definition(dFI, Type1Definition::make_string("Weight", s, "readonly def")); |
| 265 |
if (Type1Definition *t1d = font->fi_dict("isFixedPitch")) { |
| 266 |
bool v; |
| 267 |
if (t1d->value_bool(v)) |
| 268 |
output->add_definition(dFI, Type1Definition::make_literal("isFixedPitch", (v ? "true" : "false"), "def")); |
| 269 |
} |
| 270 |
add_number_def(output, dFI, "ItalicAngle", font); |
| 271 |
add_number_def(output, dFI, "UnderlinePosition", font); |
| 272 |
add_number_def(output, dFI, "UnderlineThickness", font); |
| 273 |
output->skeleton_fontinfo_end(); |
| 274 |
|
| 275 |
// Encoding, other font dictionary entries |
| 276 |
output->add_type1_encoding(new Type1Encoding(*font->type1_encoding())); |
| 277 |
add_number_def(output, dF, "StrokeWidth", font); |
| 278 |
if (!xuid_extension) |
| 279 |
add_number_def(output, dF, "UniqueID", font); |
| 280 |
add_copy_def(output, dF, "XUID", font, "readonly def"); |
| 281 |
if (xuid_extension) { |
| 282 |
Vector<double> xuid; |
| 283 |
if (Type1Definition *xuid_def = output->dict("XUID")) |
| 284 |
xuid_def->value_numvec(xuid); |
| 285 |
if (!xuid.size()) { |
| 286 |
Type1Definition *uid_def = font->dict("UniqueID"); |
| 287 |
int uid; |
| 288 |
if (uid_def && uid_def->value_int(uid)) { |
| 289 |
xuid.push_back(1); |
| 290 |
xuid.push_back(uid); |
| 291 |
} |
| 292 |
} |
| 293 |
if (xuid.size()) { |
| 294 |
for (int i = 0; i < xuid_extension->size(); i++) |
| 295 |
xuid.push_back((*xuid_extension)[i]); |
| 296 |
Type1Definition *xuid_def = output->ensure(dF, "XUID"); |
| 297 |
xuid_def->set_numvec(xuid); |
| 298 |
} |
| 299 |
} |
| 300 |
add_copy_def(output, dF, "FontBBox", font, "readonly def"); |
| 301 |
output->skeleton_fontdict_end(); |
| 302 |
|
| 303 |
// Private dictionary |
| 304 |
add_copy_def(output, dP, "BlueValues", font); |
| 305 |
add_copy_def(output, dP, "OtherBlues", font); |
| 306 |
add_copy_def(output, dP, "FamilyBlues", font); |
| 307 |
add_copy_def(output, dP, "FamilyOtherBlues", font); |
| 308 |
add_number_def(output, dP, "BlueScale", font); |
| 309 |
add_number_def(output, dP, "BlueShift", font); |
| 310 |
add_number_def(output, dP, "BlueFuzz", font); |
| 311 |
add_copy_def(output, dP, "StdHW", font); |
| 312 |
add_copy_def(output, dP, "StdVW", font); |
| 313 |
add_copy_def(output, dP, "StemSnapH", font); |
| 314 |
add_copy_def(output, dP, "StemSnapV", font); |
| 315 |
add_copy_def(output, dP, "ForceBold", font); |
| 316 |
add_number_def(output, dP, "LanguageGroup", font); |
| 317 |
add_number_def(output, dP, "ExpansionFactor", font); |
| 318 |
if (!xuid_extension) |
| 319 |
add_number_def(output, dP, "UniqueID", font); |
| 320 |
output->add_definition(dP, Type1Definition::make_literal("MinFeature", "{16 16}", "|-")); |
| 321 |
output->add_definition(dP, Type1Definition::make_literal("password", "5839", "def")); |
| 322 |
output->add_definition(dP, Type1Definition::make_literal("lenIV", "0", "def")); |
| 323 |
output->skeleton_private_end(); |
| 324 |
|
| 325 |
return output; |
| 326 |
} |
| 327 |
|
| 328 |
} |