001: /*
002: * $Id: InsertMediaCommand.java,v 1.7 2002/07/22 23:16:04 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 java.io.*;
054: import java.awt.geom.*;
055: import org.openlaszlo.iv.flash.parser.*;
056: import org.openlaszlo.iv.flash.api.*;
057: import org.openlaszlo.iv.flash.api.image.*;
058: import org.openlaszlo.iv.flash.api.shape.*;
059: import org.openlaszlo.iv.flash.util.*;
060: import org.openlaszlo.iv.flash.cache.*;
061: import org.openlaszlo.iv.flash.url.*;
062:
063: import org.openlaszlo.iv.flash.context.Context;
064:
065: /**
066: * Insert Media generator command<BR>
067: * Insert gif, jpg, png, swf or swt<BR>
068: * Detect media type from its data, not from extension<BR>
069: *
070: * <P>if 'scalex' or 'scaley' are not null, then scale width or/and height
071: * of the image accordingly, the placeholder's scaling is ignored
072: * <P>if both 'scalex' and 'scaley' are null then check 'scale'. if it's true
073: * then scale image to the placeholder size, if scale is false then leave the
074: * image as is and ignore the placeholder's scaling
075: *
076: * @author Dmitry Skavish
077: */
078: public class InsertMediaCommand extends GenericCommand {
079:
080: private static final int ALWAYS_SCALE = 0;
081: private static final int NEVER_SCALE = 1;
082: private static final int IFNEEDED_SCALE = 2;
083:
084: public InsertMediaCommand() {
085: }
086:
087: public void doCommand(FlashFile file, Context context,
088: Script parent, int frameNum) throws IVException {
089: String filename = getParameter(context, "filename", "");
090: boolean cache = getBoolParameter(context, "cache", false);
091: boolean expand = getBoolParameter(context, "expand", false);
092: int scaletox = getIntParameter(context, "scalex", -1) * 20;
093: int scaletoy = getIntParameter(context, "scaley", -1) * 20;
094: String halign = getParameter(context, "halign", "left");
095: String valign = getParameter(context, "valign", "top");
096: boolean mask = getBoolParameter(context, "mask", false);
097: boolean scaleproport = getBoolParameter(context,
098: "scaleproport", false);
099: // String export = context.apply( getParameter("export","JPEG") );
100: // String quality = context.apply( getParameter("quality","80") );
101: String instancename = getParameter(context, "instancename");
102:
103: String mypscale = getParameter(context, "scale", "always");
104:
105: boolean neverScale = mypscale.equalsIgnoreCase("never")
106: || mypscale.equalsIgnoreCase("false");
107: boolean ifNeededScale = mypscale.equalsIgnoreCase("if needed");
108: boolean alwaysScale = !neverScale && !ifNeededScale;
109:
110: Instance inst = getInstance();
111:
112: IVUrl url = IVUrl.newUrl(filename, file);
113:
114: Object media = null;
115:
116: try {
117: media = file.addExternalMedia(url, cache);
118: } catch (IOException e) {
119: throw new IVException(Resource.ERRCMDFILEREAD,
120: new Object[] { url.getName(), getCommandName() }, e);
121: }
122:
123: Script script;
124: double width, height;
125:
126: if (media instanceof FlashFile) {
127: // insert flash movie
128: FlashFile flashFile = (FlashFile) media;
129: synchronized (flashFile) {
130: script = flashFile.getMainScript();
131: script.resetMain();
132: if (flashFile.isTemplate()) {
133: script.removeFileDepGlobalCommands();
134: script = script.copyScript();
135: }
136: }
137: if (flashFile.isTemplate())
138: file.processScript(script, context);
139:
140: Rectangle2D r = flashFile.getFrameSize(); // script.getBounds();
141: width = r.getWidth(); // in twixels
142: height = r.getHeight(); // in twixels
143:
144: // expand frames
145: if (expand) {
146: // create new frames if needed
147: int myTotal = script.getFrameCount();
148: parent.getFrameAt(frameNum + myTotal - 1);
149: }
150:
151: } else {
152: // insert image file
153: Bitmap bitmap = (Bitmap) media;
154: Instance myInst = bitmap.newInstance();
155: width = bitmap.getWidth() * 20; // now in twixels
156: height = bitmap.getHeight() * 20; // now in twixels
157:
158: script = inst.copyScript();
159: Frame myFrame = script.newFrame();
160: myFrame.addInstance(myInst, 1);
161: }
162:
163: inst.setScript(script);
164:
165: // add mask if needed
166: if (mask) {
167: addMask(parent, frameNum, inst, GeomHelper.newRectangle(
168: -1024, -1024, 2048, 2048));
169: }
170:
171: Rectangle2D winBounds = GeomHelper.getTransformedSize(
172: inst.matrix, GeomHelper.newRectangle(-1024, -1024,
173: 2048, 2048));
174: int winWidth = (int) winBounds.getWidth();
175: int winHeight = (int) winBounds.getHeight();
176:
177: boolean deScale = true;
178:
179: double scaleX, scaleY, translateX, translateY;
180:
181: scaleX = scaleY = 1.0;
182: if (scaletox >= 0 || scaletoy >= 0) {
183:
184: if (scaletox >= 0) {
185: if (!ifNeededScale || width > scaletox) {
186: scaleX = scaletox / width;
187: width = scaletox;
188: }
189: }
190:
191: if (scaletoy >= 0) {
192: if (!ifNeededScale || height > scaletoy) {
193: scaleY = scaletoy / height;
194: height = scaletoy;
195: }
196: }
197:
198: if (scaleproport) {
199: if (scaletox < 0) {
200: scaleX = scaleY;
201: width = scaleX * width;
202: }
203: if (scaletoy < 0) {
204: scaleY = scaleX;
205: height = scaleY * height;
206: }
207: }
208: } else if (alwaysScale) {
209: if (scaleproport) {
210: scaleX = winWidth / width;
211: scaleY = winHeight / height;
212: if (scaleX < scaleY) {
213: scaleY = scaleX;
214: } else {
215: scaleX = scaleY;
216: }
217: width *= scaleX;
218: height *= scaleY;
219: } else {
220: scaleX = 2048.0 / width;
221: scaleY = 2048.0 / height;
222: deScale = false;
223: }
224: } else if (ifNeededScale) {
225: if (width > winWidth || height > winHeight) {
226: scaleX = winWidth / width;
227: scaleY = winHeight / height;
228: if (scaleproport) {
229: if (scaleX < scaleY) {
230: scaleY = scaleX;
231: } else {
232: scaleX = scaleY;
233: }
234: }
235: width *= scaleX;
236: height *= scaleY;
237: }
238: }
239:
240: if (deScale) {
241:
242: // horizontal alignment
243: if (halign.equalsIgnoreCase("left")) {
244: translateX = -winWidth / 2;
245: } else if (halign.equalsIgnoreCase("right")) {
246: translateX = -width + winWidth / 2;
247: } else { // center
248: translateX = -width / 2;
249: }
250:
251: // vertical alignment
252: if (valign.equalsIgnoreCase("top")) {
253: translateY = -winHeight / 2;
254: } else if (valign.equalsIgnoreCase("bottom")) {
255: translateY = -height + winHeight / 2;
256: } else { // center
257: translateY = -height / 2;
258: }
259:
260: GeomHelper.deScaleMatrix(inst.matrix);
261: } else {
262: translateX = translateY = -1024.0;
263: }
264:
265: inst.matrix.concatenate(new AffineTransform(scaleX, 0, 0,
266: scaleY, translateX, translateY));
267:
268: if (instancename != null) {
269: inst.name = instancename;
270: }
271: }
272:
273: }
|