| 1 |
package tex4ht; |
| 2 |
/**********************************************************/ |
| 3 |
/* OoFilterImpl.java 2009-03-11-03:09 */ |
| 4 |
/* Copyright (C) 2006--2009 Eitan M. Gurari */ |
| 5 |
/* */ |
| 6 |
/* This work may be distributed and/or modified under the */ |
| 7 |
/* conditions of the LaTeX Project Public License, either */ |
| 8 |
/* version 1.3 of this license or (at your option) any */ |
| 9 |
/* later version. The latest version of this license is */ |
| 10 |
/* in */ |
| 11 |
/* http://www.latex-project.org/lppl.txt */ |
| 12 |
/* and version 1.3 or later is part of all distributions */ |
| 13 |
/* of LaTeX version 2003/12/01 or later. */ |
| 14 |
/* */ |
| 15 |
/* This work has the LPPL maintenance status "maintained".*/ |
| 16 |
/* */ |
| 17 |
/* This Current Maintainer of this work */ |
| 18 |
/* is Eitan M. Gurari. */ |
| 19 |
/* */ |
| 20 |
/* [email protected] */ |
| 21 |
/* http://www.cse.ohio-state.edu/~gurari */ |
| 22 |
/**********************************************************/ |
| 23 |
|
| 24 |
import java.util.*; |
| 25 |
import org.xml.sax.helpers.*; |
| 26 |
import org.xml.sax.*; |
| 27 |
import java.io.PrintWriter; |
| 28 |
import java.io.BufferedReader; |
| 29 |
import java.io.FileReader; |
| 30 |
|
| 31 |
public class OoFilter extends XMLFilterImpl { |
| 32 |
PrintWriter out = null; |
| 33 |
public OoFilter( PrintWriter out, |
| 34 |
PrintWriter log, boolean trace ){ |
| 35 |
this.out = out; |
| 36 |
} |
| 37 |
public void startElement(String ns, String sName, |
| 38 |
String qName, Attributes attr) { |
| 39 |
if( qName.equals( "draw:frame" ) ){ |
| 40 |
String name = attr.getValue("draw:name"); |
| 41 |
if( (name != null) |
| 42 |
&& (attr.getValue("svg:width") == null) |
| 43 |
&& (attr.getValue("svg:hieght") == null) |
| 44 |
){ |
| 45 |
java.awt.Image image = new javax.swing.ImageIcon(name).getImage(); |
| 46 |
int width = image.getWidth(null); |
| 47 |
int height = image.getHeight(null); |
| 48 |
if( (width>0) && (height>0) ){ |
| 49 |
org.xml.sax.helpers.AttributesImpl attrs = |
| 50 |
new org.xml.sax.helpers.AttributesImpl( attr ); |
| 51 |
attrs.addAttribute(null, "svg:width", "svg:width", |
| 52 |
"String", (width * 72 / 110) + "pt"); |
| 53 |
attrs.addAttribute(null, "svg:width", "svg:height", |
| 54 |
"String", (height * 72 / 110) + "pt"); |
| 55 |
attr = attrs; |
| 56 |
} } |
| 57 |
|
| 58 |
} |
| 59 |
try{ |
| 60 |
super.startElement(ns, sName, qName, attr); |
| 61 |
} catch( Exception e ){ |
| 62 |
System.err.println( "--- OoFilter Error 1 --- " + e); |
| 63 |
} } |
| 64 |
public void processingInstruction(String target, String fileName) { |
| 65 |
if( target.equals("tex4ht-lg") ){ |
| 66 |
fileName = fileName.trim(); |
| 67 |
if( fileName.replaceAll(" ","").startsWith("file=") ){ |
| 68 |
HashMap <String,HashMap <String,String>> map |
| 69 |
= new HashMap<String,HashMap <String,String>>(); |
| 70 |
String [] font_css_base = {"" , "-", ""}; |
| 71 |
String [] font_css_mag = {"x-x-", ""}; |
| 72 |
int base_font_size = 10; |
| 73 |
int second; |
| 74 |
|
| 75 |
int length = fileName.length(); |
| 76 |
int loc = fileName.indexOf(fileName.charAt(length-1)); |
| 77 |
fileName = fileName.substring(loc+1,length-1); |
| 78 |
try{ |
| 79 |
String s = ""; |
| 80 |
FileReader fr = new FileReader( fileName ); |
| 81 |
BufferedReader in = new BufferedReader( fr ); |
| 82 |
try{ |
| 83 |
String key = "", body = ""; |
| 84 |
while( (s=in.readLine()) != null ){ |
| 85 |
if( s.startsWith("htfcss: ") ){ |
| 86 |
s = s.substring(8); |
| 87 |
int idx = s.indexOf(' '); |
| 88 |
if( idx == 0 ){ |
| 89 |
body += s; |
| 90 |
} else { |
| 91 |
body = body.trim(); |
| 92 |
if( body.startsWith( "@media " ) ){ |
| 93 |
body = body.substring(7).trim(); |
| 94 |
if( body.startsWith("print ") ){ |
| 95 |
body = body.substring(6).trim(); |
| 96 |
} else { key = ""; } |
| 97 |
} |
| 98 |
if( !key.equals("") ){ |
| 99 |
String [] property = body.split(";"); |
| 100 |
for( int i=0; i<property.length; i++ ){ |
| 101 |
if( !property[i].trim().equals("") ){ |
| 102 |
int indx = property[i].indexOf(":"); |
| 103 |
if( indx != -1 ){ |
| 104 |
String name = property[i].substring(0,indx).trim(); |
| 105 |
String value = (property[i]+' ').substring(indx+1).trim(); |
| 106 |
if( !name.equals("") && !value.equals("") ){ |
| 107 |
if( map.containsKey(key) ){ |
| 108 |
HashMap <String,String> entry = map.get(key); |
| 109 |
entry.put(name,value); |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
HashMap <String,String> entry = new HashMap <String,String>(); |
| 114 |
entry.put(name,value); |
| 115 |
map.put(key,entry); |
| 116 |
} |
| 117 |
|
| 118 |
} } } } } |
| 119 |
|
| 120 |
if( idx == -1 ){ |
| 121 |
key = s; body = ""; |
| 122 |
} else { |
| 123 |
key = s.substring(0,idx); |
| 124 |
body = s.substring(idx); |
| 125 |
} } } } |
| 126 |
body = body.trim(); |
| 127 |
if( body.startsWith( "@media " ) ){ |
| 128 |
body = body.substring(7).trim(); |
| 129 |
if( body.startsWith("print ") ){ |
| 130 |
body = body.substring(6).trim(); |
| 131 |
} else { key = ""; } |
| 132 |
} |
| 133 |
if( !key.equals("") ){ |
| 134 |
String [] property = body.split(";"); |
| 135 |
for( int i=0; i<property.length; i++ ){ |
| 136 |
if( !property[i].trim().equals("") ){ |
| 137 |
int indx = property[i].indexOf(":"); |
| 138 |
if( indx != -1 ){ |
| 139 |
String name = property[i].substring(0,indx).trim(); |
| 140 |
String value = (property[i]+' ').substring(indx+1).trim(); |
| 141 |
if( !name.equals("") && !value.equals("") ){ |
| 142 |
if( map.containsKey(key) ){ |
| 143 |
HashMap <String,String> entry = map.get(key); |
| 144 |
entry.put(name,value); |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
HashMap <String,String> entry = new HashMap <String,String>(); |
| 149 |
entry.put(name,value); |
| 150 |
map.put(key,entry); |
| 151 |
} |
| 152 |
|
| 153 |
} } } } } |
| 154 |
|
| 155 |
} catch(java.lang.NumberFormatException e){ |
| 156 |
System.err.println( "--- OoFilter Error 3 --- Improper record: " + s); |
| 157 |
} |
| 158 |
|
| 159 |
in.close(); |
| 160 |
fr = new FileReader( fileName ); |
| 161 |
in = new BufferedReader( fr ); |
| 162 |
|
| 163 |
in.close(); |
| 164 |
fr = new FileReader( fileName ); |
| 165 |
in = new BufferedReader( fr ); |
| 166 |
try{ |
| 167 |
while( (s=in.readLine()) != null ){ |
| 168 |
if( s.startsWith("Font_css_base: ") ){ |
| 169 |
int idx = s.indexOf("Font_css_mag: "); |
| 170 |
if( idx != -1 ){ |
| 171 |
String [] pattern = s.substring(15,idx).trim().split("%s"); |
| 172 |
if( pattern.length == 3 ){ font_css_base = pattern; } |
| 173 |
pattern = (s+' ').substring(idx).trim().split("%s"); |
| 174 |
if( pattern.length == 2 ){ font_css_mag = pattern; } |
| 175 |
} } } |
| 176 |
} catch(Exception e){ |
| 177 |
System.err.println( "--- OoFilter Error 4 --- Improper record: " + s); |
| 178 |
} |
| 179 |
|
| 180 |
in.close(); |
| 181 |
fr = new FileReader( fileName ); |
| 182 |
in = new BufferedReader( fr ); |
| 183 |
try{ |
| 184 |
while( (s=in.readLine()) != null ){ |
| 185 |
if( s.startsWith("Font_Size:") ){ |
| 186 |
base_font_size = Integer.parseInt( s.substring(10).trim() ); |
| 187 |
} } |
| 188 |
} catch(java.lang.NumberFormatException e){ |
| 189 |
System.err.println( "--- OoFilter Error 5 --- Improper record: " + s); |
| 190 |
} |
| 191 |
|
| 192 |
in.close(); |
| 193 |
fr = new FileReader( fileName ); |
| 194 |
in = new BufferedReader( fr ); |
| 195 |
try{ |
| 196 |
while( (s=in.readLine()) != null ){ |
| 197 |
if( s.startsWith("Font(") ){ |
| 198 |
String [] match = s.split("\""); |
| 199 |
match[1] = match[1].trim(); |
| 200 |
match[2] = match[3].trim(); |
| 201 |
match[3] = match[5].trim(); |
| 202 |
match[4] = match[7].trim(); |
| 203 |
{ |
| 204 |
if( match[3].replaceAll("[0-9]","").equals("") ){ |
| 205 |
second = (int) |
| 206 |
( Integer.parseInt( match[3] ) |
| 207 |
* Long.parseLong( match[4] ) |
| 208 |
/ base_font_size |
| 209 |
); |
| 210 |
while( second > 700 ){ second /= 10; } |
| 211 |
} else { second = 100; } |
| 212 |
if( (int) ( Double.parseDouble(match[2]) |
| 213 |
/ Long.parseLong(match[4]) |
| 214 |
+ 0.5 |
| 215 |
) |
| 216 |
== base_font_size |
| 217 |
){ |
| 218 |
second = 100; |
| 219 |
}; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
HashMap <String,String> entry = map.get(match[1]); |
| 224 |
if( (entry != null) || (second < 98) || (second > 102) ){ |
| 225 |
String styleName = font_css_base[0] + match[1] + |
| 226 |
font_css_base[1] + match[2] + |
| 227 |
font_css_base[2]; |
| 228 |
if( !match[4].equals("100") ){ |
| 229 |
styleName += font_css_mag[0] + match[4] + font_css_mag[1]; |
| 230 |
} |
| 231 |
org.xml.sax.helpers.AttributesImpl attr = |
| 232 |
new org.xml.sax.helpers.AttributesImpl(); |
| 233 |
attr.addAttribute("", "style:name", "style:name", "String", styleName); |
| 234 |
attr.addAttribute("", "style:family", "style:family", "String", "text"); |
| 235 |
|
| 236 |
super.startElement(null, "style:style", "style:style", attr); |
| 237 |
attr = new org.xml.sax.helpers.AttributesImpl(); |
| 238 |
if( entry != null ){ |
| 239 |
Object [] name = entry.keySet().toArray(); |
| 240 |
for(int i=0; i < name.length; i++){ |
| 241 |
String value = entry.get(name[i]); |
| 242 |
attr.addAttribute("", "fo:" + (String) name[i], |
| 243 |
"fo:" + (String) name[i], |
| 244 |
"String", value); |
| 245 |
} } |
| 246 |
if( (second < 98) || (second > 102) ){ |
| 247 |
attr.addAttribute("", "fo:font-size", "fo:font-size", |
| 248 |
"String", (second / 10.0) + "pt"); |
| 249 |
} |
| 250 |
|
| 251 |
super.startElement(null, "style:text-properties", |
| 252 |
"style:text-properties", attr); |
| 253 |
super.endElement(null, "style:text-properties", |
| 254 |
"style:text-properties"); |
| 255 |
super.endElement(null, "style:style", "style:style"); |
| 256 |
} |
| 257 |
|
| 258 |
} } |
| 259 |
} catch(Exception e){ |
| 260 |
System.err.println( "--- OoFilter Error 6 --- Improper record: " + s); |
| 261 |
} |
| 262 |
|
| 263 |
} catch(Exception e){ |
| 264 |
System.err.println( "--- OoFilter Error 2 --- " + e); |
| 265 |
} |
| 266 |
|
| 267 |
} } } |
| 268 |
} |
| 269 |
|