1 /* SPDX-License-Identifier: Zlib */
2 
3 #include "utils.h"
4 
5 zathura_link_t*
poppler_link_to_zathura_link(PopplerDocument * poppler_document,PopplerAction * poppler_action,zathura_rectangle_t position)6 poppler_link_to_zathura_link(PopplerDocument* poppler_document, PopplerAction*
7     poppler_action, zathura_rectangle_t position)
8 {
9   zathura_link_type_t type     = ZATHURA_LINK_INVALID;
10   zathura_link_target_t target = { ZATHURA_LINK_DESTINATION_UNKNOWN, NULL, 0, -1, -1, -1, -1, 0 };
11 
12   /* extract link */
13   switch (poppler_action->type) {
14     case POPPLER_ACTION_NONE:
15       type = ZATHURA_LINK_NONE;
16       break;
17     case POPPLER_ACTION_GOTO_DEST: {
18       PopplerDest* poppler_destination = poppler_action->goto_dest.dest;
19       if (poppler_destination == NULL) {
20         return NULL;
21       }
22 
23       type = ZATHURA_LINK_GOTO_DEST;
24 
25       if (poppler_action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
26         poppler_destination = poppler_document_find_dest(poppler_document, poppler_destination->named_dest);
27         if (poppler_destination == NULL) {
28           return NULL;
29         }
30       }
31 
32       PopplerPage* poppler_page = poppler_document_get_page(poppler_document, poppler_destination->page_num - 1);
33       double height = 0;
34       poppler_page_get_size(poppler_page, NULL, &height);
35 
36       switch (poppler_destination->type) {
37         case POPPLER_DEST_XYZ:
38           target.destination_type = ZATHURA_LINK_DESTINATION_XYZ;
39           target.page_number      = poppler_destination->page_num - 1;
40           if (poppler_destination->change_zoom != 0) {
41             target.zoom           = poppler_destination->zoom;
42           }
43           if (poppler_destination->change_left != 0) {
44             target.left           = poppler_destination->left;
45           }
46           if (poppler_destination->change_top != 0) {
47             target.top            = height - MIN(height, poppler_destination->top);
48           }
49           break;
50         case POPPLER_DEST_FIT:
51           target.destination_type = ZATHURA_LINK_DESTINATION_FIT;
52           target.page_number      = poppler_destination->page_num - 1;
53           break;
54         case POPPLER_DEST_FITH:
55           target.destination_type = ZATHURA_LINK_DESTINATION_FITH;
56           target.page_number      = poppler_destination->page_num - 1;
57           if (poppler_destination->change_top != 0) {
58             target.top            = height - MIN(height, poppler_destination->top);
59           }
60           break;
61         case POPPLER_DEST_FITV:
62           target.destination_type = ZATHURA_LINK_DESTINATION_FITV;
63           target.page_number      = poppler_destination->page_num - 1;
64           if (poppler_destination->change_left != 0) {
65             target.left           = poppler_destination->left;
66           }
67           break;
68         case POPPLER_DEST_FITR:
69           target.destination_type = ZATHURA_LINK_DESTINATION_FITR;
70           target.page_number      = poppler_destination->page_num - 1;
71           if (poppler_destination->change_left != 0) {
72             target.left           = poppler_destination->left;
73           }
74           if (poppler_destination->change_top != 0) {
75             target.top            = height - MIN(height, poppler_destination->top);
76           }
77           target.right            = poppler_destination->right;
78           target.bottom           = height - MIN(height, poppler_destination->bottom);
79           break;
80         case POPPLER_DEST_FITB:
81           target.destination_type = ZATHURA_LINK_DESTINATION_FITB;
82           target.page_number      = poppler_destination->page_num - 1;
83           break;
84         case POPPLER_DEST_FITBH:
85           target.destination_type = ZATHURA_LINK_DESTINATION_FITBH;
86           target.page_number      = poppler_destination->page_num - 1;
87           if (poppler_destination->change_top != 0) {
88             target.top            = height - MIN(height, poppler_destination->top);
89           }
90           break;
91         case POPPLER_DEST_FITBV:
92           target.destination_type = ZATHURA_LINK_DESTINATION_FITBV;
93           target.page_number      = poppler_destination->page_num - 1;
94           target.left             = poppler_destination->top;
95           break;
96         case POPPLER_DEST_UNKNOWN:
97           target.destination_type = ZATHURA_LINK_DESTINATION_UNKNOWN;
98           target.page_number      = poppler_destination->page_num - 1;
99           break;
100         default:
101           return NULL;
102       }
103       break;
104     }
105     case POPPLER_ACTION_GOTO_REMOTE:
106       type = ZATHURA_LINK_GOTO_REMOTE;
107       if ((target.value = poppler_action->goto_remote.file_name) == NULL) {
108         return NULL;
109       }
110       break;
111     case POPPLER_ACTION_URI:
112       type         = ZATHURA_LINK_URI;
113       target.value = poppler_action->uri.uri;
114       break;
115     case POPPLER_ACTION_LAUNCH:
116       type         = ZATHURA_LINK_LAUNCH;
117       target.value = poppler_action->launch.file_name;
118       break;
119     case POPPLER_ACTION_NAMED:
120       type         = ZATHURA_LINK_NAMED;
121       target.value = poppler_action->named.named_dest;
122       break;
123     default:
124       return NULL;
125   }
126 
127   return zathura_link_new(type, position, target);
128 }
129