| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2021 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "system.h" |
| 28 |
#include "mem.h" |
| 29 |
#include "error.h" |
| 30 |
|
| 31 |
#include "dpxfile.h" |
| 32 |
|
| 33 |
#include "numbers.h" |
| 34 |
#include "pdfobj.h" |
| 35 |
#include "pdfdev.h" /* pdf_rect */ |
| 36 |
|
| 37 |
#include "pdfencoding.h" |
| 38 |
#include "pdffont.h" |
| 39 |
|
| 40 |
#include "pkfont.h" |
| 41 |
|
| 42 |
#define ENABLE_GLYPHENC 1 |
| 43 |
|
| 44 |
#ifndef PKFONT_DPI_DEFAULT |
| 45 |
#define PKFONT_DPI_DEFAULT 600u |
| 46 |
#endif |
| 47 |
|
| 48 |
static unsigned base_dpi = PKFONT_DPI_DEFAULT; |
| 49 |
|
| 50 |
void |
| 51 |
PKFont_set_dpi (int dpi) |
| 52 |
{ |
| 53 |
if (dpi <= 0) |
| 54 |
ERROR("Invalid DPI: %d\n", dpi); |
| 55 |
base_dpi = dpi; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
/* (Only) This requires TFM to get design size... */ |
| 60 |
#include "tfm.h" |
| 61 |
|
| 62 |
static unsigned |
| 63 |
truedpi (const char *ident, double point_size, unsigned bdpi) |
| 64 |
{ |
| 65 |
unsigned dpi = bdpi; |
| 66 |
double design_size; |
| 67 |
int tfm_id; |
| 68 |
|
| 69 |
tfm_id = tfm_open(ident, 0); |
| 70 |
if (tfm_id < 0) |
| 71 |
return dpi; |
| 72 |
|
| 73 |
design_size = tfm_get_design_size(tfm_id); |
| 74 |
if (design_size <= 0.0) |
| 75 |
WARN("DESGIN_SIZE <= 0.0? (TFM=\"%s\")", ident); |
| 76 |
else { |
| 77 |
dpi = (unsigned) ROUND(base_dpi * point_size / design_size, 1.0); |
| 78 |
} |
| 79 |
|
| 80 |
return dpi; |
| 81 |
} |
| 82 |
|
| 83 |
static FILE * |
| 84 |
dpx_open_pk_font_at (const char *ident, unsigned dpi) |
| 85 |
{ |
| 86 |
FILE *fp; |
| 87 |
char *fqpn; |
| 88 |
kpse_glyph_file_type kpse_file_info; |
| 89 |
|
| 90 |
/* |
| 91 |
If pdftex.map is used, there are cases for example ident is like |
| 92 |
cmr10.pfb. Because of the suffix .pfb here, mktexpk does not |
| 93 |
work right. So we remove the suffix .pfb, if it exists. |
| 94 |
*/ |
| 95 |
fqpn = strrchr(ident, '.'); |
| 96 |
if (fqpn && strcasecmp(fqpn, ".pfb") == 0) { |
| 97 |
*fqpn = '\0'; |
| 98 |
} |
| 99 |
fqpn = kpse_find_glyph(ident, dpi, kpse_pk_format, &kpse_file_info); |
| 100 |
if (!fqpn) |
| 101 |
return NULL; |
| 102 |
fp = MFOPEN(fqpn, FOPEN_RBIN_MODE); |
| 103 |
RELEASE(fqpn); |
| 104 |
|
| 105 |
return fp; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
int |
| 110 |
pdf_font_open_pkfont (pdf_font *font, const char *ident, int index, int encoding_id, int embedding, double point_size) |
| 111 |
{ |
| 112 |
unsigned dpi; |
| 113 |
FILE *fp; |
| 114 |
|
| 115 |
if (!ident || point_size <= 0.0) |
| 116 |
return -1; |
| 117 |
if (!embedding) { |
| 118 |
WARN("Ignoring no-embed option for PK font: %s", ident); |
| 119 |
} |
| 120 |
if (index != 0) { |
| 121 |
WARN("Ignoring font index option for PK font: %s", ident); |
| 122 |
} |
| 123 |
|
| 124 |
dpi = truedpi(ident, point_size, base_dpi); |
| 125 |
fp = dpx_open_pk_font_at(ident, dpi); |
| 126 |
if (!fp) |
| 127 |
return -1; |
| 128 |
MFCLOSE(fp); |
| 129 |
|
| 130 |
/* Type 3 fonts doesn't have FontName. |
| 131 |
* FontFamily is recommended for PDF 1.5. |
| 132 |
*/ |
| 133 |
font->fontname = NEW(strlen(ident)+1, char); |
| 134 |
strcpy(font->fontname, ident); |
| 135 |
|
| 136 |
if (encoding_id >= 0) { |
| 137 |
pdf_encoding_used_by_type3(encoding_id); |
| 138 |
WARN("PK font is found for font \"%s\" but non built-in encoding \"%s\" is specified.", |
| 139 |
ident, pdf_encoding_get_name(encoding_id)); |
| 140 |
#if ENABLE_GLYPHENC |
| 141 |
WARN(">> Assuming this is for glyph name assignment."); |
| 142 |
#else |
| 143 |
WARN(">> I can't reencode PK font. (not enough information available)"); |
| 144 |
WARN(">> Maybe you need to install pfb/opentype/truetype font."); |
| 145 |
#endif |
| 146 |
} |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
/* We are using Mask Image. Fill black is bit clear. |
| 153 |
* Optimizing those codes doesn't improve things. |
| 154 |
*/ |
| 155 |
static uint32_t |
| 156 |
fill_black_run (unsigned char *dp, uint32_t left, uint32_t run_count) |
| 157 |
{ |
| 158 |
const static unsigned char mask[8] = { |
| 159 |
127u, 191u, 223u, 239u, 247u, 251u, 253u, 254u |
| 160 |
}; |
| 161 |
uint32_t right = left + run_count - 1; |
| 162 |
for ( ; left <= right; left++) { |
| 163 |
dp[left / 8] &= mask[left % 8]; |
| 164 |
} |
| 165 |
return run_count; |
| 166 |
} |
| 167 |
|
| 168 |
/* Just skip bits. See decode_packed() */ |
| 169 |
static uint32_t |
| 170 |
fill_white_run (uint32_t run_count) |
| 171 |
{ |
| 172 |
return run_count; |
| 173 |
} |
| 174 |
|
| 175 |
static uint32_t |
| 176 |
pk_packed_num (uint32_t *np, int dyn_f, unsigned char *dp, uint32_t pl) |
| 177 |
{ |
| 178 |
uint32_t nmbr = 0, i = *np; |
| 179 |
int nyb, j; |
| 180 |
#define get_nyb() ((i % 2) ? dp[i/2] & 0x0f : (dp[i/2] >> 4) & 0x0f) |
| 181 |
|
| 182 |
if (i / 2 == pl) { |
| 183 |
WARN("EOD reached while unpacking pk_packed_num."); |
| 184 |
return 0; |
| 185 |
} |
| 186 |
nyb = get_nyb(); i++; |
| 187 |
if (nyb == 0) { |
| 188 |
j = 0; |
| 189 |
do { |
| 190 |
if (i / 2 == pl) { |
| 191 |
WARN("EOD reached while unpacking pk_packed_num."); |
| 192 |
break; |
| 193 |
} |
| 194 |
nyb = get_nyb(); i++; |
| 195 |
j++; |
| 196 |
} while (nyb == 0); |
| 197 |
nmbr = nyb; |
| 198 |
while (j-- > 0) { |
| 199 |
if (i / 2 == pl) { |
| 200 |
WARN("EOD reached while unpacking pk_packed_num."); |
| 201 |
break; |
| 202 |
} |
| 203 |
nyb = get_nyb(); i++; |
| 204 |
nmbr = nmbr * 16 + nyb; |
| 205 |
} |
| 206 |
nmbr += (13 - dyn_f) * 16 + dyn_f - 15; |
| 207 |
} else if (nyb <= dyn_f) { |
| 208 |
nmbr = nyb; |
| 209 |
} else if (nyb < 14) { |
| 210 |
if (i / 2 == pl) { |
| 211 |
WARN("EOD reached while unpacking pk_packed_num."); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
nmbr = (nyb - dyn_f - 1) * 16 + get_nyb() + dyn_f + 1; |
| 215 |
i++; |
| 216 |
} |
| 217 |
|
| 218 |
*np = i; |
| 219 |
return nmbr; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
static void |
| 224 |
send_out (unsigned char *rowptr, uint32_t rowbytes, pdf_obj *stream) |
| 225 |
{ |
| 226 |
pdf_add_stream(stream, (void *)rowptr, rowbytes); |
| 227 |
} |
| 228 |
|
| 229 |
static int |
| 230 |
pk_decode_packed (pdf_obj *stream, uint32_t wd, uint32_t ht, |
| 231 |
int dyn_f, int run_color, unsigned char *dp, uint32_t pl) |
| 232 |
{ |
| 233 |
unsigned char *rowptr; |
| 234 |
uint32_t rowbytes; |
| 235 |
uint32_t i, np = 0; |
| 236 |
uint32_t run_count = 0, repeat_count = 0; |
| 237 |
|
| 238 |
rowbytes = (wd + 7) / 8; |
| 239 |
rowptr = NEW(rowbytes, unsigned char); |
| 240 |
/* repeat count is applied to the *current* row. |
| 241 |
* "run" can span across rows. |
| 242 |
* If there are non-zero repeat count and if run |
| 243 |
* spans across row, first repeat and then continue. |
| 244 |
*/ |
| 245 |
for (np = 0, i = 0; i < ht; i++) { |
| 246 |
uint32_t rowbits_left, nbits; |
| 247 |
|
| 248 |
repeat_count = 0; |
| 249 |
memset(rowptr, 0xff, rowbytes); /* 1 is white */ |
| 250 |
rowbits_left = wd; |
| 251 |
/* Fill run left over from previous row */ |
| 252 |
if (run_count > 0) { |
| 253 |
nbits = MIN(rowbits_left, run_count); |
| 254 |
switch (run_color) { |
| 255 |
case 0: |
| 256 |
rowbits_left -= fill_black_run(rowptr, 0, nbits); |
| 257 |
break; |
| 258 |
case 1: |
| 259 |
rowbits_left -= fill_white_run(nbits); |
| 260 |
break; |
| 261 |
} |
| 262 |
run_count -= nbits; |
| 263 |
} |
| 264 |
|
| 265 |
/* Read nybbles until we have a full row */ |
| 266 |
while (np / 2 < pl && rowbits_left > 0) { |
| 267 |
int nyb; |
| 268 |
|
| 269 |
nyb = (np % 2) ? dp[np/2] & 0x0f : (dp[np/2] >> 4) & 0x0f; |
| 270 |
if (nyb == 14) { /* packed number "repeat_count" follows */ |
| 271 |
if (repeat_count != 0) |
| 272 |
WARN("Second repeat count for this row!"); |
| 273 |
np++; /* Consume this nybble */ |
| 274 |
repeat_count = pk_packed_num(&np, dyn_f, dp, pl); |
| 275 |
} else if (nyb == 15) { |
| 276 |
if (repeat_count != 0) |
| 277 |
WARN("Second repeat count for this row!"); |
| 278 |
np++; /* Consume this nybble */ |
| 279 |
repeat_count = 1; |
| 280 |
} else { /* run_count */ |
| 281 |
/* Interprete current nybble as packed number */ |
| 282 |
run_count = pk_packed_num(&np, dyn_f, dp, pl); |
| 283 |
nbits = MIN(rowbits_left, run_count); |
| 284 |
run_color = !run_color; |
| 285 |
run_count -= nbits; |
| 286 |
switch (run_color) { |
| 287 |
case 0: |
| 288 |
rowbits_left -= fill_black_run(rowptr, wd - rowbits_left, nbits); |
| 289 |
break; |
| 290 |
case 1: |
| 291 |
rowbits_left -= fill_white_run(nbits); |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
/* We got bitmap row data. */ |
| 297 |
send_out(rowptr, rowbytes, stream); |
| 298 |
for ( ; i < ht && repeat_count > 0; repeat_count--, i++) |
| 299 |
send_out(rowptr, rowbytes, stream); |
| 300 |
} |
| 301 |
RELEASE(rowptr); |
| 302 |
|
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
static int |
| 307 |
pk_decode_bitmap (pdf_obj *stream, uint32_t wd, uint32_t ht, |
| 308 |
int dyn_f, int run_color, unsigned char *dp, uint32_t pl) |
| 309 |
{ |
| 310 |
unsigned char *rowptr, c; |
| 311 |
uint32_t i, j, rowbytes; |
| 312 |
const static unsigned char mask[8] = { |
| 313 |
0x80u, 0x40u, 0x20u, 0x10u, 0x08u, 0x04u, 0x02u, 0x01u |
| 314 |
}; |
| 315 |
|
| 316 |
ASSERT( dyn_f == 14 ); |
| 317 |
if (run_color != 0) { |
| 318 |
WARN("run_color != 0 for bitmap pk data?"); |
| 319 |
} else if (pl < (wd * ht + 7) / 8) { |
| 320 |
WARN("Insufficient bitmap pk data. %ldbytes expected but only %ldbytes read.", |
| 321 |
(wd * ht + 7) / 8, pl); |
| 322 |
return -1; |
| 323 |
} |
| 324 |
|
| 325 |
rowbytes = (wd + 7) / 8; |
| 326 |
rowptr = NEW(rowbytes, unsigned char); |
| 327 |
memset(rowptr, 0, rowbytes); |
| 328 |
/* Flip. PK bitmap is not byte aligned for each rows. */ |
| 329 |
for (i = 0, j = 0; i < ht * wd; i++) { |
| 330 |
c = dp[i / 8] & mask[i % 8]; |
| 331 |
if (c == 0) |
| 332 |
rowptr[j / 8] |= mask[i % 8]; /* flip bit */ |
| 333 |
j++; |
| 334 |
if (j == wd) { |
| 335 |
send_out(rowptr, rowbytes, stream); |
| 336 |
memset(rowptr, 0, rowbytes); |
| 337 |
j = 0; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
static void |
| 346 |
do_preamble (FILE *fp) |
| 347 |
{ |
| 348 |
/* Check for id byte */ |
| 349 |
if (fgetc(fp) == 89) { |
| 350 |
/* Skip comment */ |
| 351 |
skip_bytes(get_unsigned_byte(fp), fp); |
| 352 |
/* Skip other header info. It's normally used for verifying this |
| 353 |
is the file wethink it is */ |
| 354 |
skip_bytes(16, fp); |
| 355 |
} else { |
| 356 |
ERROR("embed_pk_font: PK ID byte is incorrect. Are you sure this is a PK file?"); |
| 357 |
} |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
struct pk_header_ |
| 362 |
{ |
| 363 |
uint32_t pkt_len; |
| 364 |
int32_t chrcode; |
| 365 |
int32_t wd, dx, dy; |
| 366 |
uint32_t bm_wd, bm_ht; |
| 367 |
int32_t bm_hoff, bm_voff; |
| 368 |
int dyn_f, run_color; |
| 369 |
}; |
| 370 |
|
| 371 |
static int |
| 372 |
read_pk_char_header (struct pk_header_ *h, unsigned char opcode, FILE *fp) |
| 373 |
{ |
| 374 |
ASSERT(h); |
| 375 |
|
| 376 |
if ((opcode & 4) == 0) { /* short */ |
| 377 |
h->pkt_len = (opcode & 3) << 8 | get_unsigned_byte(fp); |
| 378 |
h->chrcode = get_unsigned_byte(fp); |
| 379 |
h->wd = get_unsigned_triple(fp); /* TFM width */ |
| 380 |
h->dx = get_unsigned_byte(fp) << 16; /* horizontal escapement */ |
| 381 |
h->dy = 0; |
| 382 |
h->bm_wd = get_unsigned_byte(fp); |
| 383 |
h->bm_ht = get_unsigned_byte(fp); |
| 384 |
h->bm_hoff = get_signed_byte(fp); |
| 385 |
h->bm_voff = get_signed_byte(fp); |
| 386 |
h->pkt_len -= 8; |
| 387 |
} else if ((opcode & 7) == 7) { /* long */ |
| 388 |
h->pkt_len = get_positive_quad(fp, "PK", "pkt_len"); |
| 389 |
h->chrcode = get_signed_quad(fp); |
| 390 |
h->wd = get_signed_quad(fp); |
| 391 |
h->dx = get_signed_quad(fp); /* 16.16 fixed point number in pixels */ |
| 392 |
h->dy = get_signed_quad(fp); |
| 393 |
h->bm_wd = get_positive_quad(fp, "PK", "bm_wd"); |
| 394 |
h->bm_ht = get_positive_quad(fp, "PK", "bm_ht"); |
| 395 |
h->bm_hoff = get_signed_quad(fp); |
| 396 |
h->bm_voff = get_signed_quad(fp); |
| 397 |
h->pkt_len -= 28; |
| 398 |
} else { /* extended short */ |
| 399 |
h->pkt_len = (opcode & 3) << 16 | get_unsigned_pair(fp); |
| 400 |
h->chrcode = get_unsigned_byte(fp); |
| 401 |
h->wd = get_unsigned_triple(fp); |
| 402 |
h->dx = get_unsigned_pair(fp) << 16; |
| 403 |
h->dy = 0; |
| 404 |
h->bm_wd = get_unsigned_pair(fp); |
| 405 |
h->bm_ht = get_unsigned_pair(fp); |
| 406 |
h->bm_hoff = get_signed_pair(fp); |
| 407 |
h->bm_voff = get_signed_pair(fp); |
| 408 |
h->pkt_len -= 13; |
| 409 |
} |
| 410 |
|
| 411 |
h->dyn_f = opcode / 16; |
| 412 |
h->run_color = (opcode & 8) ? 1 : 0; |
| 413 |
|
| 414 |
if ((uint32_t)h->chrcode > 0xff) |
| 415 |
{ |
| 416 |
WARN("Unable to handle long characters in PK files: code=0x%04x", h->chrcode); |
| 417 |
return -1; |
| 418 |
} |
| 419 |
|
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
/* CCITT Group 4 filter may reduce file size. */ |
| 424 |
static pdf_obj * |
| 425 |
create_pk_CharProc_stream (struct pk_header_ *pkh, |
| 426 |
double chrwid, |
| 427 |
unsigned char *pkt_ptr, uint32_t pkt_len) |
| 428 |
{ |
| 429 |
pdf_obj *stream; /* charproc */ |
| 430 |
int32_t llx, lly, urx, ury; |
| 431 |
int len; |
| 432 |
|
| 433 |
llx = -pkh->bm_hoff; |
| 434 |
lly = pkh->bm_voff - pkh->bm_ht; |
| 435 |
urx = pkh->bm_wd - pkh->bm_hoff; |
| 436 |
ury = pkh->bm_voff; |
| 437 |
|
| 438 |
stream = pdf_new_stream(STREAM_COMPRESS); |
| 439 |
/* |
| 440 |
* The following line is a "metric" for the PDF reader: |
| 441 |
* |
| 442 |
* PDF Reference Reference, 4th ed., p.385. |
| 443 |
* |
| 444 |
* The wx (first operand of d1) must be consistent with the corresponding |
| 445 |
* width in the font's Widths array. The format string of sprint() must be |
| 446 |
* consistent with write_number() in pdfobj.c. |
| 447 |
*/ |
| 448 |
len = pdf_sprint_number(work_buffer, chrwid); |
| 449 |
len += sprintf (work_buffer + len, " 0 %d %d %d %d d1\n", llx, lly, urx, ury); |
| 450 |
pdf_add_stream(stream, work_buffer, len); |
| 451 |
/* |
| 452 |
* Acrobat dislike transformation [0 0 0 0 dx dy]. |
| 453 |
* PDF Reference, 4th ed., p.147, says, |
| 454 |
* |
| 455 |
* Use of a noninvertible matrix when painting graphics objects can result in |
| 456 |
* unpredictable behavior. |
| 457 |
* |
| 458 |
* but it does not forbid use of such transformation. |
| 459 |
*/ |
| 460 |
if (pkh->bm_wd != 0 && pkh->bm_ht != 0 && pkt_len > 0) { |
| 461 |
/* Scale and translate origin to lower left corner for raster data */ |
| 462 |
len = sprintf (work_buffer, "q\n%u 0 0 %u %d %d cm\n", pkh->bm_wd, pkh->bm_ht, llx, lly); |
| 463 |
pdf_add_stream(stream, work_buffer, len); |
| 464 |
len = sprintf (work_buffer, "BI\n/W %u\n/H %u\n/IM true\n/BPC 1\nID ", pkh->bm_wd, pkh->bm_ht); |
| 465 |
pdf_add_stream(stream, work_buffer, len); |
| 466 |
/* Add bitmap data */ |
| 467 |
if (pkh->dyn_f == 14) /* bitmap */ |
| 468 |
pk_decode_bitmap(stream, |
| 469 |
pkh->bm_wd, pkh->bm_ht, |
| 470 |
pkh->dyn_f, pkh->run_color, |
| 471 |
pkt_ptr, pkt_len); |
| 472 |
else |
| 473 |
pk_decode_packed(stream, |
| 474 |
pkh->bm_wd, pkh->bm_ht, |
| 475 |
pkh->dyn_f, pkh->run_color, |
| 476 |
pkt_ptr, pkt_len); |
| 477 |
len = sprintf (work_buffer, "\nEI\nQ"); |
| 478 |
pdf_add_stream(stream, work_buffer, len); |
| 479 |
} /* Otherwise we embed an empty stream :-( */ |
| 480 |
|
| 481 |
return stream; |
| 482 |
} |
| 483 |
|
| 484 |
#define PK_XXX1 240 |
| 485 |
#define PK_XXX2 241 |
| 486 |
#define PK_XXX3 242 |
| 487 |
#define PK_XXX4 243 |
| 488 |
#define PK_YYY 244 |
| 489 |
#define PK_POST 245 |
| 490 |
#define PK_NO_OP 246 |
| 491 |
#define PK_PRE 247 |
| 492 |
|
| 493 |
#define pk_char2name(b,c) sprintf((b), "x%02X", (unsigned char)(c)) |
| 494 |
int |
| 495 |
pdf_font_load_pkfont (pdf_font *font) |
| 496 |
{ |
| 497 |
pdf_obj *fontdict; |
| 498 |
char *usedchars; |
| 499 |
char *ident; |
| 500 |
unsigned dpi; |
| 501 |
FILE *fp; |
| 502 |
double point_size, pix2charu; |
| 503 |
int opcode, code, firstchar, lastchar, prev; |
| 504 |
pdf_obj *charprocs, *procset, *encoding, *tmp_array; |
| 505 |
double widths[256]; |
| 506 |
pdf_rect bbox; |
| 507 |
char charavail[256]; |
| 508 |
#if ENABLE_GLYPHENC |
| 509 |
int encoding_id; |
| 510 |
char **enc_vec; |
| 511 |
#endif /* ENABLE_GLYPHENC */ |
| 512 |
int error = 0; |
| 513 |
|
| 514 |
if (!font->reference) |
| 515 |
return 0; |
| 516 |
|
| 517 |
ident = font->filename; |
| 518 |
point_size = font->point_size; |
| 519 |
usedchars = font->usedchars; |
| 520 |
#if ENABLE_GLYPHENC |
| 521 |
encoding_id = font->encoding_id; |
| 522 |
if (encoding_id < 0) |
| 523 |
enc_vec = NULL; |
| 524 |
else { |
| 525 |
enc_vec = pdf_encoding_get_encoding(encoding_id); |
| 526 |
} |
| 527 |
#endif /* ENABLE_GLYPHENC */ |
| 528 |
|
| 529 |
ASSERT(ident && usedchars && point_size > 0.0); |
| 530 |
|
| 531 |
dpi = truedpi(ident, point_size, base_dpi); |
| 532 |
fp = dpx_open_pk_font_at(ident, dpi); |
| 533 |
if (!fp) { |
| 534 |
ERROR("Could not find/open PK font file: %s (at %udpi)", ident, dpi); |
| 535 |
} |
| 536 |
|
| 537 |
memset(charavail, 0, 256); |
| 538 |
charprocs = pdf_new_dict(); |
| 539 |
/* Include bitmap as 72dpi image: |
| 540 |
* There seems to be problems in "scaled" bitmap glyph |
| 541 |
* rendering in several viewers. |
| 542 |
*/ |
| 543 |
pix2charu = 72. * 1000. / ((double) base_dpi) / point_size; |
| 544 |
bbox.llx = bbox.lly = HUGE_VAL; |
| 545 |
bbox.urx = bbox.ury = -HUGE_VAL; |
| 546 |
while ((opcode = fgetc(fp)) >= 0 && opcode != PK_POST) { |
| 547 |
if (opcode < 240) { |
| 548 |
struct pk_header_ pkh; |
| 549 |
|
| 550 |
error = read_pk_char_header(&pkh, opcode, fp); |
| 551 |
if (error) |
| 552 |
ERROR("Error in reading PK character header."); |
| 553 |
else if (charavail[pkh.chrcode & 0xff]) |
| 554 |
WARN("More than two bitmap image for single glyph?: font=\"%s\" code=0x%02x", |
| 555 |
ident, pkh.chrcode); |
| 556 |
|
| 557 |
if (!usedchars[pkh.chrcode & 0xff]) |
| 558 |
skip_bytes(pkh.pkt_len, fp); |
| 559 |
else { |
| 560 |
char *charname; |
| 561 |
pdf_obj *charproc; |
| 562 |
unsigned char *pkt_ptr; |
| 563 |
size_t bytesread; |
| 564 |
double charwidth; |
| 565 |
|
| 566 |
/* Charwidth in PDF units */ |
| 567 |
charwidth = ROUND(1000.0 * pkh.wd / (((double) (1<<20))*pix2charu), 0.1); |
| 568 |
widths[pkh.chrcode & 0xff] = charwidth; |
| 569 |
|
| 570 |
/* Update font BBox info */ |
| 571 |
bbox.llx = MIN(bbox.llx, -pkh.bm_hoff); |
| 572 |
bbox.lly = MIN(bbox.lly, (double)pkh.bm_voff - (double)pkh.bm_ht); |
| 573 |
bbox.urx = MAX(bbox.urx, (double)pkh.bm_wd - (double)pkh.bm_hoff); |
| 574 |
bbox.ury = MAX(bbox.ury, pkh.bm_voff); |
| 575 |
|
| 576 |
pkt_ptr = NEW(pkh.pkt_len, unsigned char); |
| 577 |
if ((bytesread = fread(pkt_ptr, 1, pkh.pkt_len, fp))!= pkh.pkt_len) { |
| 578 |
ERROR("Only %ld bytes PK packet read. (expected %ld bytes)", |
| 579 |
bytesread, pkh.pkt_len); |
| 580 |
} |
| 581 |
charproc = create_pk_CharProc_stream(&pkh, charwidth, pkt_ptr, bytesread); |
| 582 |
RELEASE(pkt_ptr); |
| 583 |
if (!charproc) |
| 584 |
ERROR("Unpacking PK character data failed."); |
| 585 |
#if ENABLE_GLYPHENC |
| 586 |
if (encoding_id >= 0 && enc_vec) { |
| 587 |
charname = (char *) enc_vec[pkh.chrcode & 0xff]; |
| 588 |
if (!charname) { |
| 589 |
WARN("\".notdef\" glyph used in font (code=0x%02x): %s", pkh.chrcode, ident); |
| 590 |
charname = work_buffer; |
| 591 |
pk_char2name(charname, pkh.chrcode); |
| 592 |
} |
| 593 |
} |
| 594 |
else |
| 595 |
#endif /* ENABLE_GLYPHENC */ |
| 596 |
{ |
| 597 |
charname = work_buffer; |
| 598 |
pk_char2name(charname, pkh.chrcode); |
| 599 |
} |
| 600 |
|
| 601 |
pdf_add_dict(charprocs, pdf_new_name(charname), pdf_ref_obj(charproc)); /* _FIXME_ */ |
| 602 |
pdf_release_obj(charproc); |
| 603 |
} |
| 604 |
charavail[pkh.chrcode & 0xff] = 1; |
| 605 |
} else { /* A command byte */ |
| 606 |
switch (opcode) { |
| 607 |
case PK_NO_OP: break; |
| 608 |
case PK_XXX1: case PK_XXX2: case PK_XXX3: case PK_XXX4: |
| 609 |
{ |
| 610 |
int32_t len = get_unsigned_num(fp, opcode-PK_XXX1); |
| 611 |
if (len < 0) |
| 612 |
WARN("PK: Special with %d bytes???", len); |
| 613 |
else |
| 614 |
skip_bytes(len, fp); |
| 615 |
break; |
| 616 |
} |
| 617 |
case PK_YYY: skip_bytes(4, fp); break; |
| 618 |
case PK_PRE: do_preamble(fp); break; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
MFCLOSE(fp); |
| 623 |
|
| 624 |
/* Check if we really got all glyphs needed. */ |
| 625 |
for (code = 0; code < 256; code++) { |
| 626 |
if (usedchars[code] && !charavail[code]) |
| 627 |
WARN("Missing glyph code=0x%02x in PK font \"%s\".", code, ident); |
| 628 |
} |
| 629 |
|
| 630 |
/* Now actually fill fontdict. */ |
| 631 |
fontdict = pdf_font_get_resource(font); |
| 632 |
|
| 633 |
pdf_add_dict(fontdict, |
| 634 |
pdf_new_name("CharProcs"), pdf_ref_obj(charprocs)); |
| 635 |
pdf_release_obj(charprocs); |
| 636 |
|
| 637 |
/* |
| 638 |
* Resources: |
| 639 |
* |
| 640 |
* PDF Reference 4th ed. describes it as "Optional but strongly recommended". |
| 641 |
* There are no reason to put it in our case, but we will put this. |
| 642 |
* We do not care about compatibility with Acrobat 2.x. (See implementation |
| 643 |
* note 47, Appendix H of PDF Ref., 4th ed.). |
| 644 |
*/ |
| 645 |
procset = pdf_new_dict(); |
| 646 |
tmp_array = pdf_new_array(); |
| 647 |
pdf_add_array(tmp_array, pdf_new_name("PDF")); |
| 648 |
pdf_add_array(tmp_array, pdf_new_name("ImageB")); |
| 649 |
pdf_add_dict(procset, |
| 650 |
pdf_new_name("ProcSet"), tmp_array); |
| 651 |
pdf_add_dict(fontdict, |
| 652 |
pdf_new_name("Resources"), procset); |
| 653 |
|
| 654 |
/* Encoding */ |
| 655 |
tmp_array = pdf_new_array(); |
| 656 |
prev = -2; firstchar = 255; lastchar = 0; |
| 657 |
for (code = 0; code < 256; code++) { |
| 658 |
char *charname; |
| 659 |
if (usedchars[code]) { |
| 660 |
if (code < firstchar) firstchar = code; |
| 661 |
if (code > lastchar) lastchar = code; |
| 662 |
if (code != prev + 1) |
| 663 |
pdf_add_array(tmp_array, pdf_new_number(code)); |
| 664 |
|
| 665 |
#if ENABLE_GLYPHENC |
| 666 |
if (encoding_id >= 0 && enc_vec) { |
| 667 |
charname = (char *) enc_vec[(unsigned char) code]; |
| 668 |
if (!charname) { |
| 669 |
charname = work_buffer; |
| 670 |
pk_char2name(charname, code); |
| 671 |
} |
| 672 |
} |
| 673 |
else |
| 674 |
#endif /* ENABLE_GLYPHENC */ |
| 675 |
{ |
| 676 |
charname = work_buffer; |
| 677 |
pk_char2name(charname, code); |
| 678 |
} |
| 679 |
pdf_add_array(tmp_array, pdf_new_name(charname)); |
| 680 |
prev = code; |
| 681 |
} |
| 682 |
} |
| 683 |
if (firstchar > lastchar) { |
| 684 |
ERROR("Unexpected error: firstchar > lastchar (%d %d)", |
| 685 |
firstchar, lastchar); |
| 686 |
pdf_release_obj(tmp_array); |
| 687 |
return -1; |
| 688 |
} |
| 689 |
#if ENABLE_GLYPHENC |
| 690 |
if (encoding_id < 0 || !enc_vec) { |
| 691 |
#else |
| 692 |
if (1) { |
| 693 |
#endif /* ENABLE_GLYPHENC */ |
| 694 |
encoding = pdf_new_dict(); |
| 695 |
pdf_add_dict(encoding, |
| 696 |
pdf_new_name("Type"), pdf_new_name("Encoding")); |
| 697 |
pdf_add_dict(encoding, |
| 698 |
pdf_new_name("Differences"), tmp_array); |
| 699 |
pdf_add_dict(fontdict, |
| 700 |
pdf_new_name("Encoding"), pdf_ref_obj(encoding)); |
| 701 |
pdf_release_obj(encoding); |
| 702 |
} else |
| 703 |
pdf_release_obj(tmp_array); |
| 704 |
|
| 705 |
/* FontBBox: Accurate value is important. |
| 706 |
*/ |
| 707 |
tmp_array = pdf_new_array(); |
| 708 |
pdf_add_array(tmp_array, pdf_new_number(bbox.llx)); |
| 709 |
pdf_add_array(tmp_array, pdf_new_number(bbox.lly)); |
| 710 |
pdf_add_array(tmp_array, pdf_new_number(bbox.urx)); |
| 711 |
pdf_add_array(tmp_array, pdf_new_number(bbox.ury)); |
| 712 |
pdf_add_dict (fontdict , pdf_new_name("FontBBox"), tmp_array); |
| 713 |
|
| 714 |
/* Widths: |
| 715 |
* Indirect reference preffered. (See PDF Reference) |
| 716 |
*/ |
| 717 |
tmp_array = pdf_new_array(); |
| 718 |
for (code = firstchar; code <= lastchar; code++) { |
| 719 |
if (usedchars[code]) |
| 720 |
pdf_add_array(tmp_array, pdf_new_number(widths[code])); |
| 721 |
else { |
| 722 |
pdf_add_array(tmp_array, pdf_new_number(0)); |
| 723 |
} |
| 724 |
} |
| 725 |
pdf_add_dict(fontdict, |
| 726 |
pdf_new_name("Widths"), pdf_ref_obj(tmp_array)); |
| 727 |
pdf_release_obj(tmp_array); |
| 728 |
|
| 729 |
/* FontMatrix */ |
| 730 |
tmp_array = pdf_new_array(); |
| 731 |
pdf_add_array(tmp_array, pdf_new_number(0.001 * pix2charu)); |
| 732 |
pdf_add_array(tmp_array, pdf_new_number(0.0)); |
| 733 |
pdf_add_array(tmp_array, pdf_new_number(0.0)); |
| 734 |
pdf_add_array(tmp_array, pdf_new_number(0.001 * pix2charu)); |
| 735 |
pdf_add_array(tmp_array, pdf_new_number(0.0)); |
| 736 |
pdf_add_array(tmp_array, pdf_new_number(0.0)); |
| 737 |
pdf_add_dict (fontdict , pdf_new_name("FontMatrix"), tmp_array); |
| 738 |
|
| 739 |
|
| 740 |
pdf_add_dict(fontdict, |
| 741 |
pdf_new_name("FirstChar"), pdf_new_number(firstchar)); |
| 742 |
pdf_add_dict(fontdict, |
| 743 |
pdf_new_name("LastChar"), pdf_new_number(lastchar)); |
| 744 |
|
| 745 |
return 0; |
| 746 |
} |