annotations.c 11.4 KB
Newer Older
Moritz Lipp's avatar
Moritz Lipp committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/* See LICENSE file for license and copyright information */

#include "annotations.h"

#include <stdlib.h>
#include <string.h>

struct zathura_annotation_s {
  zathura_annotation_type_t type; /**< Type of the annotation */
  zathura_annotation_flag_t flags; /**< Flags of the annotation */
  char* name; /**< Name of the annotation */
  char* content; /**< Content of the annotation */
  time_t modification_date; /**< Modification date */
14
  zathura_page_t* page; /**< Zathura page */
15
  zathura_rectangle_t position; /**< Position of the annotation */
16
17
18
19
20
21
22
  void* user_data; /**< Custom data */

  union {
    struct {
      char* label; /**< Label */
      char* subject; /**< Subject of the annotation */
      zathura_annotation_popup_t* popup; /**< Optional popup */
23
      time_t creation_date; /**< Creation date */
24
    } markup;
25
26
27
28
29
    struct {
      zathura_annotation_text_icon_t icon; /**< The icon of the text annotation */
      zathura_annotation_text_state_t state; /**< The state of the text annotation */
      bool opened; /**< Open status of the text annotation */
    } text;
30
31
32
33
34
35
36
  } data;
};

struct zathura_annotation_popup_s {
  double opacity; /**< The opacity of the popup */
  zathura_rectangle_t position; /**< Position of the annotation */
  bool opened; /**< true if popup is opened */
Moritz Lipp's avatar
Moritz Lipp committed
37
38
39
40
41
42
43
44
};

static char* __strdup(const char* string);

zathura_annotation_t*
zathura_annotation_new(zathura_annotation_type_t type)
{
  zathura_annotation_t* annotation = calloc(1, sizeof(zathura_annotation_t));
45
46
47
48
49
  if (annotation == NULL) {
    return NULL;
  }

  switch (type) {
50
    case ZATHURA_ANNOTATION_MARKUP:
51
      annotation->data.markup.creation_date = (time_t) -1;
52
      break;
53
54
55
    case ZATHURA_ANNOTATION_TEXT:
      annotation->data.text.icon  = ZATHURA_ANNOTATION_TEXT_ICON_UNKNOWN;
      annotation->data.text.state = ZATHURA_ANNOTATION_TEXT_STATE_UNKNOWN;
56
57
58
59
    default:
      free(annotation);
      return NULL;
  }
Moritz Lipp's avatar
Moritz Lipp committed
60

61
62
  annotation->type              = type;
  annotation->modification_date = (time_t) -1;
Moritz Lipp's avatar
Moritz Lipp committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

  return annotation;
}

void
zathura_annotation_free(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
    return;
  }

  if (annotation->name) {
    free(annotation->name);
  }

  if (annotation->content) {
    free(annotation->content);
  }

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  switch (annotation->type) {
    case ZATHURA_ANNOTATION_MARKUP:
      if (annotation->data.markup.label != NULL) {
        free(annotation->data.markup.label);
      }

      if (annotation->data.markup.subject != NULL) {
        free(annotation->data.markup.subject);
      }

      if (annotation->data.markup.popup != NULL) {
        zathura_annotation_popup_free(annotation->data.markup.popup);
      }
      break;
    default:
      break;
  }

Moritz Lipp's avatar
Moritz Lipp committed
100
101
102
103
104
105
106
107
108
109
  free(annotation);
}

void*
zathura_annotation_get_data(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
    return NULL;
  }

110
  return annotation->user_data;
Moritz Lipp's avatar
Moritz Lipp committed
111
112
113
114
115
116
117
118
119
}

void
zathura_annotation_set_data(zathura_annotation_t* annotation, void* data)
{
  if (annotation == NULL) {
    return;
  }

120
  annotation->user_data = data;
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
}

zathura_annotation_type_t
zathura_annotation_get_type(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
    return ZATHURA_ANNOTATION_UNKNOWN;
  }

  return annotation->type;
}

zathura_annotation_flag_t
zathura_annotation_get_flags(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
    return ZATHURA_ANNOTATION_FLAG_UNKNOWN;
  }

  return annotation->flags;
}

void
zathura_annotation_set_flags(zathura_annotation_t* annotation, zathura_annotation_flag_t flags)
{
  if (annotation == NULL) {
    return;
  }

  annotation->flags = flags;
}

