| 1 |
/* |
| 2 |
* This file implements support for adding proper character encodings to |
| 3 |
* bitmap fonts, so searching and copying text works better in PDFs |
| 4 |
* generated by the dvips Postscript output that uses bitmap fonts. |
| 5 |
* |
| 6 |
* Written by Tomas Rokicki in June/July 2019. Public domain. |
| 7 |
* The dvips-all.enc file used here is also public domain. |
| 8 |
* |
| 9 |
* It turns out we need to do more than just add encodings to the fonts |
| 10 |
* for most PDF viewers, so this file also takes care of those issues. |
| 11 |
*/ |
| 12 |
/* |
| 13 |
* Our first concern is finding and reading encoding files for bitmap |
| 14 |
* fonts, and, if such cannot be found, determining a reasonable |
| 15 |
* alternative. |
| 16 |
* |
| 17 |
* We could use the code in writet1 to read an encoding file, but |
| 18 |
* we would need to make some messy changes for that to work, and we |
| 19 |
* really want this to be a minimal impact to existing code (especially |
| 20 |
* code such as write_t1 that we don't have a good handle on). |
| 21 |
* |
| 22 |
* We want to deduplicate (combine identical) encodings to lower the |
| 23 |
* memory and file size impact. But we have to be careful; if the |
| 24 |
* document requires multiple sections, we need to redownload encodings |
| 25 |
* just like we do fonts. |
| 26 |
*/ |
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#ifndef STANDALONE |
| 30 |
#include <kpathsea/config.h> |
| 31 |
#endif |
| 32 |
#include "dvips.h" |
| 33 |
#include "protos.h" |
| 34 |
/* |
| 35 |
* Do we use this functionality? If 0, then no; if 1, then yes, but |
| 36 |
* suppress warnings; if 2, then warn. |
| 37 |
*/ |
| 38 |
int encodetype3 = 1 ; |
| 39 |
void bitmapencopt(int v) { |
| 40 |
if (0 <= v && v <= 2) |
| 41 |
encodetype3 = v ; |
| 42 |
else |
| 43 |
error("! Bad option to J") ; |
| 44 |
} |
| 45 |
/* |
| 46 |
* TODO |
| 47 |
* |
| 48 |
* Initially we support standalone compilation for testing but |
| 49 |
* this code will be ripped out before release. |
| 50 |
*/ |
| 51 |
#ifdef STANDALONE |
| 52 |
#undef fopen |
| 53 |
#undef fclose |
| 54 |
#endif |
| 55 |
/* |
| 56 |
* Reading the encodings. |
| 57 |
* |
| 58 |
* Encodings we read are returned as an array C strings. We do not |
| 59 |
* attempt to parse the contents of the encodings, because they |
| 60 |
* may contain additional dictionaries or other PostScript code |
| 61 |
* that can possibly do more advanced things. |
| 62 |
*/ |
| 63 |
#define MAX_LINE_LENGTH 256 |
| 64 |
/* |
| 65 |
* A structure for storing deduplicated encodings. These are deduplicated |
| 66 |
* in dvips memory on load. We also store information on whether the |
| 67 |
* encoding has been downloaded in a particular section. This is stored |
| 68 |
* as a singly-linked list for all distinct encodings we have used so |
| 69 |
* far. |
| 70 |
*/ |
| 71 |
struct bmenc { |
| 72 |
const char **enc ; // the encoding itself |
| 73 |
int downloaded_seq ; // -1: not downloaded; else, sequence number |
| 74 |
struct bmenc *next ; // next encoding in linear linked list |
| 75 |
unsigned char existsbm[32] ; // a bitmap of characters with names |
| 76 |
} ; |
| 77 |
static struct bmenc *bmlist ; |
| 78 |
/* |
| 79 |
* This structure maintains a relationship between a font name and a |
| 80 |
* bitmap encoding. Again, linear search is used. |
| 81 |
*/ |
| 82 |
struct bmfontenc { |
| 83 |
const char *fontname ; // dvi font name |
| 84 |
struct bmenc *enc ; // the encoding to use |
| 85 |
struct bmfontenc *next ; // next encoding |
| 86 |
} ; |
| 87 |
static struct bmfontenc *bmfontenclist ; |
| 88 |
/* |
| 89 |
* We load the default encodings for all fonts en masse from he |
| 90 |
* dvips-all.enc file, and store them here. We use an array and |
| 91 |
* sort the input for quicker search. We don't need to deduplicate |
| 92 |
* because we assume whatever generated dvips-all.enc already did |
| 93 |
* deduplication. |
| 94 |
*/ |
| 95 |
static int numstatic ; |
| 96 |
static struct allfontenc { |
| 97 |
const char *fontname ; |
| 98 |
const char **enc ; |
| 99 |
} *bmfontarr ; |
| 100 |
/* |
| 101 |
* While loading dvips-all.enc, we track the capacity of our allfontenc |
| 102 |
* array, how many have names, the capacity in strings of our encoding |
| 103 |
* that is being built up, and how many strings we still can store in |
| 104 |
* the encoding built up. |
| 105 |
*/ |
| 106 |
static int capstatic, namedstatic ; |
| 107 |
static int in_working_enc, working_enc_left ; |
| 108 |
const char **working_enc ; |
| 109 |
/* |
| 110 |
* Add another fontname to the list (from the dvips-all.enc file). |
| 111 |
* If needed expand the working arrays. If there is currently |
| 112 |
* an encoding, that means that we need to first assign that |
| 113 |
* encoding to the list of font names we have built up, so do |
| 114 |
* that first. If the fontname passed is the null string, this is |
| 115 |
* the last call done just to finish up the last encoding. |
| 116 |
*/ |
| 117 |
static void add_fontname(const char *fontname) { |
| 118 |
int i ; |
| 119 |
if (in_working_enc) { // flush |
| 120 |
const char **e = working_enc ; |
| 121 |
while (numstatic < namedstatic) |
| 122 |
bmfontarr[numstatic++].enc = e ; |
| 123 |
working_enc += in_working_enc + 1 ; |
| 124 |
working_enc_left -= in_working_enc + 1 ; |
| 125 |
in_working_enc = 0 ; |
| 126 |
} |
| 127 |
if (fontname == 0) |
| 128 |
return ; |
| 129 |
if (namedstatic >= capstatic) { |
| 130 |
int ncapstatic = capstatic * 2 + 20 ; |
| 131 |
struct allfontenc *nbmfontarr = |
| 132 |
(struct allfontenc *)mymalloc(sizeof(struct allfontenc) * ncapstatic) ; |
| 133 |
if (capstatic) { |
| 134 |
memcpy(nbmfontarr, bmfontarr, sizeof(struct allfontenc)*capstatic) ; |
| 135 |
free(bmfontarr) ; |
| 136 |
} |
| 137 |
bmfontarr = nbmfontarr ; |
| 138 |
for (i=capstatic; i<ncapstatic; i++) { |
| 139 |
bmfontarr[i].fontname = 0 ; |
| 140 |
bmfontarr[i].enc = 0 ; |
| 141 |
} |
| 142 |
capstatic = ncapstatic ; |
| 143 |
} |
| 144 |
bmfontarr[namedstatic].fontname = fontname ; |
| 145 |
bmfontarr[namedstatic].enc = 0 ; |
| 146 |
namedstatic++ ; |
| 147 |
} |
| 148 |
/* |
| 149 |
* Add an encoding line to the working encoding; if needed, reallocate |
| 150 |
* the working array. |
| 151 |
*/ |
| 152 |
static void add_encline(const char *encline) { |
| 153 |
if (in_working_enc + 1 >= working_enc_left) { |
| 154 |
int i ; |
| 155 |
int new_enc_size = 100 + 2 * in_working_enc ; |
| 156 |
const char **new_working_enc = |
| 157 |
(const char **)mymalloc(sizeof(const char **) * new_enc_size) ; |
| 158 |
for (i=0; i<new_enc_size; i++) |
| 159 |
new_working_enc[i] = 0 ; |
| 160 |
for (i=0; i<in_working_enc; i++) |
| 161 |
new_working_enc[i] = working_enc[i] ; |
| 162 |
working_enc = new_working_enc ; |
| 163 |
working_enc_left = new_enc_size - in_working_enc ; |
| 164 |
} |
| 165 |
working_enc[in_working_enc++] = encline ; |
| 166 |
} |
| 167 |
/* |
| 168 |
* Parse the encoding file. If use_all flag is set then we are parsing |
| 169 |
* the dvips_all file which can have multiple encodings, each preceded |
| 170 |
* by a set of lines with font names followed by a colon character. |
| 171 |
* |
| 172 |
* If we are parsing them all, we build a sorted global list of entries |
| 173 |
* for later lookup, and always return 0. Otherwise we just expect a |
| 174 |
* single encoding and we store that away. |
| 175 |
*/ |
| 176 |
static const char **parseencodingfile(FILE *f, int use_all) { |
| 177 |
char encbuf[MAX_LINE_LENGTH+1] ; |
| 178 |
size_t i ; |
| 179 |
for (i=0; i<sizeof(encbuf); i++) |
| 180 |
encbuf[i] = 0 ; |
| 181 |
while (fgets(encbuf, MAX_LINE_LENGTH, f) != 0) { |
| 182 |
char *p = encbuf ; |
| 183 |
char *q = p + strlen(p) - 1 ; |
| 184 |
while (q > p && *q < ' ') // kill line terminators |
| 185 |
*q-- = 0 ; |
| 186 |
q = p ; |
| 187 |
while (*q && *q != ' ' && *q != ':') |
| 188 |
q++ ; |
| 189 |
if (use_all && *q == ':') { // looks like a font name |
| 190 |
if (q[1] >= ' ' || !use_all) |
| 191 |
error( |
| 192 |
"! unexpected colon or extra stuff at end of line in encoding file?") ; |
| 193 |
*q = 0 ; |
| 194 |
add_fontname(strdup(p)) ; |
| 195 |
} else { |
| 196 |
add_encline(strdup(p)) ; |
| 197 |
} |
| 198 |
} |
| 199 |
if (use_all) |
| 200 |
add_fontname(0) ; // flushes last encoding |
| 201 |
if (use_all) { |
| 202 |
// sort the static fonts for quicker search; Shell sort |
| 203 |
int h = 1 ; |
| 204 |
while (h < numstatic) |
| 205 |
h = 3 * h + 1 ; |
| 206 |
while (h > 1) { |
| 207 |
int i ; |
| 208 |
h /= 3 ; |
| 209 |
for (i=h; i<numstatic; i++) { |
| 210 |
int j = i ; |
| 211 |
while (j >= h && |
| 212 |
strcmp(bmfontarr[j-h].fontname, bmfontarr[j].fontname) > 0) { |
| 213 |
const char *na = bmfontarr[j-h].fontname ; |
| 214 |
const char **en = bmfontarr[j-h].enc ; |
| 215 |
bmfontarr[j-h].fontname = bmfontarr[j].fontname ; |
| 216 |
bmfontarr[j-h].enc = bmfontarr[j].enc ; |
| 217 |
bmfontarr[j].fontname = na ; |
| 218 |
bmfontarr[j].enc = en ; |
| 219 |
j -= h ; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
return 0 ; |
| 224 |
} else { |
| 225 |
const char **e = working_enc ; |
| 226 |
if (e == 0) |
| 227 |
error("! No lines in encoding?") ; |
| 228 |
return e ; |
| 229 |
} |
| 230 |
} |
| 231 |
/* |
| 232 |
* Given a font name, find an encoding file. We prefix all encoding |
| 233 |
* files with "dvips-" to distinguish these encodings from those used |
| 234 |
* in the psfonts.map, since the syntax, requirements, and function |
| 235 |
* is somewhat different. |
| 236 |
*/ |
| 237 |
#define MAX_NAME_SIZE 256 |
| 238 |
static FILE *bitmap_enc_search(const char *fontname) { |
| 239 |
char namebuf[MAX_NAME_SIZE+1] ; |
| 240 |
if (fontname == 0 || strlen(fontname) > 128) |
| 241 |
error("! excessively long font name") ; |
| 242 |
sprintf(namebuf, "dvips-%s.enc", fontname) ; |
| 243 |
#ifdef STANDALONE |
| 244 |
return fopen(namebuf, FOPEN_RBIN_MODE) ; |
| 245 |
#else |
| 246 |
return search(kpse_enc_format, namebuf, FOPEN_RBIN_MODE) ; |
| 247 |
#endif |
| 248 |
} |
| 249 |
/* |
| 250 |
* Are two raw encodings identical? They must be exactly the same; there |
| 251 |
* is no effort towards comparing semantics, just precise text. |
| 252 |
*/ |
| 253 |
static int eqencoding(const char **a, const char **b) { |
| 254 |
int i ; |
| 255 |
if (a == b) |
| 256 |
return 1 ; |
| 257 |
for (i=0; a[i] != 0 && b[i] != 0; i++) |
| 258 |
if (a[i] != b[i] && (a[i] == 0 || b[i] == 0 || strcmp(a[i], b[i]) != 0)) |
| 259 |
return 0 ; |
| 260 |
return 1 ; |
| 261 |
} |
| 262 |
/* |
| 263 |
* Try to parse an encoding to identify which names exist and which don't. |
| 264 |
* If we can't trivially parse it we just mark all names as existing. |
| 265 |
* We properly parse the following types of tokens: |
| 266 |
* [ (at start) ] (at end) : ignored |
| 267 |
* /name |
| 268 |
* digits { /.notdef } repeat |
| 269 |
* |
| 270 |
* In all cases the spaces and linebreaks are arbitrary. |
| 271 |
* |
| 272 |
* The state machine works as follows: |
| 273 |
* |
| 274 |
* 'B': begin; expect [ |
| 275 |
* 'N': between entries; expect name or digits or ] |
| 276 |
* 'E': finished (saw ]); expect nothing more |
| 277 |
* '#': just saw digits; expect only { |
| 278 |
* '{': just saw {; expect only name and should be /.notdef |
| 279 |
* 'L': just saw a name inside {}; expect only } |
| 280 |
* '}': just saw }; expect only repeat |
| 281 |
*/ |
| 282 |
static int trytoparseenc(struct bmenc *bme) { |
| 283 |
int i ; |
| 284 |
const char **enc = bme->enc ; |
| 285 |
const char *p ; |
| 286 |
int seenchars = 0 ; |
| 287 |
int num = 0 ; |
| 288 |
char state = 'B' ; |
| 289 |
for (i=0; i<32; i++) |
| 290 |
bme->existsbm[i] = 255 ; |
| 291 |
while (*enc != 0) { |
| 292 |
p = *enc ; |
| 293 |
enc++ ; |
| 294 |
while (*p && *p <= ' ') |
| 295 |
p++ ; |
| 296 |
while (*p != 0) { |
| 297 |
switch (state) { |
| 298 |
case 'B': if (*p != '[') return 0 ; |
| 299 |
p++ ; |
| 300 |
state = 'N' ; |
| 301 |
break ; |
| 302 |
case 'N': if (*p == ']') { |
| 303 |
p++ ; |
| 304 |
state = 'E' ; |
| 305 |
} else if (*p == '/') { |
| 306 |
if (seenchars >= 256) |
| 307 |
return 0 ; |
| 308 |
if (strncmp(p, "/.notdef", 8) == 0 && |
| 309 |
(p[8] <= ' ' || strchr("{}[]<>()%/", p[8]) == 0)) { |
| 310 |
bme->existsbm[seenchars>>3] &= ~(1<<(seenchars & 7)) ; |
| 311 |
} |
| 312 |
// see PostScript language reference manual syntax for this |
| 313 |
p++ ; |
| 314 |
while (*p > ' ' && strchr("{}[]<>()%/", *p) == 0) |
| 315 |
p++ ; |
| 316 |
seenchars++ ; |
| 317 |
} else if ('0' <= *p && *p <= '9') { |
| 318 |
num = 0 ; |
| 319 |
while (num < 256 && '0' <= *p && *p <= '9') |
| 320 |
num = 10 * num + *p++ - '0' ; |
| 321 |
state = '#' ; |
| 322 |
} else |
| 323 |
return 0 ; |
| 324 |
break ; |
| 325 |
case '#': if (*p != '{') return 0 ; |
| 326 |
p++ ; |
| 327 |
state = '{' ; |
| 328 |
break ; |
| 329 |
case '{': if (strncmp(p, "/.notdef", 8) != 0) |
| 330 |
return 0 ; |
| 331 |
p += 8 ; |
| 332 |
if (*p > ' ' && strchr("{}[]<>()%/", *p) == 0) |
| 333 |
return 0 ; |
| 334 |
while (num > 0) { |
| 335 |
if (seenchars >= 256) |
| 336 |
return 0 ; |
| 337 |
bme->existsbm[seenchars>>3] &= ~(1<<(seenchars & 7)) ; |
| 338 |
seenchars++ ; |
| 339 |
num-- ; |
| 340 |
} |
| 341 |
state = 'L' ; |
| 342 |
break ; |
| 343 |
case 'L': if (*p != '}') |
| 344 |
return 0 ; |
| 345 |
p++ ; |
| 346 |
state = '}' ; |
| 347 |
break ; |
| 348 |
case '}': if (strncmp(p, "repeat", 6) != 0) |
| 349 |
return 0 ; |
| 350 |
p += 6 ; |
| 351 |
state = 'N' ; |
| 352 |
break ; |
| 353 |
default: |
| 354 |
error("! internal error in encoding vector parse") ; |
| 355 |
} |
| 356 |
while (*p && *p <= ' ') |
| 357 |
p++ ; |
| 358 |
} |
| 359 |
} |
| 360 |
if (seenchars != 256) |
| 361 |
return 0 ; |
| 362 |
return 1 ; |
| 363 |
} |
| 364 |
static void parseenc(struct bmenc *bme) { |
| 365 |
int i ; |
| 366 |
if (trytoparseenc(bme) == 0) { |
| 367 |
printf("Failed to parse it.\n") ; |
| 368 |
for (i=0; i<32; i++) |
| 369 |
bme->existsbm[i] = 255 ; |
| 370 |
} |
| 371 |
} |
| 372 |
/* |
| 373 |
* Add an encoding to our list of deduplicated encodings. |
| 374 |
*/ |
| 375 |
struct bmenc *addbmenc(const char **enc) { |
| 376 |
struct bmenc *r = (struct bmenc *)mymalloc(sizeof(struct bmenc)) ; |
| 377 |
r->downloaded_seq = -1 ; |
| 378 |
r->enc = enc ; |
| 379 |
r->next = bmlist ; |
| 380 |
parseenc(r) ; |
| 381 |
bmlist = r ; |
| 382 |
return r ; |
| 383 |
} |
| 384 |
/* |
| 385 |
* Given a particular encoding, walk through our encoding list and |
| 386 |
* see if it already exists; if so, return the existing one. |
| 387 |
* The set of distinct encodings in a particular document is |
| 388 |
* expected to be small (a few dozen at most). |
| 389 |
*/ |
| 390 |
static struct bmenc *deduplicateencoding(const char **enc) { |
| 391 |
struct bmenc *p ; |
| 392 |
for (p=bmlist; p!=0; p=p->next) |
| 393 |
if (eqencoding(p->enc, enc)) |
| 394 |
return p ; |
| 395 |
return addbmenc(enc) ; |
| 396 |
} |
| 397 |
/* |
| 398 |
* Attempt to load an encoding; first, find the file, and then parse |
| 399 |
* the file. |
| 400 |
*/ |
| 401 |
static const char **bitmap_enc_load(const char *fontname, int use_all) { |
| 402 |
FILE *f = bitmap_enc_search(use_all ? "all" : fontname) ; |
| 403 |
if (f != 0) { |
| 404 |
const char **r = parseencodingfile(f, use_all) ; |
| 405 |
fclose(f) ; |
| 406 |
return r ; |
| 407 |
} |
| 408 |
return 0 ; |
| 409 |
} |
| 410 |
/* |
| 411 |
* Start a section: mark all encodings as not downloaded yet. |
| 412 |
*/ |
| 413 |
static int curbmseq ; |
| 414 |
void bmenc_startsection() { |
| 415 |
struct bmenc *p ; |
| 416 |
for (p=bmlist; p!=0; p=p->next) |
| 417 |
p->downloaded_seq = -1 ; |
| 418 |
curbmseq = 0 ; |
| 419 |
} |
| 420 |
/* |
| 421 |
* Try to find a font in the all encoding list. This list is sorted |
| 422 |
* so we can use binary search. |
| 423 |
*/ |
| 424 |
static const char **bitmap_all_find(const char *fontname) { |
| 425 |
int ptr, bit; |
| 426 |
if (numstatic == 0) |
| 427 |
return 0 ; |
| 428 |
ptr = 0 ; |
| 429 |
bit = 1 ; |
| 430 |
while ((bit << 1) < numstatic) |
| 431 |
bit <<= 1 ; |
| 432 |
while (bit > 0) { |
| 433 |
int t = ptr + bit ; |
| 434 |
if (t < numstatic && strcmp(fontname, bmfontarr[t].fontname) >= 0) |
| 435 |
ptr = t ; |
| 436 |
bit >>= 1 ; |
| 437 |
} |
| 438 |
if (strcmp(fontname, bmfontarr[ptr].fontname) == 0) |
| 439 |
return bmfontarr[ptr].enc ; |
| 440 |
else |
| 441 |
return 0 ; |
| 442 |
} |
| 443 |
/* |
| 444 |
* Download the encoding and set the sequence number. |
| 445 |
*/ |
| 446 |
static void downloadenc(struct bmenc *enc) { |
| 447 |
int fresh = 0 ; |
| 448 |
char cmdbuf[16] ; |
| 449 |
if (enc->downloaded_seq < 0) { |
| 450 |
int i ; |
| 451 |
newline() ; |
| 452 |
for (i=0; enc->enc[i]; i++) |
| 453 |
pslineout(enc->enc[i]) ; |
| 454 |
enc->downloaded_seq = curbmseq++ ; |
| 455 |
fresh = 1 ; |
| 456 |
} |
| 457 |
newline() ; |
| 458 |
sprintf(cmdbuf, "/EN%d", enc->downloaded_seq) ; |
| 459 |
if (fresh) { |
| 460 |
cmdout("A") ; |
| 461 |
psnameout(cmdbuf) ; |
| 462 |
cmdout("X") ; |
| 463 |
} else { |
| 464 |
// we use load here specifically to defer execution until the |
| 465 |
// new font dictionary is on the stack and being built. |
| 466 |
psnameout(cmdbuf) ; |
| 467 |
cmdout("load") ; |
| 468 |
} |
| 469 |
} |
| 470 |
/* |
| 471 |
* Send out the new encoding, font bounding box, and font matrix. |
| 472 |
* If we cannot find an encoding file for this font, do what we |
| 473 |
* used to do instead (don't give it an encoding or resize or |
| 474 |
* rescale). |
| 475 |
*/ |
| 476 |
static struct bmenc *getencoding_seq(const char *fontname) ; |
| 477 |
int downloadbmencoding(const char *name, double scale, fontdesctype *curfnt) { |
| 478 |
int slop; |
| 479 |
int i ; |
| 480 |
int seq ; |
| 481 |
int llx = curfnt->llx ; |
| 482 |
int lly = curfnt->lly ; |
| 483 |
int urx = curfnt->urx ; |
| 484 |
int ury = curfnt->ury ; |
| 485 |
struct bmenc *bme = getencoding_seq(name) ; |
| 486 |
if (bme == 0) |
| 487 |
return -1 ; |
| 488 |
seq = bme->downloaded_seq ; |
| 489 |
/* |
| 490 |
* Check that every character defined in the font has a name in the |
| 491 |
* PostScript vector, and complain if this is not the case. |
| 492 |
*/ |
| 493 |
for (i=0; i<256 && i<curfnt->maxchars; i++) { |
| 494 |
if ((curfnt->chardesc[i].flags2 & EXISTS) && |
| 495 |
!(bme->existsbm[i>>3] & (1 << (i & 7)))) { |
| 496 |
fprintf_str(stderr, |
| 497 |
"Can't use PostScript encoding vector for font %s; character %d has no name.\n", |
| 498 |
name, i) ; |
| 499 |
return -1 ; |
| 500 |
} |
| 501 |
} |
| 502 |
cmdout("IEn") ; |
| 503 |
cmdout("S") ; |
| 504 |
psnameout("/IEn") ; |
| 505 |
cmdout("X") ; |
| 506 |
cmdout("FBB") ; |
| 507 |
cmdout("FMat") ; |
| 508 |
psnameout("/FMat") ; |
| 509 |
specialout('[') ; |
| 510 |
doubleout(1.0/scale) ; |
| 511 |
numout(0) ; |
| 512 |
numout(0) ; |
| 513 |
doubleout(-1.0/scale) ; |
| 514 |
numout(0) ; |
| 515 |
numout(0) ; |
| 516 |
specialout(']') ; |
| 517 |
cmdout("N") ; |
| 518 |
psnameout("/FBB") ; |
| 519 |
// we add a bit of slop here, because this is only used for |
| 520 |
// highlighting, and in theory if the bounding box is too |
| 521 |
// tight, on some RIPs, characters could be clipped. |
| 522 |
slop = 1; |
| 523 |
specialout('[') ; |
| 524 |
numout(llx-slop) ; |
| 525 |
numout(lly-slop) ; |
| 526 |
numout(urx+slop) ; |
| 527 |
numout(ury+slop) ; |
| 528 |
specialout(']') ; |
| 529 |
cmdout("N") ; |
| 530 |
newline() ; |
| 531 |
return seq ; |
| 532 |
} |
| 533 |
/* |
| 534 |
* Finish up the downloaded encoding; scale the font, and restore the |
| 535 |
* previous global definitions. |
| 536 |
*/ |
| 537 |
void finishbitmapencoding(const char *name, double scale) { |
| 538 |
psnameout(name) ; |
| 539 |
cmdout("load") ; |
| 540 |
numout(0) ; |
| 541 |
cmdout(name+1) ; |
| 542 |
cmdout("currentfont") ; |
| 543 |
doubleout(scale) ; |
| 544 |
cmdout("scalefont") ; |
| 545 |
cmdout("put") ; |
| 546 |
psnameout("/FMat") ; |
| 547 |
cmdout("X") ; |
| 548 |
psnameout("/FBB") ; |
| 549 |
cmdout("X") ; |
| 550 |
psnameout("/IEn") ; |
| 551 |
cmdout("X") ; |
| 552 |
newline(); |
| 553 |
} |
| 554 |
/* |
| 555 |
* We warn if we have to use a built-in encoding, and set this value to 1. |
| 556 |
* We warn again if we cannot find a built-in encoding and have to |
| 557 |
* default to previous behavior, and set this value to 2. In any case, |
| 558 |
* we only issue warnings if the encodetype3 flag is set to a value greater |
| 559 |
* than one; we don't want to bother folks about this new functionality. |
| 560 |
*/ |
| 561 |
static int warned_about_missing_encoding = 0 ; |
| 562 |
/* |
| 563 |
* Print a warning message. |
| 564 |
*/ |
| 565 |
static void bmenc_warn(const char *fontname, const char *msg) { |
| 566 |
if (encodetype3 > 1) |
| 567 |
fprintf_str(stderr, |
| 568 |
"dvips: Static bitmap font encoding for font %s (and others?): %s.\n", |
| 569 |
fontname, msg) ; |
| 570 |
} |
| 571 |
/* |
| 572 |
* About to download a font; find the encoding sequence number. |
| 573 |
* If needed, download a new sequence. If we can't find one, use |
| 574 |
* -1; this font may not work as well for copy/paste and text search. |
| 575 |
*/ |
| 576 |
static int tried_all = 0 ; // have we tried to load dvips-all.enc |
| 577 |
static struct bmenc *getencoding_seq(const char *fontname) { |
| 578 |
struct bmenc *enc = 0 ; |
| 579 |
struct bmfontenc *p = bmfontenclist ; |
| 580 |
for (; p!=0; p=p->next) |
| 581 |
if (strcmp(fontname, p->fontname) == 0) { |
| 582 |
enc = p->enc ; |
| 583 |
if (enc == 0) // remember failures |
| 584 |
return 0 ; |
| 585 |
break ; |
| 586 |
} |
| 587 |
// not in list; try to load it from a file |
| 588 |
if (enc == 0) { |
| 589 |
const char **e = bitmap_enc_load(fontname, 0) ; |
| 590 |
// did not find it; try to load it from the dvips-all list. |
| 591 |
if (e == 0) { |
| 592 |
if (!tried_all) { |
| 593 |
bitmap_enc_load(fontname, 1) ; |
| 594 |
tried_all = 1 ; |
| 595 |
} |
| 596 |
e = bitmap_all_find(fontname) ; |
| 597 |
} |
| 598 |
if (e) |
| 599 |
enc = deduplicateencoding(e) ; |
| 600 |
} |
| 601 |
// did not find this in the list; add it to the list |
| 602 |
if (p == 0) { |
| 603 |
p = (struct bmfontenc *)mymalloc(sizeof(struct bmfontenc)) ; |
| 604 |
p->fontname = strdup(fontname) ; |
| 605 |
p->enc = enc ; |
| 606 |
p->next = bmfontenclist ; |
| 607 |
// parseenc(enc) ; |
| 608 |
bmfontenclist = p ; |
| 609 |
} |
| 610 |
if (enc == 0) { |
| 611 |
if (warned_about_missing_encoding < 2) { |
| 612 |
bmenc_warn(fontname, "no encoding found") ; |
| 613 |
warned_about_missing_encoding = 2 ; |
| 614 |
} |
| 615 |
return 0 ; // don't download an encoding |
| 616 |
} |
| 617 |
downloadenc(enc) ; |
| 618 |
return enc ; |
| 619 |
} |
| 620 |
#ifdef STANDALONE |
| 621 |
/* |
| 622 |
* Standalone test code: |
| 623 |
*/ |
| 624 |
void error(const char *s) { |
| 625 |
fprintf(stderr, "Failed: %s\n", s) ; |
| 626 |
if (*s == '!') |
| 627 |
exit(0) ; |
| 628 |
} |
| 629 |
char *mymalloc(int sz) { |
| 630 |
return (char *)malloc(sz) ; |
| 631 |
} |
| 632 |
int pos = 0 ; |
| 633 |
int idok = 1 ; |
| 634 |
const int MAXLINE = 75 ; |
| 635 |
void newline() { |
| 636 |
printf("\n") ; |
| 637 |
idok = 1 ; |
| 638 |
pos = 0 ; |
| 639 |
} |
| 640 |
void doubleout(double f) { |
| 641 |
printf("%g", f) ; |
| 642 |
pos += 8 ; |
| 643 |
} |
| 644 |
void pslineout(const char *s) { |
| 645 |
printf("%s\n", s) ; |
| 646 |
} |
| 647 |
void numout(int num) { |
| 648 |
int len = 1 ; |
| 649 |
int t = num / 10 ; |
| 650 |
while (t>0) { |
| 651 |
len++ ; |
| 652 |
t /= 10 ; |
| 653 |
} |
| 654 |
if (pos + len >= MAXLINE) |
| 655 |
newline() ; |
| 656 |
if (!idok) |
| 657 |
specialout(' ') ; |
| 658 |
printf("%d", num) ; |
| 659 |
pos += len ; |
| 660 |
idok = 0 ; |
| 661 |
} |
| 662 |
void cmdout(const char *s) { |
| 663 |
if (pos + strlen(s) >= MAXLINE) |
| 664 |
newline() ; |
| 665 |
if (*s != '/' && !idok) |
| 666 |
specialout(' ') ; |
| 667 |
printf("%s", s) ; |
| 668 |
pos += strlen(s) ; |
| 669 |
idok = 0 ; |
| 670 |
} |
| 671 |
void psnameout(const char *s) { |
| 672 |
cmdout(s) ; |
| 673 |
} |
| 674 |
void specialout(char c) { |
| 675 |
if (pos + 1 >= MAXLINE) |
| 676 |
newline() ; |
| 677 |
printf("%c", c) ; |
| 678 |
pos++ ; |
| 679 |
idok = 1 ; |
| 680 |
} |
| 681 |
int main(int argc, char *argv[]) { |
| 682 |
bmenc_startsection() ; |
| 683 |
for (int i=1; i<argc; i++) { |
| 684 |
struct bmenc *r = getencoding_seq(argv[i]) ; |
| 685 |
int seq = r ? r->downloaded_seq : -1 ; |
| 686 |
printf("\n") ; |
| 687 |
printf("Result for %s is %d\n", argv[i], seq) ; |
| 688 |
} |
| 689 |
} |
| 690 |
#endif |