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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/* 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.
*/
/* XPS interpreter - tiles for pattern rendering */
#include "ghostxps.h"
#include "gxdevsop.h"
/*
* Parse a tiling brush (visual and image brushes at this time) common
* properties. Use the callback to draw the individual tiles.
*/
enum { TILE_NONE, TILE_TILE, TILE_FLIP_X, TILE_FLIP_Y, TILE_FLIP_X_Y };
struct tile_closure_s
{
xps_context_t *ctx;
char *base_uri;
xps_resource_t *dict;
xps_item_t *tag;
gs_rect viewbox;
int tile_mode;
void *user;
int (*func)(xps_context_t*, char*, xps_resource_t*, xps_item_t*, void*);
};
static int
xps_paint_tiling_brush_clipped(struct tile_closure_s *c)
{
xps_context_t *ctx = c->ctx;
int code;
gs_moveto(ctx->pgs, c->viewbox.p.x, c->viewbox.p.y);
gs_lineto(ctx->pgs, c->viewbox.p.x, c->viewbox.q.y);
gs_lineto(ctx->pgs, c->viewbox.q.x, c->viewbox.q.y);
gs_lineto(ctx->pgs, c->viewbox.q.x, c->viewbox.p.y);
gs_closepath(ctx->pgs);
gs_clip(ctx->pgs);
gs_newpath(ctx->pgs);
code = c->func(c->ctx, c->base_uri, c->dict, c->tag, c->user);
if (code < 0)
return gs_rethrow(code, "cannot draw clipped tile");
return 0;
}
static int
xps_paint_tiling_brush(const gs_client_color *pcc, gs_gstate *pgs)
{
struct tile_closure_s *c = pcc->pattern->client_data;
xps_context_t *ctx = c->ctx;
gs_gstate *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");
}
int
xps_high_level_pattern(xps_context_t *ctx)
{
gs_matrix m;
gs_rect bbox;
gs_fixed_rect clip_box;
int code;
gx_device_color *pdc = gs_currentdevicecolor_inline(ctx->pgs);
const gs_client_pattern *ppat = gs_getpattern(&pdc->ccolor);
gs_pattern1_instance_t *pinst =
(gs_pattern1_instance_t *)gs_currentcolor(ctx->pgs)->pattern;
code = gx_pattern_cache_add_dummy_entry(ctx->pgs, pinst,
ctx->pgs->device->color_info.depth);
if (code < 0)
return code;
code = gs_gsave(ctx->pgs);
if (code < 0)
return code;
dev_proc(ctx->pgs->device, get_initial_matrix)(ctx->pgs->device, &m);
gs_setmatrix(ctx->pgs, &m);
code = gs_bbox_transform(&ppat->BBox, &ctm_only(ctx->pgs), &bbox);
if (code < 0) {
gs_grestore(ctx->pgs);
return code;
}
clip_box.p.x = float2fixed(bbox.p.x);
clip_box.p.y = float2fixed(bbox.p.y);
clip_box.q.x = float2fixed(bbox.q.x);
clip_box.q.y = float2fixed(bbox.q.y);
code = gx_clip_to_rectangle(ctx->pgs, &clip_box);
if (code < 0) {
gs_grestore(ctx->pgs);
return code;
}
{
pattern_accum_param_s param;
param.pinst = (void *)pinst;
param.graphics_state = (void *)ctx->pgs;
param.pinst_id = pinst->id;
code = (*dev_proc(ctx->pgs->device, dev_spec_op))((gx_device *)ctx->pgs->device,
gxdso_pattern_start_accum, ¶m, sizeof(pattern_accum_param_s));
}
if (code < 0) {
gs_grestore(ctx->pgs);
return code;
}
code = xps_paint_tiling_brush(&pdc->ccolor, ctx->pgs);
if (code) {
gs_grestore(ctx->pgs);
return gs_rethrow(code, "high level pattern brush function failed");
}
code = gs_grestore(ctx->pgs);
if (code < 0)
return code;
{
pattern_accum_param_s param;
param.pinst = (void *)pinst;
param.graphics_state = (void *)ctx->pgs;
param.pinst_id = pinst->id;
code = (*dev_proc(ctx->pgs->device, dev_spec_op))((gx_device *)ctx->pgs->device,
gxdso_pattern_finish_accum, ¶m, sizeof(pattern_accum_param_s));
}
return code;
}
static int
xps_remap_pattern(const gs_client_color *pcc, gs_gstate *pgs)
{
gs_client_pattern *ppat = (gs_client_pattern *)gs_getpattern(pcc);
struct tile_closure_s *c = pcc->pattern->client_data;
xps_context_t *ctx = c->ctx;
int code;
/* pgs->device is the newly created pattern accumulator, but we want to test the device
* that is 'behind' that, the actual output device, so we use the one from
* the saved XPS graphics state.
*/
code = dev_proc(ctx->pgs->device, dev_spec_op)(ctx->pgs->device,
gxdso_pattern_can_accum, ppat, ppat->uid.id);
if (code == 1) {
/* Device handles high-level patterns, so return 'remap'.
* This closes the internal accumulator device, as we no longer need
* it, and the error trickles back up to the PDL client. The client
* must then take action to start the device's accumulator, draw the
* pattern, close the device's accumulator and generate a cache entry.
*/
return gs_error_Remap_Color;
} else {
code = xps_paint_tiling_brush(pcc, pgs);
if (code)
return gs_rethrow(code, "remap pattern brush function failed");
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;
float opacity;
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;
/* 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_adjust_profile_rc(cs->cmm_icc_profile_data, 1, "xps_parse_tiling_brush");
sa = gs_currentstrokeadjust(ctx->pgs);
gs_setstrokeadjust(ctx->pgs, false);
gs_makepattern(&gscolor, &gspat, &transform, ctx->pgs, NULL);
gscolor.pattern->client_data = &closure;
gs_setpattern(ctx->pgs, &gscolor);
/* If the tiling brush has an opacity, it was already set in the group
that we are filling. Reset to 1.0 here to avoid double application
when the tiling actually occurs */
opacity = gs_getfillconstantalpha(ctx->pgs);
gs_setfillconstantalpha(ctx->pgs, 1.0);
gs_setstrokeconstantalpha(ctx->pgs, 1.0);
xps_fill(ctx);
gs_setfillconstantalpha(ctx->pgs, opacity);
gs_setstrokeconstantalpha(ctx->pgs, opacity);
gs_setstrokeadjust(ctx->pgs, sa);
gsicc_adjust_profile_rc(cs->cmm_icc_profile_data, -1, "xps_parse_tiling_brush");
/* 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;
}
|