1 50 51 package org.openlaszlo.iv.flash.commands; 52 53 import org.openlaszlo.iv.flash.parser.*; 54 import org.openlaszlo.iv.flash.api.*; 55 import org.openlaszlo.iv.flash.api.image.*; 56 import org.openlaszlo.iv.flash.api.shape.*; 57 import org.openlaszlo.iv.flash.api.text.*; 58 import org.openlaszlo.iv.flash.util.*; 59 import org.openlaszlo.iv.flash.cache.*; 60 import org.openlaszlo.iv.flash.url.*; 61 62 import org.openlaszlo.iv.flash.fop.*; 63 import org.openlaszlo.iv.flash.context.Context; 64 import java.io.*; 65 import java.util.*; 66 import java.awt.geom.*; 67 68 73 74 public class FOPCommand extends GenericCommand 75 { 76 public FOPCommand() {} 77 78 public void doCommand( FlashFile file, Context context, Script parent, int frame ) throws IVException 79 { 80 String filename = getParameter( context, "filename" ); 81 boolean cache = getBoolParameter( context, "cache", false ); 82 boolean scale = getBoolParameter( context, "scale", true ); 83 boolean maintainAspectRatio = getBoolParameter( context, "maintainAspectRatio", true ); 84 String linkHandler = getParameter( context, "linkhandler", null ); 85 String instancename = getParameter( context, "instancename" ); 86 87 Instance inst = getInstance(); 88 89 if( filename == null ) 90 { 91 throw new IVException( Resource.NOTEXT ); 92 } 93 94 String text = null; 95 96 IVUrl url = IVUrl.newUrl( filename, file ); 97 98 100 if( cache ) 101 { 102 text = ( String ) MediaCache.getMedia( url ); 103 } 104 105 if( text == null ) 106 { 107 InputStream is = null; 108 109 try 110 { 111 is = url.getInputStream(); 112 BufferedReader reader = new BufferedReader( new InputStreamReader(is) ); 113 StringBuffer sb = new StringBuffer (100); 114 String s = reader.readLine(); 115 116 while( s != null ) 117 { 118 sb.append( s ); 119 sb.append( '\n' ); 120 s = reader.readLine(); 121 } 122 123 text = sb.toString(); 124 125 } 126 catch( IOException e ) 127 { 128 throw new IVException(Resource.ERRCMDFILEREAD, new Object [] {url.getName(), getCommandName()}, e); 129 } 130 finally 131 { 132 try 133 { 134 if ( is != null ) is.close(); 135 } 136 catch( IOException e ) {} 137 } 138 139 MediaCache.addMedia( url, text, text.length(), cache ); 140 } 141 142 try 143 { 144 FOPHelper fopHelper = new FOPHelper(); 145 146 fopHelper.setXSLFOInputSource( text ); 147 148 if ( linkHandler != null ) 149 { 150 fopHelper.setLinkHandler( linkHandler ); 151 } 152 153 fopHelper.render(); 154 155 Script script = fopHelper.getScript(); 156 157 inst.setScript( script ); 158 159 if ( scale ) 160 { 161 Rectangle2D rect = fopHelper.getMaxPageBounds(); 162 AffineTransform m = inst.matrix; 163 164 double width = rect.getWidth(); 165 double height = rect.getHeight(); 166 167 double widthFactor = ( m.getScaleX() / width ); 168 double heightFactor = ( m.getScaleY() / height ); 169 170 if ( maintainAspectRatio ) 171 { 172 if ( widthFactor < heightFactor ) 173 { 174 heightFactor = widthFactor; 175 } 176 else 177 { 178 widthFactor = heightFactor; 179 } 180 } 181 182 double scaleX = 2048.0 * widthFactor; 183 double scaleY = 2048.0 * heightFactor; 184 inst.matrix = new AffineTransform( scaleX, 0, 0, scaleY, 185 m.getTranslateX() - ( scaleX * width ) / 2, 186 m.getTranslateY() - ( scaleY * height ) / 2 187 ); 188 189 } 190 } 191 catch ( Exception e ) 192 { 193 throw new IVException( e ); 194 } 195 196 if ( instancename != null ) 197 { 198 inst.name = instancename; 199 } 200 } 201 } 202 203 | Popular Tags |