page.c 2.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* See LICENSE file for license and copyright information */

#include "plugin.h"

zathura_error_t
pdf_page_init(zathura_page_t* page)
{
  if (page == NULL) {
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
  }

  zathura_document_t* document     = zathura_page_get_document(page);
  mupdf_document_t* mupdf_document = zathura_document_get_data(document);
  mupdf_page_t* mupdf_page         = calloc(1, sizeof(mupdf_page_t));
  unsigned int index               = zathura_page_get_index(page);

  if (mupdf_page == NULL) {
    return  ZATHURA_ERROR_OUT_OF_MEMORY;
  }

  mupdf_page->ctx = mupdf_document->ctx;
  if (mupdf_page->ctx == NULL) {
    goto error_free;
  }

  /* load page */
  fz_try (mupdf_page->ctx) {
Moritz Lipp's avatar
Moritz Lipp committed
28
    mupdf_page->page = fz_load_page(mupdf_document->ctx, mupdf_document->document, index);
29 30 31 32
  } fz_catch (mupdf_page->ctx) {
    goto error_free;
  }

Moritz Lipp's avatar
Moritz Lipp committed
33
  fz_bound_page(mupdf_document->ctx, (fz_page*) mupdf_page->page, &mupdf_page->bbox);
34 35 36 37

  /* setup text */
  mupdf_page->extracted_text = false;

Moritz Lipp's avatar
Moritz Lipp committed
38 39 40
  fz_rect mediabox;
  mupdf_page->text = fz_new_stext_page(mupdf_page->ctx,
      fz_bound_page(mupdf_page->ctx, mupdf_page->page, &mediabox));
41 42 43 44
  if (mupdf_page->text == NULL) {
    goto error_free;
  }

45 46 47 48 49 50
  zathura_page_set_data(page, mupdf_page);

  /* get page dimensions */
  zathura_page_set_width(page,  mupdf_page->bbox.x1 - mupdf_page->bbox.x0);
  zathura_page_set_height(page, mupdf_page->bbox.y1 - mupdf_page->bbox.y0);

51 52 53 54 55 56 57 58 59 60
  return ZATHURA_ERROR_OK;

error_free:

  pdf_page_clear(page, mupdf_page);

  return ZATHURA_ERROR_UNKNOWN;
}

zathura_error_t
61
pdf_page_clear(zathura_page_t* page, void* data)
62 63 64 65 66
{
  if (page == NULL) {
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
  }

67
  mupdf_page_t* mupdf_page         = data;
68 69 70 71 72
  zathura_document_t* document     = zathura_page_get_document(page);
  mupdf_document_t* mupdf_document = zathura_document_get_data(document);

  if (mupdf_page != NULL) {
    if (mupdf_page->text != NULL) {
Christian Hesse's avatar
Christian Hesse committed
73
      fz_drop_stext_page(mupdf_page->ctx, mupdf_page->text);
74 75 76
    }

    if (mupdf_page->page != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
77
      fz_drop_page(mupdf_document->ctx, mupdf_page->page);
78 79 80 81 82 83 84 85
    }

    free(mupdf_page);
  }

  return ZATHURA_ERROR_UNKNOWN;
}