annotations.c 8.87 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
23
24
25
26
27
28
29
30
  void* user_data; /**< Custom data */

  union {
    struct {
      char* label; /**< Label */
      char* subject; /**< Subject of the annotation */
      zathura_annotation_popup_t* popup; /**< Optional popup */
    } markup;
  } 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
31
32
33
34
35
36
37
38
};

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));
39
40
41
42
43
  if (annotation == NULL) {
    return NULL;
  }

  switch (type) {
44
    case ZATHURA_ANNOTATION_MARKUP:
45
46
47
48
49
      break;
    default:
      free(annotation);
      return NULL;
  }
Moritz Lipp's avatar
Moritz Lipp committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

  annotation->type = type;

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

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  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
89
90
91
92
93
94
95
96
97
98
  free(annotation);
}

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

99
  return annotation->user_data;
Moritz Lipp's avatar
Moritz Lipp committed
100
101
102
103
104
105
106
107
108
}

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

109
  annotation->user_data = data;
Moritz Lipp's avatar
Moritz Lipp committed
110
111
112
113
114
115
116
117
118
119
120
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
}

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

210
zathura_page_t*
Moritz Lipp's avatar
Moritz Lipp committed
211
212
213
zathura_annotation_get_page_index(zathura_annotation_t* annotation)
{
  if (annotation == NULL) {
214
    return NULL;
Moritz Lipp's avatar
Moritz Lipp committed
215
216
  }

217
  return annotation->page;
Moritz Lipp's avatar
Moritz Lipp committed
218
219
220
}

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

227
  annotation->page = page;
Moritz Lipp's avatar
Moritz Lipp committed
228
229
}

230
231
zathura_rectangle_t
zathura_annotation_get_position(zathura_annotation_t* annotation)
232
{
233
  zathura_rectangle_t rectangle = { 0, 0, 0, 0 };
234

235
236
237
238
239
240
  if (annotation != NULL) {
    rectangle.x1 = annotation->position.x1;
    rectangle.x2 = annotation->position.x2;
    rectangle.y1 = annotation->position.y1;
    rectangle.y2 = annotation->position.y2;
  }
241

242
  return rectangle;
243
244
245
246
}

void
zathura_annotation_set_position(zathura_annotation_t* annotation,
247
    zathura_rectangle_t position)
248
249
250
251
252
{
  if (annotation == NULL) {
    return;
  }

253
254
255
256
257
258
259
260
261
262
263
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
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
425
426
427
428
  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;
}

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;
429
430
}

Moritz Lipp's avatar
Moritz Lipp committed
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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);
}