| 1 |
/* writet3.c: write a Type 3 (bitmap PK) font. |
| 2 |
Copyright 1996-2019 Han The Thanh, <[email protected]> |
| 3 |
|
| 4 |
This file is part of pdfTeX. |
| 5 |
|
| 6 |
pdfTeX is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
pdfTeX is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License along |
| 17 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "ptexlib.h" |
| 21 |
#include <kpathsea/tex-glyph.h> |
| 22 |
#include <kpathsea/magstep.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#define T3_BUF_SIZE 1024 |
| 26 |
|
| 27 |
typedef char t3_line_entry; |
| 28 |
define_array(t3_line); |
| 29 |
|
| 30 |
FILE *t3_file; |
| 31 |
static boolean t3_image_used; |
| 32 |
|
| 33 |
static integer t3_char_procs[256]; |
| 34 |
static float t3_char_widths[256]; |
| 35 |
static int t3_glyph_num; |
| 36 |
static float t3_font_scale; |
| 37 |
static integer t3_b0, t3_b1, t3_b2, t3_b3; |
| 38 |
static boolean is_pk_font; |
| 39 |
|
| 40 |
#define t3_open() \ |
| 41 |
open_input(&t3_file, kpse_miscfonts_format, FOPEN_RBIN_MODE) |
| 42 |
#define t3_close() xfclose(t3_file, cur_file_name) |
| 43 |
#define t3_getchar() xgetc(t3_file) |
| 44 |
#define t3_eof() feof(t3_file) |
| 45 |
#define t3_prefix(s) str_prefix(t3_line_array, s) |
| 46 |
#define t3_putchar(c) pdfout(c) |
| 47 |
|
| 48 |
#define t3_check_eof() \ |
| 49 |
if (t3_eof()) \ |
| 50 |
pdftex_fail("unexpected end of file"); |
| 51 |
|
| 52 |
static void t3_getline(void) |
| 53 |
{ |
| 54 |
int c; |
| 55 |
restart: |
| 56 |
t3_line_ptr = t3_line_array; |
| 57 |
c = t3_getchar(); |
| 58 |
while (!t3_eof()) { |
| 59 |
alloc_array(t3_line, 1, T3_BUF_SIZE); |
| 60 |
append_char_to_buf(c, t3_line_ptr, t3_line_array, t3_line_limit); |
| 61 |
if (c == 10) |
| 62 |
break; |
| 63 |
c = t3_getchar(); |
| 64 |
} |
| 65 |
alloc_array(t3_line, 2, T3_BUF_SIZE); |
| 66 |
append_eol(t3_line_ptr, t3_line_array, T3_BUF_SIZE); |
| 67 |
if (t3_line_ptr - t3_line_array < 2 || *t3_line_array == '%') { |
| 68 |
if (!t3_eof()) |
| 69 |
goto restart; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
static void t3_putline(void) |
| 74 |
{ |
| 75 |
char *p = t3_line_array; |
| 76 |
while (p < t3_line_ptr) |
| 77 |
t3_putchar(*p++); |
| 78 |
} |
| 79 |
|
| 80 |
static void update_bbox(integer llx, integer lly, integer urx, integer ury, |
| 81 |
boolean is_first_glyph) |
| 82 |
{ |
| 83 |
if (is_first_glyph) { |
| 84 |
t3_b0 = llx; |
| 85 |
t3_b1 = lly; |
| 86 |
t3_b2 = urx; |
| 87 |
t3_b3 = ury; |
| 88 |
} else { |
| 89 |
if (llx < t3_b0) |
| 90 |
t3_b0 = llx; |
| 91 |
if (lly < t3_b1) |
| 92 |
t3_b1 = lly; |
| 93 |
if (urx > t3_b2) |
| 94 |
t3_b2 = urx; |
| 95 |
if (ury > t3_b3) |
| 96 |
t3_b3 = ury; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
static void t3_write_glyph(internalfontnumber f) |
| 101 |
{ |
| 102 |
static char t3_begin_glyph_str[] = "\\pdfglyph"; |
| 103 |
static char t3_end_glyph_str[] = "\\endglyph"; |
| 104 |
int glyph_index; |
| 105 |
int width, height, depth, llx, lly, urx, ury; |
| 106 |
char *p; |
| 107 |
t3_getline(); |
| 108 |
if (t3_prefix(t3_begin_glyph_str)) { |
| 109 |
if (sscanf(t3_line_array + strlen(t3_begin_glyph_str) + 1, |
| 110 |
"%i %i %i %i %i %i %i %i =", &glyph_index, |
| 111 |
&width, &height, &depth, &llx, &lly, &urx, &ury) != 8) { |
| 112 |
remove_eol(p, t3_line_array); |
| 113 |
pdftex_fail("invalid glyph preamble: `%s'", t3_line_array); |
| 114 |
} |
| 115 |
if (glyph_index < fontbc[f] || glyph_index > fontec[f]) |
| 116 |
return; |
| 117 |
} else |
| 118 |
return; |
| 119 |
if (!pdfcharmarked(f, glyph_index)) { |
| 120 |
while (!t3_prefix(t3_end_glyph_str)) { |
| 121 |
t3_check_eof(); |
| 122 |
t3_getline(); |
| 123 |
} |
| 124 |
return; |
| 125 |
} |
| 126 |
update_bbox(llx, lly, urx, ury, t3_glyph_num == 0); |
| 127 |
t3_glyph_num++; |
| 128 |
pdfnewdict(0, 0, 0); |
| 129 |
t3_char_procs[glyph_index] = objptr; |
| 130 |
if (width == 0) |
| 131 |
t3_char_widths[glyph_index] = |
| 132 |
(getcharwidth(f, glyph_index) / t3_font_scale) / pdffontsize[f]; |
| 133 |
else |
| 134 |
t3_char_widths[glyph_index] = width; |
| 135 |
pdfbeginstream(); |
| 136 |
t3_getline(); |
| 137 |
pdf_printf("%i 0 %i %i %i %i d1\nq\n", |
| 138 |
(int) t3_char_widths[glyph_index], (int) llx, |
| 139 |
(int) lly, (int) urx, (int) ury); |
| 140 |
while (!t3_prefix(t3_end_glyph_str)) { |
| 141 |
t3_check_eof(); |
| 142 |
if (t3_prefix("BI")) |
| 143 |
t3_image_used = true; |
| 144 |
t3_putline(); |
| 145 |
t3_getline(); |
| 146 |
} |
| 147 |
pdf_puts("Q\n"); |
| 148 |
pdfendstream(); |
| 149 |
} |
| 150 |
|
| 151 |
static integer get_pk_font_scale(internalfontnumber f) |
| 152 |
{ |
| 153 |
return |
| 154 |
dividescaled(pkscalefactor, |
| 155 |
dividescaled(pdffontsize[f], onehundredbp, |
| 156 |
fixeddecimaldigits + 2), 0); |
| 157 |
} |
| 158 |
|
| 159 |
static integer pk_char_width(internalfontnumber f, scaled w) |
| 160 |
{ |
| 161 |
return |
| 162 |
dividescaled(dividescaled(w, pdffontsize[f], 7), |
| 163 |
get_pk_font_scale(f), 0); |
| 164 |
} |
| 165 |
|
| 166 |
scaled getpkcharwidth(internalfontnumber f, scaled w) |
| 167 |
{ |
| 168 |
return (get_pk_font_scale(f) / 100000.0) * |
| 169 |
(pk_char_width(f, w) / 100.0) * pdffontsize[f]; |
| 170 |
} |
| 171 |
|
| 172 |
static boolean writepk(internalfontnumber f) |
| 173 |
{ |
| 174 |
kpse_glyph_file_type font_ret; |
| 175 |
integer llx, lly, urx, ury; |
| 176 |
integer cw, rw, i, j; |
| 177 |
halfword *row; |
| 178 |
char *name; |
| 179 |
chardesc cd; |
| 180 |
boolean is_null_glyph, check_preamble; |
| 181 |
integer dpi; |
| 182 |
dpi = |
| 183 |
kpse_magstep_fix(round |
| 184 |
(fixedpkresolution * |
| 185 |
(((float) pdffontsize[f]) / fontdsize[f])), |
| 186 |
fixedpkresolution, NULL); |
| 187 |
cur_file_name = makecstring(fontname[f]); |
| 188 |
name = kpse_find_pk(cur_file_name, (unsigned) dpi, &font_ret); |
| 189 |
if (name == NULL || |
| 190 |
!FILESTRCASEEQ(cur_file_name, font_ret.name) || |
| 191 |
!kpse_bitmap_tolerance((float) font_ret.dpi, (float) dpi)) { |
| 192 |
pdftex_fail("Font %s at %i not found", cur_file_name, (int) dpi); |
| 193 |
} |
| 194 |
t3_file = xfopen(name, FOPEN_RBIN_MODE); |
| 195 |
recorder_record_input(name); |
| 196 |
t3_image_used = true; |
| 197 |
is_pk_font = true; |
| 198 |
tex_printf(" <%s", (char *) name); |
| 199 |
cd.rastersize = 256; |
| 200 |
cd.raster = xtalloc(cd.rastersize, halfword); |
| 201 |
check_preamble = true; |
| 202 |
while (readchar(check_preamble, &cd) != 0) { |
| 203 |
check_preamble = false; |
| 204 |
if (!pdfcharmarked(f, cd.charcode)) |
| 205 |
continue; |
| 206 |
t3_char_widths[cd.charcode] = |
| 207 |
pk_char_width(f, getcharwidth(f, cd.charcode)); |
| 208 |
if (cd.cwidth < 1 || cd.cheight < 1) { |
| 209 |
cd.xescape = cd.cwidth = round(t3_char_widths[cd.charcode] / 100.0); |
| 210 |
cd.cheight = 1; |
| 211 |
cd.xoff = 0; |
| 212 |
cd.yoff = 0; |
| 213 |
is_null_glyph = true; |
| 214 |
} else |
| 215 |
is_null_glyph = false; |
| 216 |
llx = -cd.xoff; |
| 217 |
lly = cd.yoff - cd.cheight + 1; |
| 218 |
urx = cd.cwidth + llx + 1; |
| 219 |
ury = cd.cheight + lly; |
| 220 |
update_bbox(llx, lly, urx, ury, t3_glyph_num == 0); |
| 221 |
t3_glyph_num++; |
| 222 |
pdfnewdict(0, 0, 0); |
| 223 |
t3_char_procs[cd.charcode] = objptr; |
| 224 |
pdfbeginstream(); |
| 225 |
pdfprintreal(t3_char_widths[cd.charcode], 2); |
| 226 |
pdf_printf(" 0 %i %i %i %i d1\n", |
| 227 |
(int) llx, (int) lly, (int) urx, (int) ury); |
| 228 |
if (is_null_glyph) |
| 229 |
goto end_stream; |
| 230 |
pdf_printf("q\n%i 0 0 %i %i %i cm\nBI\n", (int) cd.cwidth, |
| 231 |
(int) cd.cheight, (int) llx, (int) lly); |
| 232 |
pdf_printf("/W %i\n/H %i\n", (int) cd.cwidth, (int) cd.cheight); |
| 233 |
pdf_puts("/IM true\n/BPC 1\n/D [1 0]\nID "); |
| 234 |
cw = (cd.cwidth + 7) / 8; |
| 235 |
rw = (cd.cwidth + 15) / 16; |
| 236 |
row = cd.raster; |
| 237 |
for (i = 0; i < cd.cheight; i++) { |
| 238 |
for (j = 0; j < rw - 1; j++) { |
| 239 |
pdfout(*row / 256); |
| 240 |
pdfout(*row % 256); |
| 241 |
row++; |
| 242 |
} |
| 243 |
pdfout(*row / 256); |
| 244 |
if (2 * rw == cw) |
| 245 |
pdfout(*row % 256); |
| 246 |
row++; |
| 247 |
} |
| 248 |
pdf_puts("\nEI\nQ\n"); |
| 249 |
end_stream: |
| 250 |
pdfendstream(); |
| 251 |
} |
| 252 |
xfree(cd.raster); |
| 253 |
cur_file_name = NULL; |
| 254 |
return true; |
| 255 |
} |
| 256 |
|
| 257 |
/* We must remove duplicate glyph names since the duplicates render the |
| 258 |
output invalid. We give a warning, since this should only happen with |
| 259 |
ill-defined .enc files that should get fixed. */ |
| 260 |
static void remove_duplicate_glyph_names(char **g, const char *encname) |
| 261 |
{ |
| 262 |
struct avl_table *gl_tree; |
| 263 |
char *aa; |
| 264 |
int i; |
| 265 |
gl_tree = avl_create(comp_string_entry, NULL, &avl_xallocator); |
| 266 |
assert(gl_tree != NULL); |
| 267 |
for (i = 0; i < 256; i++) { |
| 268 |
if (g[i] == notdef) |
| 269 |
continue; |
| 270 |
aa = (char *) avl_find(gl_tree, g[i]); |
| 271 |
if (aa == NULL) { |
| 272 |
aa = (char *) avl_probe(gl_tree, g[i]); |
| 273 |
assert(aa != NULL); |
| 274 |
} else { |
| 275 |
pdftex_warn("%s: duplicate glyph name at position %d: %s", |
| 276 |
encname, i, g[i]); |
| 277 |
xfree(g[i]); |
| 278 |
g[i] = (char *) notdef; |
| 279 |
} |
| 280 |
} |
| 281 |
avl_destroy(gl_tree, NULL); |
| 282 |
} |
| 283 |
|
| 284 |
void writet3(fm_entry * fm, int objnum, internalfontnumber f) |
| 285 |
{ |
| 286 |
static char t3_font_scale_str[] = "\\pdffontscale"; |
| 287 |
int i; |
| 288 |
integer wptr, eptr, cptr; |
| 289 |
int first_char, last_char; |
| 290 |
integer pk_font_scale; |
| 291 |
boolean is_notdef; |
| 292 |
fe_entry *fe; |
| 293 |
char **glyph_names; |
| 294 |
integer tounicode_objnum; |
| 295 |
t3_glyph_num = 0; |
| 296 |
t3_image_used = false; |
| 297 |
for (i = 0; i < 256; i++) { |
| 298 |
t3_char_procs[i] = 0; |
| 299 |
t3_char_widths[i] = 0; |
| 300 |
} |
| 301 |
fe = fm && fm->encname ? get_fe_entry(fm->encname) : NULL; |
| 302 |
glyph_names = fe ? fe->glyph_names : NULL; |
| 303 |
if (glyph_names) |
| 304 |
remove_duplicate_glyph_names(glyph_names, fm->encname); |
| 305 |
packfilename(fontname[f], getnullstr(), maketexstring(".pgc")); |
| 306 |
cur_file_name = makecstring(makenamestring()); |
| 307 |
is_pk_font = false; |
| 308 |
if (!t3_open()) { |
| 309 |
if (writepk(f)) |
| 310 |
goto write_font_dict; |
| 311 |
else { |
| 312 |
cur_file_name = NULL; |
| 313 |
return; |
| 314 |
} |
| 315 |
} |
| 316 |
tex_printf("<%s", nameoffile + 1); |
| 317 |
t3_getline(); |
| 318 |
if (!t3_prefix(t3_font_scale_str) || |
| 319 |
sscanf(t3_line_array + strlen(t3_font_scale_str) + 1, "%g", |
| 320 |
&t3_font_scale) < 1 || t3_font_scale <= 0 |
| 321 |
|| t3_font_scale > 1000) { |
| 322 |
pdftex_fail("missing or invalid font scale"); |
| 323 |
} |
| 324 |
while (!t3_eof()) |
| 325 |
t3_write_glyph(f); |
| 326 |
write_font_dict: |
| 327 |
for (i = fontbc[f]; i <= fontec[f]; i++) |
| 328 |
if (pdfcharmarked(f, i)) |
| 329 |
break; |
| 330 |
first_char = i; |
| 331 |
for (i = fontec[f]; i > first_char; i--) |
| 332 |
if (pdfcharmarked(f, i)) |
| 333 |
break; |
| 334 |
last_char = i; |
| 335 |
/* write ToUnicode entry if we can */ |
| 336 |
if (fixedgentounicode > 0 && !pdffontnobuiltintounicode[f] && fe != NULL) |
| 337 |
tounicode_objnum = write_tounicode(glyph_names, fm->tfm_name, fe->name); |
| 338 |
else |
| 339 |
tounicode_objnum = 0; |
| 340 |
pdfbegindict(objnum, 1); /* Type 3 font dictionary */ |
| 341 |
pdf_puts("/Type /Font\n/Subtype /Type3\n"); |
| 342 |
pdf_printf("/Name /F%i\n", (int) f); |
| 343 |
if (pdffontattr[f] != getnullstr()) { |
| 344 |
pdfprint(pdffontattr[f]); |
| 345 |
pdf_puts("\n"); |
| 346 |
} |
| 347 |
if (is_pk_font) { |
| 348 |
pk_font_scale = get_pk_font_scale(f); |
| 349 |
pdf_puts("/FontMatrix ["); |
| 350 |
pdfprintreal(pk_font_scale, 5); |
| 351 |
pdf_puts(" 0 0 "); |
| 352 |
pdfprintreal(pk_font_scale, 5); |
| 353 |
pdf_puts(" 0 0]\n"); |
| 354 |
} else |
| 355 |
pdf_printf("/FontMatrix [%g 0 0 %g 0 0]\n", |
| 356 |
(double) t3_font_scale, (double) t3_font_scale); |
| 357 |
pdf_printf("/%s [ %i %i %i %i ]\n", |
| 358 |
font_key[FONTBBOX1_CODE].pdfname, |
| 359 |
(int) t3_b0, (int) t3_b1, (int) t3_b2, (int) t3_b3); |
| 360 |
pdf_printf("/Resources << /ProcSet [ /PDF %s] >>\n", |
| 361 |
t3_image_used ? "/ImageB " : ""); |
| 362 |
pdf_printf("/FirstChar %i\n/LastChar %i\n", first_char, last_char); |
| 363 |
wptr = pdfnewobjnum(); |
| 364 |
eptr = pdfnewobjnum(); |
| 365 |
cptr = pdfnewobjnum(); |
| 366 |
pdf_printf("/Widths %i 0 R\n/Encoding %i 0 R\n/CharProcs %i 0 R\n", |
| 367 |
(int) wptr, (int) eptr, (int) cptr); |
| 368 |
if (tounicode_objnum != 0) |
| 369 |
pdf_printf("/ToUnicode %i 0 R\n", (int) tounicode_objnum); |
| 370 |
pdfenddict(); |
| 371 |
pdfbeginobj(wptr, 1); /* chars width array */ |
| 372 |
pdf_puts("["); |
| 373 |
if (is_pk_font) |
| 374 |
for (i = first_char; i <= last_char; i++) { |
| 375 |
pdfprintreal(t3_char_widths[i], 2); |
| 376 |
pdf_puts(" "); |
| 377 |
} else |
| 378 |
for (i = first_char; i <= last_char; i++) |
| 379 |
pdf_printf("%i ", (int) t3_char_widths[i]); |
| 380 |
pdf_puts("]\n"); |
| 381 |
pdfendobj(); |
| 382 |
pdfbegindict(eptr, 1); /* encoding dictionary */ |
| 383 |
pdf_printf("/Type /Encoding\n/Differences [%i", first_char); |
| 384 |
if (t3_char_procs[first_char] == 0) { |
| 385 |
pdf_printf("/%s", notdef); |
| 386 |
is_notdef = true; |
| 387 |
} else { |
| 388 |
if (glyph_names |
| 389 |
&& glyph_names[first_char] |
| 390 |
&& glyph_names[first_char] != notdef) |
| 391 |
pdf_printf("/%s", glyph_names[first_char]); |
| 392 |
else |
| 393 |
pdf_printf("/a%i", first_char); |
| 394 |
is_notdef = false; |
| 395 |
} |
| 396 |
for (i = first_char + 1; i <= last_char; i++) { |
| 397 |
if (t3_char_procs[i] == 0) { |
| 398 |
if (!is_notdef) { |
| 399 |
pdf_printf(" %i/%s", i, notdef); |
| 400 |
is_notdef = true; |
| 401 |
} |
| 402 |
} else { |
| 403 |
if (is_notdef) { |
| 404 |
pdf_printf(" %i", i); |
| 405 |
is_notdef = false; |
| 406 |
} |
| 407 |
if (glyph_names && glyph_names[i] && glyph_names[i] != notdef) |
| 408 |
pdf_printf("/%s", glyph_names[i]); |
| 409 |
else |
| 410 |
pdf_printf("/a%i", i); |
| 411 |
} |
| 412 |
} |
| 413 |
pdf_puts("]\n"); |
| 414 |
pdfenddict(); |
| 415 |
pdfbegindict(cptr, 1); /* CharProcs dictionary */ |
| 416 |
for (i = first_char; i <= last_char; i++) |
| 417 |
if (t3_char_procs[i] != 0) { |
| 418 |
if (glyph_names && glyph_names[i] && glyph_names[i] != notdef) |
| 419 |
pdf_printf("/%s %i 0 R\n", glyph_names[i], |
| 420 |
(int) t3_char_procs[i]); |
| 421 |
else |
| 422 |
pdf_printf("/a%i %i 0 R\n", (int) i, |
| 423 |
(int) t3_char_procs[i]); |
| 424 |
} |
| 425 |
pdfenddict(); |
| 426 |
t3_close(); |
| 427 |
tex_printf(">"); |
| 428 |
cur_file_name = NULL; |
| 429 |
} |