char*
zathura_annotation_get_content(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
    return NULL;
  }

  return annotation->content;
}

void
zathura_annotation_set_content(zathura_annotation_t* annotation, char* content)
{
  if (annotation == NULL) {
    return;
  }

  if (content != NULL) {
    annotation->content = __strdup(content);
  } else {
    annotation->content = NULL;
  }
}

char*
zathura_annotation_get_name(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
    return NULL;
  }

  return annotation->name;
}

void
zathura_annotation_set_name(zathura_annotation_t* annotation, const char* name)
{
  if (annotation == NULL) {
    return;
  }

  if (name != NULL) {
    annotation->name = __strdup(name);
  } else {
    annotation->name = NULL;
  }
}

time_t
zathura_annotation_get_modified(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
    return (time_t) -1;
  }

  return annotation->modification_date;
}

void
zathura_annotation_set_modified(zathura_annotation_t* annotation, time_t modification_date)
{
  if (annotation == NULL) {
    return;
  }

  annotation->modification_date = modification_date;
}

221
zathura_page_t*
Moritz Lipp's avatar
Moritz Lipp committed
222
223
224
zathura_annotation_get_page_index(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
225
    return NULL;
Moritz Lipp's avatar
Moritz Lipp committed
226
227
  }

228
  return annotation->page;
Moritz Lipp's avatar
Moritz Lipp committed
229
230
231
}

void
232
zathura_annotation_set_page(zathura_annotation_t* annotation, zathura_page_t* page)
Moritz Lipp's avatar
Moritz Lipp committed
233
234
235
236
237
{
  if (annotation == NULL) {
    return;
  }

238
  annotation->page = page;
Moritz Lipp's avatar
Moritz Lipp committed
239
240
}

241
242
zathura_rectangle_t
zathura_annotation_get_position(zathura_annotation_t* annotation)
243
{
244
  zathura_rectangle_t rectangle = { 0, 0, 0, 0 };
245

246
247
248
249
250
251
  if (annotation != NULL) {
    rectangle.x1 = annotation->position.x1;
    rectangle.x2 = annotation->position.x2;
    rectangle.y1 = annotation->position.y1;
    rectangle.y2 = annotation->position.y2;
  }
252

253
  return rectangle;
254
255
256
257
}

void
zathura_annotation_set_position(zathura_annotation_t* annotation,
258
    zathura_rectangle_t position)
259
260
261
262
263
{
  if (annotation == NULL) {
    return;
  }

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  annotation->position.x1 = position.x1;
  annotation->position.x2 = position.x2;
  annotation->position.y1 = position.y1;
  annotation->position.y2 = position.y2;
}

char*
zathura_annotation_markup_get_label(zathura_annotation_t* annotation)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_MARKUP) {
    return NULL;
  }

  return annotation->data.markup.label;
}

void
zathura_annotation_markup_set_label(zathura_annotation_t* annotation, const char* label)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_MARKUP) {
    return;
  }

  if (annotation->data.markup.label != NULL) {
    free(annotation->data.markup.label);
  }

  if (label != NULL) {
    annotation->data.markup.label = __strdup(label);
  } else {
    annotation->data.markup.label = NULL;
  }
}

char*
zathura_annotation_markup_get_subject(zathura_annotation_t* annotation)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_MARKUP) {
    return NULL;
  }

  return annotation->data.markup.subject;
}

void
zathura_annotation_markup_set_subject(zathura_annotation_t* annotation, const char* subject)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_MARKUP) {
    return;
  }
  if (annotation->data.markup.subject != NULL) {
    free(annotation->data.markup.subject);
  }

  if (subject != NULL) {
    annotation->data.markup.subject = __strdup(subject);
  } else {
    annotation->data.markup.subject = NULL;
  }
}

zathura_annotation_popup_t*
zathura_annotation_markup_get_popup(zathura_annotation_t* annotation)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_MARKUP) {
    return NULL;
  }

  return annotation->data.markup.popup;
}

void
zathura_annotation_markup_set_popup(zathura_annotation_t* annotation, zathura_annotation_popup_t* popup)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_MARKUP || popup == NULL) {
    return;
  }

  annotation->data.markup.popup = popup;
}

