forked from tberg12/ocular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitializeFont.java
167 lines (145 loc) · 6.34 KB
/
InitializeFont.java
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
package edu.berkeley.cs.nlp.ocular.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import edu.berkeley.cs.nlp.ocular.data.textreader.Charset;
import edu.berkeley.cs.nlp.ocular.font.Font;
import edu.berkeley.cs.nlp.ocular.image.FontRenderer;
import edu.berkeley.cs.nlp.ocular.image.ImageUtils.PixelType;
import edu.berkeley.cs.nlp.ocular.lm.LanguageModel;
import edu.berkeley.cs.nlp.ocular.model.CharacterTemplate;
import tberg.murphy.fig.Option;
import tberg.murphy.fileio.f;
import tberg.murphy.indexer.Indexer;
import tberg.murphy.threading.BetterThreader;
/**
* @author Taylor Berg-Kirkpatrick ([email protected])
*/
public class InitializeFont extends OcularRunnable {
@Option(gloss = "Path to the language model file (so that it knows which characters to create images for).")
public static String inputLmPath = null; // Required.
@Option(gloss = "Output font file path.")
public static String outputFontPath = null; // Required.
@Option(gloss = "Path to a file that contains a custom list of font names that may be used to initialize the font. The file should contain one font name per line. Default: Use all valid fonts found on the computer.")
public static String allowedFontsPath = null;
@Option(gloss = "Number of threads to use.")
public static int numFontInitThreads = 8;
@Option(gloss = "Max template width as fraction of text line height.")
public static double templateMaxWidthFraction = 1.0;
@Option(gloss = "Min template width as fraction of text line height.")
public static double templateMinWidthFraction = 0.0;
@Option(gloss = "Max space template width as fraction of text line height.")
public static double spaceMaxWidthFraction = 1.0;
@Option(gloss = "Min space template width as fraction of text line height.")
public static double spaceMinWidthFraction = 0.0;
public static void main(String[] args) {
System.out.println("InitializeFont");
InitializeFont main = new InitializeFont();
main.doMain(main, args);
}
protected void validateOptions() {
if (inputLmPath == null) throw new IllegalArgumentException("-inputLmPath not set");
if (outputFontPath == null) throw new IllegalArgumentException("-outputFontPath not set");
}
public void run(List<String> commandLineArgs) {
Set<String> allowedFonts = getAllowedFontsListFromFile();
//System.out.println(allowedFonts);
final LanguageModel lm = InitializeLanguageModel.readLM(inputLmPath);
final Indexer<String> charIndexer = lm.getCharacterIndexer();
final CharacterTemplate[] templates = new CharacterTemplate[charIndexer.size()];
final PixelType[][][][] fontPixelData = FontRenderer.getRenderedFont(charIndexer, CharacterTemplate.LINE_HEIGHT, allowedFonts);
// final PixelType[][][] fAndBarFontPixelData = buildFAndBarFontPixelData(charIndexer, fontPixelData);
BetterThreader.Function<Integer,Object> func = new BetterThreader.Function<Integer,Object>(){public void call(Integer c, Object ignore){
String currChar = charIndexer.getObject(c);
if (!currChar.equals(Charset.SPACE)) {
templates[c] = new CharacterTemplate(currChar, (float) templateMaxWidthFraction, (float) templateMinWidthFraction);
// if (currChar.equals(Charset.LONG_S)) {
// templates[c].initializeAndSetPriorFromFontData(fAndBarFontPixelData);
// } else {
templates[c].initializeAndSetPriorFromFontData(fontPixelData[c]);
// }
} else {
templates[c] = new CharacterTemplate(Charset.SPACE, (float) spaceMaxWidthFraction, (float) spaceMinWidthFraction);
}
}};
BetterThreader<Integer,Object> threader = new BetterThreader<Integer,Object>(func, numFontInitThreads);
for (int c=0; c<templates.length; ++c) threader.addFunctionArgument(c);
threader.run();
Map<String,CharacterTemplate> charTemplates = new HashMap<String, CharacterTemplate>();
for (CharacterTemplate template : templates) {
charTemplates.put(template.getCharacter(), template);
}
System.out.println("Writing intialized font to" + outputFontPath);
InitializeFont.writeFont(new Font(charTemplates), outputFontPath);
}
private Set<String> getAllowedFontsListFromFile() {
Set<String> allowedFonts = new HashSet<String>();
if (allowedFontsPath != null) {
for (String fontName : f.readLines(allowedFontsPath)) {
allowedFonts.add(fontName);
}
}
return allowedFonts;
}
// private static PixelType[][][] buildFAndBarFontPixelData(Indexer<String> charIndexer, PixelType[][][][] fontPixelData) {
// List<PixelType[][]> fAndBarFontPixelData = new ArrayList<PixelType[][]>();
// if (charIndexer.contains("f")) {
// int c = charIndexer.getIndex("f");
// for (PixelType[][] datum : fontPixelData[c]) {
// fAndBarFontPixelData.add(datum);
// }
// }
// if (charIndexer.contains("|")) {
// int c = charIndexer.getIndex("|");
// for (PixelType[][] datum : fontPixelData[c]) {
// fAndBarFontPixelData.add(datum);
// }
// }
// return fAndBarFontPixelData.toArray(new PixelType[0][][]);
// }
@SuppressWarnings("unchecked")
public static Font readFont(String fontPath) {
ObjectInputStream in = null;
try {
File file = new File(fontPath);
if (!file.exists()) {
throw new RuntimeException("Serialized font file " + fontPath + " not found");
}
in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(file)));
Object obj = in.readObject();
{ // TODO: For legacy font models...
if (obj instanceof Map<?, ?>)
return new Font((Map<String, CharacterTemplate>)obj);
}
return (Font) obj;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (in != null)
try { in.close(); } catch (IOException e) { throw new RuntimeException(e); }
}
}
public static void writeFont(Font font, String fontPath) {
ObjectOutputStream out = null;
try {
new File(fontPath).getAbsoluteFile().getParentFile().mkdirs();
out = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(fontPath)));
out.writeObject(font);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (out != null)
try { out.close(); } catch (IOException e) { throw new RuntimeException(e); }
}
}
}