page.c 2.34 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 28 29 30 31
/* See LICENSE file for license and copyright information */

#define _POSIX_C_SOURCE 1

#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;
  }

  zathura_page_set_data(page, mupdf_page);

  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
32
    mupdf_page->page = fz_load_page(mupdf_document->ctx, mupdf_document->document, index);
33 34 35 36
  } fz_catch (mupdf_page->ctx) {
    goto error_free;
  }

Moritz Lipp's avatar
Moritz Lipp committed
37
  fz_bound_page(mupdf_document->ctx, (fz_page*) mupdf_page->page, &mupdf_page->bbox);
38 39 40 41 42 43 44 45

  /* 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);

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

Moritz Lipp's avatar
Moritz Lipp committed
46 47 48
  fz_rect mediabox;
  mupdf_page->text = fz_new_stext_page(mupdf_page->ctx,
      fz_bound_page(mupdf_page->ctx, mupdf_page->page, &mediabox));
49 50 51 52
  if (mupdf_page->text == NULL) {
    goto error_free;
  }

Christian Hesse's avatar
Christian Hesse committed
53
  mupdf_page->sheet = fz_new_stext_sheet(mupdf_page->ctx);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  if (mupdf_page->sheet == NULL) {
    goto error_free;
  }

  return ZATHURA_ERROR_OK;

error_free:

  pdf_page_clear(page, mupdf_page);

  return ZATHURA_ERROR_UNKNOWN;
}

zathura_error_t
pdf_page_clear(zathura_page_t* page, mupdf_page_t* mupdf_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);

  if (mupdf_page != NULL) {
    if (mupdf_page->text != NULL) {
Christian Hesse's avatar
Christian Hesse committed
79
      fz_drop_stext_page(mupdf_page->ctx, mupdf_page->text);
80 81 82
    }

    if (mupdf_page->sheet != NULL) {
Christian Hesse's avatar
Christian Hesse committed
83
      fz_drop_stext_sheet(mupdf_page->ctx, mupdf_page->sheet);
84 85 86
    }

    if (mupdf_page->page != NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
87
      fz_drop_page(mupdf_document->ctx, mupdf_page->page);
88 89 90 91 92 93 94 95
    }

    free(mupdf_page);
  }

  return ZATHURA_ERROR_UNKNOWN;
}