001: /*
002: * $Id: FOPCommand.java,v 1.3 2002/02/24 02:10:19 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.commands;
052:
053: import org.openlaszlo.iv.flash.parser.*;
054: import org.openlaszlo.iv.flash.api.*;
055: import org.openlaszlo.iv.flash.api.image.*;
056: import org.openlaszlo.iv.flash.api.shape.*;
057: import org.openlaszlo.iv.flash.api.text.*;
058: import org.openlaszlo.iv.flash.util.*;
059: import org.openlaszlo.iv.flash.cache.*;
060: import org.openlaszlo.iv.flash.url.*;
061:
062: import org.openlaszlo.iv.flash.fop.*;
063: import org.openlaszlo.iv.flash.context.Context;
064: import java.io.*;
065: import java.util.*;
066: import java.awt.geom.*;
067:
068: /**
069: * Insert Text generator command<BR>
070: *
071: * @author James Taylor
072: */
073:
074: public class FOPCommand extends GenericCommand {
075: public FOPCommand() {
076: }
077:
078: public void doCommand(FlashFile file, Context context,
079: Script parent, int frame) throws IVException {
080: String filename = getParameter(context, "filename");
081: boolean cache = getBoolParameter(context, "cache", false);
082: boolean scale = getBoolParameter(context, "scale", true);
083: boolean maintainAspectRatio = getBoolParameter(context,
084: "maintainAspectRatio", true);
085: String linkHandler = getParameter(context, "linkhandler", null);
086: String instancename = getParameter(context, "instancename");
087:
088: Instance inst = getInstance();
089:
090: if (filename == null) {
091: throw new IVException(Resource.NOTEXT);
092: }
093:
094: String text = null;
095:
096: IVUrl url = IVUrl.newUrl(filename, file);
097:
098: // check in cache first
099:
100: if (cache) {
101: text = (String) MediaCache.getMedia(url);
102: }
103:
104: if (text == null) {
105: InputStream is = null;
106:
107: try {
108: is = url.getInputStream();
109: BufferedReader reader = new BufferedReader(
110: new InputStreamReader(is));
111: StringBuffer sb = new StringBuffer(100);
112: String s = reader.readLine();
113:
114: while (s != null) {
115: sb.append(s);
116: sb.append('\n');
117: s = reader.readLine();
118: }
119:
120: text = sb.toString();
121:
122: } catch (IOException e) {
123: throw new IVException(
124: Resource.ERRCMDFILEREAD,
125: new Object[] { url.getName(), getCommandName() },
126: e);
127: } finally {
128: try {
129: if (is != null)
130: is.close();
131: } catch (IOException e) {
132: }
133: }
134:
135: MediaCache.addMedia(url, text, text.length(), cache);
136: }
137:
138: try {
139: FOPHelper fopHelper = new FOPHelper();
140:
141: fopHelper.setXSLFOInputSource(text);
142:
143: if (linkHandler != null) {
144: fopHelper.setLinkHandler(linkHandler);
145: }
146:
147: fopHelper.render();
148:
149: Script script = fopHelper.getScript();
150:
151: inst.setScript(script);
152:
153: if (scale) {
154: Rectangle2D rect = fopHelper.getMaxPageBounds();
155: AffineTransform m = inst.matrix;
156:
157: double width = rect.getWidth();
158: double height = rect.getHeight();
159:
160: double widthFactor = (m.getScaleX() / width);
161: double heightFactor = (m.getScaleY() / height);
162:
163: if (maintainAspectRatio) {
164: if (widthFactor < heightFactor) {
165: heightFactor = widthFactor;
166: } else {
167: widthFactor = heightFactor;
168: }
169: }
170:
171: double scaleX = 2048.0 * widthFactor;
172: double scaleY = 2048.0 * heightFactor;
173: inst.matrix = new AffineTransform(scaleX, 0, 0, scaleY,
174: m.getTranslateX() - (scaleX * width) / 2, m
175: .getTranslateY()
176: - (scaleY * height) / 2);
177:
178: }
179: } catch (Exception e) {
180: throw new IVException(e);
181: }
182:
183: if (instancename != null) {
184: inst.name = instancename;
185: }
186: }
187: }
|