aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/pdf/FOToPDFRoleMap.java
blob: 26595bca164c1efe49b4587300e4cd9024cdbf4a (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.render.pdf;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;

import org.apache.fop.events.EventBroadcaster;
import org.apache.fop.pdf.PDFName;
import org.apache.fop.pdf.PDFObject;
import org.apache.fop.pdf.PDFStructElem;

/**
 * This class provides the standard mappings from Formatting Objects to PDF structure types.
 */
final class FOToPDFRoleMap {

    /**
     * Standard structure types defined by the PDF Reference, Fourth Edition (PDF 1.5).
     */
    private static final Map STANDARD_STRUCTURE_TYPES = new HashMap();

    private static final Map DEFAULT_MAPPINGS = new java.util.HashMap();

    private static final PDFName THEAD;
    private static final PDFName NON_STRUCT;

    static {
        // Create PDFNames for the standard structure types
        // Table 10.18: Grouping elements
        addStructureType("Document");
        addStructureType("Part");
        addStructureType("Art");
        addStructureType("Sect");
        addStructureType("Div");
        addStructureType("BlockQuote");
        addStructureType("Caption");
        addStructureType("TOC");
        addStructureType("TOCI");
        addStructureType("Index");
        addStructureType("NonStruct");
        addStructureType("Private");
        // Table 10.20: Paragraphlike elements
        addStructureType("H");
        addStructureType("H1");
        addStructureType("H2");
        addStructureType("H3");
        addStructureType("H4");
        addStructureType("H5");
        addStructureType("H6");
        addStructureType("P");
        // Table 10.21: List elements
        addStructureType("L");
        addStructureType("LI");
        addStructureType("Lbl");
        addStructureType("LBody");
        // Table 10.22: Table elements
        addStructureType("Table");
        addStructureType("TR");
        addStructureType("TH");
        addStructureType("TD");
        addStructureType("THead");
        addStructureType("TBody");
        addStructureType("TFoot");
        // Table 10.23: Inline-level structure elements
        addStructureType("Span");
        addStructureType("Quote");
        addStructureType("Note");
        addStructureType("Reference");
        addStructureType("BibEntry");
        addStructureType("Code");
        addStructureType("Link");
        addStructureType("Annot");
        // Table 10.24: Ruby and Warichu elements
        addStructureType("Ruby");
        addStructureType("RB");
        addStructureType("RT");
        addStructureType("RP");
        addStructureType("Warichu");
        addStructureType("WT");
        addStructureType("WP");
        // Table 10.25: Illustration elements
        addStructureType("Figure");
        addStructureType("Formula");
        addStructureType("Form");

        NON_STRUCT = (PDFName) STANDARD_STRUCTURE_TYPES.get("NonStruct");
        assert NON_STRUCT != null;
        THEAD = (PDFName) STANDARD_STRUCTURE_TYPES.get("THead");
        assert THEAD != null;

        // Create the standard mappings
        // Declarations and Pagination and Layout Formatting Objects
        addMapping("root",                      "Document");
        addMapping("page-sequence",             "Part");
        addMapping("flow",                      "Sect");
        addMapping("static-content",            "Sect");
        // Block-level Formatting Objects
        addMapping("block",                     "P");
        addMapping("block-container",           "Div");
        // Inline-level Formatting Objects
        addMapping("character",                 "Span");
        addMapping("external-graphic",          "Figure");
        addMapping("instream-foreign-object",   "Figure");
        addMapping("inline",                    "Span");
        addMapping("inline-container",          "Div");
        addMapping("page-number",               "Quote");
        addMapping("page-number-citation",      "Quote");
        addMapping("page-number-citation-last", "Quote");
        // Formatting Objects for Tables
        addMapping("table-and-caption",         "Div");
        addMapping("table",                     "Table");
        addMapping("table-caption",             "Caption");
        addMapping("table-header",              "THead");
        addMapping("table-footer",              "TFoot");
        addMapping("table-body",                "TBody");
        addMapping("table-row",                 "TR");
        addMapping("table-cell",                new TableCellMapper());
        // Formatting Objects for Lists
        addMapping("list-block",                "L");
        addMapping("list-item",                 "LI");
        addMapping("list-item-body",            "LBody");
        addMapping("list-item-label",           "Lbl");
        // Dynamic Effects: Link and Multi Formatting Objects
        addMapping("basic-link",                "Link");
        // Out-of-Line Formatting Objects
        addMapping("float",                     "Div");
        addMapping("footnote",                  "Note");
        addMapping("footnote-body",             "Sect");
        addMapping("wrapper",                   "Span");
        addMapping("marker",                    "Private");
    }

    private static void addStructureType(String structureType) {
        STANDARD_STRUCTURE_TYPES.put(structureType, new PDFName(structureType));
    }

    private static void addMapping(String fo, String structureType) {
        PDFName type = (PDFName) STANDARD_STRUCTURE_TYPES.get(structureType);
        assert type != null;
        addMapping(fo, new SimpleMapper(type));
    }

    private static void addMapping(String fo, Mapper mapper) {
        DEFAULT_MAPPINGS.put(fo, mapper);
    }


    /**
     * Maps a Formatting Object to a PDFName representing the associated structure type.
     * @param fo the formatting object's local name
     * @param parent the parent of the structure element to be mapped
     * @return the structure type or null if no match could be found
     */
    public static PDFName mapFormattingObject(String fo, PDFObject parent) {
        Mapper mapper = (Mapper)DEFAULT_MAPPINGS.get(fo);
        if (mapper != null) {
            return mapper.getStructureType(parent);
        } else {
            return NON_STRUCT;
        }
    }

    public static PDFName mapFormattingObject(Node fo, PDFObject parent,
            EventBroadcaster eventBroadcaster) {
        PDFName type = null;
        Node role = fo.getAttributes().getNamedItemNS(null, "role");
        if (role == null) {
            type = mapFormattingObject(fo.getLocalName(), parent);
        } else {
            String customType = role.getNodeValue();
            type = (PDFName) STANDARD_STRUCTURE_TYPES.get(customType);
            if (type == null) {
                String foName = fo.getLocalName();
                type = mapFormattingObject(foName, parent);
                PDFEventProducer.Provider.get(eventBroadcaster).nonStandardStructureType(fo,
                        foName, customType, type.toString().substring(1));
            }
        }
        assert type != null;
        return type;
    }

    private static interface Mapper {
        PDFName getStructureType(PDFObject parent);
    }

    private static class SimpleMapper implements Mapper {

        private PDFName structureType;

        public SimpleMapper(PDFName structureType) {
            this.structureType = structureType;
        }

        public PDFName getStructureType(PDFObject parent) {
            return structureType;
        }

    }

    private static class TableCellMapper implements Mapper {

        public PDFName getStructureType(PDFObject parent) {
            PDFStructElem grandParent = (PDFStructElem)
                ((PDFStructElem)parent).getParentStructElem();
            //TODO What to do with cells from table-footer? Currently they are mapped on TD.
            PDFName type;
            if (THEAD.equals(grandParent.getStructureType())) {
               type = (PDFName) STANDARD_STRUCTURE_TYPES.get("TH");
            } else {
                type = (PDFName) STANDARD_STRUCTURE_TYPES.get("TD");
            }
            assert type != null;
            return type;
        }

    }

    private FOToPDFRoleMap() { }
}