1
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright © 2016 Adrian Johnson
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Adrian Johnson.
32
 *
33
 * Contributor(s):
34
 *	Adrian Johnson <ajohnson@redneon.com>
35
 */
36

            
37
#include "cairoint.h"
38

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

            
41
/* Tagged PDF must have one of these tags at the top level */
42
static const char * _cairo_tag_stack_tagged_pdf_top_level_element_list[] =
43
{
44
    "Document",
45
    "Part",
46
    "Art",
47
    "Sect",
48
    "Div",
49
    NULL
50
};
51

            
52
/* List of valid tag names. Table numbers reference PDF 32000 */
53
static const char * _cairo_tag_stack_struct_pdf_list[] =
54
{
55
    /* Table 333 - Grouping Elements */
56
    "Document",
57
    "Part",
58
    "Art",
59
    "Sect",
60
    "Div",
61
    "BlockQuote",
62
    "Caption",
63
    "TOC",
64
    "TOCI",
65
    "Index",
66
    "NonStruct",
67
    "Private",
68

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

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

            
76
    /* Table 337 - Standard structure types for table elements */
77
    "Table",
78
    "TR", "TH", "TD",
79
    "THead", "TBody", "TFoot",
80

            
81
    /* Table 338 - Standard structure types for inline-level structure elements */
82
    "Span",
83
    "Quote",
84
    "Note",
85
    "Reference",
86
    "BibEntry",
87
    "Code",
88
    "Link", /* CAIRO_TAG_LINK */
89
    "Annot",
90
    "Ruby",
91
    "Warichu",
92

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

            
97
    /* Table 340 - Standard structure types for illustration elements */
98
    "Figure",
99
    "Formula",
100
    "Form",
101

            
102
    /* Section 14.8.2.2.2 - Artifacts */
103
    "Artifact",
104

            
105
    NULL
106
};
107

            
108
/* List of cairo specific tag names */
109
static const char * _cairo_tag_stack_cairo_tag_list[] =
110
{
111
    CAIRO_TAG_DEST,
112
    CAIRO_TAG_CONTENT,
113
    CAIRO_TAG_CONTENT_REF,
114
    NULL
115
};
116

            
117
void
118
10
_cairo_tag_stack_init (cairo_tag_stack_t *stack)
119
{
120
10
    cairo_list_init (&stack->list);
121
10
    stack->type = TAG_TREE_TYPE_NO_TAGS;
122
10
    stack->size = 0;
123
10
}
124

            
125
void
126
10
_cairo_tag_stack_fini (cairo_tag_stack_t *stack)
127
{
128
10
    while (! cairo_list_is_empty (&stack->list)) {
129
	cairo_tag_stack_elem_t *elem;
130

            
131
	elem = cairo_list_first_entry (&stack->list, cairo_tag_stack_elem_t, link);
132
	cairo_list_del (&elem->link);
133
	free (elem->name);
134
	free (elem->attributes);
135
	free (elem);
136
    }
137
10
}
138

            
139
cairo_tag_stack_structure_type_t
140
5
_cairo_tag_stack_get_structure_type (cairo_tag_stack_t *stack)
141
{
142
5
    return stack->type;
143
}
144

            
145
static cairo_bool_t
146
name_in_list (const char *name, const char **list)
147
{
148
    if (! name)
149
	return FALSE;
150

            
151
    while (*list) {
152
	if (strcmp (name, *list) == 0)
153
	    return TRUE;
154
	list++;
155
    }
156

            
157
    return FALSE;
158
}
159

            
160
cairo_int_status_t
161
_cairo_tag_stack_push (cairo_tag_stack_t *stack,
162
		       const char        *name,
163
		       const char        *attributes)