345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
time_t
zathura_annotation_markup_get_creation_date(zathura_annotation_t* annotation)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_MARKUP) {
    return (time_t) -1;
  }

  return annotation->data.markup.creation_date;
}

void
zathura_annotation_markup_set_creation_date(zathura_annotation_t* annotation, time_t creation_date)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_MARKUP) {
    return;
  }

  annotation->data.markup.creation_date = creation_date;
}

zathura_annotation_text_icon_t
zathura_annotation_text_get_icon(zathura_annotation_t* annotation)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_TEXT) {
    return ZATHURA_ANNOTATION_TEXT_ICON_UNKNOWN;
  }

  return annotation->data.text.icon;
}

void
zathura_annotation_text_set_icon(zathura_annotation_t* annotation, zathura_annotation_text_icon_t icon)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_TEXT) {
    return;
  }

  annotation->data.text.icon = icon;
}

zathura_annotation_text_state_t
zathura_annotation_text_get_state(zathura_annotation_t* annotation)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_TEXT) {
    return ZATHURA_ANNOTATION_TEXT_STATE_UNKNOWN;
  }

  return annotation->data.text.state;
}

void
zathura_annotation_text_set_state(zathura_annotation_t* annotation, zathura_annotation_text_state_t state)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_TEXT) {
    return;
  }

  annotation->data.text.state = state;
}

bool
zathura_annotation_text_get_open_status(zathura_annotation_t* annotation)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_TEXT) {
    return false;
  }

  return annotation->data.text.opened;
}

void
zathura_annotation_text_set_open_status(zathura_annotation_t* annotation, bool opened)
{
  if (annotation == NULL || annotation->type != ZATHURA_ANNOTATION_TEXT) {
    return;
  }

  annotation->data.text.opened = opened;
}

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
zathura_annotation_popup_t*
zathura_annotation_popup_new()
{
  zathura_annotation_popup_t* popup = calloc(1, sizeof(zathura_annotation_popup_t));
  if (popup == NULL) {
    return NULL;
  }

  popup->opacity = 1.0;

  return popup;
}

void
zathura_annotation_popup_free(zathura_annotation_popup_t* popup)
{
  if (popup == NULL) {
    return;
  }

  free(popup);
}

zathura_rectangle_t
zathura_annotation_popup_get_position(zathura_annotation_popup_t* popup)
{
  zathura_rectangle_t rectangle = { 0, 0, 0, 0 };

  if (popup != NULL) {
    rectangle.x1 = popup->position.x1;
    rectangle.x2 = popup->position.x2;
    rectangle.y1 = popup->position.y1;
    rectangle.y2 = popup->position.y2;
  }

  return rectangle;
}

void
zathura_annotation_popup_set_position(zathura_annotation_popup_t* popup, zathura_rectangle_t position)
{
  if (popup == NULL) {
    return;
  }

  popup->position.x1 = position.x1;
  popup->position.x2 = position.x2;
  popup->position.y1 = position.y1;
  popup->position.y2 = position.y2;
}

double
zathura_annotation_popup_get_opacity(zathura_annotation_popup_t* popup)
{
  if (popup == NULL) {
    return 0.0;
  }

  return popup->opacity;
}

void
zathura_annotation_popup_set_opacity(zathura_annotation_popup_t* popup, double opacity)
{
  if (popup == NULL) {
    return;
  }

  if (opacity < 0) {
    popup->opacity = 0;
  } else if (opacity > 1) {
    popup->opacity = 1;
  } else {
    popup->opacity = opacity;
  }
}

bool
zathura_annotation_popup_get_open_status(zathura_annotation_popup_t* popup)
{
  if (popup == NULL) {
    return false;
  }

  return popup->opened;
}

void
zathura_annotation_popup_set_open_status(zathura_annotation_popup_t* popup, bool opened)
{
  if (popup == NULL) {
    return;
  }

  popup->opened = opened;
520
521
}

Moritz Lipp's avatar
Moritz Lipp committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
static char*
__strdup(const char* string)
{
  if (string == NULL) {
    return NULL;
  }

  size_t length = strlen(string) + 1;

  char* new_string = malloc(sizeof(char) * length);
  if (new_string == NULL) {
    return NULL;
  }

  return (char*) memcpy(new_string, string, length);
}