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

#include <glib.h>

#include "plugin.h"

girara_list_t*
8
pdf_page_links_get(zathura_page_t* page, void* data, zathura_error_t* error)
9 10 11 12 13 14 15 16
{
  if (page == NULL) {
    if (error != NULL) {
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
    }
    goto error_ret;
  }

17
  mupdf_page_t* mupdf_page = data;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  zathura_document_t* document = zathura_page_get_document(page);
  if (document == NULL || mupdf_page == NULL || mupdf_page->page == NULL) {
    goto error_ret;
  }

  mupdf_document_t* mupdf_document = zathura_document_get_data(document);;

  girara_list_t* list = girara_list_new2((girara_free_function_t) zathura_link_free);
  if (list == NULL) {
    if (error != NULL) {
      *error = ZATHURA_ERROR_OUT_OF_MEMORY;
    }
    goto error_free;
  }

Moritz Lipp's avatar
Moritz Lipp committed
33
  fz_link* link = fz_load_links(mupdf_document->ctx, mupdf_page->page);
34 35 36 37 38 39 40 41 42 43 44
  for (; link != NULL; link = link->next) {
    /* extract position */
    zathura_rectangle_t position;
    position.x1 = link->rect.x0;
    position.x2 = link->rect.x1;
    position.y1 = link->rect.y0;
    position.y2 = link->rect.y1;

    zathura_link_type_t type     = ZATHURA_LINK_INVALID;
    zathura_link_target_t target = { 0 };

Moritz Lipp's avatar
Moritz Lipp committed
45 46 47 48 49
    if (fz_is_external_link(mupdf_document->ctx, link->uri) == 1) {
      if (strstr(link->uri, "file://") == link->uri) {
        type         = ZATHURA_LINK_GOTO_REMOTE;
        target.value = link->uri;
      } else {
50
        type         = ZATHURA_LINK_URI;
Moritz Lipp's avatar
Moritz Lipp committed
51 52 53 54 55 56 57 58 59 60 61 62
        target.value = link->uri;
      }
    } else {
      float x = 0;
      float y = 0;

      type                    = ZATHURA_LINK_GOTO_DEST;
      target.destination_type = ZATHURA_LINK_DESTINATION_XYZ;
      target.page_number      = fz_resolve_link(mupdf_document->ctx,
          mupdf_document->document, link->uri, &x, &y);
      target.left  = x;
      target.top   = y;
63
      target.zoom  = 0.0;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    }

    zathura_link_t* zathura_link = zathura_link_new(type, position, target);
    if (zathura_link != NULL) {
      girara_list_append(list, zathura_link);
    }
  }

  return list;

error_free:

  if (list != NULL) {
    girara_list_free(list);
  }

error_ret:

  return NULL;
}