• R/O
  • SSH
  • HTTPS

pdf-brewer: コミット


コミットメタ情報

リビジョン22 (tree)
日時2020-09-14 22:33:12
作者hirukawa_ryo

ログメッセージ

* pdf-brewer 0.4.4
FontLoader ロードするファイル名、フォント名を制限できるようにしました。
これでOutOfMemoryErrorが解消するとよいのですが…。

変更サマリ

差分

--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/Context.java (revision 21)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/Context.java (revision 22)
@@ -132,7 +132,7 @@
132132
133133 public void setFont(String fontName, float fontSize) throws IOException {
134134 if(fontName != null) {
135- TrueTypeFont ttf = getFontLoader().get(fontName);
135+ TrueTypeFont ttf = getFontLoader().getFont(fontName);
136136 if(ttf != null) {
137137 this.fontName = ttf.getName();
138138 }
@@ -147,7 +147,7 @@
147147 }
148148
149149 public void setFontName(String fontName) throws IOException {
150- TrueTypeFont ttf = getFontLoader().get(fontName);
150+ TrueTypeFont ttf = getFontLoader().getFont(fontName);
151151 if(ttf != null) {
152152 this.fontName = ttf.getName();
153153 }
--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/FontLoader.java (revision 21)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/FontLoader.java (revision 22)
@@ -6,6 +6,7 @@
66 import java.text.Normalizer;
77 import java.util.ArrayList;
88 import java.util.Arrays;
9+import java.util.Collection;
910 import java.util.Collections;
1011 import java.util.Comparator;
1112 import java.util.HashMap;
@@ -23,11 +24,44 @@
2324
2425 public class FontLoader {
2526
27+ public static final List<String> FILENAMES_YUGOTHIC;
28+ public static final List<String> FILENAMES_YUMINCHO;
29+
30+ static {
31+ FILENAMES_YUGOTHIC = Arrays.asList(
32+ "yugothib.ttf",
33+ "yugothic-bold.ttf",
34+ "yugothic.ttf",
35+ "yugothil.ttf",
36+ "yugothb.ttc",
37+ "yugothl.ttc",
38+ "yugothm.ttc",
39+ "yugothr.ttc"
40+ );
41+ FILENAMES_YUMINCHO = Arrays.asList(
42+ "yumin.ttf",
43+ "yumindb.ttf",
44+ "yuminl.ttf"
45+ );
46+ }
47+
48+ public static File getDefaultFontDir() {
49+ String windir = System.getenv("windir");
50+ if(windir != null) {
51+ return new File(windir, "Fonts");
52+ }
53+ return null;
54+ }
55+
2656 private Map<String, TrueTypeFont> fonts = new HashMap<String, TrueTypeFont>();
27- private Map<TrueTypeFont, File> files = new HashMap<TrueTypeFont, File>();
28-
57+ private Map<TrueTypeFont, File> ttcFiles = new HashMap<TrueTypeFont, File>();
58+
2959 public FontLoader(File fontDir) {
30- load(fontDir);
60+ this(fontDir, null, null);
61+ }
62+
63+ public FontLoader(File fontDir, Collection<String> fileNames, Collection<String> fontNames) {
64+ load(fontDir, fileNames, fontNames);
3165
3266 TrueTypeFont serif;
3367 TrueTypeFont serifBold;
@@ -34,10 +68,10 @@
3468 TrueTypeFont sansSerif;
3569 TrueTypeFont sansSerifBold;
3670
37- serif = get("YuMincho-Regular");
38- serifBold = get("YuMincho-Demibold");
39- sansSerif = get("YuGothic-Regular");
40- sansSerifBold = get("YuGothic-Bold");
71+ serif = getFont("YuMincho-Regular");
72+ serifBold = getFont("YuMincho-Demibold");
73+ sansSerif = getFont("YuGothic-Regular");
74+ sansSerifBold = getFont("YuGothic-Bold");
4175
4276 if(serif != null && serifBold != null && sansSerif != null && sansSerifBold != null) {
4377 try {
@@ -68,7 +102,7 @@
68102 return list;
69103 }
70104
71- public TrueTypeFont get(String name) {
105+ public TrueTypeFont getFont(String name) {
72106 if(name == null || name.isBlank()) {
73107 return null;
74108 }
@@ -90,12 +124,28 @@
90124
91125 return null;
92126 }
93-
94- public File getFile(TrueTypeFont ttf) throws IOException {
95- return files.get(ttf);
127+
128+ public File getTtcFile(TrueTypeFont ttf) throws IOException {
129+ return ttcFiles.get(ttf);
96130 }
97-
98- public void load(File dir) {
131+
132+ public void load(File dir, Collection<String> fileNames, Collection<String> fontNames) {
133+ Set<String> fileNameSet = null;
134+ if(fileNames != null) {
135+ fileNameSet = new HashSet<String>();
136+ for(String fileName : fileNames) {
137+ fileNameSet.add(fileName.toLowerCase());
138+ }
139+ }
140+
141+ Set<String> fontNameSet = null;
142+ if(fontNames != null) {
143+ fontNameSet = new HashSet<String>();
144+ for(String fontName : fontNames) {
145+ fontNameSet.add(normalize(fontName));
146+ }
147+ }
148+
99149 TTFParser parser = new TTFParser();
100150 File[] ttfFiles = dir.listFiles(new FilenameFilter() {
101151 @Override
@@ -104,11 +154,32 @@
104154 }
105155 });
106156 for(File file : ttfFiles) {
157+ if(fileNameSet != null && !fileNameSet.contains(file.getName().toLowerCase())) {
158+ // fileNames が指定されている場合は一致するファイルのみを処理対象にします。(一致しない場合は continue で次へ)
159+ continue;
160+ }
161+ TrueTypeFont ttf = null;
107162 try {
108- TrueTypeFont ttf = parser.parse(file);
109- register(ttf);
163+ ttf = parser.parse(file);
164+
165+ if(fontNameSet == null) {
166+ register(ttf);
167+ ttf = null; // 登録したttfがfinallyでcloseされないようにします。
168+ } else {
169+ List<String> normalizedNames = getNormalizedNames(ttf);
170+ if(!Collections.disjoint(fontNameSet, normalizedNames)) {
171+ for(String s : normalizedNames) {
172+ register(s, ttf);
173+ ttf = null; // 登録したttfがfinallyでcloseされないようにします。
174+ }
175+ }
176+ }
110177 } catch(IOException e) {
111178 e.printStackTrace();
179+ } finally {
180+ if(ttf != null) {
181+ try { ttf.close(); } catch(IOException ignore) {}
182+ }
112183 }
113184 }
114185
@@ -119,14 +190,38 @@
119190 }
120191 });
121192 for(File file : ttcFiles) {
193+ if(fileNameSet != null && !fileNameSet.contains(file.getName().toLowerCase())) {
194+ // fileNames が指定されている場合は一致するファイルのみを処理対象にします。(一致しない場合は continue で次へ)
195+ continue;
196+ }
197+
122198 TrueTypeCollection ttc = null;
123199 try {
200+ final Set<String> _fontNameSet = fontNameSet;
124201 ttc = new TrueTypeCollection(file);
125202 ttc.processAllFonts(new TrueTypeFontProcessor() {
126203 @Override
127204 public void process(TrueTypeFont ttf) throws IOException {
128- register(ttf);
129- files.put(ttf, file);
205+ try {
206+ if(_fontNameSet == null) {
207+ register(ttf);
208+ FontLoader.this.ttcFiles.put(ttf, file);
209+ ttf = null; // 登録したttfがfinallyでcloseされないようにします。
210+ } else {
211+ List<String> normalizedNames = getNormalizedNames(ttf);
212+ if(!Collections.disjoint(_fontNameSet, normalizedNames)) {
213+ for(String s : normalizedNames) {
214+ register(s, ttf);
215+ }
216+ FontLoader.this.ttcFiles.put(ttf, file);
217+ ttf = null; // 登録したttfがfinallyでcloseされないようにします。
218+ }
219+ }
220+ } finally {
221+ if(ttf != null) {
222+ try { ttf.close(); } catch(IOException ignore) {}
223+ }
224+ }
130225 }
131226 });
132227 } catch(IOException e) {
@@ -148,7 +243,12 @@
148243 fonts.put(normalize(name), ttf);
149244 return;
150245 }
151-
246+ for(String nomalizedName : getNormalizedNames(ttf)) {
247+ fonts.put(nomalizedName, ttf);
248+ }
249+ }
250+
251+ public List<String> getNormalizedNames(TrueTypeFont ttf) throws IOException {
152252 List<String> names = new ArrayList<String>();
153253 String fontName = ttf.getName();
154254 if(fontName != null && !fontName.isBlank()) {
@@ -208,12 +308,10 @@
208308 }
209309 }
210310 }
211-
212- for(String s : names) {
213- fonts.put(s, ttf);
214- }
311+ return names;
215312 }
216-
313+
314+
217315 private String normalize(String name) {
218316 String s = name.replace('\t', '-').replace('\u3000', '-').replace(' ', '-').toLowerCase();
219317 while(s.contains("--")) {
@@ -231,4 +329,13 @@
231329 }
232330 return sb.toString();
233331 }
332+
333+ public void close() {
334+ ttcFiles.clear();
335+
336+ for(TrueTypeFont ttf : fonts.values()) {
337+ try { ttf.close(); } catch(IOException ignore) {}
338+ }
339+ fonts.clear();
340+ }
234341 }
--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/PdfBrewer.java (revision 21)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/PdfBrewer.java (revision 22)
@@ -7,9 +7,9 @@
77 import java.io.OutputStream;
88 import java.nio.file.Files;
99 import java.nio.file.Path;
10-import java.nio.file.Paths;
1110 import java.util.ArrayDeque;
1211 import java.util.Calendar;
12+import java.util.Collection;
1313 import java.util.Deque;
1414 import java.util.HashMap;
1515 import java.util.HashSet;
@@ -33,6 +33,13 @@
3333 import net.osdn.pdf_brewer.instruction.text.TextBufferingInstruction;
3434
3535 public class PdfBrewer {
36+
37+ public static void main(String[] args) throws Exception {
38+
39+ new PdfBrewer();
40+ System.in.read();
41+ }
42+
3643
3744 private static PDFont FONT_NULL;
3845
@@ -70,34 +77,37 @@
7077 return title + " " + version;
7178 }
7279
73- public static Path getDefaultFontDir() {
74- String windir = System.getenv("windir");
75- if(windir != null) {
76- return Paths.get(windir, "Fonts");
77- }
78- return null;
79- }
80-
8180 private PDRectangle mediaBox;
8281 private PDDocument document;
8382 private PDPage page;
8483 private PDPageContentStream stream;
8584 private FontLoader fontLoader;
85+ private boolean isFontLoaderAutoGenerated;
8686 private Map<String, PDFont> fonts = new HashMap<String, PDFont>();
8787 private Set<TrueTypeCollection> ttcLoaded = new HashSet<TrueTypeCollection>();
8888
8989 public PdfBrewer() {
90- this(getDefaultFontDir());
90+ this(FontLoader.getDefaultFontDir().toPath(), null, null);
9191 }
92-
93- public PdfBrewer(Path fontDir) {
94- fontLoader = new FontLoader(fontDir.toFile());
95- document = new PDDocument();
92+
93+ public PdfBrewer(Path fontDir, Collection<String> fileNames, Collection<String> fontNames) {
94+ this(new FontLoader(fontDir.toFile(), fileNames, fontNames));
95+ isFontLoaderAutoGenerated = true;
96+ }
97+
98+ public PdfBrewer(FontLoader fontLoader) {
99+ if(fontLoader == null) {
100+ throw new IllegalArgumentException("fontLoader is null");
101+ }
102+ this.fontLoader = fontLoader;
103+ this.document = new PDDocument();
96104
97105 PDDocumentInformation info = document.getDocumentInformation();
98106 info.setProducer(getDefaultProducer());
99107 info.setCreator(getDefaultCreator());
100108 }
109+
110+
101111
102112 public FontLoader getFontLoader() {
103113 return fontLoader;
@@ -172,13 +182,13 @@
172182
173183 public PDFont loadFont(String fontName) throws IOException {
174184 PDFont font = null;
175- TrueTypeFont ttf = getFontLoader().get(fontName);
185+ TrueTypeFont ttf = getFontLoader().getFont(fontName);
176186 if(ttf != null) {
177187 font = fonts.get(ttf.getName());
178188 if(font == null) {
179- File file = getFontLoader().getFile(ttf);
180- if(file != null) {
181- TrueTypeCollection ttc = new TrueTypeCollection(file);
189+ File ttcFile = getFontLoader().getTtcFile(ttf);
190+ if(ttcFile != null) {
191+ TrueTypeCollection ttc = new TrueTypeCollection(ttcFile);
182192 font = PDType0Font.load(document, ttc.getFontByName(ttf.getName()), true);
183193 ttcLoaded.add(ttc);
184194 } else {
@@ -288,10 +298,21 @@
288298 */
289299
290300 document.save(output);
291- document.close();
292-
301+ }
302+
303+ public void close() throws IOException {
304+ if(document != null) {
305+ document.close();
306+ document = null;
307+ }
308+
293309 for(TrueTypeCollection ttc : ttcLoaded) {
294310 ttc.close();
295311 }
312+ ttcLoaded.clear();
313+
314+ if(isFontLoaderAutoGenerated) {
315+ fontLoader.close();
316+ }
296317 }
297318 }
--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/instruction/text/Font.java (revision 21)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/instruction/text/Font.java (revision 22)
@@ -27,7 +27,7 @@
2727 }
2828 if(sb.length() >= 2) {
2929 String name = sb.substring(1);
30- TrueTypeFont ttf = fontLoader.get(name);
30+ TrueTypeFont ttf = fontLoader.getFont(name);
3131 if(ttf != null) {
3232 fontName = ttf.getName();
3333 }
旧リポジトリブラウザで表示