📄 dscparse.cpp
字号:
if (dsc->eol) { /* if previous line was complete, increment line count */ dsc->line_count++; if (dsc->skip_lines) dsc->skip_lines--; } /* skip over \n which followed \r */ if (dsc->last_cr && dsc->line[0] == '\n') { dsc->data_index++; dsc->line++; } dsc->last_cr = FALSE; /* look for EOL */ dsc->eol = FALSE; for (p = dsc->line; p < last; p++) { if (*p == '\r') { p++; if ((p<last) && (*p == '\n')) p++; /* include line feed also */ else dsc->last_cr = TRUE; /* we might need to skip \n */ dsc->eol = TRUE; /* dsc->line is a complete line */ break; } if (*p == '\n') { p++; dsc->eol = TRUE; /* dsc->line is a complete line */ break; } if (*p == '\032') { /* MS-DOS Ctrl+Z */ dsc->eol = TRUE; } } if (dsc->eol == FALSE) { /* we haven't got a complete line yet */ if (dsc->data_length - dsc->data_index < sizeof(dsc->data)/2) { /* buffer is less than half full, ask for some more */ dsc->line_length = 0; return 0; } } dsc->data_index += dsc->line_length = (p - dsc->line); } while (dsc->skip_lines && dsc->line_length); if (dsc->line_length == 0) return 0; if ((dsc->line[0]=='%') && (dsc->line[1]=='%')) { /* handle recursive %%BeginDocument */ if ((dsc->skip_document) && dsc->line_length && COMPARE(dsc->line, "%%EndDocument")) { dsc->skip_document--; } /* handle embedded lines or binary data */ if (COMPARE(dsc->line, "%%BeginData:")) { /* %%BeginData: <numberof>[ <type> [ <bytesorlines> ] ] * <numberof> ::= <uint> (Lines or physical bytes) * <type> ::= Hex | Binary | ASCII (Type of data) * <bytesorlines> ::= Bytes | Lines (Read in bytes or lines) */ char begindata[MAXSTR+1]; int cnt; unsigned int num; const char *numberof, *bytesorlines; if ((num = dsc->line_length) >= sizeof(begindata)-1) num = sizeof(begindata)-1; memcpy(begindata, dsc->line, num); begindata[num] = '\0'; numberof = strtok(begindata+12, " \r\n"); strtok(NULL, " \r\n"); /* dump type */ bytesorlines = strtok(NULL, " \r\n"); if (bytesorlines == NULL) bytesorlines = "Bytes"; if ( (numberof == NULL) || (bytesorlines == NULL) ) { /* invalid usage of %%BeginData */ /* ignore that we ever saw it */ int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: case CDSC_RESPONSE_CANCEL: break; case CDSC_RESPONSE_IGNORE_ALL: return 0; } } else { cnt = atoi(numberof); if (cnt) { if (bytesorlines && (dsc_stricmp(bytesorlines, "Lines")==0)) { /* skip cnt lines */ if (dsc->skip_lines == 0) { /* we are not already skipping lines */ dsc->skip_lines = cnt+1; } } else { /* byte count doesn't includes \n or \r\n */ /* or \r of %%BeginData: */ /* skip cnt bytes */ if (dsc->skip_bytes == 0) { /* we are not already skipping lines */ dsc->skip_bytes = cnt; } } } } } else if (COMPARE(dsc->line, "%%BeginBinary:")) { /* byte count doesn't includes \n or \r\n or \r of %%BeginBinary:*/ unsigned long cnt = atoi(dsc->line + 14); if (dsc->skip_bytes == 0) { /* we are not already skipping lines */ dsc->skip_bytes = cnt; } } } if ((dsc->line[0]=='%') && (dsc->line[1]=='%') && COMPARE(dsc->line, "%%BeginDocument:") ) { /* Skip over embedded document, recursively */ dsc->skip_document++; } if (!dsc->long_line && (dsc->line_length > DSC_LINE_LENGTH)) { dsc_error(dsc, CDSC_MESSAGE_LONG_LINE, dsc->line, dsc->line_length); dsc->long_line = TRUE; } return dsc->line_length;}/* Save last DSC line, for use with %%+ */dsc_private void dsc_save_line(CDSC *dsc){ int len = min(sizeof(dsc->last_line), dsc->line_length); memcpy(dsc->last_line, dsc->line, len);}/* display unknown DSC line */dsc_private void dsc_unknown(CDSC *dsc){ if (dsc->debug_print_fn) { char line[DSC_LINE_LENGTH]; unsigned int length = min(DSC_LINE_LENGTH-1, dsc->line_length); sprintf(line, "Unknown in %s section at line %d:\n ", dsc_scan_section_name[dsc->scan_section], dsc->line_count); dsc_debug_print(dsc, line); strncpy(line, dsc->line, length); line[length] = '\0'; dsc_debug_print(dsc, line); }}dsc_private GSBOOLdsc_is_section(char *line){ if ( !((line[0]=='%') && (line[1]=='%')) ) return FALSE; if (IS_DSC(line, "%%BeginPreview")) return TRUE; if (IS_DSC(line, "%%BeginDefaults")) return TRUE; if (IS_DSC(line, "%%BeginProlog")) return TRUE; if (IS_DSC(line, "%%BeginSetup")) return TRUE; if (IS_DSC(line, "%%Page:")) return TRUE; if (IS_DSC(line, "%%Trailer")) return TRUE; if (IS_DSC(line, "%%EOF")) return TRUE; return FALSE;}dsc_private GSDWORDdsc_get_dword(const unsigned char *buf){ GSDWORD dw; dw = (GSDWORD)buf[0]; dw += ((GSDWORD)buf[1])<<8; dw += ((GSDWORD)buf[2])<<16; dw += ((GSDWORD)buf[3])<<24; return dw;}dsc_private GSWORDdsc_get_word(const unsigned char *buf){ GSWORD w; w = (GSWORD)buf[0]; w |= (GSWORD)(buf[1]<<8); return w;}dsc_private intdsc_read_doseps(CDSC *dsc){ unsigned char *line = (unsigned char *)dsc->line; if ((dsc->doseps = (CDSCDOSEPS *)dsc_memalloc(dsc, sizeof(CDSCDOSEPS))) == NULL) return CDSC_ERROR; /* no memory */ dsc->doseps->ps_begin = dsc_get_dword(line+4); dsc->doseps->ps_length = dsc_get_dword(line+8); dsc->doseps->wmf_begin = dsc_get_dword(line+12); dsc->doseps->wmf_length = dsc_get_dword(line+16); dsc->doseps->tiff_begin = dsc_get_dword(line+20); dsc->doseps->tiff_length = dsc_get_dword(line+24); dsc->doseps->checksum = dsc_get_word(line+28); dsc->doseps_end = dsc->doseps->ps_begin + dsc->doseps->ps_length; /* move data_index backwards to byte after doseps header */ dsc->data_index -= dsc->line_length - 30; /* we haven't read a line of PostScript code yet */ dsc->line_count = 0; /* skip from current position to start of PostScript section */ dsc->skip_bytes = dsc->doseps->ps_begin - 30; if (dsc->doseps->tiff_begin) dsc->preview = CDSC_TIFF; if (dsc->doseps->wmf_begin) dsc->preview = CDSC_WMF; return CDSC_OK;}dsc_private int dsc_parse_pages(CDSC *dsc){ int ip, io; unsigned int i; char *p; int n; if ((dsc->page_pages != 0) && (dsc->scan_section == scan_comments)) { int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: case CDSC_RESPONSE_CANCEL: return CDSC_OK; /* ignore duplicate comments in header */ case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } if ((dsc->page_pages != 0) && (dsc->scan_section == scan_trailer)) { int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: case CDSC_RESPONSE_CANCEL: break; /* use duplicate comments in header */ case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } n = IS_DSC(dsc->line, "%%+") ? 3 : 8; while (IS_WHITE(dsc->line[n])) n++; p = dsc->line + n; if (COMPARE(p, "atend")) { int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: /* assume (atend) */ /* we should mark it as deferred */ break; case CDSC_RESPONSE_CANCEL: /* ignore it */ break; case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } else if (COMPARE(p, "(atend)")) { /* do nothing */ /* we should mark it as deferred */ } else { ip = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); if (i) { n+=i; dsc->page_pages = ip; io = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); if (i) { /* DSC 2 uses extra integer to indicate page order */ /* DSC 3 uses %%PageOrder: */ if (dsc->page_order == CDSC_ORDER_UNKNOWN) switch (io) { case -1: dsc->page_order = CDSC_DESCEND; break; case 0: dsc->page_order = CDSC_SPECIAL; break; case 1: dsc->page_order = CDSC_ASCEND; break; } } } else { int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: case CDSC_RESPONSE_CANCEL: /* ignore it */ break; case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } } return CDSC_OK;}dsc_private int dsc_parse_bounding_box(CDSC *dsc, CDSCBBOX** pbbox, int offset){ unsigned int i, n; int llx, lly, urx, ury; float fllx, flly, furx, fury; char *p; /* Process first %%BoundingBox: in comments, and last in trailer */ if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) { int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: case CDSC_RESPONSE_CANCEL: return CDSC_OK; /* ignore duplicate comments in header */ case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } if ((*pbbox != NULL) && (dsc->scan_section == scan_pages)) { int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: case CDSC_RESPONSE_CANCEL: return CDSC_OK; /* ignore duplicate comments in header */ case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } if ((*pbbox != NULL) && (dsc->scan_section == scan_trailer)) { int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: case CDSC_RESPONSE_CANCEL: break; /* use duplicate comments in trailer */ case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } if (*pbbox != NULL) { dsc_memfree(dsc, *pbbox); *pbbox = NULL; } /* should only process first %%BoundingBox: */ while (IS_WHITE(dsc->line[offset])) offset++; p = dsc->line + offset; if (COMPARE(p, "atend")) { int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: /* assume (atend) */ /* we should mark it as deferred */ break; case CDSC_RESPONSE_CANCEL: /* ignore it */ break; case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } else if (COMPARE(p, "(atend)")) { /* do nothing */ /* we should mark it as deferred */ } else { /* llx = */ lly = urx = ury = 0; n = offset; llx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); n += i; if (i) lly = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); n += i; if (i) urx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); n += i; if (i) ury = dsc_get_int(dsc->line+n, dsc->line_length-n, &i); if (i) { *pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX)); if (*pbbox == NULL) return CDSC_ERROR; /* no memory */ (*pbbox)->llx = llx; (*pbbox)->lly = lly; (*pbbox)->urx = urx; (*pbbox)->ury = ury; } else { int rc = dsc_error(dsc, CDSC_MESSAGE_BBOX, dsc->line, dsc->line_length); switch (rc) { case CDSC_RESPONSE_OK: /* fllx = */ flly = furx = fury = 0.0; n = offset; n += i; fllx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); n += i; if (i) flly = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); n += i; if (i) furx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); n += i; if (i) fury = dsc_get_real(dsc->line+n, dsc->line_length-n, &i); if (i) { *pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX)); if (*pbbox == NULL) return CDSC_ERROR; /* no memory */ (*pbbox)->llx = (int)fllx; (*pbbox)->lly = (int)flly; (*pbbox)->urx = (int)(furx+0.999); (*pbbox)->ury = (int)(fury+0.999); } return CDSC_OK; case CDSC_RESPONSE_CANCEL: return CDSC_OK; case CDSC_RESPONSE_IGNORE_ALL: return CDSC_NOTDSC; } } } return CDSC_OK;}dsc_private int dsc_parse_float_bounding_box(CDSC *dsc, CDSCFBBOX** pbbox, int offset){ unsigned int i, n; float fllx, flly, furx, fury; char *p; /* Process first %%HiResBoundingBox: or %%CropBox: in comments, * and last in trailer. */ if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) { int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line, dsc->line_length); switch (rc) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -