Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
7.6 KiB

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