164
{
165
    cairo_tag_stack_elem_t *elem;
166

            
167
    if (! name_in_list (name, _cairo_tag_stack_struct_pdf_list) &&
168
	! name_in_list (name, _cairo_tag_stack_cairo_tag_list))
169
    {
170
	stack->type = TAG_TYPE_INVALID;
171
	return _cairo_tag_error ("Invalid tag: %s", name);
172
    }
173

            
174
    cairo_tag_stack_elem_t *top = _cairo_tag_stack_top_elem (stack);
175
    if (top &&
176
	(strcmp (top->name, CAIRO_TAG_CONTENT) == 0 ||
177
	 strcmp (top->name, CAIRO_TAG_CONTENT_REF) == 0 ||
178
	 strcmp (top->name, "Artifact") == 0))
179
    {
180
	return _cairo_tag_error ("%s tag can not contain nested tags",
181
				 (strcmp (top->name, CAIRO_TAG_CONTENT) == 0) ? "CAIRO_TAG_CONTENT" :
182
				 ((strcmp (top->name, CAIRO_TAG_CONTENT_REF) == 0) ? "CAIRO_TAG_CONTENT_REF" : top->name));
183
    }
184

            
185
    if (stack->type == TAG_TREE_TYPE_NO_TAGS) {
186
	if (name_in_list (name, _cairo_tag_stack_tagged_pdf_top_level_element_list))
187
	    stack->type = TAG_TREE_TYPE_TAGGED;
188
	else if (strcmp (name, "Link") == 0)
189
	    stack->type = TAG_TREE_TYPE_LINK_ONLY;
190
	else if (name_in_list (name, _cairo_tag_stack_struct_pdf_list))
191
	    stack->type = TAG_TREE_TYPE_STRUCTURE;
192
    } else {
193
	if (stack->type == TAG_TREE_TYPE_LINK_ONLY &&
194
	    (strcmp (name, "Link") != 0) &&
195
	    name_in_list (name, _cairo_tag_stack_struct_pdf_list))
196
	{
197
	    stack->type = TAG_TREE_TYPE_STRUCTURE;
198
	}
199
    }
200

            
201
    elem = _cairo_calloc (sizeof(cairo_tag_stack_elem_t));
202
    if (unlikely (elem == NULL))
203
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
204

            
205
    elem->name = strdup (name);
206
    if (unlikely (elem->name == NULL))
207
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
208

            
209
    if (attributes) {
210
	elem->attributes = strdup (attributes);
211
	if (unlikely (elem->attributes == NULL))
212
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
213
    } else {
214
	elem->attributes = NULL;
215
    }
216

            
217
    elem->data = NULL;
218

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

            
222
    return CAIRO_STATUS_SUCCESS;
223
}
224

            
225
cairo_private void
226
_cairo_tag_stack_set_top_data (cairo_tag_stack_t *stack,
227
			       void        *data)
228
{
229
    cairo_tag_stack_elem_t *top;
230

            
231
    top = _cairo_tag_stack_top_elem (stack);
232
    if (top)
233
	top->data = data;
234
}
235

            
236
cairo_int_status_t
237
_cairo_tag_stack_pop (cairo_tag_stack_t *stack,
238
		      const char *name,
239
		      cairo_tag_stack_elem_t **elem)
240
{
241
    cairo_tag_stack_elem_t *top;
242

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

            
249
    cairo_list_del (&top->link);
250
    stack->size--;
251
    if (strcmp (top->name, name) != 0) {
252
	cairo_status_t status =
253
	    _cairo_tag_error ("cairo_tag_end(\"%s\") does not matching previous begin tag \"%s\"",
254
			      name, top->name);
255
	stack->type = TAG_TYPE_INVALID;
256
	_cairo_tag_stack_free_elem (top);
257
	return status;
258
    }
259

            
260
    if (elem)
261
	*elem = top;
262
    else
263
	_cairo_tag_stack_free_elem (top);
264

            
265
    return CAIRO_STATUS_SUCCESS;
266
}
267

            
268
cairo_tag_stack_elem_t *
269
_cairo_tag_stack_top_elem (cairo_tag_stack_t *stack)
270
{
271
    if (cairo_list_is_empty (&stack->list))
272
	return NULL;
273

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

            
277
void
278
45
_cairo_tag_stack_foreach (cairo_tag_stack_t *stack,
279
			  void (*func)(cairo_tag_stack_elem_t *elem,
280
				       void *closure),
281
			  void *closure)
282
{
283
    cairo_tag_stack_elem_t *elem;
284

            
285
45
    cairo_list_foreach_entry (elem, cairo_tag_stack_elem_t, &stack->list, link) {
286
	func (elem, closure);
287
    }
288
45
}
289

            
290
void
291
_cairo_tag_stack_free_elem (cairo_tag_stack_elem_t *elem)
292
{
293
    free (elem->name);
294
    free (elem->attributes);
295
    free (elem);
296
}
297

            
298
cairo_tag_type_t
299
_cairo_tag_get_type (const char *name)
300
{
301
    if (! name_in_list (name, _cairo_tag_stack_struct_pdf_list) &&
302
	! name_in_list (name, _cairo_tag_stack_cairo_tag_list))
303
	return TAG_TYPE_INVALID;
304

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

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

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

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

            
317
    return TAG_TYPE_STRUCTURE;
318
}
319

            
320
cairo_status_t
321
_cairo_tag_error (const char *fmt, ...)
322
{
323
    va_list ap;
324

            
325
    if (getenv ("CAIRO_DEBUG_TAG") != NULL) {
326
	printf ("TAG ERROR: ");
327
	va_start (ap, fmt);
328
	vprintf (fmt, ap);
329
	va_end (ap);
330
	printf ("\n");
331
    }
332
    return _cairo_error (CAIRO_STATUS_TAG_ERROR);
333
}
334

            
335
void
336
_cairo_tag_warning (const char *fmt, ...)
337
{
338
    va_list ap;
339

            
340
    if (getenv ("CAIRO_DEBUG_TAG") != NULL) {
341
	printf ("TAG WARNING: ");
342
	va_start (ap, fmt);
343
	vprintf (fmt, ap);
344
	va_end (ap);
345
	printf ("\n");
346
    }
347
}