Graphviz 13.0.0~dev.20250210.0415
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 v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.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(GVJ_t * job, usershape_t *us)
34{
35 PopplerDocument *document = NULL;
36 int num_pages;
37
38 assert(job);
39 assert(us);
40 assert(us->name);
41
42 if (us->data) {
43 if (us->datafree == gvloadimage_poppler_free)
44 document = us->data; /* use cached data */
45 else {
46 us->datafree(us); /* free incompatible cache data */
47 us->data = NULL;
48 us->datafree = NULL;
49 }
50
51 }
52
53 if (!document) { /* read file into cache */
55 return NULL;
56 switch (us->type) {
57 case FT_PDF: {
58
59 char *absolute;
60 if (g_path_is_absolute(us->name)) {
61 absolute = g_strdup (us->name);
62 } else {
63 char *dir = g_get_current_dir();
64 absolute = g_build_filename(dir, us->name, NULL);
65 free (dir);
66 }
67
68 GError *error = NULL;
69 char *uri = g_filename_to_uri(absolute, NULL, &error);
70
71 g_free(absolute);
72 if (uri == NULL) {
73 printf("%s\n", error->message);
74 g_error_free(error);
75 return NULL;
76 }
77
78 document = poppler_document_new_from_file (uri, NULL, &error);
79 g_free(uri);
80 if (document == NULL) {
81 printf("%s\n", error->message);
82 g_error_free(error);
83 return NULL;
84 }
85
86 // check page 1 exists
87
88 num_pages = poppler_document_get_n_pages (document);
89 if (num_pages < 1) {
90 printf("poppler fail: num_pages %d, must be at least 1", num_pages);
91 return NULL;
92 }
93 break;
94 }
95
96 default:
97 break;
98 }
99
100 if (document) {
101 us->data = document;
102 us->datafree = gvloadimage_poppler_free;
103 }
104
106 }
107
108 return document;
109}
110
111static void gvloadimage_poppler_cairo(GVJ_t * job, usershape_t *us, boxf b, bool filled)
112{
113 (void)filled;
114
115 PopplerDocument* document = gvloadimage_poppler_load(job, us);
116 PopplerPage* page;
117
118 cairo_t *cr = job->context; /* target context */
119 cairo_surface_t *surface; /* source surface */
120
121 if (document) {
122
123 // already done this once, so no err checking
124 page = poppler_document_get_page (document, 0);
125
126 cairo_save(cr);
127
128 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, (int)us->w, (int)us->h);
129 cairo_surface_reference(surface);
130
131 cairo_set_source_surface(cr, surface, 0, 0);
132 cairo_translate(cr, b.LL.x, -b.UR.y);
133 cairo_scale(cr, (b.UR.x - b.LL.x)/(us->w), (b.UR.y - b.LL.y)/(us->h));
134 poppler_page_render (page, cr);
135 cairo_paint (cr);
136
137 cairo_restore(cr);
138 }
139}
140
142 gvloadimage_poppler_cairo
143};
144#endif
145#endif
146
148#ifdef HAVE_PANGOCAIRO
149#ifdef HAVE_POPPLER
150 {FORMAT_PDF_CAIRO, "pdf:cairo", 1, &engine_cairo, NULL},
151#endif
152#endif
153 {0, NULL, 0, NULL, NULL}
154};
void free(void *)
node NULL
Definition grammar.y:163
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:294
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