epdfinfo.h (7525B)
1 // Copyright (C) 2013, 2014 Andreas Politz 2 3 // Author: Andreas Politz <politza@fh-trier.de> 4 5 // This program is free software; you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 #ifndef _EPDF_H_ 19 #define _EPDF_H_ _EPDF_H_ 20 #include "config.h" 21 #include <glib.h> 22 #include <poppler.h> 23 #include <png.h> 24 25 /* Some library functions print warnings to stdout, inhibit it. */ 26 #define DISCARD_STDOUT(saved_fd) \ 27 do { \ 28 int __fd; \ 29 fflush(stdout); \ 30 saved_fd = dup(1); \ 31 __fd = open("/dev/null", O_WRONLY); \ 32 dup2(__fd, 1); \ 33 close(__fd); \ 34 } while (0) 35 36 #define UNDISCARD_STDOUT(saved_fd) \ 37 do { \ 38 fflush(stdout); \ 39 dup2(saved_fd, 1); \ 40 close(saved_fd); \ 41 } while (0) 42 43 /* Writing responses */ 44 #define OK_BEGIN() \ 45 do { \ 46 puts("OK"); \ 47 } while (0) 48 49 #define OK_END() \ 50 do { \ 51 puts("."); \ 52 fflush (stdout); \ 53 } while (0) 54 55 #define OK() \ 56 do { \ 57 puts ("OK\n."); \ 58 fflush (stdout); \ 59 } while (0) 60 61 /* Dealing with image data. */ 62 #ifdef WORDS_BIGENDIAN 63 #define ARGB_TO_RGB(rgb, argb) \ 64 do { \ 65 rgb[0] = argb[1]; \ 66 rgb[1] = argb[2]; \ 67 rgb[2] = argb[3]; \ 68 } while (0) 69 70 #define ARGB_EQUAL(argb1, argb2) \ 71 (argb1[1] == argb2[1] \ 72 && argb1[2] == argb2[2] \ 73 && argb1[3] == argb2[3]) 74 75 #else 76 #define ARGB_TO_RGB(rgb, argb) \ 77 do { \ 78 rgb[0] = argb[2]; \ 79 rgb[1] = argb[1]; \ 80 rgb[2] = argb[0]; \ 81 } while (0) 82 83 #define ARGB_EQUAL(argb1, argb2) \ 84 (argb1[0] == argb2[0] \ 85 && argb1[1] == argb2[1] \ 86 && argb1[2] == argb2[2]) 87 #endif 88 89 #define NORMALIZE_PAGE_ARG(doc, first, last) \ 90 *first = MAX(1, *first); \ 91 if (*last <= 0) \ 92 *last = poppler_document_get_n_pages (doc); \ 93 else \ 94 *last = MIN(*last, poppler_document_get_n_pages (doc)); 95 96 /* png_jmpbuf is supposed to be not available in older versions of 97 libpng. */ 98 #ifndef png_jmpbuf 99 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) 100 #endif 101 102 #ifndef HAVE_ERROR_H 103 # define error(status, errno, fmt, args...) \ 104 do { \ 105 int error = (errno); \ 106 fflush (stdout); \ 107 fprintf (stderr, "%s: " fmt, PACKAGE_NAME, ## args); \ 108 if (error) \ 109 fprintf (stderr, ": %s", strerror (error)); \ 110 fprintf (stderr, "\n"); \ 111 exit (status); \ 112 } while (0) 113 #endif 114 115 #define internal_error(fmt, args...) \ 116 error (2, 0, "internal error in %s: " fmt, __func__, ## args) 117 118 #define error_if_not(expr) \ 119 if (! (expr)) goto error; 120 121 #define perror_if_not(expr, fmt, args...) \ 122 do { \ 123 if (! (expr)) \ 124 { \ 125 printf_error_response ((fmt), ## args); \ 126 goto error; \ 127 } \ 128 } while (0) 129 130 #define cerror_if_not(expr, error_msg, fmt, args...) \ 131 do { \ 132 if (! (expr)) \ 133 { \ 134 if (error_msg) \ 135 *(error_msg) = g_strdup_printf((fmt), ## args); \ 136 goto error; \ 137 } \ 138 } while (0) 139 140 /* Declare commands */ 141 #define DEC_CMD(name) \ 142 {#name, cmd_ ## name, cmd_ ## name ## _spec, \ 143 G_N_ELEMENTS (cmd_ ## name ## _spec)} 144 145 #define DEC_CMD2(command, name) \ 146 {name, cmd_ ## command, cmd_ ## command ## _spec, \ 147 G_N_ELEMENTS (cmd_ ## command ## _spec)} 148 149 /* Declare option */ 150 #define DEC_DOPT(name, type, sname) \ 151 {name, type, offsetof (document_options_t, sname)} 152 153 enum suffix_char { NONE, COLON, NEWLINE}; 154 155 enum image_type { PPM, PNG }; 156 157 typedef struct 158 { 159 PopplerAnnotMapping *amap; 160 gchar *key; 161 } annotation_t; 162 163 typedef enum 164 { 165 ARG_INVALID = 0, 166 ARG_DOC, 167 ARG_BOOL, 168 ARG_STRING, 169 ARG_NONEMPTY_STRING, 170 ARG_NATNUM, 171 ARG_EDGE, 172 ARG_EDGE_OR_NEGATIVE, 173 ARG_EDGES, 174 ARG_EDGES_OR_POSITION, 175 ARG_COLOR, 176 ARG_REST 177 } command_arg_type_t; 178 179 typedef struct 180 { 181 const char *name; 182 command_arg_type_t type; 183 size_t offset; 184 } document_option_t; 185 186 typedef struct 187 { 188 PopplerColor bg, fg; 189 gboolean usecolors; 190 gboolean printed; 191 } render_options_t; 192 193 typedef struct 194 { 195 render_options_t render; 196 } document_options_t; 197 198 typedef struct 199 { 200 PopplerDocument *pdf; 201 char *filename; 202 char *passwd; 203 struct 204 { 205 GHashTable *keys; /* key => page */ 206 GList **pages; /* page array */ 207 } annotations; 208 document_options_t options; 209 } document_t; 210 211 typedef struct 212 { 213 command_arg_type_t type; 214 union 215 { 216 gboolean flag; 217 const char *string; 218 long natnum; 219 document_t *doc; 220 gdouble edge; 221 PopplerColor color; 222 PopplerRectangle rectangle; 223 #ifdef HAVE_POPPLER_ANNOT_MARKUP 224 PopplerQuadrilateral quadrilateral; 225 #endif 226 struct 227 { 228 char * const *args; 229 int nargs; 230 } rest; 231 } value; 232 } command_arg_t; 233 234 typedef struct 235 { 236 GHashTable *documents; 237 } epdfinfo_t; 238 239 typedef struct 240 { 241 const char *name; 242 void (* execute) (const epdfinfo_t *ctxt, const command_arg_t *args); 243 const command_arg_type_t *args_spec; 244 int nargs; 245 } command_t; 246 #endif /* _EPDF_H_ */