document.c 5.8 KB
Newer Older
1 2 3 4 5 6 7
/* See LICENSE file for license and copyright information */

#define _POSIX_C_SOURCE 1

#include <mupdf/fitz.h>
#include <mupdf/pdf.h>

8 9
#include <glib-2.0/glib.h>

10 11
#include "plugin.h"

12 13
#define LENGTH(x) (sizeof(x)/sizeof((x)[0]))

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
zathura_error_t
pdf_document_open(zathura_document_t* document)
{
  zathura_error_t error = ZATHURA_ERROR_OK;
  if (document == NULL) {
    error = ZATHURA_ERROR_INVALID_ARGUMENTS;
    goto error_ret;
  }

  mupdf_document_t* mupdf_document = calloc(1, sizeof(mupdf_document_t));
  if (mupdf_document == NULL) {
    error = ZATHURA_ERROR_OUT_OF_MEMORY;
    goto error_ret;
  }

  mupdf_document->ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
  if (mupdf_document->ctx == NULL) {
    error = ZATHURA_ERROR_UNKNOWN;
    goto error_free;
  }

  /* open document */
  const char* path     = zathura_document_get_path(document);
  const char* password = zathura_document_get_password(document);

39
  fz_try(mupdf_document->ctx){
Moritz Lipp's avatar
Moritz Lipp committed
40 41 42
    fz_register_document_handlers(mupdf_document->ctx);

    mupdf_document->document = fz_open_document(mupdf_document->ctx, path);
43 44 45 46
  }
  fz_catch(mupdf_document->ctx){
    error = ZATHURA_ERROR_UNKNOWN;
    return error;
47 48 49 50 51 52 53 54
  }

  if (mupdf_document->document == NULL) {
    error = ZATHURA_ERROR_UNKNOWN;
    goto error_free;
  }

  /* authenticate if password is required and given */
Moritz Lipp's avatar
Moritz Lipp committed
55 56
  if (fz_needs_password(mupdf_document->ctx, mupdf_document->document) != 0) {
    if (password == NULL || fz_authenticate_password(mupdf_document->ctx, mupdf_document->document, (char*) password) == 0) {
57 58 59 60 61
      error = ZATHURA_ERROR_INVALID_PASSWORD;
      goto error_free;
    }
  }

Moritz Lipp's avatar
Moritz Lipp committed
62
  zathura_document_set_number_of_pages(document, fz_count_pages(mupdf_document->ctx, mupdf_document->document));
63 64 65 66 67 68 69 70
  zathura_document_set_data(document, mupdf_document);

  return error;

error_free:

  if (mupdf_document != NULL) {
    if (mupdf_document->document != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
71
      fz_drop_document(mupdf_document->ctx, mupdf_document->document);
72 73
    }
    if (mupdf_document->ctx != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
74
      fz_drop_context(mupdf_document->ctx);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    }

    free(mupdf_document);
  }

  zathura_document_set_data(document, NULL);

error_ret:

  return error;
}

zathura_error_t
pdf_document_free(zathura_document_t* document, mupdf_document_t* mupdf_document)
{
  if (document == NULL || mupdf_document == NULL) {
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
  }

Moritz Lipp's avatar
Moritz Lipp committed
94 95
  fz_drop_document(mupdf_document->ctx, mupdf_document->document);
  fz_drop_context(mupdf_document->ctx);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  free(mupdf_document);
  zathura_document_set_data(document, NULL);

  return ZATHURA_ERROR_OK;
}

zathura_error_t
pdf_document_save_as(zathura_document_t* document, mupdf_document_t*
    mupdf_document, const char* path)
{
  if (document == NULL || mupdf_document == NULL || path == NULL) {
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
  }

  fz_try (mupdf_document->ctx) {
111
    pdf_save_document(mupdf_document->ctx, (pdf_document*) mupdf_document->document, (char*) path, NULL);
112 113 114 115 116 117 118
  } fz_catch (mupdf_document->ctx) {
    return ZATHURA_ERROR_UNKNOWN;
  }

  return ZATHURA_ERROR_OK;
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
girara_list_t*
pdf_document_get_information(zathura_document_t* document, mupdf_document_t*
    mupdf_document, zathura_error_t* error)
{
  if (document == NULL || mupdf_document == NULL || error == NULL) {
    if (error != NULL) {
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
    }
  }

  girara_list_t* list = zathura_document_information_entry_list_new();
  if (list == NULL) {
    if (error != NULL) {
      *error = ZATHURA_ERROR_UNKNOWN;
    }
    return NULL;
  }

  fz_try (mupdf_document->ctx) {
    pdf_obj* trailer = pdf_trailer(mupdf_document->ctx, (pdf_document*) mupdf_document->document);
    pdf_obj* info_dict = pdf_dict_get(mupdf_document->ctx, trailer, PDF_NAME_Info);

    /* get string values */
    typedef struct info_value_s {
      const char* property;
      zathura_document_information_type_t type;
    } info_value_t;

    static const info_value_t string_values[] = {
      { "Title",    ZATHURA_DOCUMENT_INFORMATION_TITLE },
      { "Author",   ZATHURA_DOCUMENT_INFORMATION_AUTHOR },
      { "Subject",  ZATHURA_DOCUMENT_INFORMATION_SUBJECT },
      { "Keywords", ZATHURA_DOCUMENT_INFORMATION_KEYWORDS },
      { "Creator",  ZATHURA_DOCUMENT_INFORMATION_CREATOR },
      { "Producer", ZATHURA_DOCUMENT_INFORMATION_PRODUCER }
    };

    for (unsigned int i = 0; i < LENGTH(string_values); i++) {
      pdf_obj* value = pdf_dict_gets(mupdf_document->ctx, info_dict, string_values[i].property);
      if (value == NULL) {
        continue;
      }

      char* str_value = pdf_to_str_buf(mupdf_document->ctx, value);
      if (str_value == NULL || strlen(str_value) == 0) {
        continue;
      }

      zathura_document_information_entry_t* entry =
        zathura_document_information_entry_new(
          string_values[i].type,
          str_value
        );

    if (entry != NULL) {
      girara_list_append(list, entry);
    }
    }

    static const info_value_t time_values[] = {
      { "CreationDate", ZATHURA_DOCUMENT_INFORMATION_CREATION_DATE },
      { "ModDate",      ZATHURA_DOCUMENT_INFORMATION_MODIFICATION_DATE }
    };

    for (unsigned int i = 0; i < LENGTH(time_values); i++) {
      pdf_obj* value = pdf_dict_gets(mupdf_document->ctx, info_dict, time_values[i].property);
      if (value == NULL) {
        continue;
      }

      char* str_value = pdf_to_str_buf(mupdf_document->ctx, value);
      if (str_value == NULL || strlen(str_value) == 0) {
        continue;
      }

      zathura_document_information_entry_t* entry =
        zathura_document_information_entry_new(
          time_values[i].type,
          str_value // FIXME: Convert to common format
        );

      if (entry != NULL) {
        girara_list_append(list, entry);
      }
    }
  } fz_catch (mupdf_document->ctx) {
    if (error != NULL) {
      *error = ZATHURA_ERROR_UNKNOWN;
    }
    return NULL;
  }

  return list;
}