summaryrefslogtreecommitdiff
path: root/src/cairo-tag-stack.c
blob: 65444c907763eeb73ffcb51298eeb15be5cee895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2016 Adrian Johnson
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is Adrian Johnson.
 *
 * Contributor(s):
 *	Adrian Johnson <ajohnson@redneon.com>
 */

#include "cairoint.h"

#include "cairo-tag-stack-private.h"

/* Tagged PDF must have one of these tags at the top level */
static const char * _cairo_tag_stack_tagged_pdf_top_level_element_list[] =
{
    "Document",
    "Part",
    "Art",
    "Sect",
    "Div",
    NULL
};

/* List of valid tag names. Table numbers reference PDF 32000 */
static const char * _cairo_tag_stack_struct_pdf_list[] =
{
    /* Table 333 - Grouping Elements */
    "Document",
    "Part",
    "Art",
    "Sect",
    "Div",
    "BlockQuote",
    "Caption",
    "TOC",
    "TOCI",
    "Index",
    "NonStruct",
    "Private",

    /* Table 335 - Standard structure types for paragraphlike elements */
    "P", "H",
    "H1", "H2", "H3", "H4", "H5", "H6",

    /* Table 336 - Standard structure types for list elements */
    "L", "LI", "Lbl", "LBody",

    /* Table 337 - Standard structure types for table elements */
    "Table",
    "TR", "TH", "TD",
    "THead", "TBody", "TFoot",

    /* Table 338 - Standard structure types for inline-level structure elements */
    "Span",
    "Quote",
    "Note",
    "Reference",
    "BibEntry",
    "Code",
    "Link", /* CAIRO_TAG_LINK */
    "Annot",
    "Ruby",
    "Warichu",

    /* Table 339 - Standard structure types for Ruby and Warichu elements */
    "RB", "RT", "RP",
    "WT", "WP",

    /* Table 340 - Standard structure types for illustration elements */
    "Figure",
    "Formula",
    "Form",

    /* Section 14.8.2.2.2 - Artifacts */
    "Artifact",

    NULL
};

/* List of cairo specific tag names */
static const char * _cairo_tag_stack_cairo_tag_list[] =
{
    CAIRO_TAG_DEST,
    CAIRO_TAG_CONTENT,
    CAIRO_TAG_CONTENT_REF,
    NULL
};

void
_cairo_tag_stack_init (cairo_tag_stack_t *stack)
{
    cairo_list_init (&stack->list);
    stack->type = TAG_TREE_TYPE_NO_TAGS;
    stack->size = 0;
}

void
_cairo_tag_stack_fini (cairo_tag_stack_t *stack)
{
    while (! cairo_list_is_empty (&stack->list)) {
	cairo_tag_stack_elem_t *elem;

	elem = cairo_list_first_entry (&stack->list, cairo_tag_stack_elem_t, link);
	cairo_list_del (&elem->link);
	free (elem->name);
	free (elem->attributes);
	free (elem);
    }
}

cairo_tag_stack_structure_type_t
_cairo_tag_stack_get_structure_type (cairo_tag_stack_t *stack)
{
    return stack->type;
}

static cairo_bool_t
name_in_list (const char *name, const char **list)
{
    if (! name)
	return FALSE;

    while (*list) {
	if (strcmp (name, *list) == 0)
	    return TRUE;
	list++;
    }

    return FALSE;
}

cairo_int_status_t
_cairo_tag_stack_push (cairo_tag_stack_t *stack,
		       const char        *name,
		       const char        *attributes)
{
    cairo_tag_stack_elem_t *elem;

    if (! name_in_list (name, _cairo_tag_stack_struct_pdf_list) &&
	! name_in_list (name, _cairo_tag_stack_cairo_tag_list))
    {
	stack->type = TAG_TYPE_INVALID;
	return _cairo_tag_error ("Invalid tag: %s", name);
    }

    cairo_tag_stack_elem_t *top = _cairo_tag_stack_top_elem (stack);
    if (top &&
	(strcmp (top->name, CAIRO_TAG_CONTENT) == 0 ||
	 strcmp (top->name, CAIRO_TAG_CONTENT_REF) == 0 ||
	 strcmp (top->name, "Artifact") == 0))
    {
	return _cairo_tag_error ("%s tag can not contain nested tags",
				 (strcmp (top->name, CAIRO_TAG_CONTENT) == 0) ? "CAIRO_TAG_CONTENT" :
				 ((strcmp (top->name, CAIRO_TAG_CONTENT_REF) == 0) ? "CAIRO_TAG_CONTENT_REF" : top->name));
    }

    if (stack->type == TAG_TREE_TYPE_NO_TAGS) {
	if (name_in_list (name, _cairo_tag_stack_tagged_pdf_top_level_element_list))
	    stack->type = TAG_TREE_TYPE_TAGGED;
	else if (strcmp (name, "Link") == 0)
	    stack->type = TAG_TREE_TYPE_LINK_ONLY;
	else if (name_in_list (name, _cairo_tag_stack_struct_pdf_list))
	    stack->type = TAG_TREE_TYPE_STRUCTURE;
    } else {
	if (stack->type == TAG_TREE_TYPE_LINK_ONLY &&
	    (strcmp (name, "Link") != 0) &&
	    name_in_list (name, _cairo_tag_stack_struct_pdf_list))
	{
	    stack->type = TAG_TREE_TYPE_STRUCTURE;
	}
    }

    elem = _cairo_malloc (sizeof(cairo_tag_stack_elem_t));
    if (unlikely (elem == NULL))
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);

    elem->name = strdup (name);
    if (unlikely (elem->name == NULL))
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);

    if (attributes) {
	elem->attributes = strdup (attributes);
	if (unlikely (elem->attributes == NULL))
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    } else {
	elem->attributes = NULL;
    }

    elem->data = NULL;

    cairo_list_add_tail (&elem->link, &stack->list);
    stack->size++;

    return CAIRO_STATUS_SUCCESS;
}

