From 15b9cdd84caf784abeb911ca7aaf1d317d201e88 Mon Sep 17 00:00:00 2001 From: Paul Gardiner Date: Mon, 22 Jul 2013 11:09:22 +0100 Subject: Collect together all code to do with appearance-stream creation --- source/pdf/pdf-annot.c | 309 ------------------------------------------------- 1 file changed, 309 deletions(-) (limited to 'source/pdf/pdf-annot.c') diff --git a/source/pdf/pdf-annot.c b/source/pdf/pdf-annot.c index 4c76fce9..991478f6 100644 --- a/source/pdf/pdf-annot.c +++ b/source/pdf/pdf-annot.c @@ -1,6 +1,5 @@ #include "mupdf/pdf.h" -#define SMALL_FLOAT (0.00001) static pdf_obj * resolve_dest_rec(pdf_document *doc, pdf_obj *dest, int depth) @@ -841,63 +840,6 @@ pdf_delete_annot(pdf_document *doc, pdf_page *page, pdf_annot *annot) doc->dirty = 1; } -static fz_colorspace *pdf_to_color(pdf_document *doc, pdf_obj *col, float color[4]) -{ - fz_colorspace *cs; - int i, ncol = pdf_array_len(col); - - switch (ncol) - { - case 1: cs = fz_device_gray(doc->ctx); break; - case 3: cs = fz_device_rgb(doc->ctx); break; - case 4: cs = fz_device_cmyk(doc->ctx); break; - default: return NULL; - } - - for (i = 0; i < ncol; i++) - color[i] = pdf_to_real(pdf_array_get(col, i)); - - return cs; -} - -static fz_point * -quadpoints(pdf_document *doc, pdf_obj *annot, int *nout) -{ - fz_context *ctx = doc->ctx; - pdf_obj *quad = pdf_dict_gets(annot, "QuadPoints"); - fz_point *qp = NULL; - int i, n; - - if (!quad) - return NULL; - - n = pdf_array_len(quad); - - if (n%8 != 0) - return NULL; - - fz_var(qp); - fz_try(ctx) - { - qp = fz_malloc_array(ctx, n/2, sizeof(fz_point)); - - for (i = 0; i < n; i += 2) - { - qp[i/2].x = pdf_to_real(pdf_array_get(quad, i)); - qp[i/2].y = pdf_to_real(pdf_array_get(quad, i+1)); - } - } - fz_catch(ctx) - { - fz_free(ctx, qp); - fz_rethrow(ctx); - } - - *nout = n/2; - - return qp; -} - void pdf_set_markup_annot_quadpoints(pdf_document *doc, pdf_annot *annot, fz_point *qp, int n) { @@ -985,254 +927,3 @@ pdf_set_ink_annot_list(pdf_document *doc, pdf_annot *annot, fz_point *pts, int * for (i = 0; i < 3; i++) pdf_array_push_drop(col, pdf_new_real(doc, color[i])); } - -void -pdf_set_annot_appearance(pdf_document *doc, pdf_annot *annot, fz_rect *rect, fz_display_list *disp_list) -{ - fz_context *ctx = doc->ctx; - pdf_obj *obj = annot->obj; - const fz_matrix *page_ctm = &annot->page->ctm; - fz_matrix ctm; - fz_matrix mat = fz_identity; - fz_device *dev = NULL; - pdf_xobject *xobj = NULL; - - fz_invert_matrix(&ctm, page_ctm); - - fz_var(dev); - fz_try(ctx) - { - pdf_obj *ap_obj; - fz_rect trect = *rect; - - fz_transform_rect(&trect, &ctm); - - pdf_dict_puts_drop(obj, "Rect", pdf_new_rect(doc, &trect)); - - /* See if there is a current normal appearance */ - ap_obj = pdf_dict_getp(obj, "AP/N"); - if (!pdf_is_stream(doc, pdf_to_num(ap_obj), pdf_to_gen(ap_obj))) - ap_obj = NULL; - - if (ap_obj == NULL) - { - ap_obj = pdf_new_xobject(doc, &trect, &mat); - pdf_dict_putp_drop(obj, "AP/N", ap_obj); - } - else - { - pdf_xref_ensure_incremental_object(doc, pdf_to_num(ap_obj)); - pdf_dict_puts_drop(ap_obj, "Rect", pdf_new_rect(doc, &trect)); - pdf_dict_puts_drop(ap_obj, "Matrix", pdf_new_matrix(doc, &mat)); - } - - dev = pdf_new_pdf_device(doc, ap_obj, pdf_dict_gets(ap_obj, "Resources"), &mat); - fz_run_display_list(disp_list, dev, &ctm, &fz_infinite_rect, NULL); - fz_free_device(dev); - - /* Mark the appearance as changed - required for partial update */ - xobj = pdf_load_xobject(doc, ap_obj); - if (xobj) - { - xobj->iteration++; - pdf_drop_xobject(ctx, xobj); - } - - doc->dirty = 1; - - update_rect(ctx, annot); - } - fz_catch(ctx) - { - fz_free_device(dev); - fz_rethrow(ctx); - } -} - -void -pdf_set_markup_appearance(pdf_document *doc, pdf_annot *annot, float color[3], float alpha, float line_thickness, float line_height) -{ - fz_context *ctx = doc->ctx; - const fz_matrix *page_ctm = &annot->page->ctm; - fz_path *path = NULL; - fz_stroke_state *stroke = NULL; - fz_device *dev = NULL; - fz_display_list *strike_list = NULL; - int i, n; - fz_point *qp = quadpoints(doc, annot->obj, &n); - - if (!qp || n <= 0) - return; - - fz_var(path); - fz_var(stroke); - fz_var(dev); - fz_var(strike_list); - fz_try(ctx) - { - fz_rect rect = fz_empty_rect; - - rect.x0 = rect.x1 = qp[0].x; - rect.y0 = rect.y1 = qp[0].y; - for (i = 0; i < n; i++) - fz_include_point_in_rect(&rect, &qp[i]); - - strike_list = fz_new_display_list(ctx); - dev = fz_new_list_device(ctx, strike_list); - - for (i = 0; i < n; i += 4) - { - fz_point pt0 = qp[i]; - fz_point pt1 = qp[i+1]; - fz_point up; - float thickness; - - up.x = qp[i+2].x - qp[i+1].x; - up.y = qp[i+2].y - qp[i+1].y; - - pt0.x += line_height * up.x; - pt0.y += line_height * up.y; - pt1.x += line_height * up.x; - pt1.y += line_height * up.y; - - thickness = sqrtf(up.x * up.x + up.y * up.y) * line_thickness; - - if (!stroke || fz_abs(stroke->linewidth - thickness) < SMALL_FLOAT) - { - if (stroke) - { - // assert(path) - fz_stroke_path(dev, path, stroke, page_ctm, fz_device_rgb(ctx), color, alpha); - fz_drop_stroke_state(ctx, stroke); - stroke = NULL; - fz_free_path(ctx, path); - path = NULL; - } - - stroke = fz_new_stroke_state(ctx); - stroke->linewidth = thickness; - path = fz_new_path(ctx); - } - - fz_moveto(ctx, path, pt0.x, pt0.y); - fz_lineto(ctx, path, pt1.x, pt1.y); - } - - if (stroke) - { - fz_stroke_path(dev, path, stroke, page_ctm, fz_device_rgb(ctx), color, alpha); - } - - fz_transform_rect(&rect, page_ctm); - pdf_set_annot_appearance(doc, annot, &rect, strike_list); - } - fz_always(ctx) - { - fz_free(ctx, qp); - fz_free_device(dev); - fz_drop_stroke_state(ctx, stroke); - fz_free_path(ctx, path); - fz_drop_display_list(ctx, strike_list); - } - fz_catch(ctx) - { - fz_rethrow(ctx); - } -} - -void -pdf_set_ink_appearance(pdf_document *doc, pdf_annot *annot) -{ - fz_context *ctx = doc->ctx; - const fz_matrix *page_ctm = &annot->page->ctm; - fz_path *path = NULL; - fz_stroke_state *stroke = NULL; - fz_device *dev = NULL; - fz_display_list *strike_list = NULL; - - fz_var(path); - fz_var(stroke); - fz_var(dev); - fz_var(strike_list); - fz_try(ctx) - { - fz_rect rect = fz_empty_rect; - fz_colorspace *cs; - float color[4]; - float width; - pdf_obj *list; - int n, m, i, j; - - cs = pdf_to_color(doc, pdf_dict_gets(annot->obj, "C"), color); - if (!cs) - { - cs = fz_device_rgb(ctx); - color[0] = 1.0f; - color[1] = 0.0f; - color[2] = 0.0f; - } - - width = pdf_to_real(pdf_dict_gets(pdf_dict_gets(annot->obj, "BS"), "W")); - if (width == 0.0f) - width = 1.0f; - - list = pdf_dict_gets(annot->obj, "InkList"); - - n = pdf_array_len(list); - - strike_list = fz_new_display_list(ctx); - dev = fz_new_list_device(ctx, strike_list); - path = fz_new_path(ctx); - stroke = fz_new_stroke_state(ctx); - stroke->linewidth = width; - - for (i = 0; i < n; i ++) - { - fz_point pt_last; - pdf_obj *arc = pdf_array_get(list, i); - m = pdf_array_len(arc); - - for (j = 0; j < m-1; j += 2) - { - fz_point pt; - pt.x = pdf_to_real(pdf_array_get(arc, j)); - pt.y = pdf_to_real(pdf_array_get(arc, j+1)); - - if (i == 0 && j == 0) - { - rect.x0 = rect.x1 = pt.x; - rect.y0 = rect.y1 = pt.y; - } - else - { - fz_include_point_in_rect(&rect, &pt); - } - - if (j == 0) - fz_moveto(ctx, path, pt.x, pt.y); - else - fz_curvetov(ctx, path, pt_last.x, pt_last.y, (pt.x + pt_last.x) / 2, (pt.y + pt_last.y) / 2); - pt_last = pt; - } - fz_lineto(ctx, path, pt_last.x, pt_last.y); - } - - fz_stroke_path(dev, path, stroke, page_ctm, cs, color, 1.0f); - - fz_expand_rect(&rect, width); - - fz_transform_rect(&rect, page_ctm); - pdf_set_annot_appearance(doc, annot, &rect, strike_list); - } - fz_always(ctx) - { - fz_free_device(dev); - fz_drop_stroke_state(ctx, stroke); - fz_free_path(ctx, path); - fz_drop_display_list(ctx, strike_list); - } - fz_catch(ctx) - { - fz_rethrow(ctx); - } -} -- cgit v1.2.3