Debian Codegraph @

src/main/java/org/jfree/pdf/internal/Pages.java

raw
1 /* =====================================================================
2 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
3 * =====================================================================
4 *
5 * (C)opyright 2013-2022, by David Gilbert. All rights reserved.
6 *
7 * https://github.com/jfree/orsonpdf
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
23 * Other names may be trademarks of their respective owners.]
24 *
25 * If you do not wish to be bound by the terms of the GPL, an alternative
26 * runtime license is available to JFree sponsors:
27 *
28 * https://github.com/sponsors/jfree
29 *
30 */
31
32 package org.jfree.pdf.internal;
33
34 import java.awt.Font;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import org.jfree.pdf.DefaultFontMapper;
40 import org.jfree.pdf.FontMapper;
41 import org.jfree.pdf.PDFDocument;
42 import org.jfree.pdf.Page;
43 import org.jfree.pdf.dictionary.Dictionary;
44 import org.jfree.pdf.util.Args;
45
46 /**
47 * A {@code PDFObject} that maintains the list of pages for the document.
48 * When a {@link PDFDocument} is created, it will create an instance of
49 * {@code Pages} and add it to the document catalog. You won't normally
50 * interact directly with this class.
51 */
52 public final class Pages extends PDFObject {
53
54 /** The PDF document. */
55 private final PDFDocument parent;
56
57 private final List<Page> pages;
58
59 /** The list of font objects used in the document. */
60 private final List<PDFFont> fonts;
61
62 private final Map<FontKey, PDFFont> fontMap;
63
64 private int nextFont = 1;
65
66 private final FontMapper fontMapper;
67
68 /**
69 * Creates a new {@code Pages} object.
70 *
71 * @param number the PDF object number.
72 * @param generation the PDF object generation number.
73 * @param parent the PDF document ({@code null} not permitted).
74 */
75 public Pages(int number, int generation, PDFDocument parent) {
76 super(number, generation);
77 Args.nullNotPermitted(parent, "parent");
78 this.parent = parent;
79 this.pages = new ArrayList<>();
80 this.fonts = new ArrayList<>();
81 this.fontMap = new HashMap<>();
82 this.fontMapper = new DefaultFontMapper();
83 }
84
85 /**
86 * Returns the PDF document that the pages belong to.
87 *
88 * @return The PDF document (never {@code null}).
89 */
90 public PDFDocument getDocument() {
91 return this.parent;
92 }
93
94 /**
95 * Returns a list of the pages in this object.
96 *
97 * @return A list of the pages.
98 */
99 public List<Page> getPages() {
100 return this.pages;
101 }
102
103 /**
104 * Returns a list of fonts used in these pages.
105 *
106 * @return A list of fonts.
107 */
108 public List<PDFFont> getFonts() {
109 return this.fonts;
110 }
111
112 /**
113 * Returns the PDF font with the specified name, or {@code null} if
114 * there is no font with that name.
115 *
116 * @param name the font name.
117 *
118 * @return The PDF font or {@code null}.
119 */
120 public PDFFont getFont(String name) {
121 for (PDFFont f : this.fonts) {
122 if (f.getName().equals(name)) {
123 return f;
124 }
125 }
126 return null;
127 }
128
129 /**
130 * Adds a page to the collection.
131 *
132 * @param page the page.
133 */
134 public void add(Page page) {
135 this.pages.add(page);
136 }
137
138 /**
139 * Finds or creates a font reference for the specified AWT font.
140 *
141 * @param f the font ({@code null} not permitted).
142 *
143 * @return The font reference.
144 */
145 public String findOrCreateFontReference(Font f) {
146 // for now, map all fonts to one of the standard PDF fonts
147 FontKey fontKey = FontKey.createFontKey(f);
148 PDFFont pdfFont = this.fontMap.get(fontKey);
149 if (pdfFont == null) {
150 int number = this.parent.getNextNumber();
151 String name = "/F" + this.nextFont + "-"
152 + f.getFamily().replace(' ', '_');
153 String baseFont = this.fontMapper.mapToBaseFont(f);
154 this.nextFont++;
155 pdfFont = new PDFFont(number, 0, name, "/" + baseFont,
156 "/MacRomanEncoding");
157 this.fonts.add(pdfFont);
158 this.fontMap.put(fontKey, pdfFont);
159 }
160 return pdfFont.getName();
161 }
162
163 private Dictionary createDictionary() {
164 Dictionary dictionary = new Dictionary("/Pages");
165 Page[] pagesArray = new Page[this.pages.size()];
166 for (int i = 0; i < this.pages.size(); i++) {
167 pagesArray[i] = this.pages.get(i);
168 }
169 dictionary.put("/Kids", pagesArray);
170 dictionary.put("/Count", pages.size());
171 return dictionary;
172 }
173
174 @Override
175 public byte[] getObjectBytes() {
176 return createDictionary().toPDFBytes();
177 }
178
179 }