static void pdf_load_radial_shading(fz_shade *shade, pdf_xref *xref, fz_obj *dict, int funcs, pdf_function **func) { fz_obj *obj; float d0, d1; int e0, e1; float x0, y0, r0, x1, y1, r1; struct vertex p1, p2; fz_context *ctx = xref->ctx; obj = fz_dict_gets(ctx, dict, "Coords"); x0 = fz_to_real(ctx, fz_array_get(ctx, obj, 0)); y0 = fz_to_real(ctx, fz_array_get(ctx, obj, 1)); r0 = fz_to_real(ctx, fz_array_get(ctx, obj, 2)); x1 = fz_to_real(ctx, fz_array_get(ctx, obj, 3)); y1 = fz_to_real(ctx, fz_array_get(ctx, obj, 4)); r1 = fz_to_real(ctx, fz_array_get(ctx, obj, 5)); d0 = 0; d1 = 1; obj = fz_dict_gets(ctx, dict, "Domain"); if (fz_array_len(ctx, obj) == 2) { d0 = fz_to_real(ctx, fz_array_get(ctx, obj, 0)); d1 = fz_to_real(ctx, fz_array_get(ctx, obj, 1)); } e0 = e1 = 0; obj = fz_dict_gets(ctx, dict, "Extend"); if (fz_array_len(ctx, obj) == 2) { e0 = fz_to_bool(ctx, fz_array_get(ctx, obj, 0)); e1 = fz_to_bool(ctx, fz_array_get(ctx, obj, 1)); } pdf_sample_shade_function(ctx, shade, funcs, func, d0, d1); shade->type = FZ_RADIAL; shade->extend[0] = e0; shade->extend[1] = e1; p1.x = x0; p1.y = y0; p1.c[0] = r0; pdf_add_vertex(ctx, shade, &p1); p2.x = x1; p2.y = y1; p2.c[0] = r1; pdf_add_vertex(ctx, shade, &p2); }
static void pdf_load_axial_shading(fz_shade *shade, pdf_xref *xref, fz_obj *dict, int funcs, pdf_function **func) { fz_obj *obj; float d0, d1; int e0, e1; float x0, y0, x1, y1; struct vertex p1, p2; obj = fz_dict_gets(dict, "Coords"); x0 = fz_to_real(fz_array_get(obj, 0)); y0 = fz_to_real(fz_array_get(obj, 1)); x1 = fz_to_real(fz_array_get(obj, 2)); y1 = fz_to_real(fz_array_get(obj, 3)); d0 = 0; d1 = 1; obj = fz_dict_gets(dict, "Domain"); if (fz_array_len(obj) == 2) { d0 = fz_to_real(fz_array_get(obj, 0)); d1 = fz_to_real(fz_array_get(obj, 1)); } e0 = e1 = 0; obj = fz_dict_gets(dict, "Extend"); if (fz_array_len(obj) == 2) { e0 = fz_to_bool(fz_array_get(obj, 0)); e1 = fz_to_bool(fz_array_get(obj, 1)); } pdf_sample_shade_function(shade, funcs, func, d0, d1); shade->type = FZ_LINEAR; shade->extend[0] = e0; shade->extend[1] = e1; p1.x = x0; p1.y = y0; p1.c[0] = 0; pdf_add_vertex(shade, &p1); p2.x = x1; p2.y = y1; p2.c[0] = 0; pdf_add_vertex(shade, &p2); }
static fz_error pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict, fz_stream *cstm, int forcemask) { fz_stream *stm; fz_pixmap *tile; fz_obj *obj, *res; fz_error error; int w, h, bpc, n; int imagemask; int interpolate; int indexed; fz_colorspace *colorspace; fz_pixmap *mask; /* explicit mask/softmask image */ int usecolorkey; int colorkey[FZ_MAX_COLORS * 2]; float decode[FZ_MAX_COLORS * 2]; int stride; unsigned char *samples; int i, len; /* special case for JPEG2000 images */ if (pdf_is_jpx_image(dict)) { tile = NULL; error = pdf_load_jpx_image(&tile, xref, dict); if (error) return fz_rethrow(error, "cannot load jpx image"); if (forcemask) { if (tile->n != 2) { fz_drop_pixmap(tile); return fz_throw("softmask must be grayscale"); } mask = fz_alpha_from_gray(tile, 1); fz_drop_pixmap(tile); *imgp = mask; return fz_okay; } *imgp = tile; return fz_okay; } w = fz_to_int(fz_dict_getsa(dict, "Width", "W")); h = fz_to_int(fz_dict_getsa(dict, "Height", "H")); bpc = fz_to_int(fz_dict_getsa(dict, "BitsPerComponent", "BPC")); imagemask = fz_to_bool(fz_dict_getsa(dict, "ImageMask", "IM")); interpolate = fz_to_bool(fz_dict_getsa(dict, "Interpolate", "I")); indexed = 0; usecolorkey = 0; colorspace = NULL; mask = NULL; if (imagemask) bpc = 1; if (w == 0) return fz_throw("image width is zero"); if (h == 0) return fz_throw("image height is zero"); if (bpc == 0) return fz_throw("image depth is zero"); if (w > (1 << 16)) return fz_throw("image is too wide"); if (h > (1 << 16)) return fz_throw("image is too high"); obj = fz_dict_getsa(dict, "ColorSpace", "CS"); if (obj && !imagemask && !forcemask) { /* colorspace resource lookup is only done for inline images */ if (fz_is_name(obj)) { res = fz_dict_get(fz_dict_gets(rdb, "ColorSpace"), obj); if (res) obj = res; } error = pdf_load_colorspace(&colorspace, xref, obj); if (error) return fz_rethrow(error, "cannot load image colorspace"); if (!strcmp(colorspace->name, "Indexed")) indexed = 1; n = colorspace->n; } else { n = 1; } obj = fz_dict_getsa(dict, "Decode", "D"); if (obj) { for (i = 0; i < n * 2; i++) decode[i] = fz_to_real(fz_array_get(obj, i)); } else { float maxval = indexed ? (1 << bpc) - 1 : 1; for (i = 0; i < n * 2; i++) decode[i] = i & 1 ? maxval : 0; } obj = fz_dict_getsa(dict, "SMask", "Mask"); if (fz_is_dict(obj)) { /* Not allowed for inline images */ if (!cstm) { error = pdf_load_image_imp(&mask, xref, rdb, obj, NULL, 1); if (error) { if (colorspace) fz_drop_colorspace(colorspace); return fz_rethrow(error, "cannot load image mask/softmask"); } } } else if (fz_is_array(obj)) { usecolorkey = 1; for (i = 0; i < n * 2; i++) colorkey[i] = fz_to_int(fz_array_get(obj, i)); } /* Allocate now, to fail early if we run out of memory */ tile = fz_new_pixmap_with_limit(colorspace, w, h); if (!tile) { if (colorspace) fz_drop_colorspace(colorspace); if (mask) fz_drop_pixmap(mask); return fz_throw("out of memory"); } if (colorspace) fz_drop_colorspace(colorspace); tile->mask = mask; tile->interpolate = interpolate; stride = (w * n * bpc + 7) / 8; if (cstm) { stm = pdf_open_inline_stream(cstm, xref, dict, stride * h); } else { error = pdf_open_stream(&stm, xref, fz_to_num(dict), fz_to_gen(dict)); if (error) { fz_drop_pixmap(tile); return fz_rethrow(error, "cannot open image data stream (%d 0 R)", fz_to_num(dict)); } } samples = fz_calloc(h, stride); len = fz_read(stm, samples, h * stride); if (len < 0) { fz_close(stm); fz_free(samples); fz_drop_pixmap(tile); return fz_rethrow(len, "cannot read image data"); } /* Make sure we read the EOF marker (for inline images only) */ if (cstm) { unsigned char tbuf[512]; int tlen = fz_read(stm, tbuf, sizeof tbuf); if (tlen < 0) fz_catch(tlen, "ignoring error at end of image"); if (tlen > 0) fz_warn("ignoring garbage at end of image"); } fz_close(stm); /* Pad truncated images */ if (len < stride * h) { fz_warn("padding truncated image (%d 0 R)", fz_to_num(dict)); memset(samples + len, 0, stride * h - len); } /* Invert 1-bit image masks */ if (imagemask) { /* 0=opaque and 1=transparent so we need to invert */ unsigned char *p = samples; len = h * stride; for (i = 0; i < len; i++) p[i] = ~p[i]; } fz_unpack_tile(tile, samples, n, bpc, stride, indexed); fz_free(samples); if (usecolorkey) pdf_mask_color_key(tile, n, colorkey); if (indexed) { fz_pixmap *conv; fz_decode_indexed_tile(tile, decode, (1 << bpc) - 1); conv = pdf_expand_indexed_pixmap(tile); fz_drop_pixmap(tile); tile = conv; } else { fz_decode_tile(tile, decode); } *imgp = tile; return fz_okay; }
static pdf_annot * pdf_update_tx_widget_annot(pdf_xref *xref, fz_obj *obj) { fz_obj *ap, *res, *value; fz_rect rect; fz_buffer *content, *base_ap; int flags, align, rotate, is_multiline; float font_size, x, y; char *font_name; unsigned short *ucs2, *rest; fz_context *ctx = xref->ctx; if (strcmp(fz_to_name(ctx, fz_dict_gets(ctx, obj, "Subtype")), "Widget") != 0) return NULL; if (!fz_to_bool(ctx, pdf_dict_get_inheritable(xref, NULL, "NeedAppearances")) && pdf_get_ap_stream(xref, obj)) return NULL; value = pdf_dict_get_inheritable(xref, obj, "FT"); if (strcmp(fz_to_name(ctx, value), "Tx") != 0) return NULL; ap = pdf_dict_get_inheritable(xref, obj, "DA"); value = pdf_dict_get_inheritable(xref, obj, "V"); if (!ap || !value) return NULL; res = pdf_dict_get_inheritable(xref, obj, "DR"); rect = pdf_to_rect(ctx, fz_dict_gets(ctx, obj, "Rect")); rotate = fz_to_int(ctx, fz_dict_gets(ctx, fz_dict_gets(ctx, obj, "MK"), "R")); rect = fz_transform_rect(fz_rotate(rotate), rect); flags = fz_to_int(ctx, fz_dict_gets(ctx, obj, "Ff")); is_multiline = (flags & (1 << 12)) != 0; if ((flags & (1 << 25) /* richtext */)) fz_warn(ctx, "missing support for richtext fields"); align = fz_to_int(ctx, fz_dict_gets(ctx, obj, "Q")); font_size = pdf_extract_font_size(xref, fz_to_str_buf(ctx, ap), &font_name); if (!font_size || !font_name) font_size = is_multiline ? 10 /* FIXME */ : floor(rect.y1 - rect.y0 - 2); content = fz_new_buffer(ctx, 256); base_ap = fz_new_buffer(ctx, 256); pdf_prepend_ap_background(content, xref, obj); fz_buffer_printf(ctx, content, "/Tx BMC q 1 1 %.4f %.4f re W n BT %s ", rect.x1 - rect.x0 - 2.0f, rect.y1 - rect.y0 - 2.0f, fz_to_str_buf(ctx, ap)); fz_buffer_printf(ctx, base_ap, "/Tx BMC q BT %s ", fz_to_str_buf(ctx, ap)); if (font_name) { fz_buffer_printf(ctx, content, "/%s %.4f Tf ", font_name, font_size); fz_buffer_printf(ctx, base_ap, "/%s %.4f Tf ", font_name, font_size); fz_free(ctx, font_name); } y = 0.5f * (rect.y1 - rect.y0) + 0.6f * font_size; if (is_multiline) y = rect.y1 - rect.y0 - 2; fz_buffer_printf(ctx, content, "1 0 0 1 2 %.4f Tm ", y); ucs2 = pdf_to_ucs2(ctx, value); for (rest = ucs2; *rest; rest++) if (*rest > 0xFF) *rest = '?'; if ((flags & (1 << 13) /* password */)) for (rest = ucs2; *rest; rest++) *rest = '*'; x = 0; rest = ucs2; if ((flags & (1 << 24) /* comb */)) { pdf_append_combed_line(xref, res, content, base_ap, ucs2, font_size, rect.x1 - rect.x0, fz_to_int(ctx, pdf_dict_get_inheritable(xref, obj, "MaxLen"))); rest = L""; } while (*rest) rest = pdf_append_line(xref, res, content, base_ap, rest, font_size, align, rect.x1 - rect.x0 - 4.0f, is_multiline, &x); fz_free(ctx, ucs2); fz_buffer_printf(ctx, content, "ET Q EMC"); fz_drop_buffer(ctx, base_ap); rect = fz_transform_rect(fz_rotate(-rotate), rect); return pdf_create_annot(ctx, rect, fz_keep_obj(obj), content, res ? fz_keep_obj(res) : NULL, 0); }