>From a9d9ed301da1d15d0f879d2193a19a27596e866b Mon Sep 17 00:00:00 2001 From: Raymond S Brand Date: Mon, 16 Nov 2020 11:36:33 -0500 Subject: [PATCH 2/2] Add defined constants for MIME types, pdf outlining, and tags --- Cairo.xs | 5 ++++ CairoSurface.xs | 33 ++++++++++++++++++++++++ examples/mime-unique-id.pl | 12 ++++----- examples/pdf-tagged-text.pl | 23 +++++++---------- lib/Cairo.pm | 50 +++++++++++++++++++++++++++++++++++-- t/CairoSurface.t | 15 +++++------ 6 files changed, 108 insertions(+), 30 deletions(-) diff --git a/Cairo.xs b/Cairo.xs index d3d4509..de46a60 100644 --- a/Cairo.xs +++ b/Cairo.xs @@ -904,6 +904,11 @@ void cairo_tag_begin (cairo_t * cr, const char_utf8 * tag_name, const char_utf8 void cairo_tag_end (cairo_t * cr, const char_utf8 * tag_name); +BOOT: + HV *stash = gv_stashpv("Cairo", 0); + newCONSTSUB (stash, "TAG_DEST", newSVpv (CAIRO_TAG_DEST, 0)); + newCONSTSUB (stash, "TAG_LINK", newSVpv (CAIRO_TAG_LINK, 0)); + #endif # --------------------------------------------------------------------------- # diff --git a/CairoSurface.xs b/CairoSurface.xs index 0d213e8..fadef73 100644 --- a/CairoSurface.xs +++ b/CairoSurface.xs @@ -409,12 +409,41 @@ cairo_surface_get_mime_data (cairo_surface_t *surface, const char *mime_type); OUTPUT: RETVAL +BOOT: + HV *stashsurface = gv_stashpv("Cairo::Surface", 0); + newCONSTSUB (stashsurface, "MIME_TYPE_JP2", newSVpv (CAIRO_MIME_TYPE_JP2, 0)); + newCONSTSUB (stashsurface, "MIME_TYPE_JPEG", newSVpv (CAIRO_MIME_TYPE_JPEG, 0)); + newCONSTSUB (stashsurface, "MIME_TYPE_PNG", newSVpv (CAIRO_MIME_TYPE_PNG, 0)); + newCONSTSUB (stashsurface, "MIME_TYPE_URI", newSVpv (CAIRO_MIME_TYPE_URI, 0)); + #endif #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) cairo_bool_t cairo_surface_supports_mime_type (cairo_surface_t *surface, const char *mime_type); +BOOT: + newCONSTSUB (stashsurface, "MIME_TYPE_UNIQUE_ID", newSVpv (CAIRO_MIME_TYPE_UNIQUE_ID, 0)); + +#endif + +#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 14, 0) + +BOOT: + newCONSTSUB (stashsurface, "MIME_TYPE_JBIG2", newSVpv (CAIRO_MIME_TYPE_JBIG2, 0)); + newCONSTSUB (stashsurface, "MIME_TYPE_JBIG2_GLOBAL", newSVpv (CAIRO_MIME_TYPE_JBIG2_GLOBAL, 0)); + newCONSTSUB (stashsurface, "MIME_TYPE_JBIG2_GLOBAL_ID", newSVpv (CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID, 0)); + +#endif + +#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0) + +BOOT: + newCONSTSUB (stashsurface, "MIME_TYPE_CCITT_FAX", newSVpv (CAIRO_MIME_TYPE_CCITT_FAX, 0)); + newCONSTSUB (stashsurface, "MIME_TYPE_CCITT_FAX_PARAMS", newSVpv (CAIRO_MIME_TYPE_CCITT_FAX_PARAMS, 0)); + newCONSTSUB (stashsurface, "MIME_TYPE_EPS", newSVpv (CAIRO_MIME_TYPE_EPS, 0)); + newCONSTSUB (stashsurface, "MIME_TYPE_EPS_PARAMS", newSVpv (CAIRO_MIME_TYPE_EPS_PARAMS, 0)); + #endif #ifdef CAIRO_HAS_PNG_FUNCTIONS @@ -627,6 +656,10 @@ cairo_pdf_surface_version_to_string (...) int cairo_pdf_surface_add_outline (cairo_surface_t *surface, int parent_id, const char *utf8, const char *link_attribs, cairo_pdf_outline_flags_t flags); +BOOT: + HV *stashpdfsurface = gv_stashpv("Cairo::PdfSurface", 0); + newCONSTSUB(stashpdfsurface, "OUTLINE_ROOT", newSViv(CAIRO_PDF_OUTLINE_ROOT)); + void cairo_pdf_surface_set_metadata (cairo_surface_t *surface, cairo_pdf_metadata_t metadata, const char_utf8 * utf8); void cairo_pdf_surface_set_page_label (cairo_surface_t *surface, const char *utf8); diff --git a/examples/mime-unique-id.pl b/examples/mime-unique-id.pl index 131ddbf..b236964 100755 --- a/examples/mime-unique-id.pl +++ b/examples/mime-unique-id.pl @@ -38,8 +38,6 @@ use constant JPG_FILENAME => 'romedalen.jpg', OUTPUT_FILENAME => 'mime-unique-id.perl.pdf', M_PI => 3.1415926, - CAIRO_MIME_TYPE_JPEG => 'image/jpeg', - CAIRO_MIME_TYPE_UNIQUE_ID => 'application/x-cairo.uuid', }; @@ -53,9 +51,9 @@ sub create_image_surface die; } - $surface->set_mime_data(CAIRO_MIME_TYPE_UNIQUE_ID, PNG_FILENAME); + $surface->set_mime_data($surface->MIME_TYPE_UNIQUE_ID, PNG_FILENAME); - $surface->set_mime_data(CAIRO_MIME_TYPE_UNIQUE_ID, 'image'); + $surface->set_mime_data($surface->MIME_TYPE_UNIQUE_ID, 'image'); return $surface; } @@ -87,14 +85,14 @@ sub create_recording_surface_with_mime_jpg } close($FH); - $surface->set_mime_data(CAIRO_MIME_TYPE_JPEG, $data); + $surface->set_mime_data($surface->MIME_TYPE_JPEG, $data); if ($surface->status() ne 'success') { say $surface->status(); die; } - $surface->set_mime_data(CAIRO_MIME_TYPE_UNIQUE_ID, 'jpeg'); + $surface->set_mime_data($surface->MIME_TYPE_UNIQUE_ID, 'jpeg'); if ($surface->status() ne 'success') { say $surface->status(); @@ -172,7 +170,7 @@ sub create_recording_surface } $cr = undef; - $surface->set_mime_data(CAIRO_MIME_TYPE_UNIQUE_ID, $bounded ? 'recording bounded' : 'recording unbounded'); + $surface->set_mime_data($surface->MIME_TYPE_UNIQUE_ID, $bounded ? 'recording bounded' : 'recording unbounded'); if ($surface->status() ne 'success') { say $surface->status(); diff --git a/examples/pdf-tagged-text.pl b/examples/pdf-tagged-text.pl index 5bf64a1..67162df 100755 --- a/examples/pdf-tagged-text.pl +++ b/examples/pdf-tagged-text.pl @@ -33,11 +33,6 @@ use constant TEXT_SIZE => 12, HEADING_HEIGHT => 50, MARGIN => 50, - - CAIRO_TAG_DEST => 'cairo.dest', - CAIRO_TAG_LINK => 'Link', - - CAIRO_PDF_OUTLINE_ROOT => 0, }; @@ -186,9 +181,9 @@ sub draw_contents $cr->tag_begin('TOCI', ''); $cr->tag_begin('Reference', ''); - $cr->tag_begin(CAIRO_TAG_LINK, "dest='".$$section[1]."'"); + $cr->tag_begin(Cairo::TAG_LINK, "dest='".$$section[1]."'"); $cr->show_text($$section[1]); - $cr->tag_end(CAIRO_TAG_LINK); + $cr->tag_end(Cairo::TAG_LINK); $cr->tag_end('Reference'); $cr->tag_end('TOCI'); @@ -210,7 +205,7 @@ sub draw_section draw_page_num($cr, undef, $page_num++); $y_pos = MARGIN; - $parent = CAIRO_PDF_OUTLINE_ROOT; + $parent = $cr->get_target->OUTLINE_ROOT; } else { @@ -232,9 +227,9 @@ sub draw_section $cr->move_to(MARGIN, $y_pos); $cr->tag_begin($level_data[$$section[0]]->[1], ''); - $cr->tag_begin(CAIRO_TAG_DEST, "name='".$$section[1]."'"); + $cr->tag_begin(Cairo::TAG_DEST, "name='".$$section[1]."'"); $cr->show_text($$section[1]); - $cr->tag_end(CAIRO_TAG_DEST); + $cr->tag_end(Cairo::TAG_DEST); $cr->tag_end($level_data[$$section[0]]->[1]); $y_pos += HEADING_HEIGHT; @@ -291,16 +286,16 @@ sub create_document $cr->tag_begin("Document", ''); draw_cover($cr); - $surface->add_outline(CAIRO_PDF_OUTLINE_ROOT, 'Cover', 'page=1', ['bold']); + $surface->add_outline($surface->OUTLINE_ROOT, 'Cover', 'page=1', ['bold']); $cr->show_page(); $page_num = 0; draw_page_num($cr, $roman_numerals[$page_num++], 0); $y_pos = MARGIN; - $surface->add_outline(CAIRO_PDF_OUTLINE_ROOT, "Contents", "dest='TOC'", ['bold']); + $surface->add_outline($surface->OUTLINE_ROOT, "Contents", "dest='TOC'", ['bold']); - $cr->tag_begin(CAIRO_TAG_DEST, "name='TOC' internal"); + $cr->tag_begin(Cairo::TAG_DEST, "name='TOC' internal"); $cr->tag_begin("TOC", ''); foreach (@contents) @@ -309,7 +304,7 @@ sub create_document } $cr->tag_end("TOC"); - $cr->tag_end(CAIRO_TAG_DEST); + $cr->tag_end(Cairo::TAG_DEST); $page_num = 1; foreach (@contents) diff --git a/lib/Cairo.pm b/lib/Cairo.pm index 808ca31..eb88c53 100644 --- a/lib/Cairo.pm +++ b/lib/Cairo.pm @@ -358,6 +358,16 @@ C<$cr-Erestore> to restore to the saved state. =back +=item Predefined names: + +=over + +=item Cairo::TAG_DEST [1.16] + +=item Cairo::TAG_LINK [1.16] + +=back + =item $cr->copy_page =item $cr->show_page @@ -1379,6 +1389,40 @@ For hysterical reasons, you can also use the following syntax: =item $mime_type: string +=over + +=item Predefined MIME types: + +=over + +=item Cairo::Surface::MIME_TYPE_JP2 [1.10] + +=item Cairo::Surface::MIME_TYPE_JPEG [1.10] + +=item Cairo::Surface::MIME_TYPE_PNG [1.10] + +=item Cairo::Surface::MIME_TYPE_URI [1.10] + +=item Cairo::Surface::MIME_TYPE_UNIQUE_ID [1.12] + +=item Cairo::Surface::MIME_TYPE_JBIG2 [1.14] + +=item Cairo::Surface::MIME_TYPE_JBIG2_GLOBAL [1.14] + +=item Cairo::Surface::MIME_TYPE_JBIG2_GLOBAL_PARAMS [1.14] + +=item Cairo::Surface::MIME_TYPE_CCITT_FAX [1.16] + +=item Cairo::Surface::MIME_TYPE_CCITT_FAX_PARAMS [1.16] + +=item Cairo::Surface::MIME_TYPE_EPS [1.16] + +=item Cairo::Surface::MIME_TYPE_EPS_PARAMS [1.16] + +=back + +=back + =item $mime_data: binary data string =back @@ -1523,11 +1567,13 @@ For hysterical reasons, you can also use the following syntax: =back -=item $id = $surface->add_outline($parent_id, $name, $attributes, $flags) [1.16] +=item $item_id = $surface->add_outline($parent_id, $name, $attributes, $flags) [1.16] =over -=item $id: int, item ID +=item $item_id: int, item ID + +=item $parent_id: parent item id or Cairo::PdfSurface::OUTLINE_ROOT =item $name: string, item display diff --git a/t/CairoSurface.t b/t/CairoSurface.t index f3c43b3..8825d55 100644 --- a/t/CairoSurface.t +++ b/t/CairoSurface.t @@ -12,7 +12,7 @@ use warnings; use Config; # for byteorder -use Test::More tests => 99; +use Test::More tests => 100; use constant IMG_WIDTH => 256; use constant IMG_HEIGHT => 256; @@ -216,7 +216,7 @@ SKIP: { } SKIP: { - skip 'pdf surface', 17 + skip 'pdf surface', 18 unless Cairo::HAS_PDF_SURFACE; my $surf = Cairo::PdfSurface->create ('tmp.pdf', IMG_WIDTH, IMG_HEIGHT); @@ -269,16 +269,17 @@ SKIP: { } SKIP: { - skip 'new stuff', 3 + skip 'new stuff', 4 unless Cairo::VERSION >= Cairo::VERSION_ENCODE (1, 10, 0); my $mime_data = 'mime data for {set,get}_mime_data'; - is ($surf->set_mime_data('image/jpeg', $mime_data), 'success'); + is ($surf->MIME_TYPE_JPEG, 'image/jpeg'); + is ($surf->set_mime_data($surf->MIME_TYPE_JPEG, $mime_data), 'success'); my $recovered_mime_data = $surf->get_mime_data('unset mime type'); is ($recovered_mime_data, undef); - $recovered_mime_data = $surf->get_mime_data('image/jpeg'); + $recovered_mime_data = $surf->get_mime_data($surf->MIME_TYPE_JPEG); is ($recovered_mime_data, $mime_data); } @@ -286,7 +287,7 @@ SKIP: { skip 'new stuff', 2 unless Cairo::VERSION >= Cairo::VERSION_ENCODE (1, 12, 0); - is ($surf->supports_mime_type('image/jpeg'), 1); + is ($surf->supports_mime_type(Cairo::Surface::MIME_TYPE_JPEG), 1); is ($surf->supports_mime_type('unsupported mime type'), 0); } @@ -311,7 +312,7 @@ SKIP: { $surf->set_thumbnail_size(20, 20); is ($surf->status(), 'success'); - my $parent = $surf->add_outline(0, 'Cover', "dest='page=1'", ['bold']); + my $parent = $surf->add_outline($surf->OUTLINE_ROOT(), 'Cover', "dest='page=1'", ['bold']); $parent = $surf->add_outline($parent, 'Chapter 1', 'page=2', ['bold', 'open']); $parent = $surf->add_outline($parent, 'Section 1', 'page=2', ['open']); $parent = $surf->add_outline($parent, 'Section 1.1', 'page=2', ['italic']); -- 2.20.1