page.c 8.51 KB
Newer Older
1
/* SPDX-License-Identifier: Zlib */
Moritz Lipp's avatar
Moritz Lipp committed
2 3 4 5 6 7 8

#include <girara/session.h>
#include <girara/utils.h>
#include <glib/gi18n.h>

#include "document.h"
#include "page.h"
9
#include "plugin.h"
Moritz Lipp's avatar
Moritz Lipp committed
10
#include "utils.h"
11
#include "internal.h"
Moritz Lipp's avatar
Moritz Lipp committed
12
#include "types.h"
Moritz Lipp's avatar
Moritz Lipp committed
13 14 15 16

struct zathura_page_s {
  double height; /**< Page height */
  double width; /**< Page width */
17
  unsigned int index; /**< Page number */
Moritz Lipp's avatar
Moritz Lipp committed
18 19 20 21 22 23
  void* data; /**< Custom data */
  bool visible; /**< Page is visible */
  zathura_document_t* document; /**< Document */
};

zathura_page_t*
24
zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
25
{
26
  if (document == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
27
    if (error != NULL) {
28
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
29 30 31 32
    }
    goto error_ret;
  }

33
  /* init page */
34 35 36 37 38 39 40
  zathura_page_t* page = g_try_malloc0(sizeof(zathura_page_t));
  if (page == NULL) {
    if (error != NULL) {
      *error = ZATHURA_ERROR_OUT_OF_MEMORY;
    }
    goto error_ret;
  }
Moritz Lipp's avatar
Moritz Lipp committed
41

42
  page->index    = index;
43 44 45 46
  page->visible  = false;
  page->document = document;

  /* init plugin */
47
  zathura_plugin_t* plugin = zathura_document_get_plugin(document);
48
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
49 50

  if (functions->page_init == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
51
    if (error != NULL) {
52
      *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
53
    }
54
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
55 56
  }

57
  zathura_error_t ret = functions->page_init(page);
58
  if (ret != ZATHURA_ERROR_OK) {
59 60
    if (error != NULL) {
      *error = ret;
Moritz Lipp's avatar
Moritz Lipp committed
61
    }
62 63
    goto error_free;
  }
Moritz Lipp's avatar
Moritz Lipp committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77

  return page;

error_free:

  if (page != NULL) {
    zathura_page_free(page);
  }

error_ret:

  return NULL;
}

78
zathura_error_t
Moritz Lipp's avatar
Moritz Lipp committed
79 80
zathura_page_free(zathura_page_t* page)
{
81
  if (page == NULL) {
82
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
83 84
  }

85
  if (page->document == NULL) {
86
    g_free(page);
87
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
88 89
  }

90
  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
91
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
92
  if (functions->page_clear == NULL) {
93
    return ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
94 95
  }

96
  zathura_error_t error = functions->page_clear(page, page->data);
97 98 99 100

  g_free(page);

  return error;
Moritz Lipp's avatar
Moritz Lipp committed
101 102 103 104 105 106 107 108 109 110 111 112 113
}

zathura_document_t*
zathura_page_get_document(zathura_page_t* page)
{
  if (page == NULL) {
    return NULL;
  }

  return page->document;
}

unsigned int
114
zathura_page_get_index(zathura_page_t* page)
Moritz Lipp's avatar
Moritz Lipp committed
115 116 117 118 119
{
  if (page == NULL) {
    return 0;
  }

120
  return page->index;
Moritz Lipp's avatar
Moritz Lipp committed
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
}

double
zathura_page_get_width(zathura_page_t* page)
{
  if (page == NULL) {
    return -1;
  }

  return page->width;
}

void
zathura_page_set_width(zathura_page_t* page, double width)
{
  if (page == NULL) {
    return;
  }

  page->width = width;
}

double
zathura_page_get_height(zathura_page_t* page)
{
  if (page == NULL) {
    return -1;
  }

  return page->height;
}

void
zathura_page_set_height(zathura_page_t* page, double height)
{
  if (page == NULL) {
    return;
  }

  page->height = height;
}

bool
zathura_page_get_visibility(zathura_page_t* page)
{
  if (page == NULL) {
    return false;
  }

  return page->visible;
}

void
zathura_page_set_visibility(zathura_page_t* page, bool visibility)
{
  if (page == NULL) {
    return;
  }

  page->visible = visibility;
}

void*
zathura_page_get_data(zathura_page_t* page)
{
  if (page == NULL) {
    return NULL;
  }

  return page->data;
}

void
zathura_page_set_data(zathura_page_t* page, void* data)
{
  if (page == NULL) {
    return;
  }

  page->data = data;
}

girara_list_t*
204
zathura_page_search_text(zathura_page_t* page, const char* text, zathura_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
205
{
206
  if (page == NULL || page->document == NULL || text == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
207
    if (error != NULL) {
208
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
209 210 211 212
    }
    return NULL;
  }

213
  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
214
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
215
  if (functions->page_search_text == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
216
    if (error != NULL) {
217
      *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
218 219 220 221
    }
    return NULL;
  }

222
  return functions->page_search_text(page, page->data, text, error);
Moritz Lipp's avatar
Moritz Lipp committed
223 224 225
}

girara_list_t*
226
zathura_page_links_get(zathura_page_t* page, zathura_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
227
{
228
  if (page == NULL || page->document == NULL ) {
Moritz Lipp's avatar
Moritz Lipp committed
229
    if (error != NULL) {
230
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
231 232 233 234
    }
    return NULL;
  }

235
  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
236
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
237
  if (functions->page_links_get == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
238
    if (error != NULL) {
239
      *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
240 241 242 243
    }
    return NULL;
  }

244
  return functions->page_links_get(page, page->data, error);
Moritz Lipp's avatar
Moritz Lipp committed
245 246
}

247
zathura_error_t
Moritz Lipp's avatar
Moritz Lipp committed
248 249 250 251 252 253
zathura_page_links_free(girara_list_t* UNUSED(list))
{
  return false;
}

girara_list_t*
254
zathura_page_form_fields_get(zathura_page_t* page, zathura_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
255
{
256
  if (page == NULL || page->document == NULL ) {
Moritz Lipp's avatar
Moritz Lipp committed
257
    if (error != NULL) {
258
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
259 260 261 262
    }
    return NULL;
  }

263
  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
264
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
265
  if (functions->page_form_fields_get == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
266
    if (error != NULL) {
267
      *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
268 269 270 271
    }
    return NULL;
  }

272
  return functions->page_form_fields_get(page, page->data, error);
Moritz Lipp's avatar
Moritz Lipp committed
273 274
}

275
zathura_error_t
Moritz Lipp's avatar
Moritz Lipp committed
276 277
zathura_page_form_fields_free(girara_list_t* UNUSED(list))
{
278
  return ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
279 280 281
}

girara_list_t*
282
zathura_page_images_get(zathura_page_t* page, zathura_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
283
{
284
  if (page == NULL || page->document == NULL ) {
Moritz Lipp's avatar
Moritz Lipp committed
285
    if (error != NULL) {
286
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
287 288 289 290
    }
    return NULL;
  }

291
  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
292
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
293
  if (functions->page_images_get == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
294
    if (error != NULL) {
295
      *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
296 297 298 299
    }
    return NULL;
  }

300
  return functions->page_images_get(page, page->data, error);
Moritz Lipp's avatar
Moritz Lipp committed
301 302 303
}

cairo_surface_t*
304
zathura_page_image_get_cairo(zathura_page_t* page, zathura_image_t* image, zathura_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
305
{
306
  if (page == NULL || page->document == NULL  || image == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
307
    if (error != NULL) {
308
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
309 310 311 312
    }
    return NULL;
  }

313
  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
314
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
315
  if (functions->page_image_get_cairo == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
316
    if (error != NULL) {
317
      *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
318 319 320 321
    }
    return NULL;
  }

322
  return functions->page_image_get_cairo(page, page->data, image, error);
Moritz Lipp's avatar
Moritz Lipp committed
323 324
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
325 326
char*
zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_error_t* error)
Moritz Lipp's avatar
Moritz Lipp committed
327
{
328
  if (page == NULL || page->document == NULL ) {
Moritz Lipp's avatar
Moritz Lipp committed
329
    if (error) {
330
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
331 332 333 334
    }
    return NULL;
  }

335
  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
336
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
337
  if (functions->page_get_text == NULL) {
Moritz Lipp's avatar
Moritz Lipp committed
338
    if (error) {
339
      *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
340 341 342 343
    }
    return NULL;
  }

344
  return functions->page_get_text(page, page->data, rectangle, error);
Moritz Lipp's avatar
Moritz Lipp committed
345 346
}

347
zathura_error_t
Moritz Lipp's avatar
Moritz Lipp committed
348 349
zathura_page_render(zathura_page_t* page, cairo_t* cairo, bool printing)
{
350
  if (page == NULL  || page->document == NULL || cairo == NULL) {
351
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
Moritz Lipp's avatar
Moritz Lipp committed
352 353
  }

354
  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
355
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
356
  if (functions->page_render_cairo == NULL) {
357
    return ZATHURA_ERROR_NOT_IMPLEMENTED;
Moritz Lipp's avatar
Moritz Lipp committed
358 359
  }

360
  return functions->page_render_cairo(page, page->data, cairo, printing);
Moritz Lipp's avatar
Moritz Lipp committed
361
}
362 363 364 365 366 367 368 369 370 371 372 373

char*
zathura_page_get_label(zathura_page_t* page, zathura_error_t* error)
{
  if (page == NULL || page->document == NULL) {
    if (error) {
      *error = ZATHURA_ERROR_INVALID_ARGUMENTS;
    }
    return NULL;
  }

  zathura_plugin_t* plugin = zathura_document_get_plugin(page->document);
374
  const zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  if (functions->page_get_label == NULL) {
    if (error) {
      *error = ZATHURA_ERROR_NOT_IMPLEMENTED;
    }
    return NULL;
  }

  char* ret = NULL;
  zathura_error_t e = functions->page_get_label(page, page->data, &ret);
  if (e != ZATHURA_ERROR_OK) {
    if (error) {
      *error = e;
    }
    return NULL;
  }

  return ret;
}