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.

122 lines
3.5 KiB

5 years ago
  1. // Copyright (C) 2013, 2014 Andreas Politz
  2. // This program is free software; you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, either version 3 of the License, or
  5. // (at your option) any later version.
  6. // This program is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. // GNU General Public License for more details.
  10. // You should have received a copy of the GNU General Public License
  11. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. #include <config.h>
  13. #include <PDFDocEncoding.h>
  14. #include <Annot.h>
  15. #include <glib.h>
  16. #include <glib-object.h>
  17. #include <poppler-features.h>
  18. extern "C"
  19. {
  20. GType poppler_annot_get_type (void) G_GNUC_CONST;
  21. GType poppler_annot_markup_get_type (void) G_GNUC_CONST;
  22. #define POPPLER_TYPE_ANNOT (poppler_annot_get_type ())
  23. #define POPPLER_ANNOT(obj) \
  24. (G_TYPE_CHECK_INSTANCE_CAST ((obj), POPPLER_TYPE_ANNOT, PopplerAnnot))
  25. #define POPPLER_IS_ANNOT_MARKUP(obj) \
  26. (G_TYPE_CHECK_INSTANCE_TYPE ((obj), POPPLER_TYPE_ANNOT_MARKUP))
  27. #define POPPLER_TYPE_ANNOT_MARKUP (poppler_annot_markup_get_type ())
  28. #if POPPLER_CHECK_VERSION(0,72,0)
  29. #define GET_CSTR c_str
  30. #else
  31. #define GET_CSTR getCString
  32. #endif
  33. struct PopplerAnnot
  34. {
  35. GObject parent_instance;
  36. Annot *annot;
  37. };
  38. struct PopplerAnnotMarkup
  39. {
  40. GObject parent_instance;
  41. };
  42. struct PopplerRectangle
  43. {
  44. double x1;
  45. double y1;
  46. double x2;
  47. double y2;
  48. };
  49. // This function does not modify its argument s, but for
  50. // compatibility reasons (e.g. getLength in GooString.h before 2015)
  51. // with older poppler code, it can't be declared as such.
  52. char *_xpoppler_goo_string_to_utf8(/* const */ GooString *s)
  53. {
  54. char *result;
  55. if (! s)
  56. return NULL;
  57. if (s->hasUnicodeMarker()) {
  58. result = g_convert (s->GET_CSTR () + 2,
  59. s->getLength () - 2,
  60. "UTF-8", "UTF-16BE", NULL, NULL, NULL);
  61. } else {
  62. int len;
  63. gunichar *ucs4_temp;
  64. int i;
  65. len = s->getLength ();
  66. ucs4_temp = g_new (gunichar, len + 1);
  67. for (i = 0; i < len; ++i) {
  68. ucs4_temp[i] = pdfDocEncoding[(unsigned char)s->getChar(i)];
  69. }
  70. ucs4_temp[i] = 0;
  71. result = g_ucs4_to_utf8 (ucs4_temp, -1, NULL, NULL, NULL);
  72. g_free (ucs4_temp);
  73. }
  74. return result;
  75. }
  76. #ifdef HAVE_POPPLER_ANNOT_WRITE
  77. // Set the rectangle of an annotation. It was first added in v0.26.
  78. void xpoppler_annot_set_rectangle (PopplerAnnot *a, PopplerRectangle *rectangle)
  79. {
  80. GooString *state = (GooString*) a->annot->getAppearState ();
  81. char *ustate = _xpoppler_goo_string_to_utf8 (state);
  82. a->annot->setRect (rectangle->x1, rectangle->y1,
  83. rectangle->x2, rectangle->y2);
  84. a->annot->setAppearanceState (ustate);
  85. g_free (ustate);
  86. }
  87. #endif
  88. // This function is in the library, but the enforced date parsing is
  89. // incomplete (at least in some versions), because it ignores the
  90. // timezone.
  91. gchar *xpoppler_annot_markup_get_created (PopplerAnnotMarkup *poppler_annot)
  92. {
  93. AnnotMarkup *annot;
  94. GooString *text;
  95. g_return_val_if_fail (POPPLER_IS_ANNOT_MARKUP (poppler_annot), NULL);
  96. annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT (poppler_annot)->annot);
  97. text = (GooString*) annot->getDate ();
  98. return text ? _xpoppler_goo_string_to_utf8 (text) : NULL;
  99. }
  100. }