📄 gdeveprn.c
字号:
{ ms_MediaCode old_diff, /* difference between old flags and desired flags */ new_diff; /* difference between new flags and desired flags */ /* Ignore the size information */ old_code = ms_flags(old_code); new_code = ms_flags(new_code); /* Determine differences to desired flags */ old_diff = old_code ^ desired; new_diff = new_code ^ desired; /* Check for exact matches */ if (old_diff == 0) return false; if (new_diff == 0) return true; /* Is the difference at most MS_TRANSVERSE_FLAG? */ old_diff = old_diff & ~MS_TRANSVERSE_FLAG; new_diff = new_diff & ~MS_TRANSVERSE_FLAG; if (old_diff == 0) return false; if (new_diff == 0) return true; /* Loop over the remaining optional flags */ if (optional != NULL) { const ms_MediaCode *opt = optional; while (*opt != ms_none) { old_diff = old_diff & ~*opt; new_diff = new_diff & ~*opt; if (old_diff == 0) { if (new_diff != 0) return false; /* At this point both are matches at the same level of optional flags. Now look for the last preceding flag in which they differ. */ { ms_MediaCode diff = ms_flags(old_code ^ new_code); while (optional < opt && (diff & *opt) == 0) opt--; if ((diff & *opt) == 0) { if ((diff & MS_TRANSVERSE_FLAG) == 0) return false; /* old and new differ in MS_TRANSVERSE_FLAG */ return (new_code & MS_TRANSVERSE_FLAG) == (desired & MS_TRANSVERSE_FLAG); } return (new_code & *opt) == (desired & *opt); } } if (new_diff == 0) return true; opt++; } } return false; /* Both codes are mismatches at this point */}/****************************************************************************** Function: flag_match This function returns true iff 'code' is an acceptable match for the flag request.******************************************************************************/static bool flag_match(ms_MediaCode desired, const ms_MediaCode *optional, ms_MediaCode code){ code = (ms_flags(code) ^ desired) & ~MS_TRANSVERSE_FLAG; if (code == 0) return true; if (optional == NULL) return false; while (*optional != ms_none && code != 0) { code = code & ~*optional; optional++; } return code == 0;}/****************************************************************************** Function: eprn_set_page_layout This function determines media size, sheet orientation in pixmap device space, the orientation of default user space, and the imageable area. It should be called whenever the page device parameters "PageSize" and "LeadingEdge", the media flags, or the page descriptions have been changed. The function returns zero on success and a non-zero value otherwise. In the latter case, an error message has been issued. This can only occur if the media size is not supported with the flags requested. On success, the following variables in the device structure are consistent: width, height, MediaSize[], HWMargins[], eprn.code, eprn.default_orientation, eprn.right_shift, eprn.down_shift.******************************************************************************/int eprn_set_page_layout(eprn_Device *dev){ bool no_match = true, /* Are the requested flags supported for some size? */ landscape = dev->MediaSize[0] > dev->MediaSize[1]; /* It's not documented, but 'MediaSize' is the requested "PageSize" page device parameter value and hence is to be interpreted in default (not default default!) user space. */ const char *epref = dev->eprn.CUPS_messages? CUPS_ERRPREF: ""; const eprn_CustomPageDescription *best_cmatch = NULL; /* best custom page size match */ eprn_Eprn *eprn = &dev->eprn; const eprn_PageDescription *best_cdmatch = NULL, /* best custom page size match in discrete list*/ *best_dmatch = NULL, /* best discrete match */ *pd; /* loop variable */ float /* Page width and height in bp with w <= h (in a moment): */ w = dev->MediaSize[0], h = dev->MediaSize[1], /* pixmap device space margins in bp (canonical order): */ margins[4]; int quarters; ms_MediaCode desired = eprn->desired_flags;#ifdef EPRN_TRACE if_debug3(EPRN_TRACE_CHAR, "! eprn_set_page_layout(): PageSize = [%.0f %.0f], " "desired_flags = 0x%04X.\n", dev->MediaSize[0], dev->MediaSize[1], (unsigned int)desired);#endif /* Ensure w <= h */ if (w > h) { float temp; temp = w; w = h; h = temp; /* This has effectively split 'MediaSize[]' into 'w', 'h' and 'landscape'. */ } /* Initialization of primary return value */ eprn->code = ms_none; /* Put the LeadingEdge value into the desired flag pattern if it's set */ if (eprn->leading_edge_set) { if (eprn->default_orientation % 2 == 0) /* true on short edge first */ desired &= ~MS_TRANSVERSE_FLAG; else desired |= MS_TRANSVERSE_FLAG; } /* Find best match in discrete sizes */ if (eprn->media_overrides == NULL) pd = eprn->cap->sizes; else pd = eprn->media_overrides; while (pd->code != ms_none) { const ms_SizeDescription *ms = ms_find_size_from_code(pd->code); if (ms->dimen[0] > 0.0 /* ignore variable sizes */ && fabs(w - ms->dimen[0]) <= 5.0 && fabs(h - ms->dimen[1]) <= 5.0) { /* The size does match at 5 bp tolerance. This value has been chosen arbitrarily to be equal to PostScript's PageSize matching tolerance during media selection. The tolerance should really be that at which the printer in question distinguishes between sizes or smaller than that in order to at least prevent printing on unsupported sizes. */ if (best_dmatch == NULL || better_flag_match(desired, eprn->optional_flags, best_dmatch->code, pd->code)) best_dmatch = pd; if (flag_match(desired, eprn->optional_flags, pd->code)) no_match = false; } pd++; } /* Next find the best match among the custom size descriptions */ if (eprn->cap->custom != NULL) { const eprn_CustomPageDescription *cp = eprn->cap->custom; /* First check whether the size is in the supported range */ while (cp->width_max > 0.0) { if (cp->width_min <= w && w <= cp->width_max && cp->height_min <= h && h <= cp->height_max) { /* The size does match. */ if (best_cmatch == NULL || better_flag_match(desired, eprn->optional_flags, best_cmatch->code, cp->code)) best_cmatch = cp; if (eprn->media_overrides == NULL && flag_match(desired, eprn->optional_flags, cp->code)) no_match = false; } cp++; } /* If we have read a media configuration file, the flags to be matched must be sought in 'media_overrides'. */ if (best_cmatch != NULL && eprn->media_overrides != NULL) { for (pd = eprn->media_overrides; pd->code != ms_none; pd++) { if (ms_without_flags(pd->code) == ms_CustomPageSize) { if (best_cdmatch == NULL || better_flag_match(desired, eprn->optional_flags, best_cdmatch->code, pd->code)) best_cdmatch = pd; if (flag_match(desired, eprn->optional_flags, pd->code)) no_match = false; } } } } /* Now the 'best_*match' variables indicate for each of the categories of page descriptions to which extent the size is supported at all (non-NULL value) and what the best flag match in the category is. Here we now check for NULL values, i.e., size matches. */ if (best_dmatch == NULL) { /* No discrete match */ if (best_cmatch == NULL) { /* No match at all. */ eprintf3("%s" ERRPREF "This document requests a page size of %.0f x %.0f bp.\n", epref, dev->MediaSize[0], dev->MediaSize[1]); if (eprn->cap->custom == NULL) { /* The printer does not support custom page sizes */ if (eprn->media_overrides != NULL) eprintf1( "%s The media configuration file does not contain an entry for " " this size.\n", epref); else eprintf2("%s This size is not supported by the %s.\n", epref, eprn->cap->name); } else eprintf3( "%s This size is not supported as a discrete size and it exceeds " "the\n" "%s custom page size limits for the %s.\n", epref, epref, eprn->cap->name); return -1; } if (eprn->media_overrides != NULL && best_cdmatch == NULL) { eprintf6("%s" ERRPREF "This document requests a page size of %.0f x %.0f bp\n" "%s but there is no entry for this size in the " "media configuration file\n" "%s %s.\n", epref, dev->MediaSize[0], dev->MediaSize[1], epref, epref, eprn->media_file); return -1; } } /* Now we have: best_dmatch != NULL || best_cmatch != NULL && (eprn->media_overrides == NULL || best_cdmatch != NULL). */ /* Find a flag match among the size matches found so far */ { ms_MediaCode custom_code = ms_none; /* best custom page size match (either from cmatch or dcmatch) */ if (best_cmatch != NULL && (eprn->media_overrides == NULL || best_cdmatch != NULL)) custom_code = (eprn->media_overrides == NULL? best_cmatch->code: best_cdmatch->code); if (best_dmatch == NULL || best_cmatch != NULL && better_flag_match(desired, eprn->optional_flags, best_dmatch->code, custom_code)) { if (flag_match(desired, eprn->optional_flags, custom_code)) { if (eprn->media_overrides == NULL) { eprn->code = best_cmatch->code; margins[0] = best_cmatch->left; margins[1] = best_cmatch->bottom; margins[2] = best_cmatch->right; margins[3] = best_cmatch->top; } else { eprn->code = best_cdmatch->code; margins[0] = best_cdmatch->left; margins[1] = best_cdmatch->bottom; margins[2] = best_cdmatch->right; margins[3] = best_cdmatch->top; } } } else { if (flag_match(desired, eprn->optional_flags, best_dmatch->code)) { eprn->code = best_dmatch->code; margins[0] = best_dmatch->left; margins[1] = best_dmatch->bottom; margins[2] = best_dmatch->right; margins[3] = best_dmatch->top; } } } /* If we've found a match, 'code' is no longer 'ms_none'. */ if (eprn->code == ms_none) { eprn_flag_mismatch(eprn, no_match); return -1; } /* Adapt the orientation of default default user space if not prescribed */ if (!eprn->leading_edge_set) { if (eprn->code & MS_TRANSVERSE_FLAG) eprn->default_orientation = 3; /* This leads to 0 if landscape orientation is requested. */ else eprn->default_orientation = 0; } /* Now 'eprn->default_orientation % 2' describes the sheet's orientation in pixmap device space. If this does not agree with the width and height values in the device instance, we'll have to adapt them. This is only necessary if there is a significant difference between width and height. */ if (fabs(w - h) > 1 /* arbitrary */ && (eprn->default_orientation % 2 == 0) != (dev->width/dev->HWResolution[0] <= dev->height/dev->HWResolution[1])) { bool reallocate = false;#ifdef EPRN_TRACE if_debug0(EPRN_TRACE_CHAR, "! eprn_set_page_layout(): width-height change is necessary.\n");#endif /* Free old storage if the device is open */ if (dev->is_open) {#ifdef EPRN_TRACE if_debug0(EPRN_TRACE_CHAR, "! eprn_set_page_layout(): Device is open.\n");#endif reallocate = true; /* One could try and call the allocation/reallocation routines of the prn device directly, but they are not available in older ghostscript versions and this method is safer anyway because it relies on a documented API. */ gdev_prn_close((gx_device *)dev); /* ignore the result */ } /* Now set width and height via gx_device_set_media_size(). This function sets 'MediaSize[]', 'width', and 'height' based on the assumption that default user space has a y axis which is vertical in pixmap device space. This may be wrong and we have to fix it. Because fixing 'MediaSize[]' is simpler, gx_device_set_media_size() is called such that it gives the correct values for 'width' and 'height'. */ if (eprn->default_orientation % 2 == 0) { /* portrait orientation of the sheet in pixmap device space */ gx_device_set_media_size((gx_device *)dev, w, h); if (landscape) { dev->MediaSize[0] = h; dev->MediaSize[1] = w; } } else { /* landscape orientation in pixmap device space (transverse) */ gx_device_set_media_size((gx_device *)dev, h, w); if (!landscape) { dev->MediaSize[0] = w; dev->MediaSize[1] = h; } } /* If the device is/was open, reallocate storage */ if (reallocate) { int rc; rc = gdev_prn_open((gx_device *)dev); if (rc < 0) { eprintf2("%s" ERRPREF "Failure of gdev_prn_open(), code is %d.\n", epref, rc); return rc; } } } /* Increase the bottom margin for coloured modes except if it is exactly zero */ if (eprn->colour_model != eprn_DeviceGray && margins[1] != 0.0) margins[1] += eprn->cap->bottom_increment; /* Number of +90-degree rotations needed for default user space: */ quarters = eprn->default_orientation; if (landscape) quarters = (quarters + 1)%4; /* Store the top and left margins in the device structure for use by eprn_get_initial_matrix() and set the margins of the printable area if we may. gx_device_set_margins() (see gsdevice.c) copies the margins[] array to HWMargins[] which is presumably to be interpreted in default user space (see gs_initclip() in gspath.c), and if its second argument is true it also modifies the offset variable Margins[]. The first property means that gx_device_set_margins() can only be used if default user space and pixmap device space have the same "up" direction, and the second appropriates a parameter which is intended for the user. */ if (eprn->keep_margins) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -