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
|
/* Copyright (C) 2001-2023 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., 39 Mesa Street, Suite 108A, San Francisco,
CA 94129, USA, for further information.
*/
/* pxerrors.c */
/* PCL XL error reporting */
#include "memory_.h"
#include "stdio_.h" /* for sprintf */
#include "string_.h"
#include "gsmemory.h"
#include "gstypes.h" /* for gsmatrix.h */
#include "gsccode.h" /* for gxfont.h */
#include "gsmatrix.h" /* for gsfont.h */
#include "gsstate.h" /* for gsfont.h, gspath.h */
#include "gspaint.h" /* for gs_erasepage */
#include "gscoord.h"
#include "gspath.h"
#include "gsutil.h"
#include "gxfixed.h" /* for gxchar.h */
#include "gxchar.h"
#include "gxfont.h"
#include "scommon.h" /* for pxparse.h */
#include "pxbfont.h"
#include "pxerrors.h"
#include "pxparse.h"
#include "pxptable.h" /* for px_operator_names */
#include "pxstate.h"
#include "pxfont.h"
/* Imported operators */
px_operator_proc(pxEndPage);
px_operator_proc(pxSetPageDefaultCTM);
/* set the font for the error page */
static px_font_t *
px_error_setfont(px_state_t * pxs)
{
int code;
px_font_t *pxfont = pl_alloc_font(pxs->memory, "px_error_setfont");
if (pxfont == 0)
return NULL;
pxfont->storage = pxfsInternal;
pxfont->font_type = plft_Unicode; /* as good as any */
pxfont->data_are_permanent = true;
code = px_define_font(pxfont, (byte *) px_bitmap_font_header,
px_bitmap_font_header_size,
gs_next_ids(pxs->memory, 1), pxs);
{
const byte *cdatanext, *cdata = px_bitmap_font_char_data;
while (*cdata && code >= 0) {
cdatanext = cdata + 16 +
((uint16at(cdata + 11, true) + 7) >> 3) *
uint16at(cdata + 13, true) + 1; /* add one to clear the char code */
code = pl_font_add_glyph(pxfont, *cdata, cdata + 1, (int)(cdatanext - cdata - 2));
cdata = cdatanext;
}
}
if (code < 0) {
pl_free_font(pxs->memory, pxfont, "px_error_setfont");
return NULL;
}
gs_setfont(pxs->pgs, pxfont->pfont);
pxfont->pfont->FontMatrix = pxfont->pfont->orig_FontMatrix;
return pxfont;
}
/* ---------------- Procedures ---------------- */
/* Record a warning. */
/* Return 1 if the warning table overflowed. */
/* If save_all is false, only remember the last warning with the same */
/* first word as this one. */
int
px_record_warning(const char *message, bool save_all, px_state_t * pxs)
{
uint end = pxs->warning_length;
char *str = pxs->warnings + end;
char *word_end = strchr(message, ' ');
if (end + strlen(message) + 1 > px_max_warning_message)
return 1;
if (!save_all && word_end) { /* Delete any existing message of the same type. */
/* (There is at most one.) */
uint word_len = word_end - message;
char *next = pxs->warnings;
uint len1;
for (; next != str; next += len1) {
len1 = strlen(next) + 1;
if (len1 > word_len && !strncmp(next, message, word_len)) { /* Delete the old message. */
memmove(next, next + len1, str - (next + len1));
str -= len1;
break;
}
}
}
strcpy(str, message);
pxs->warning_length = str + strlen(str) + 1 - pxs->warnings;
return 0;
}
/* Generate a line of an error message starting at internal position N; */
/* return an updated value of N. When done, return -1. */
int
px_error_message_line(char message[px_max_error_line + 1], int N,
const char *subsystem, int code,
const px_parser_state_t * st, const px_state_t * pxs)
{
if (N == 0) {
strcpy(message, "PCL XL error\n");
return 1;
}
if (code == errorWarningsReported) {
/*
* Generate a line of warnings.
* 1 = first line, otherwise N = position in warnings buffer.
*/
switch (N) {
case 1:
N = 0;
/* falls through */
default:
if (N == pxs->warning_length)
return -1;
{
const char *str = pxs->warnings + N;
uint len = strlen(str);
uint warn_len;
strcpy(message, " Warning: ");
warn_len = strlen(message) + 1;
if (len > px_max_error_line - warn_len) {
strncat(message, str, px_max_error_line - warn_len);
message[px_max_error_line - 1] = 0;
} else
strcat(message, str);
strcat(message, "\n");
return N + len + 1;
}
}
} else { /* Generate the N'th line of an error message. */
char *end;
switch (N) {
case 1:
gs_snprintf(message, px_max_error_line, " Subsystem: %s\n", subsystem);
break;
case 2:
strcpy(message, " Error: ");
{
char *end = message + strlen(message);
if (pxs->error_line[0]) { /* Ignore the error code, use the error line. */
int len = strlen(pxs->error_line);
int max_len = px_max_error_line - 2 - strlen(message);
if (len <= max_len)
strcpy(end, pxs->error_line);
else {
strncpy(end, pxs->error_line, max_len);
message[px_max_error_line - 1] = 0;
}
strcat(end, "\n");
} else if (code >= px_error_first && code < px_error_next)
gs_snprintf(end, px_max_error_line - strlen(message), "%s\n",
px_error_names[code - px_error_first]);
else
gs_snprintf(end, px_max_error_line - strlen(message), "Internal error 0x%x\n", code);
}
break;
case 3:
{
int last_operator = st->last_operator;
const char *oname;
strcpy(message, " Operator: ");
end = message + strlen(message);
if (last_operator >= 0x40 && last_operator < 0xc0 &&
(oname =
px_operator_names[last_operator - 0x40]) != 0)
gs_snprintf(end, px_max_error_line - strlen(message), "%s\n", oname);
else
gs_snprintf(end, px_max_error_line - strlen(message), "0x%02x\n", last_operator);
}
break;
case 4:
strcpy(message, " Position: ");
end = message + strlen(message);
if (st->parent_operator_count)
gs_snprintf(end, px_max_error_line - strlen(message), "%ld;%ld\n", st->parent_operator_count,
st->operator_count);
else
gs_snprintf(end, px_max_error_line - strlen(message), "%ld\n", st->operator_count);
break;
default:
return -1;
}
return N + 1;
}
}
/* Begin an error page. Return the initial Y value. */
int
px_begin_error_page(px_state_t * pxs, int* y)
{
int code;
gs_gstate *pgs = pxs->pgs;
code = gs_initgraphics(pgs);
if (code < 0) return code;
code = gs_erasepage(pgs);
if (code < 0) return code;
/* Don't call pxSetPageDefaultCTM -- we don't want rotation or */
/* unusual Units of Measure -- but do invert the Y axis. */
/*pxSetPageDefaultCTM(NULL, pxs); */
{
gs_point pt;
px_get_default_media_size(pxs, &pt);
gs_translate(pgs, 0.0, pt.y);
gs_scale(pgs, 1.0, -1.0);
*y = 90;
}
return 0;
}
/* Print a message on an error page. */
/* Return the updated Y value. */
int
px_error_page_show(const char *message, int ytop, px_state_t * pxs)
{
gs_gstate *pgs = pxs->pgs;
int y = ytop;
const char *m = message;
const char *p;
gs_text_enum_t *penum;
gs_text_params_t text;
int code;
/* Normalize for a 10-point font. */
#define point_size 10.0
double scale = 72.0 / px_bitmap_font_resolution *
point_size / px_bitmap_font_point_size;
px_font_t *pxfont = px_error_setfont(pxs);
if (pxfont == NULL)
return_error(errorInsufficientMemory);
text.operation = TEXT_FROM_STRING | TEXT_DO_DRAW | TEXT_RETURN_WIDTH;
/* Peel off the next line and display it. */
for (p = m;; m = ++p) {
while (*p != 0 && *p != '\n')
++p;
gs_moveto(pgs, 36.0, y);
gs_scale(pgs, scale, scale);
text.data.chars = (gs_char *) m;
text.size = p - m;
code = gs_text_begin(pgs, &text, pxs->memory, &penum);
if (code >= 0) {
code = gs_text_process(penum);
if (code > 0)
code = gs_note_error(errorBadFontData); /* shouldn't happen! */
if (code >= 0) {
gs_text_release(pgs, penum, "pxtext");
}
}
gs_scale(pgs, 1 / scale, 1 / scale);
y += (int)(point_size * 8 / 5);
if (code < 0)
break;
if (!*p || !p[1])
break;
}
pl_free_font(pxs->memory, pxfont, "px_error_page_show");
return (code < 0 ? code : y);
}
/* Reset the warning table. */
void
px_reset_errors(px_state_t * pxs)
{
pxs->error_line[0] = 0;
pxs->warning_length = 0;
}
/* ---------------- Error names ---------------- */
#undef pxerrors_INCLUDED
#define INCLUDE_ERROR_NAMES
#include "pxerrors.h"
#undef INCLUDE_ERROR_NAMES
|