cairo_private void
_cairo_tag_stack_set_top_data (cairo_tag_stack_t *stack,
			       void        *data)
{
    cairo_tag_stack_elem_t *top;

    top = _cairo_tag_stack_top_elem (stack);
    if (top)
	top->data = data;
}

cairo_int_status_t
_cairo_tag_stack_pop (cairo_tag_stack_t *stack,
		      const char *name,
		      cairo_tag_stack_elem_t **elem)
{
    cairo_tag_stack_elem_t *top;

    top = _cairo_tag_stack_top_elem (stack);
    if (!top) {
	stack->type = TAG_TYPE_INVALID;
	return _cairo_tag_error ("cairo_tag_end(\"%s\") no matching begin tag", name);
    }

    cairo_list_del (&top->link);
    stack->size--;
    if (strcmp (top->name, name) != 0) {
	stack->type = TAG_TYPE_INVALID;
	_cairo_tag_stack_free_elem (top);
	return _cairo_tag_error ("cairo_tag_end(\"%s\") does not matching previous begin tag \"%s\"",
				 name, top->name);
    }

    if (elem)
	*elem = top;
    else
	_cairo_tag_stack_free_elem (top);

    return CAIRO_STATUS_SUCCESS;
}

cairo_tag_stack_elem_t *
_cairo_tag_stack_top_elem (cairo_tag_stack_t *stack)
{
    if (cairo_list_is_empty (&stack->list))
	return NULL;

    return cairo_list_last_entry (&stack->list, cairo_tag_stack_elem_t, link);
}

void
_cairo_tag_stack_foreach (cairo_tag_stack_t *stack,
			  void (*func)(cairo_tag_stack_elem_t *elem,
				       void *closure),
			  void *closure)
{
    cairo_tag_stack_elem_t *elem;

    cairo_list_foreach_entry (elem, cairo_tag_stack_elem_t, &stack->list, link) {
	func (elem, closure);
    }
}

void
_cairo_tag_stack_free_elem (cairo_tag_stack_elem_t *elem)
{
    free (elem->name);
    free (elem->attributes);
    free (elem);
}

cairo_tag_type_t
_cairo_tag_get_type (const char *name)
{
    if (! name_in_list (name, _cairo_tag_stack_struct_pdf_list) &&
	! name_in_list (name, _cairo_tag_stack_cairo_tag_list))
	return TAG_TYPE_INVALID;

    if (strcmp(name, "Link") == 0)
	return (TAG_TYPE_LINK | TAG_TYPE_STRUCTURE);

    if (strcmp(name, CAIRO_TAG_DEST) == 0)
	return TAG_TYPE_DEST;

    if (strcmp(name, CAIRO_TAG_CONTENT) == 0)
	return TAG_TYPE_CONTENT;

    if (strcmp(name, CAIRO_TAG_CONTENT_REF) == 0)
	return TAG_TYPE_CONTENT_REF;

    return TAG_TYPE_STRUCTURE;
}

cairo_status_t
_cairo_tag_error (const char *fmt, ...)
{
    va_list ap;

    if (getenv ("CAIRO_DEBUG_TAG") != NULL) {
	printf ("TAG ERROR: ");
	va_start (ap, fmt);
	vprintf (fmt, ap);
	va_end (ap);
	printf ("\n");
    }
    return _cairo_error (CAIRO_STATUS_TAG_ERROR);
}