1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
|
/* Copyright (C) 2001-2021 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* plchar.c */
/* PCL font handling library -- operations on individual characters */
#include "math_.h"
#include "memory_.h"
#include "stdio_.h" /* for gdebug.h */
#include "gdebug.h"
#include "gserrors.h"
#include "gstypes.h"
#include "gsmemory.h"
#include "gsstruct.h"
#include "gsmatrix.h"
#include "gsstate.h"
#include "gschar.h"
#include "gsimage.h"
#include "gspaint.h"
#include "gspath.h"
#include "gsbittab.h"
#include "gxarith.h" /* for igcd */
#include "gxfont.h"
#include "gxfont42.h"
#include "plfont.h"
#include "plvalue.h"
#include "gscoord.h"
#include "gsstate.h"
#include "gxdevice.h"
#include "gxdevmem.h"
#include "gxpath.h"
/* We really shouldn't need the following, but currently they are needed */
/* for pgs->path and penum->log2_current_scale in pl_tt_build_char. */
#include "gxfixed.h"
#include "gxchar.h"
#include "gxfcache.h"
#include "gzstate.h"
/* agfa includes */
#undef true
#undef false
#undef frac_bits
#include "cgconfig.h"
#include "ufstport.h"
#include "shareinc.h"
#include "gxfapiu.h"
#include "plchar.h"
#if UFST_VERSION_MAJOR >= 6 && UFST_VERSION_MINOR >= 2
#undef true
#undef false
#define false FALSE
#define true TRUE
#define UNICODE UFST_UNICODE
#endif
/* ---------------- UFST utilities ---------------- */
#define UFST_SCALE 16
#define FXD_ONE (1L << UFST_SCALE) /* fixed point 1.0000 */
static const pl_font_t *plfont_last; /* last selected font */
static const gs_matrix pl_identmtx = { identity_matrix_body };
extern IF_STATE if_state;
extern PIF_STATE pIFS;
/*
* Set up a generic FONTCONTEXT structure.
*
* NB: UFST automatically inverts the y-axis when generating a bitmap, so
* it is necessary to account for that change in this routine.
*/
static void
pl_init_fc(const pl_font_t * plfont,
gs_gstate * pgs,
int need_outline, FONTCONTEXT * pfc, bool width_request)
{
gs_font *pfont = plfont->pfont;
/* set the current tranformation matrix - EM's... if this is a
width request we don't necessarily have a current graphics
state... use identity for resolution and ctm */
gs_matrix mat;
double xres, yres;
if (width_request) {
gs_make_identity(&mat);
xres = yres = 1;
} else {
gs_currentmatrix(pgs, &mat);
xres = gs_currentdevice(pgs)->HWResolution[0];
yres = gs_currentdevice(pgs)->HWResolution[1];
}
pfc->font_id = 0;
pfc->xspot = F_ONE;
pfc->yspot = F_ONE;
/* symbol set id used for no symbol set mapping */
pfc->ssnum = 0x8000;
/* filled in if downloaded font and/or symbol set. */
pfc->font_hdr = NULL;
pfc->dl_ssnum = 0;
/* union selector for transformation type m0..m3 - pcl uses pt.
size */
pfc->fc_type = FC_MAT2_TYPE;
/* calculate point size, set size etc based on current CTM in EM's */
{
double hx = hypot(mat.xx, mat.xy);
double hy = hypot(mat.yx, mat.yy);
/* fixed point scaling */
double mscale = 1L << 16;
pfc->s.m2.matrix_scale = 16;
pfc->s.m2.point_size = (int)((hy * plfont->pts_per_inch / yres) + 0.5) * 8; /* 1/8ths */
pfc->s.m2.set_size =
(int)((hx * plfont->pts_per_inch / xres) + 0.5) * 8;
pfc->s.m2.m[0] = mscale * mat.xx / hx;
pfc->s.m2.m[1] = mscale * -mat.xy / hx;
pfc->s.m2.m[2] = mscale * mat.yx / hy;
pfc->s.m2.m[3] = mscale * -mat.yy / hy;
pfc->s.m2.world_scale = 16;
pfc->s.m2.xworld_res = mscale * xres;
pfc->s.m2.yworld_res = mscale * yres;
}
if (need_outline) {
pfc->s.m2.m[1] = -pfc->s.m2.m[1];
pfc->s.m2.m[3] = -pfc->s.m2.m[3];
}
/* always use our own symbol set mapping */
pfc->ExtndFlags = EF_NOSYMSETMAP;
if (plfont->is_xl_format) {
pfc->ExtndFlags = EF_XLFONT_TYPE;
if ((pfont->WMode & 0x1) != 0) /* vertical substitution */
pfc->ExtndFlags |= EF_VERTSUBS_TYPE;
} else if (plfont->scaling_technology == plfst_TrueType
&& plfont->large_sizes) {
pfc->ExtndFlags = EF_FORMAT16_TYPE | EF_GALLEYSEG_TYPE;
if ((pfont->WMode & 0x1) != 0) /* vertical substitution */
pfc->ExtndFlags |= EF_VERTSUBS_TYPE;
}
pfc->ExtndFlags |= EF_NOUSBOUNDBOX; /* UFST 5.0+ addition */
/* handle artificial emboldening */
if (plfont->bold_fraction && !need_outline) {
pfc->pcl6bold = 32768 * plfont->bold_fraction;
} else
pfc->pcl6bold = 0;
/* set the format */
pfc->format = FC_PCL6_EMU | FC_INCHES_TYPE;
pfc->format |= (need_outline ? FC_LINEAR_TYPE : FC_BITMAP_TYPE);
}
/*
* Set the current UFST font (any type).
*/
static int
pl_set_ufst_font(const pl_font_t * plfont, FONTCONTEXT * pfc)
{
uint status = CGIFfont(FSA pfc);
FONT_METRICS fm;
if (status != 0)
dmprintf1(plfont->pfont->memory, "CGIFfont error %d\n", status);
else {
/* UFST 6.2 *appears* to need this to "concretize" the font
* we've just set.
*/
status = CGIFfont_metrics(&fm);
if (status != 0)
dprintf1("CGIFfont error %d\n", status);
else
plfont_last = plfont; /* record this font for use in call-backs */
}
return status;
}
/*
* Set the current path from a character outline. This is more general than
* may be necessary, depending on how the UFST module is compiled.
*/
static int
image_outline_char(PIFOUTLINE pols,
const gs_matrix_fixed * pmat,
gx_path * ppath,
gs_font * pfont, bool outline_sub_for_bitmap)
{
UW16 il, numLoops = pols->ol.num_loops;
byte *pbase = (byte *) & pols->ol.loop;
int ishift = fixed_fraction_bits + pols->VLCpower;
fixed tx = pmat->tx_fixed, ty = pmat->ty_fixed;
for (il = 0; il < numLoops; il++) {
OUTLINE_LOOP *ploop = &pols->ol.loop[il];
uint numSegmts = ploop->num_segmts;
byte *pseg = pbase + ploop->segmt_offset;
PINTRVECTOR pcoord = (PINTRVECTOR) (pbase + ploop->coord_offset);
int code;
while (numSegmts-- > 0) {
int segtype = *pseg++;
int ip, npts;
gs_fixed_point pt[3];
if (segtype == 2 || segtype > 3)
return_error(gs_error_rangecheck);
npts = (segtype == 3 ? 3 : 1);
for (ip = 0; ip < npts; ip++, ++pcoord) {
pt[ip].x = (pcoord->x << ishift) + tx;
/* apparently when the ufst substitutes an outline for
a bitmap because it is too large the matrix is
mirrored compared with a font that was rendered
directly with an outline. NB, this should be
looked at more carefully when time permits. */
if (outline_sub_for_bitmap)
pt[ip].y = (-pcoord->y << ishift) + ty;
else
pt[ip].y = (pcoord->y << ishift) + ty;
}
switch (segtype) {
case 0: /* moveto */
code = gx_path_add_point(ppath, pt[0].x, pt[0].y);
break;
case 1: /* lineto */
code = gx_path_add_line(ppath, pt[0].x, pt[0].y);
break;
case 3: /* curveto */
code = gx_path_add_curve(ppath,
pt[0].x, pt[0].y,
pt[1].x, pt[1].y,
pt[2].x, pt[2].y);
}
if (code < 0)
return code;
}
if ((code = gx_path_close_subpath(ppath)) < 0)
return code;
}
return 0;
}
/*
* Get the widt from a UFST character (any type). The caller should have
* set the font type in advance.
*/
static int
pl_ufst_char_width(uint char_code,
const void *pgs, gs_point * pwidth, FONTCONTEXT * pfc)
{
UW16 chIdloc = char_code;
UW16 fontWidth[2];
int status;
WIDTH_LIST_INPUT_ENTRY fcode;
if (pwidth != NULL)
pwidth->x = pwidth->y = 0;
CGIFchIdptr(FSA(VOID *) & chIdloc, NULL);
fcode.CharType.TT_unicode = char_code;
if ((status = CGIFwidth2(FSA & fcode, 1, 4, fontWidth)) != 0) {
return status;
}
if (fontWidth[0] == ERR_char_unavailable || fontWidth[1] == 0)
return 1;
else if (pwidth != NULL) {
double fontw = (double) fontWidth[0] / (double) fontWidth[1];
int code = gs_distance_transform(fontw, 0.0, &pl_identmtx, pwidth);
return code < 0 ? code : 0;
} else
return 0;
}
/*
* Generate a UFST character.
*/
static int
pl_ufst_make_char(gs_show_enum * penum,
gs_gstate * pgs,
gs_font * pfont, gs_char chr, FONTCONTEXT * pfc)
{
MEM_HANDLE memhdl;
UW16 status, chIdloc = chr;
gs_matrix sv_ctm, tmp_ctm;
bool outline_sub_for_bitmap = false;
int wasValid;
/* ignore illegitimate characters */
if (chr == 0xffff)
return 0;
CGIFchIdptr(FSA(VOID *) & chIdloc, NULL);
if ((status = CGIFchar_handle(FSA chr, &memhdl, 0)) != 0 &&
status != ERR_fixed_space) {
/* if too large for a bitmap, try an outline */
if ((status >= ERR_bm_gt_oron && status <= ERRdu_pix_range) ||
(status == ERR_bm_buff)) {
pfc->format = (pfc->format & ~FC_BITMAP_TYPE) | FC_CUBIC_TYPE;
if ((status = CGIFfont(FSA pfc)) == 0) {
CGIFchIdptr(FSA(VOID *) & chIdloc, NULL);
status = CGIFchar_handle(FSA chr, &memhdl, 0);
outline_sub_for_bitmap = true;
}
}
if (status != 0) {
/* returning status causes the job to be aborted */
return gs_setcharwidth(penum, pgs, 0.0, 0.0);
}
}
wasValid = pgs->char_tm_valid;
/* move to device space */
gs_currentmatrix(pgs, &sv_ctm);
gs_make_identity(&tmp_ctm);
tmp_ctm.tx = sv_ctm.tx;
tmp_ctm.ty = sv_ctm.ty;
gs_setmatrix(pgs, &tmp_ctm);
pgs->char_tm_valid = wasValid;
if (FC_ISBITMAP(pfc)) {
PIFBITMAP psbm = (PIFBITMAP) MEMptr(memhdl);
float wbox[6];
gs_image_t image;
gs_image_enum *ienum;
int code;
gs_point aw;
/* set up the cache device */
gs_distance_transform((double) psbm->escapement / psbm->du_emx,
0.0, &sv_ctm, &aw);
wbox[0] = aw.x;
wbox[1] = aw.y;
wbox[2] = psbm->xorigin / 16.0 + psbm->left_indent;
wbox[3] = -psbm->yorigin / 16.0 + psbm->top_indent;
wbox[4] = psbm->black_width + wbox[2];
wbox[5] = psbm->black_depth + wbox[3];
/* if (status == ERR_fixed_space)
* we are relying on ufst to
* send a zero sized image; we then cache the escapements of the space character
* psbm->bm = psbm->width = psbm->height = 0;
* note that the outline code can't be reached on ERR_fixed_space
*/
if ((code = gs_setcachedevice(penum, pgs, wbox)) < 0) {
MEMfree(FSA CACHE_POOL, memhdl);
gs_setmatrix(pgs, &sv_ctm);
return code;
}
/* set up the image */
ienum = gs_image_enum_alloc(pgs->memory, "pl_ufst_make_char");
if (ienum == 0) {
MEMfree(FSA CACHE_POOL, memhdl);
gs_setmatrix(pgs, &sv_ctm);
return_error(gs_error_VMerror);
}
gs_image_t_init_mask(&image, true);
image.Width = psbm->width << 3;
image.Height = psbm->depth;
gs_make_identity(&image.ImageMatrix);
image.ImageMatrix.tx = -psbm->xorigin / 16.0;
image.ImageMatrix.ty = psbm->yorigin / 16.0;
image.adjust = true;
code = pl_image_bitmap_char(ienum,
&image,
(byte *) psbm->bm,
psbm->width, 0, NULL, pgs);
gs_free_object(pgs->memory, ienum, "pl_ufst_make_char");
MEMfree(FSA CACHE_POOL, memhdl);
gs_setmatrix(pgs, &sv_ctm);
return (code < 0 ? code : 0);
} else { /* outline */
PIFOUTLINE pols = (PIFOUTLINE) MEMptr(memhdl);
float scale = pow(2, pols->VLCpower);
float wbox[6];
int code;
gs_point aw;
/* set up the cache device */
gs_distance_transform((double) pols->escapement / pols->du_emx,
0.0, &sv_ctm, &aw);
wbox[0] = aw.x;
wbox[1] = aw.y;
wbox[2] = scale * pols->left;
wbox[3] = scale * pols->bottom;
wbox[4] = scale * pols->right;
wbox[5] = scale * pols->top;
if (status == ERR_fixed_space) {
MEMfree(FSA CACHE_POOL, memhdl);
code = gs_setcharwidth(penum, pgs, wbox[0], wbox[1]);
gs_setmatrix(pgs, &sv_ctm);
return code;
} else if ((code = gs_setcachedevice(penum, pgs, wbox)) < 0) {
MEMfree(FSA CACHE_POOL, memhdl);
gs_setmatrix(pgs, &sv_ctm);
return code;
}
code =
image_outline_char(pols, &pgs->ctm, pgs->path, pfont,
outline_sub_for_bitmap);
if (code >= 0) {
code = gs_fill(pgs);
}
MEMfree(FSA CACHE_POOL, memhdl);
gs_setmatrix(pgs, &sv_ctm);
return (code < 0 ? code : 0);
}
}
/* ---------------- MicroType font support ---------------- */
/*
* MicroType accepts unicode values a glyph identifiers, so no explicit
* encoding is necessary.
*/
static gs_glyph
pl_mt_encode_char(gs_font * pfont, gs_char pchr, gs_glyph_space_t not_used)
{
return (gs_glyph) pchr;
}
/*
* Set the current UFST font to be a MicroType font.
*/
static int
pl_set_mt_font(gs_gstate * pgs,
const pl_font_t * plfont, int need_outline, FONTCONTEXT * pfc)
{
pl_init_fc(plfont, pgs, need_outline, pfc, /* width request iff */
pgs == NULL);
pfc->font_id = ((gs_font_base *) (plfont->pfont))->UID.id;
#ifdef UFST_FROM_ROM
pfc->format |= FC_ROM_TYPE | FC_NOUSBOUNDBOX;
#endif
pfc->format |= FC_FCO_TYPE;
return pl_set_ufst_font(plfont, pfc);
}
/* Render a MicroType character. */
static int
pl_mt_build_char(gs_show_enum * penum,
gs_gstate * pgs, gs_font * pfont, gs_char chr, gs_glyph glyph)
{
const pl_font_t *plfont = (const pl_font_t *)pfont->client_data;
FONTCONTEXT fc;
if (pl_set_mt_font(pgs, plfont, gs_show_in_charpath(penum), &fc) != 0)
return 0;
return pl_ufst_make_char(penum, pgs, pfont, chr, &fc);
}
#define MAX_LIST_SIZE 100
int list_size = 0;
typedef struct pl_glyph_width_node_s pl_glyph_width_node_t;
struct pl_glyph_width_node_s
{
uint char_code;
uint font_id;
gs_point width;
pl_glyph_width_node_t *next;
};
pl_glyph_width_node_t *head = NULL;
/* add at the front of the list */
static int
pl_glyph_width_cache_node_add(gs_memory_t * mem, gs_id font_id,
uint char_code, gs_point * pwidth)
{
pl_glyph_width_node_t *node =
(pl_glyph_width_node_t *) gs_alloc_bytes(mem,
sizeof
(pl_glyph_width_node_t),
"pl_glyph_width_cache_node_add");
if (node == NULL)
return -1;
if (head == NULL) {
head = node;
head->next = NULL;
} else {
node->next = head;
head = node;
}
head->char_code = char_code;
head->font_id = font_id;
head->width = *pwidth;
list_size++;
return 0;
}
static int
pl_glyph_width_cache_node_search(gs_id font_id, uint char_code,
gs_point * pwidth)
{
pl_glyph_width_node_t *current = head;
while (current) {
if (char_code == current->char_code && font_id == current->font_id) {
*pwidth = current->width;
return 0;
}
current = current->next;
}
return -1;
}
static void
pl_glyph_width_list_remove(gs_memory_t * mem)
{
pl_glyph_width_node_t *current = head;
while (current) {
pl_glyph_width_node_t *next = current->next;
gs_free_object(mem, current, "pl_glyph_width_list_remove");
current = next;
}
head = NULL;
list_size = 0;
return;
}
/* Get character existence and escapement for an MicroType font. */
static int
pl_mt_char_width(const pl_font_t * plfont,
const void *pgs, uint char_code, gs_point * pwidth)
{
FONTCONTEXT fc;
int code;
if (list_size > MAX_LIST_SIZE)
pl_glyph_width_list_remove(plfont->pfont->memory);
code =
pl_glyph_width_cache_node_search(plfont->pfont->id, char_code,
pwidth);
if (code < 0) { /* not found */
/* FIXME inconsistent error code return values follow */
if (pl_set_mt_font(NULL /* graphics state */ , plfont, false, &fc) !=
0)
return 0;
code = pl_ufst_char_width(char_code, pgs, pwidth, &fc);
if (code == 0)
code = pl_glyph_width_cache_node_add(plfont->pfont->memory,
plfont->pfont->id,
char_code, pwidth);
}
return code;
}
static int
pl_mt_char_metrics(const pl_font_t * plfont, const void *pgs, uint char_code,
float metrics[4])
{
gs_point width;
metrics[0] = metrics[1] = metrics[2] = metrics[3] = 0;
if (0 == pl_mt_char_width(plfont, pgs, char_code, &width)) {
/* width is correct,
stefan foo: lsb is missing. */
metrics[2] = width.x;
/* metrics[0] = left_side_bearing;
*/
}
return 0;
}
/*
* Callback from UFST to pass PCLEO IF character data starting with header.
*
* For TrueType fonts, the glyph table within a font is indexed by the glyph
* id., rather than the unicode. The char_glyphs table in the font maps the
* latter to the former.
*/
static LPUB8
pl_PCLchId2ptr(FSP UW16 chId)
{
const pl_font_t *plfont = plfont_last;
if (plfont_last == NULL)
return NULL; /* something wrong */
/* check for a TrueType font */
if (plfont->char_glyphs.table != NULL) {
pl_tt_char_glyph_t *ptcg = pl_tt_lookup_char(plfont, chId);
if (ptcg->chr == gs_no_char)
return NULL; /* something wrong */
chId = ptcg->glyph;
}
return (LPUB8) pl_font_lookup_glyph(plfont, chId)->data;
}
/*
* callback from UFST to pass PCLEO TT character data starting with header.
*/
static LPUB8
pl_PCLglyphID2Ptr(FSP UW16 chId)
{
if (plfont_last == NULL)
return NULL; /* something wrong */
else
return (LPUB8) (pl_font_lookup_glyph(plfont_last, chId)->data);
}
/*
* callback from UFST to pass PCLEO compound character data starting
* with header.
*/
static LPUB8
pl_PCLEO_charptr(LPUB8 pfont_hdr, UW16 char_code)
{
if (plfont_last == NULL || plfont_last->header != pfont_hdr) {
return NULL; /* something wrong */
} else
return pl_PCLchId2ptr(FSA char_code);
}
void
plu_set_callbacks()
{
gx_set_UFST_Callbacks(pl_PCLEO_charptr, pl_PCLchId2ptr,
pl_PCLglyphID2Ptr);
/* nothing */
}
void
pl_mt_init_procs(gs_font_base * pfont)
{
pfont->procs.encode_char = pl_mt_encode_char;
pfont->procs.build_char = pl_mt_build_char;
#define plfont ((pl_font_t *)pfont->client_data)
plfont->char_width = pl_mt_char_width;
plfont->char_metrics = pl_mt_char_metrics;
#undef plfont
}
|