/* Note that .setdevice clears the current pagedevice. */ int zsetdevice(i_ctx_t *i_ctx_p) { gx_device *dev = gs_currentdevice(igs); os_ptr op = osp; int code = 0; check_write_type(*op, t_device); if (dev->LockSafetyParams) { /* do additional checking if locked */ if(op->value.pdevice != dev) /* don't allow a different device */ return_error(e_invalidaccess); } #ifndef PSI_INCLUDED /* the language switching build shouldn't install a new device here. The language switching machinery installs a shared device. */ code = gs_setdevice_no_erase(igs, op->value.pdevice); #endif if (code < 0) return code; make_bool(op, code != 0); /* erase page if 1 */ clear_pagedevice(istate); return code; }
/* retrieve the current pcl state and initialize pcl */ static int pxPassthrough_init(px_state_t * pxs) { int code; if (gs_debug_c('i')) dmprintf(pxs->memory, "passthrough: initializing global pcl state\n"); global_pcs = pcl_get_gstate(pxs->pcls); /* default to pcl5c */ global_pcs->personality = 0; /* for now we do not support intepolation in XL passthrough mode. */ global_pcs->interpolate = false; /* we don't see a nice way to support the following options with passthrough at this time (NB) */ global_pcs->page_set_on_command_line = false; global_pcs->res_set_on_command_line = false; global_pcs->high_level_device = false; { char buf[100]; int ret; stream_cursor_read r; ret = gs_sprintf(buf, "@PJL SET PAPERLENGTH = %d\n@PJL SET PAPERWIDTH = %d\n", (int)(pxs->media_dims.y * 10 + .5), (int)(pxs->media_dims.x * 10 + .5)); r.ptr = (byte *) buf - 1; r.limit = (byte *) buf + ret - 1; pjl_proc_process(pxs->pjls, &r); } /* do an initial reset to set up a permanent reset. The motivation here is to avoid tracking down a slew of memory leaks */ global_pcs->xfm_state.paper_size = pcl_get_default_paper(global_pcs); pcl_do_resets(global_pcs, pcl_reset_initial); pcl_do_resets(global_pcs, pcl_reset_permanent); /* initialize pcl and install xl's page device in pcl's state */ pcl_init_state(global_pcs, pxs->memory); code = gs_setdevice_no_erase(global_pcs->pgs, gs_currentdevice(pxs->pgs)); if (code < 0) return code; /* yet another reset with the new page device */ global_pcs->xfm_state.paper_size = pcl_get_default_paper(global_pcs); pcl_do_resets(global_pcs, pcl_reset_initial); /* set the parser state and initialize the pcl parser */ global_pcl_parser_state.definitions = global_pcs->pcl_commands; global_pcl_parser_state.hpgl_parser_state = &global_gl_parser_state; pcl_process_init(&global_pcl_parser_state); /* default 600 to match XL allow PCL to override */ global_pcs->uom_cp = 7200L / 600L; gs_setgray(global_pcs->pgs, 0); return 0; }
static int /* ESC & l <sd_enum> S */ pcl_simplex_duplex_print(pcl_args_t *pargs, pcl_state_t *pcs) { int code; bool reopen = false; /* oddly the command goes to the next page irrespective of arguments */ code = pcl_end_page_if_marked(pcs); if ( code < 0 ) return code; pcl_home_cursor(pcs); switch ( int_arg(pargs) ) { case 0: pcs->duplex = false; break; case 1: pcs->duplex = true; pcs->bind_short_edge = false; break; case 2: pcs->duplex = true; pcs->bind_short_edge = true; break; default: return 0; } code = put_param1_bool(pcs, "Duplex", pcs->duplex); switch ( code ) { case 1: /* reopen device */ reopen = true; case 0: break; case gs_error_undefined: return 0; default: /* error */ if ( code < 0 ) return code; } code = put_param1_bool(pcs, "BindShortEdge", pcs->bind_short_edge); switch ( code ) { case 1: /* reopen device */ reopen = true; case 0: case gs_error_undefined: break; default: /* error */ if ( code < 0 ) return code; } return (reopen ? gs_setdevice_no_erase(pcs->pgs, gs_currentdevice(pcs->pgs)) : 0); }
/* Set a device into an interpreter instance */ static int /* ret 0 ok, else -ve error code */ ps_impl_set_device( pl_interp_instance_t *instance, /* interp instance to use */ gx_device *device /* device to set (open or closed) */ ) { int code = 0; int exit_code = 0; ps_interp_instance_t *psi = (ps_interp_instance_t *)instance; gs_state *pgs = psi->minst->i_ctx_p->pgs; /* Initialize device ICC profile */ code = gsicc_init_device_profile(pgs, device); if (code < 0) return code; /* Set the device into the gstate */ code = gs_setdevice_no_erase(pgs, device); if (code >= 0 ) code = gs_erasepage(pgs); if (code < 0) return code; /* install a screen appropriate for the device */ { const char *screen_str = ".setdefaultscreen\n"; code = gsapi_run_string_continue(psi->plmemory->gs_lib_ctx, screen_str, strlen(screen_str), 0, &exit_code); /* needs more input this is not an error */ if ( code == e_NeedInput ) code = 0; if (code < 0) return code; } return exit_code; }
/* Note that .putdeviceparams clears the current pagedevice. */ static int zputdeviceparams(i_ctx_t *i_ctx_p) { uint count = ref_stack_counttomark(&o_stack); ref *prequire_all; ref *ppolicy; ref *pdev; gx_device *dev; stack_param_list list; int code; int old_width, old_height; int i, dest; if (count == 0) return_error(e_unmatchedmark); prequire_all = ref_stack_index(&o_stack, count); ppolicy = ref_stack_index(&o_stack, count + 1); pdev = ref_stack_index(&o_stack, count + 2); if (pdev == 0) return_error(e_stackunderflow); check_type_only(*prequire_all, t_boolean); check_write_type_only(*pdev, t_device); dev = pdev->value.pdevice; code = stack_param_list_read(&list, &o_stack, 0, ppolicy, prequire_all->value.boolval, iimemory); if (code < 0) return code; old_width = dev->width; old_height = dev->height; code = gs_putdeviceparams(dev, (gs_param_list *) & list); /* Check for names that were undefined or caused errors. */ for (dest = count - 2, i = 0; i < count >> 1; i++) if (list.results[i] < 0) { *ref_stack_index(&o_stack, dest) = *ref_stack_index(&o_stack, count - (i << 1) - 2); gs_errorname(i_ctx_p, list.results[i], ref_stack_index(&o_stack, dest - 1)); dest -= 2; } iparam_list_release(&list); if (code < 0) { /* There were errors reported. */ ref_stack_pop(&o_stack, dest + 1); return 0; } if (code > 0 || (code == 0 && (dev->width != old_width || dev->height != old_height))) { /* * The device was open and is now closed, or its dimensions have * changed. If it was the current device, call setdevice to * reinstall it and erase the page. */ /****** DOESN'T FIND ALL THE GSTATES THAT REFERENCE THE DEVICE. ******/ if (gs_currentdevice(igs) == dev) { bool was_open = dev->is_open; code = gs_setdevice_no_erase(igs, dev); /* If the device wasn't closed, setdevice won't erase the page. */ if (was_open && code >= 0) code = 1; } } if (code < 0) return code; ref_stack_pop(&o_stack, count + 1); make_bool(osp, code); clear_pagedevice(istate); return 0; }
int main(int argc, const char *argv[]) { char achar = '0'; gs_memory_t *mem; gs_state *pgs; const gx_device *const *list; gx_device *dev; gx_device_bbox *bbdev; int code; gp_init(); mem = gs_malloc_init(); gs_lib_init1(mem); if (argc < 2 || (achar = argv[1][0]) < '1' || achar > '0' + countof(tests) - 1 ) { lprintf1("Usage: gslib 1..%c\n", '0' + (char)countof(tests) - 1); gs_abort(mem); } gs_debug['@'] = 1; gs_debug['?'] = 1; /*gs_debug['B'] = 1; *//****** PATCH ******/ /*gs_debug['L'] = 1; *//****** PATCH ******/ /* * gs_iodev_init must be called after the rest of the inits, for * obscure reasons that really should be documented! */ gs_iodev_init(mem); /****** WRONG ******/ gs_lib_device_list(&list, NULL); gs_copydevice(&dev, list[0], mem); check_device_separable(dev); gx_device_fill_in_procs(dev); bbdev = gs_alloc_struct_immovable(mem, gx_device_bbox, &st_device_bbox, "bbox"); gx_device_bbox_init(bbdev, dev, mem); code = dev_proc(dev, get_profile)(dev, &bbdev->icc_struct); rc_increment(bbdev->icc_struct); /* Print out the device name just to test the gsparam.c API. */ { gs_c_param_list list; gs_param_string nstr; gs_c_param_list_write(&list, mem); code = gs_getdeviceparams(dev, (gs_param_list *) & list); if (code < 0) { lprintf1("getdeviceparams failed! code = %d\n", code); gs_abort(mem); } gs_c_param_list_read(&list); code = param_read_string((gs_param_list *) & list, "Name", &nstr); if (code < 0) { lprintf1("reading Name failed! code = %d\n", code); gs_abort(mem); } dputs("Device name = "); debug_print_string(nstr.data, nstr.size); dputs("\n"); gs_c_param_list_release(&list); } /* * If this is a device that takes an OutputFile, set the OutputFile * to "-" in the copy. */ { gs_c_param_list list; gs_param_string nstr; gs_c_param_list_write(&list, mem); param_string_from_string(nstr, "-"); code = param_write_string((gs_param_list *)&list, "OutputFile", &nstr); if (code < 0) { lprintf1("writing OutputFile failed! code = %d\n", code); gs_abort(mem); } gs_c_param_list_read(&list); code = gs_putdeviceparams(dev, (gs_param_list *)&list); gs_c_param_list_release(&list); if (code < 0 && code != gs_error_undefined) { lprintf1("putdeviceparams failed! code = %d\n", code); gs_abort(mem); } } dev = (gx_device *) bbdev; pgs = gs_state_alloc(mem); gs_setdevice_no_erase(pgs, dev); /* can't erase yet */ { gs_point dpi; gs_screen_halftone ht; gs_dtransform(pgs, 72.0, 72.0, &dpi); ht.frequency = min(fabs(dpi.x), fabs(dpi.y)) / 16.001; ht.angle = 0; ht.spot_function = odsf; gs_setscreen(pgs, &ht); } /* gsave and grestore (among other places) assume that */ /* there are at least 2 gstates on the graphics stack. */ /* Ensure that now. */ gs_gsave(pgs); gs_erasepage(pgs); code = (*tests[achar - '1']) (pgs, mem); gs_output_page(pgs, 1, 1); { gs_rect bbox; gx_device_bbox_bbox(bbdev, &bbox); dprintf4("Bounding box: [%g %g %g %g]\n", bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y); } if (code) dprintf1("**** Test returned code = %d.\n", code); dputs("Done. Press <enter> to exit."); fgetc(mem->gs_lib_ctx->fstdin); gs_lib_finit(0, 0, mem); return 0; #undef mem }
/* Set a device into an interperter instance */ static int /* ret 0 ok, else -ve error code */ pxl_impl_set_device( pl_interp_instance_t *instance, /* interp instance to use */ gx_device *device /* device to set (open or closed) */ ) { int code; pxl_interp_instance_t *pxli = (pxl_interp_instance_t *)instance; px_state_t *pxs = pxli->pxs; enum {Sbegin, Ssetdevice, Sinitg, Sgsave, Serase, Sdone} stage; stage = Sbegin; gs_opendevice(device); pxs->interpolate = pxl_get_interpolation(instance); /* Set the device into the gstate */ stage = Ssetdevice; if ((code = gs_setdevice_no_erase(pxli->pgs, device)) < 0) /* can't erase yet */ goto pisdEnd; /* Initialize device ICC profile */ code = gsicc_init_device_profile(pxli->pgs, device); if (code < 0) return code; /* Init XL graphics */ stage = Sinitg; if ((code = px_initgraphics(pxli->pxs)) < 0) goto pisdEnd; /* Do inits of gstate that may be reset by setdevice */ gs_setaccuratecurves(pxli->pgs, true); /* All H-P languages want accurate curves. */ /* disable hinting at high res */ if (gs_currentdevice(pxli->pgs)->HWResolution[0] >= 300) gs_setgridfittt(pxs->font_dir, 0); /* gsave and grestore (among other places) assume that */ /* there are at least 2 gstates on the graphics stack. */ /* Ensure that now. */ stage = Sgsave; if ( (code = gs_gsave(pxli->pgs)) < 0) goto pisdEnd; stage = Serase; if ( (code = gs_erasepage(pxli->pgs)) < 0 ) goto pisdEnd; stage = Sdone; /* success */ /* Unwind any errors */ pisdEnd: switch (stage) { case Sdone: /* don't undo success */ break; case Serase: /* gs_erasepage failed */ /* undo gsave */ gs_grestore_only(pxli->pgs); /* destroys gs_save stack */ /* fall thru to next */ case Sgsave: /* gsave failed */ case Sinitg: /* undo setdevice */ gs_nulldevice(pxli->pgs); /* fall thru to next */ case Ssetdevice: /* gs_setdevice failed */ case Sbegin: /* nothing left to undo */ break; } return code; }