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