79 static public final Color defaultRTFColor = Color.black;
80
81 static public final float defaultFontSize = 12f;
82
83 static public final String defaultFontFamily = "Helvetica";
84
85 /* constants so we can avoid allocating objects in inner loops */
86 final static private Object MagicToken;
87
88 /* An array of character-keyword pairs. This could be done
89 as a dictionary (and lookup would be quicker), but that
90 would require allocating an object for every character
91 written (slow!). */
92 static class CharacterKeywordPair
93 { public char character; public String keyword; }
94 static protected CharacterKeywordPair[] textKeywords;
95
96 static {
97 MagicToken = new Object();
98
99 Dictionary textKeywordDictionary = RTFReader.textKeywords;
100 Enumeration keys = textKeywordDictionary.keys();
101 Vector<CharacterKeywordPair> tempPairs = new Vector<CharacterKeywordPair>();
102 while(keys.hasMoreElements()) {
103 CharacterKeywordPair pair = new CharacterKeywordPair();
104 pair.keyword = (String)keys.nextElement();
105 pair.character = ((String)textKeywordDictionary.get(pair.keyword)).charAt(0);
106 tempPairs.addElement(pair);
107 }
108 textKeywords = new CharacterKeywordPair[tempPairs.size()];
109 tempPairs.copyInto(textKeywords);
110 }
111
112 static final char[] hexdigits = { '0', '1', '2', '3', '4', '5', '6', '7',
113 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
114
115 static public void writeDocument(Document d, OutputStream to)
116 throws IOException
117 {
118 RTFGenerator gen = new RTFGenerator(to);
119 Element root = d.getDefaultRootElement();
120
121 gen.examineElement(root);
122 gen.writeRTFHeader();
123 gen.writeDocumentProperties(d);
124
125 /* TODO this assumes a particular element structure; is there
323 String font;
324 while(fonts.hasMoreElements()) {
325 font = fonts.nextElement();
326 Integer num = fontTable.get(font);
327 sortedFontTable[num.intValue()] = font;
328 }
329 writeBegingroup();
330 writeControlWord("fonttbl");
331 for(index = 0; index < fontCount; index ++) {
332 writeControlWord("f", index);
333 writeControlWord("fnil"); /* TODO: supply correct font style */
334 writeText(sortedFontTable[index]);
335 writeText(";");
336 }
337 writeEndgroup();
338 writeLineBreak();
339
340 /* write color table */
341 if (colorCount > 1) {
342 Color[] sortedColorTable = new Color[colorCount];
343 Enumeration colors = colorTable.keys();
344 Color color;
345 while(colors.hasMoreElements()) {
346 color = (Color)colors.nextElement();
347 Integer num = colorTable.get(color);
348 sortedColorTable[num.intValue()] = color;
349 }
350 writeBegingroup();
351 writeControlWord("colortbl");
352 for(index = 0; index < colorCount; index ++) {
353 color = sortedColorTable[index];
354 if (color != null) {
355 writeControlWord("red", color.getRed());
356 writeControlWord("green", color.getGreen());
357 writeControlWord("blue", color.getBlue());
358 }
359 writeRawString(";");
360 }
361 writeEndgroup();
362 writeLineBreak();
363 }
|
79 static public final Color defaultRTFColor = Color.black;
80
81 static public final float defaultFontSize = 12f;
82
83 static public final String defaultFontFamily = "Helvetica";
84
85 /* constants so we can avoid allocating objects in inner loops */
86 final static private Object MagicToken;
87
88 /* An array of character-keyword pairs. This could be done
89 as a dictionary (and lookup would be quicker), but that
90 would require allocating an object for every character
91 written (slow!). */
92 static class CharacterKeywordPair
93 { public char character; public String keyword; }
94 static protected CharacterKeywordPair[] textKeywords;
95
96 static {
97 MagicToken = new Object();
98
99 Dictionary<String, String> textKeywordDictionary = RTFReader.textKeywords;
100 Enumeration<String> keys = textKeywordDictionary.keys();
101 Vector<CharacterKeywordPair> tempPairs = new Vector<CharacterKeywordPair>();
102 while(keys.hasMoreElements()) {
103 CharacterKeywordPair pair = new CharacterKeywordPair();
104 pair.keyword = keys.nextElement();
105 pair.character = textKeywordDictionary.get(pair.keyword).charAt(0);
106 tempPairs.addElement(pair);
107 }
108 textKeywords = new CharacterKeywordPair[tempPairs.size()];
109 tempPairs.copyInto(textKeywords);
110 }
111
112 static final char[] hexdigits = { '0', '1', '2', '3', '4', '5', '6', '7',
113 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
114
115 static public void writeDocument(Document d, OutputStream to)
116 throws IOException
117 {
118 RTFGenerator gen = new RTFGenerator(to);
119 Element root = d.getDefaultRootElement();
120
121 gen.examineElement(root);
122 gen.writeRTFHeader();
123 gen.writeDocumentProperties(d);
124
125 /* TODO this assumes a particular element structure; is there
323 String font;
324 while(fonts.hasMoreElements()) {
325 font = fonts.nextElement();
326 Integer num = fontTable.get(font);
327 sortedFontTable[num.intValue()] = font;
328 }
329 writeBegingroup();
330 writeControlWord("fonttbl");
331 for(index = 0; index < fontCount; index ++) {
332 writeControlWord("f", index);
333 writeControlWord("fnil"); /* TODO: supply correct font style */
334 writeText(sortedFontTable[index]);
335 writeText(";");
336 }
337 writeEndgroup();
338 writeLineBreak();
339
340 /* write color table */
341 if (colorCount > 1) {
342 Color[] sortedColorTable = new Color[colorCount];
343 Enumeration<Object> colors = colorTable.keys();
344 Color color;
345 while(colors.hasMoreElements()) {
346 color = (Color)colors.nextElement();
347 Integer num = colorTable.get(color);
348 sortedColorTable[num.intValue()] = color;
349 }
350 writeBegingroup();
351 writeControlWord("colortbl");
352 for(index = 0; index < colorCount; index ++) {
353 color = sortedColorTable[index];
354 if (color != null) {
355 writeControlWord("red", color.getRed());
356 writeControlWord("green", color.getGreen());
357 writeControlWord("blue", color.getBlue());
358 }
359 writeRawString(";");
360 }
361 writeEndgroup();
362 writeLineBreak();
363 }
|