static int test1(gs_state * pgs, gs_memory_t * mem) { int n; gs_scale(pgs, 72.0, 72.0); gs_translate(pgs, 4.25, 5.5); gs_scale(pgs, 4.0, 4.0); gs_newpath(pgs); for (n = 200; --n >= 0;) { int j; #define rf() (rand() / (1.0 * 0x10000 * 0x8000)) double r = rf(), g = rf(), b = rf(); double x0 = rf(), y0 = rf(), x1 = rf(), y1 = rf(), x2 = rf(), y2 = rf(); gs_setrgbcolor(pgs, r, g, b); for (j = 0; j < 6; j++) { gs_gsave(pgs); gs_rotate(pgs, 60.0 * j); gs_moveto(pgs, x0, y0); gs_lineto(pgs, x1, y1); gs_lineto(pgs, x2, y2); gs_fill(pgs); gs_grestore(pgs); } } #undef mem return 0; }
static int xps_paint_tiling_brush(const gs_client_color *pcc, gs_state *pgs) { const gs_client_pattern *ppat = gs_getpattern(pcc); struct tile_closure_s *c = ppat->client_data; xps_context_t *ctx = c->ctx; gs_state *saved_pgs; int code; saved_pgs = ctx->pgs; ctx->pgs = pgs; gs_gsave(ctx->pgs); code = xps_paint_tiling_brush_clipped(c); if (code) goto cleanup; gs_grestore(ctx->pgs); if (c->tile_mode == TILE_FLIP_X || c->tile_mode == TILE_FLIP_X_Y) { gs_gsave(ctx->pgs); gs_translate(ctx->pgs, c->viewbox.q.x * 2, 0.0); gs_scale(ctx->pgs, -1.0, 1.0); code = xps_paint_tiling_brush_clipped(c); if (code) goto cleanup; gs_grestore(ctx->pgs); } if (c->tile_mode == TILE_FLIP_Y || c->tile_mode == TILE_FLIP_X_Y) { gs_gsave(ctx->pgs); gs_translate(ctx->pgs, 0.0, c->viewbox.q.y * 2); gs_scale(ctx->pgs, 1.0, -1.0); code = xps_paint_tiling_brush_clipped(c); if (code) goto cleanup; gs_grestore(ctx->pgs); } if (c->tile_mode == TILE_FLIP_X_Y) { gs_gsave(ctx->pgs); gs_translate(ctx->pgs, c->viewbox.q.x * 2, c->viewbox.q.y * 2); gs_scale(ctx->pgs, -1.0, -1.0); code = xps_paint_tiling_brush_clipped(c); if (code) goto cleanup; gs_grestore(ctx->pgs); } ctx->pgs = saved_pgs; return 0; cleanup: gs_grestore(ctx->pgs); ctx->pgs = saved_pgs; return gs_rethrow(code, "cannot draw tile"); }
/* <sx> <sy> <matrix> scale <matrix> */ static int zscale(i_ctx_t *i_ctx_p) { os_ptr op = osp; int code; double scale[2]; if ((code = num_params(op, 2, scale)) >= 0) { code = gs_scale(igs, scale[0], scale[1]); if (code < 0) return code; } else { /* matrix operand */ gs_matrix mat; /* The num_params failure might be a stack underflow. */ check_op(2); if ((code = num_params(op - 1, 2, scale)) < 0 || (code = gs_make_scaling(scale[0], scale[1], &mat)) < 0 || (code = write_matrix(op, &mat)) < 0 ) { /* Might be a stack underflow. */ check_op(3); return code; } op[-2] = *op; } pop(2); return code; }
int pxSetPageScale(px_args_t *par, px_state_t *pxs) { int code; real sx = 1; real sy = 1; static const real units_conversion_table[3][3] = { { 1, 25.4, 254 }, /* in -> in, mill, 1/10 mill */ { 0.0394, 1, 10 }, /* mill -> in, mill, 1/10 mill */ { 0.00394, .1, 1 } /* 1/10 mill -> in, mill, 1/10 mill */ }; /* measuure and units of measure. Actually session user units divided by new user unit, bizarre. */ if ( par->pv[1] && par->pv[2] ) { /* new user measure */ real nux = real_value(par->pv[2], 0); real nuy = real_value(par->pv[2], 1); if ( nux != 0 && nuy != 0 ) { /* new measure */ pxeMeasure_t mt = par->pv[1]->value.i; /* convert to session units */ real factor = units_conversion_table[pxs->measure][mt]; real sux = nux * factor; real suy = nuy * factor; sx = pxs->units_per_measure.x / sux; sy = pxs->units_per_measure.y / suy; /* check for overflow. NB we should do a better job here */ if ( fabs(sx) > 1000.0 ) { dprintf2("warning probable overflow avoided for scaling factors %f %f\n", sx, sy ); sx = sy = 1; } } } else if ( par->pv[0] ) { /* page scale */ sx = real_value(par->pv[0], 0); sy = real_value(par->pv[0], 1); } code = gs_scale(pxs->pgs, sx, sy); if ( code < 0 ) return code; /* Post-multiply the text CTM by the scale matrix. */ { gs_matrix smat; px_gstate_t *pxgs = pxs->pxgs; gs_make_scaling(sx, sy, &smat); gs_matrix_multiply(&pxgs->text_ctm, &smat, &pxgs->text_ctm); } return 0; }
static int test6(gs_state * pgs, gs_memory_t * mem) { gs_color_space *pcs; gs_cie_abc *pabc; gs_cie_render *pcrd; static const gs_vector3 white_point = {1, 1, 1}; static const gs_cie_render_proc3 encode_abc = { {render_abc, render_abc, render_abc} }; int code; gs_color_space *rgb_cs; rgb_cs = gs_cspace_new_DeviceRGB(mem); gs_scale(pgs, 150.0, 150.0); gs_translate(pgs, 0.5, 0.5); gs_setcolorspace(pgs, rgb_cs); spectrum(pgs, 5); gs_translate(pgs, 1.2, 0.0); /* We must set the CRD before the color space. */ code = gs_cie_render1_build(&pcrd, mem, "test6"); if (code < 0) return code; gs_cie_render1_initialize(mem, pcrd, NULL, &white_point, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &encode_abc, NULL, NULL); gs_setcolorrendering(pgs, pcrd); gs_cspace_build_CIEABC(&pcs, NULL, mem); /* There should be an API for initializing CIE color spaces too.... */ pabc = pcs->params.abc; pabc->common.points.WhitePoint = white_point; gs_cie_abc_complete(pabc); /* End of initializing the color space. */ gs_setcolorspace(pgs, pcs); spectrum(pgs, 5); gs_free_object(mem, rgb_cs, "test6 rgb_cs"); return 0; }
/* Add a symbol to the path. */ static int hpgl_stick_arc_build_char(gs_show_enum *penum, gs_state *pgs, gs_font *pfont, gs_glyph uni_code, hpgl_font_type_t font_type) { int width; gs_matrix save_ctm; int code; /* we assert the font is present at this point */ width = hpgl_stick_arc_width(uni_code, font_type); /* *** incorrect comment The TRM says the stick font is based on a 32x32 unit cell, */ /* but the font we're using here is only 15x15. */ /* Also, per TRM 23-18, the character cell is only 2/3 the */ /* point size. */ gs_setcharwidth(penum, pgs, width / 1024.0 * 0.667, 0.0); gs_currentmatrix(pgs, &save_ctm); gs_scale(pgs, 1.0 / 1024.0 * .667, 1.0 / 1024.0 * .667); gs_moveto(pgs, 0.0, 0.0); code = hpgl_stick_arc_segments(pfont->memory, (void *)pgs, uni_code, font_type); if ( code < 0 ) return code; gs_setdefaultmatrix(pgs, NULL); gs_initmatrix(pgs); /* Set predictable join and cap styles. */ gs_setlinejoin(pgs, gs_join_round); gs_setmiterlimit(pgs, 2.61); /* start beveling at 45 degrees */ gs_setlinecap(pgs, gs_cap_round); { float pattern[1]; gs_setdash(pgs, pattern, 0, 0); } gs_stroke(pgs); gs_setmatrix(pgs, &save_ctm); return 0; }
static int test10(gs_state * pgs, gs_memory_t * mem) { gs_c_param_list list; gs_param_string nstr, OFstr; gs_param_float_array PSa; gs_param_float_array HWRa; gs_param_int_array HWSa; int HWSize[2]; float HWResolution[2], PageSize[2]; long MaxBitmap; int code; gx_device *dev = gs_currentdevice(pgs); float xlate_x, xlate_y; gs_rect cliprect; 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); } code = param_read_int_array((gs_param_list *) & list, "HWSize", &HWSa); if (code < 0) { lprintf1("reading HWSize failed! code = %d\n", code); gs_abort(mem); } emprintf3(mem, "HWSize[%d] = [ %d, %d ]\n", HWSa.size, HWSa.data[0], HWSa.data[1]); code = param_read_float_array((gs_param_list *) & list, "HWResolution", &HWRa); if (code < 0) { lprintf1("reading Resolution failed! code = %d\n", code); gs_abort(mem); } emprintf3(mem, "HWResolution[%d] = [ %f, %f ]\n", HWRa.size, HWRa.data[0], HWRa.data[1]); code = param_read_float_array((gs_param_list *) & list, "PageSize", &PSa); if (code < 0) { lprintf1("reading PageSize failed! code = %d\n", code); gs_abort(mem); } emprintf3(mem, "PageSize[%d] = [ %f, %f ]\n", PSa.size, PSa.data[0], PSa.data[1]); code = param_read_long((gs_param_list *) & list, "MaxBitmap", &MaxBitmap); if (code < 0) { lprintf1("reading MaxBitmap failed! code = %d\n", code); gs_abort(mem); } emprintf1(mem, "MaxBitmap = %ld\n", MaxBitmap); /* Switch to param list functions to "write" */ gs_c_param_list_write(&list, mem); /* Always set the PageSize. */ PageSize[0] = 72.0 * ypage_wid; PageSize[1] = 72.0 * xpage_len; PSa.data = PageSize; code = param_write_float_array((gs_param_list *) & list, "PageSize", &PSa); if (nstr.data[0] != 'v') { /* Set the OutputFile string file name */ OFstr.persistent = false; OFstr.data = outfile; OFstr.size = strlen(outfile); code = param_write_string((gs_param_list *) & list, "OutputFile", &OFstr); if (code < 0) { lprintf1("setting OutputFile name failed, code=%d\n", code); gs_abort(mem); } if (nstr.data[0] == 'x') { HWResolution[0] = HWResolution[1] = 72.0; } else { HWResolution[0] = HWResolution[1] = 360.0; } HWRa.data = HWResolution; HWSize[0] = (int)(HWResolution[0] * ypage_wid); HWSize[1] = (int)(HWResolution[1] * xpage_len); emprintf3(mem, "\tHWSize = [%d,%d], HWResolution = %f dpi\n", HWSize[0], HWSize[1], HWResolution[0]); HWSa.data = HWSize; code = param_write_float_array((gs_param_list *) & list, "HWResolution", &HWRa); code = param_write_int_array((gs_param_list *) & list, "HWSize", &HWSa); MaxBitmap = 1000000L; code = param_write_long((gs_param_list *) & list, "MaxBitmap", &MaxBitmap); } gs_c_param_list_read(&list); code = gs_putdeviceparams(dev, (gs_param_list *) & list); emprintf1(mem, "putdeviceparams: code=%d\n", code); gs_c_param_list_release(&list); /* note: initgraphics no longer resets the color or color space */ gs_erasepage(pgs); gs_initgraphics(pgs); { gs_color_space *cs = gs_cspace_new_DeviceGray(mem); gs_setcolorspace(pgs, cs); gs_setcolorspace(pgs, cs); gs_decrement(cs, "test10 DeviceGray"); } gs_clippath(pgs); gs_pathbbox(pgs, &cliprect); emprintf4(mem, "\tcliprect = [[%g,%g],[%g,%g]]\n", cliprect.p.x, cliprect.p.y, cliprect.q.x, cliprect.q.y); gs_newpath(pgs); switch (((rotate_value + 270) / 90) & 3) { default: case 0: /* 0 = 360 degrees in PS == 90 degrees in printer */ xlate_x = cliprect.p.x; xlate_y = cliprect.p.y; break; case 1: /* 90 degrees in PS = 180 degrees printer */ xlate_x = cliprect.q.x; xlate_y = cliprect.p.y; break; case 2: /* 180 degrees in PS == 270 degrees in printer */ xlate_x = cliprect.q.x; xlate_y = cliprect.q.y; break; case 3: /* 270 degrees in PS == 0 degrees in printer */ xlate_x = cliprect.p.x; xlate_y = cliprect.q.y; break; } emprintf2(mem, "translate origin to [ %f, %f ]\n", xlate_x, xlate_y); gs_translate(pgs, xlate_x, xlate_y); /* further move (before rotate) by user requested amount */ gs_translate(pgs, 72.0 * (float)xmove_origin, 72.0 * (float)ymove_origin); gs_rotate(pgs, (float)rotate_value + 270.0); gs_scale(pgs, scale_x * 72.0 / 2032.0, scale_y * 72.0 / 2032.0); gs_setlinecap(pgs, gs_cap_butt); gs_setlinejoin(pgs, gs_join_bevel); gs_setfilladjust(pgs, 0.0, 0.0); capture_exec(pgs); return 0; }
static int test5(gs_state * pgs, gs_memory_t * mem) { gx_device *dev = gs_currentdevice(pgs); gx_image_enum_common_t *info; gx_image_plane_t planes[5]; gx_drawing_color dcolor; int code; static const byte data3[] = { 0x00, 0x44, 0x88, 0xcc, 0x44, 0x88, 0xcc, 0x00, 0x88, 0xcc, 0x00, 0x44, 0xcc, 0x00, 0x44, 0x88 }; gs_color_space *gray_cs = gs_cspace_new_DeviceGray(mem); /* * Neither ImageType 3 nor 4 needs a current color, * but some intermediate code assumes it's valid. */ set_nonclient_dev_color(&dcolor, 0); /* Scale everything up, and fill the background. */ { gs_matrix mat; gs_currentmatrix(pgs, &mat); mat.xx = gs_copysign(98.6, mat.xx); mat.yy = gs_copysign(98.6, mat.yy); mat.tx = floor(mat.tx) + 0.499; mat.ty = floor(mat.ty) + 0.499; gs_setmatrix(pgs, &mat); } gs_setrgbcolor(pgs, 1.0, 0.9, 0.9); fill_rect1(pgs, 0.25, 0.25, 4.0, 6.0); gs_setrgbcolor(pgs, 0.5, 1.0, 0.5); #if 0 /* Make things a little more interesting.... */ gs_translate(pgs, 1.0, 1.0); gs_rotate(pgs, 10.0); gs_scale(pgs, 1.3, 0.9); #endif #define do_image(image, idata)\ BEGIN\ code = gx_device_begin_typed_image(dev, (gs_imager_state *)pgs, NULL,\ (gs_image_common_t *)&image, NULL, &dcolor, NULL, mem, &info);\ /****** TEST code >= 0 ******/\ planes[0].data = idata;\ planes[0].data_x = 0;\ planes[0].raster = (image.Height * image.BitsPerComponent + 7) >> 3;\ code = gx_image_plane_data(info, planes, image.Height);\ /****** TEST code == 1 ******/\ code = gx_image_end(info, true);\ /****** TEST code >= 0 ******/\ END #define W 4 #define H 4 /* Test an unmasked image. */ gs_gsave(pgs); { gs_image1_t image1; void *info1; gs_color_space *cs; cs = gs_cspace_new_DeviceGray(mem); gs_image_t_init(&image1, cs); /* image */ image1.ImageMatrix.xx = W; image1.ImageMatrix.yy = -H; image1.ImageMatrix.ty = H; /* data_image */ image1.Width = W; image1.Height = H; image1.BitsPerComponent = 8; gs_translate(pgs, 0.5, 4.0); code = gx_device_begin_image(dev, (gs_imager_state *) pgs, &image1, gs_image_format_chunky, NULL, &dcolor, NULL, mem, &info1); /****** TEST code >= 0 ******/ planes[0].data = data3; planes[0].data_x = 0; planes[0].raster = (image1.Height * image1.BitsPerComponent + 7) >> 3; /* Use the old image_data API. */ code = gx_image_data(info1, &planes[0].data, 0, planes[0].raster, image1.Height); /****** TEST code == 1 ******/ code = gx_image_end(info1, true); /****** TEST code >= 0 ******/ gs_free_object(mem, cs, "colorspace"); } gs_grestore(pgs); /* Test an explicitly masked image. */ gs_gsave(pgs); { gs_image3_t image3; static const byte data3mask[] = { 0x60, 0x90, 0x90, 0x60 }; static const byte data3x2mask[] = { 0x66, 0x99, 0x99, 0x66, 0x66, 0x99, 0x99, 0x66 }; gs_image3_t_init(&image3, gray_cs, interleave_scan_lines); /* image */ image3.ImageMatrix.xx = W; image3.ImageMatrix.yy = -H; image3.ImageMatrix.ty = H; /* data_image */ image3.Width = W; image3.Height = H; image3.BitsPerComponent = 8; /* MaskDict */ image3.MaskDict.ImageMatrix = image3.ImageMatrix; image3.MaskDict.Width = image3.Width; image3.MaskDict.Height = image3.Height; /* Display with 1-for-1 mask and image. */ gs_translate(pgs, 0.5, 2.0); code = gx_device_begin_typed_image(dev, (gs_imager_state *) pgs, NULL, (gs_image_common_t *) & image3, NULL, &dcolor, NULL, mem, &info); /****** TEST code >= 0 ******/ planes[0].data = data3mask; planes[0].data_x = 0; planes[0].raster = (image3.MaskDict.Height + 7) >> 3; planes[1].data = data3; planes[1].data_x = 0; planes[1].raster = (image3.Height * image3.BitsPerComponent + 7) >> 3; code = gx_image_plane_data(info, planes, image3.Height); /****** TEST code == 1 ******/ code = gx_image_end(info, true); /****** TEST code >= 0 ******/ /* Display with 2-for-1 mask and image. */ image3.MaskDict.ImageMatrix.xx *= 2; image3.MaskDict.ImageMatrix.yy *= 2; image3.MaskDict.ImageMatrix.ty *= 2; image3.MaskDict.Width *= 2; image3.MaskDict.Height *= 2; gs_translate(pgs, 1.5, 0.0); code = gx_device_begin_typed_image(dev, (gs_imager_state *) pgs, NULL, (gs_image_common_t *) & image3, NULL, &dcolor, NULL, mem, &info); /****** TEST code >= 0 ******/ planes[0].data = data3x2mask; planes[0].raster = (image3.MaskDict.Width + 7) >> 3; { int i; for (i = 0; i < H; ++i) { planes[1].data = 0; code = gx_image_plane_data(info, planes, 1); planes[0].data += planes[0].raster; /****** TEST code == 0 ******/ planes[1].data = data3 + i * planes[1].raster; code = gx_image_plane_data(info, planes, 1); planes[0].data += planes[0].raster; /****** TEST code >= 0 ******/ } } /****** TEST code == 1 ******/ code = gx_image_end(info, true); /****** TEST code >= 0 ******/ } gs_grestore(pgs); /* Test a chroma-keyed masked image. */ gs_gsave(pgs); { gs_image4_t image4; const byte *data4 = data3; gs_image4_t_init(&image4, gray_cs); /* image */ image4.ImageMatrix.xx = W; image4.ImageMatrix.yy = -H; image4.ImageMatrix.ty = H; /* data_image */ image4.Width = W; image4.Height = H; image4.BitsPerComponent = 8; /* Display with a single mask color. */ gs_translate(pgs, 0.5, 0.5); image4.MaskColor_is_range = false; image4.MaskColor[0] = 0xcc; do_image(image4, data4); /* Display a second time with a color range. */ gs_translate(pgs, 1.5, 0.0); image4.MaskColor_is_range = true; image4.MaskColor[0] = 0x40; image4.MaskColor[1] = 0x90; do_image(image4, data4); } gs_grestore(pgs); gs_free_object(mem, gray_cs, "test5 gray_cs"); #undef W #undef H #undef do_image return 0; }
int gslt_outline_font_glyph(gs_state *pgs, gslt_font_t *xf, int gid, gslt_outline_walker_t *walk) { gs_text_params_t params; gs_text_enum_t *textenum; gs_matrix matrix; segment *seg; curve_segment *cseg; gs_gsave(pgs); gs_make_identity(&matrix); gs_setmatrix(pgs, &matrix); gs_scale(pgs, 1000.0, 1000.0); /* otherwise we hit serious precision problems with fixpoint math */ /* set gstate params */ gs_setfont(pgs, xf->font); /* set pgs->font and invalidate existing charmatrix */ gs_make_identity(&matrix); gs_setcharmatrix(pgs, &matrix); /* set the charmatrix to identity */ /* reset the path */ gs_newpath(pgs); gs_moveto(pgs, 0.0, 0.0); /* draw the glyph, in charpath mode */ params.operation = TEXT_FROM_SINGLE_GLYPH | TEXT_DO_FALSE_CHARPATH | TEXT_RETURN_WIDTH; params.data.d_glyph = gid; params.size = 1; if (gs_text_begin(pgs, ¶ms, xf->font->memory, &textenum) != 0) return gs_throw(-1, "cannot gs_text_begin()"); if (gs_text_process(textenum) != 0) return gs_throw(-1, "cannot gs_text_process()"); gs_text_release(textenum, "gslt font outline"); /* walk the resulting path */ seg = (segment*)pgs->path->first_subpath; while (seg) { switch (seg->type) { case s_start: walk->moveto(walk->user, fixed2float(seg->pt.x) * 0.001, fixed2float(seg->pt.y) * 0.001); break; case s_line: walk->lineto(walk->user, fixed2float(seg->pt.x) * 0.001, fixed2float(seg->pt.y) * 0.001); break; case s_line_close: walk->closepath(walk->user); break; case s_curve: cseg = (curve_segment*)seg; walk->curveto(walk->user, fixed2float(cseg->p1.x) * 0.001, fixed2float(cseg->p1.y) * 0.001, fixed2float(cseg->p2.x) * 0.001, fixed2float(cseg->p2.y) * 0.001, fixed2float(seg->pt.x) * 0.001, fixed2float(seg->pt.y) * 0.001); break; } seg = seg->next; } /* and toss it away... */ gs_newpath(pgs); gs_grestore(pgs); return 0; }
int xps_parse_tiling_brush(xps_context_t *ctx, char *base_uri, xps_resource_t *dict, xps_item_t *root, int (*func)(xps_context_t*, char*, xps_resource_t*, xps_item_t*, void*), void *user) { xps_item_t *node; int code; char *opacity_att; char *transform_att; char *viewbox_att; char *viewport_att; char *tile_mode_att; /*char *viewbox_units_att;*/ /*char *viewport_units_att;*/ xps_item_t *transform_tag = NULL; gs_matrix transform; gs_rect viewbox; gs_rect viewport; float scalex, scaley; int tile_mode; opacity_att = xps_att(root, "Opacity"); transform_att = xps_att(root, "Transform"); viewbox_att = xps_att(root, "Viewbox"); viewport_att = xps_att(root, "Viewport"); tile_mode_att = xps_att(root, "TileMode"); /*viewbox_units_att = xps_att(root, "ViewboxUnits");*/ /*viewport_units_att = xps_att(root, "ViewportUnits");*/ for (node = xps_down(root); node; node = xps_next(node)) { if (!strcmp(xps_tag(node), "ImageBrush.Transform")) transform_tag = xps_down(node); if (!strcmp(xps_tag(node), "VisualBrush.Transform")) transform_tag = xps_down(node); } xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL); gs_make_identity(&transform); if (transform_att) xps_parse_render_transform(ctx, transform_att, &transform); if (transform_tag) xps_parse_matrix_transform(ctx, transform_tag, &transform); viewbox.p.x = 0.0; viewbox.p.y = 0.0; viewbox.q.x = 1.0; viewbox.q.y = 1.0; if (viewbox_att) xps_parse_rectangle(ctx, viewbox_att, &viewbox); viewport.p.x = 0.0; viewport.p.y = 0.0; viewport.q.x = 1.0; viewport.q.y = 1.0; if (viewport_att) xps_parse_rectangle(ctx, viewport_att, &viewport); /* some sanity checks on the viewport/viewbox size */ if (fabs(viewport.q.x - viewport.p.x) < 0.01) { gs_warn("skipping tile with zero width view port"); return 0; } if (fabs(viewport.q.y - viewport.p.y) < 0.01) { gs_warn("skipping tile with zero height view port"); return 0; } if (fabs(viewbox.q.x - viewbox.p.x) < 0.01) { gs_warn("skipping tile with zero width view box"); return 0; } if (fabs(viewbox.q.y - viewbox.p.y) < 0.01) { gs_warn("skipping tile with zero height view box"); return 0; } scalex = (viewport.q.x - viewport.p.x) / (viewbox.q.x - viewbox.p.x); scaley = (viewport.q.y - viewport.p.y) / (viewbox.q.y - viewbox.p.y); tile_mode = TILE_NONE; if (tile_mode_att) { if (!strcmp(tile_mode_att, "None")) tile_mode = TILE_NONE; if (!strcmp(tile_mode_att, "Tile")) tile_mode = TILE_TILE; if (!strcmp(tile_mode_att, "FlipX")) tile_mode = TILE_FLIP_X; if (!strcmp(tile_mode_att, "FlipY")) tile_mode = TILE_FLIP_Y; if (!strcmp(tile_mode_att, "FlipXY")) tile_mode = TILE_FLIP_X_Y; } gs_gsave(ctx->pgs); code = xps_begin_opacity(ctx, base_uri, dict, opacity_att, NULL, false, false); if (code) { gs_grestore(ctx->pgs); return gs_rethrow(code, "cannot create transparency group"); } /* TODO(tor): check viewport and tiling to see if we can set it to TILE_NONE */ if (tile_mode != TILE_NONE) { struct tile_closure_s closure; gs_client_pattern gspat; gs_client_color gscolor; gs_color_space *cs; bool sa; closure.ctx = ctx; closure.base_uri = base_uri; closure.dict = dict; closure.tag = root; closure.tile_mode = tile_mode; closure.user = user; closure.func = func; closure.viewbox.p.x = viewbox.p.x; closure.viewbox.p.y = viewbox.p.y; closure.viewbox.q.x = viewbox.q.x; closure.viewbox.q.y = viewbox.q.y; gs_pattern1_init(&gspat); uid_set_UniqueID(&gspat.uid, gs_next_ids(ctx->memory, 1)); gspat.PaintType = 1; gspat.TilingType = 2; gspat.PaintProc = xps_remap_pattern; gspat.client_data = &closure; /* We need to know if this tiling brush includes transparency. We could do a proper scan, but for now we'll be lazy and just look at the flag from scanning the page. */ gspat.uses_transparency = ctx->has_transparency; gspat.XStep = viewbox.q.x - viewbox.p.x; gspat.YStep = viewbox.q.y - viewbox.p.y; gspat.BBox.p.x = viewbox.p.x; gspat.BBox.p.y = viewbox.p.y; gspat.BBox.q.x = viewbox.q.x; gspat.BBox.q.y = viewbox.q.y; if (tile_mode == TILE_FLIP_X || tile_mode == TILE_FLIP_X_Y) { gspat.BBox.q.x += gspat.XStep; gspat.XStep *= 2; } if (tile_mode == TILE_FLIP_Y || tile_mode == TILE_FLIP_X_Y) { gspat.BBox.q.y += gspat.YStep; gspat.YStep *= 2; } gs_matrix_translate(&transform, viewport.p.x, viewport.p.y, &transform); gs_matrix_scale(&transform, scalex, scaley, &transform); gs_matrix_translate(&transform, -viewbox.p.x, -viewbox.p.y, &transform); cs = ctx->srgb; gs_setcolorspace(ctx->pgs, cs); gsicc_profile_reference(cs->cmm_icc_profile_data, 1); sa = gs_currentstrokeadjust(ctx->pgs); gs_setstrokeadjust(ctx->pgs, false); gs_makepattern(&gscolor, &gspat, &transform, ctx->pgs, NULL); gs_setpattern(ctx->pgs, &gscolor); xps_fill(ctx); gs_setstrokeadjust(ctx->pgs, sa); gsicc_profile_reference(cs->cmm_icc_profile_data, -1); /* gs_makepattern increments the pattern count stored in the color * structure. We will discard the color struct (its on the stack) * so we need to decrement the reference before we throw away * the structure. */ gs_pattern_reference(&gscolor, -1); } else { xps_clip(ctx); gs_concat(ctx->pgs, &transform); gs_translate(ctx->pgs, viewport.p.x, viewport.p.y); gs_scale(ctx->pgs, scalex, scaley); gs_translate(ctx->pgs, -viewbox.p.x, -viewbox.p.y); gs_moveto(ctx->pgs, viewbox.p.x, viewbox.p.y); gs_lineto(ctx->pgs, viewbox.p.x, viewbox.q.y); gs_lineto(ctx->pgs, viewbox.q.x, viewbox.q.y); gs_lineto(ctx->pgs, viewbox.q.x, viewbox.p.y); gs_closepath(ctx->pgs); gs_clip(ctx->pgs); gs_newpath(ctx->pgs); code = func(ctx, base_uri, dict, root, user); if (code < 0) { xps_end_opacity(ctx, base_uri, dict, opacity_att, NULL); gs_grestore(ctx->pgs); return gs_rethrow(code, "cannot draw tile"); } } xps_end_opacity(ctx, base_uri, dict, opacity_att, NULL); gs_grestore(ctx->pgs); return 0; }
int xps_parse_fixed_page(xps_context_t *ctx, xps_part_t *part) { xps_item_t *root, *node; xps_resource_t *dict; char *width_att; char *height_att; char base_uri[1024]; char *s; int code; if_debug1m('|', ctx->memory, "doc: parsing page %s\n", part->name); xps_strlcpy(base_uri, part->name, sizeof base_uri); s = strrchr(base_uri, '/'); if (s) s[1] = 0; root = xps_parse_xml(ctx, part->data, part->size); if (!root) return gs_rethrow(-1, "cannot parse xml"); if (strcmp(xps_tag(root), "FixedPage")) return gs_throw1(-1, "expected FixedPage element (found %s)", xps_tag(root)); width_att = xps_att(root, "Width"); height_att = xps_att(root, "Height"); if (!width_att) return gs_throw(-1, "FixedPage missing required attribute: Width"); if (!height_att) return gs_throw(-1, "FixedPage missing required attribute: Height"); dict = NULL; /* Setup new page */ { gs_memory_t *mem = ctx->memory; gs_state *pgs = ctx->pgs; gx_device *dev = gs_currentdevice(pgs); gs_param_float_array fa; float fv[2]; gs_c_param_list list; gs_c_param_list_write(&list, mem); fv[0] = atoi(width_att) / 96.0 * 72.0; fv[1] = atoi(height_att) / 96.0 * 72.0; fa.persistent = false; fa.data = fv; fa.size = 2; code = param_write_float_array((gs_param_list *)&list, ".MediaSize", &fa); if ( code >= 0 ) { gs_c_param_list_read(&list); code = gs_putdeviceparams(dev, (gs_param_list *)&list); } gs_c_param_list_release(&list); /* nb this is for the demo it is wrong and should be removed */ gs_initgraphics(pgs); /* 96 dpi default - and put the origin at the top of the page */ gs_initmatrix(pgs); code = gs_scale(pgs, 72.0/96.0, -72.0/96.0); if (code < 0) return gs_rethrow(code, "cannot set page transform"); code = gs_translate(pgs, 0.0, -atoi(height_att)); if (code < 0) return gs_rethrow(code, "cannot set page transform"); code = gs_erasepage(pgs); if (code < 0) return gs_rethrow(code, "cannot clear page"); } /* Pre-parse looking for transparency */ ctx->has_transparency = 0; for (node = xps_down(root); node; node = xps_next(node)) { if (!strcmp(xps_tag(node), "FixedPage.Resources") && xps_down(node)) if (xps_resource_dictionary_has_transparency(ctx, base_uri, xps_down(node))) ctx->has_transparency = 1; if (xps_element_has_transparency(ctx, base_uri, node)) ctx->has_transparency = 1; } /* save the state with the original device before we push */ gs_gsave(ctx->pgs); if (ctx->use_transparency && ctx->has_transparency) { code = gs_push_pdf14trans_device(ctx->pgs, false); if (code < 0) { gs_grestore(ctx->pgs); return gs_rethrow(code, "cannot install transparency device"); } } /* Draw contents */ for (node = xps_down(root); node; node = xps_next(node)) { if (!strcmp(xps_tag(node), "FixedPage.Resources") && xps_down(node)) { code = xps_parse_resource_dictionary(ctx, &dict, base_uri, xps_down(node)); if (code) { gs_pop_pdf14trans_device(ctx->pgs, false); gs_grestore(ctx->pgs); return gs_rethrow(code, "cannot load FixedPage.Resources"); } } code = xps_parse_element(ctx, base_uri, dict, node); if (code) { gs_pop_pdf14trans_device(ctx->pgs, false); gs_grestore(ctx->pgs); return gs_rethrow(code, "cannot parse child of FixedPage"); } } if (ctx->use_transparency && ctx->has_transparency) { code = gs_pop_pdf14trans_device(ctx->pgs, false); if (code < 0) { gs_grestore(ctx->pgs); return gs_rethrow(code, "cannot uninstall transparency device"); } } /* Flush page */ { code = xps_show_page(ctx, 1, true); /* copies, flush */ if (code < 0) { gs_grestore(ctx->pgs); return gs_rethrow(code, "cannot flush page"); } } /* restore the original device, discarding the pdf14 compositor */ gs_grestore(ctx->pgs); if (dict) { xps_free_resource_dictionary(ctx, dict); } xps_free_item(ctx, root); return 0; }