Graphviz 16.1.1~dev.20260919.0612
Loading...
Searching...
No Matches
gvloadimage_poppler.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <stdbool.h>
14#include <stdlib.h>
15#include <sys/stat.h>
16
18
19#ifdef HAVE_PANGOCAIRO
20#ifdef HAVE_POPPLER
21#include <poppler.h>
22#include <cairo.h>
23
24enum {
25 FORMAT_PDF_CAIRO,
26};
27
28static void gvloadimage_poppler_free(usershape_t *us)
29{
30 g_object_unref(us->data);
31}
32
33static PopplerDocument *gvloadimage_poppler_load(usershape_t *us) {
34 PopplerDocument *document = NULL;
35 int num_pages;
36
37 assert(us);
38 assert(us->name);
39
40 if (us->data) {
41 if (us->datafree == gvloadimage_poppler_free)
42 document = us->data; /* use cached data */
43 else {
44 us->datafree(us); /* free incompatible cache data */
45 us->data = NULL;
46 us->datafree = NULL;
47 }
48
49 }
50
51 if (!document) { /* read file into cache */
53 return NULL;
54 switch (us->type) {
55 case FT_PDF: {
56
57 char *absolute;
58 if (g_path_is_absolute(us->name)) {
59 absolute = g_strdup (us->name);
60 } else {
61 char *dir = g_get_current_dir();
62 absolute = g_build_filename(dir, us->name, NULL);
63 free (dir);
64 }
65
66 GError *error = NULL;
67 char *uri = g_filename_to_uri(absolute, NULL, &error);
68
69 g_free(absolute);
70 if (uri == NULL) {
71 printf("%s\n", error->message);
72 g_error_free(error);
73 return NULL;
74 }
75
76 document = poppler_document_new_from_file (uri, NULL, &error);
77 g_free(uri);
78 if (document == NULL) {
79 printf("%s\n", error->message);
80 g_error_free(error);
81 return NULL;
82 }
83
84 // check page 1 exists
85
86 num_pages = poppler_document_get_n_pages (document);
87 if (num_pages < 1) {
88 printf("poppler fail: num_pages %d, must be at least 1", num_pages);
89 return NULL;
90 }
91 break;
92 }
93
94 default:
95 break;
96 }
97
98 if (document) {
99 us->data = document;
100 us->datafree = gvloadimage_poppler_free;
101 }
102
104 }
105
106 return document;
107}
108
109static void gvloadimage_poppler_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
110{
111 (void)filled;
112
113 PopplerDocument *document = gvloadimage_poppler_load(us);
114 PopplerPage* page;
115
116 cairo_t *cr = job->context; /* target context */
117 cairo_surface_t *surface; /* source surface */
118
119 if (document) {
120
121 // already done this once, so no err checking
122 page = poppler_document_get_page (document, 0);
123
124 cairo_save(cr);
125
126 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, (int)us->w, (int)us->h);
127 cairo_surface_reference(surface);
128
129 cairo_set_source_surface(cr, surface, 0, 0);
130 cairo_translate(cr, b.LL.x, -b.UR.y);
131 cairo_scale(cr, (b.UR.x - b.LL.x)/(us->w), (b.UR.y - b.LL.y)/(us->h));
132 poppler_page_render (page, cr);
133 cairo_paint (cr);
134
135 cairo_restore(cr);
136 }
137}
138
140 gvloadimage_poppler_cairo
141};
142#endif
143#endif
144
146#ifdef HAVE_PANGOCAIRO
147#ifdef HAVE_POPPLER
148 {FORMAT_PDF_CAIRO, "pdf:cairo", 1, &engine_cairo, NULL},
149#endif
150#endif
151 {0, NULL, 0, NULL, NULL}
152};
void free(void *)
node NULL
Definition grammar.y:181
void gvusershape_file_release(usershape_t *us)
bool gvusershape_file_access(usershape_t *us)
static gvloadimage_engine_t engine_cairo
gvplugin_installed_t gvloadimage_poppler_types[]
table Syntax error
Definition htmlparse.y:288
void * context
Definition gvcjob.h:295
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
ingroup plugin_api
Definition gvplugin.h:35
double x
Definition geom.h:29
double y
Definition geom.h:29
const char * name
Definition usershape.h:54
void(* datafree)(usershape_t *us)
Definition usershape.h:65
void * data
Definition usershape.h:63
imagetype_t type
Definition usershape.h:59
double h
Definition usershape.h:61
double w
Definition usershape.h:61
@ FT_PDF
Definition usershape.h:26