001 /*
002 [Adapted from BSD licence]
003 Copyright (c) 2002 Terence Parr
004 All rights reserved.
005
006 Redistribution and use in source and binary forms, with or without
007 modification, are permitted provided that the following conditions
008 are met:
009 1. Redistributions of source code must retain the above copyright
010 notice, this list of conditions and the following disclaimer.
011 2. Redistributions in binary form must reproduce the above copyright
012 notice, this list of conditions and the following disclaimer in the
013 documentation and/or other materials provided with the distribution.
014 3. The name of the author may not be used to endorse or promote products
015 derived from this software without specific prior written permission.
016
017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package gate.wiki.antlr.plugin;
029
030 import gate.wiki.antlr.*;
031
032 import java.util.Vector;
033 import java.util.regex.Pattern;
034
035 /**
036 * @author tamara
037 * The image class takes input in the form (filename, width, height).
038 * Height and width are in percentage proportion with the textwidth.
039 * This preserves the relationship in LaTeX as well as HTML.
040 */
041 public class image implements YAMPlugin {
042
043
044 public String translate(YAMContext context, Vector args) {
045 if ( args.size()<1 ) {
046 System.err.println("Missing image name/args; line "+context.getLine());
047 return null;
048 }
049 String imageName = (String)args.elementAt(0);
050 String width = "";
051 String height = "";
052 String ret = "";
053 String lang = context.getTarget().getTargetLanguage();
054
055 if ( args.size()>1 ) {
056 width = (String)args.elementAt(1);
057 if ( args.size()>2 ) {
058 height = (String)args.elementAt(2);
059 }
060 }
061
062 if(lang.equals(Tool.HTML) || lang.equals(Tool.WIKI)) {
063
064 if(width!= "") {
065 String [] w = Pattern.compile("([a-z]+)").split(width, 3);
066 width = w[0];
067 // the 5 compensates for the difference in sizes between
068 // latex cm or pt and pixels
069 // if the input is 10cm then the image will be 510
070 // this is a temporary solution it should account for
071 // type of measurment that is passed in as well
072 width = "width = \"" + width + "%\"";
073 }
074 if(height!= "") {
075 String [] w = Pattern.compile("([a-z]+)").split(height, 3);
076 height = w[0];
077 height = "height = \"" + height + "%\"";
078 }
079 ret =
080 "%output(<img align=\"center\" src=\""
081 +imageName+"\" "+width+" "+height+">)";
082 }
083 else if(lang.equals(Tool.LATEX)) {
084 ret = "%output(\\includegraphics";
085 boolean hw = false;
086 String heightwidth="";
087 if(width!= "") {
088 width = width.substring(0,(width.length()-2)) + "." +width.substring(width.length()-2);
089 heightwidth +="width=" + width + "\\textwidth";
090 hw = true;
091 }
092 if(height!=""){
093 if(hw) {
094 heightwidth += ", ";
095 }
096 hw = true;
097 height = height.substring(0,(height.length()-2)) + "." +height.substring(height.length()-2);
098 heightwidth +="height=" + height + "\\textheight";
099 }
100 if(hw) {
101 ret += "[" + heightwidth + "]";
102 }
103 ret += "{"+imageName +"})";
104 }
105 else {
106 // just return file name for anything but HTML for now
107 ret = "[IMAGE " + imageName +"]";
108 }
109
110 // easy in html, just say <img ...>
111 //System.out.println("w,h="+width+"," +height);
112 if ( width.length()>0 ) {
113 width = "width="+width;
114 }
115 if ( height.length()>0 ) {
116 height = "height="+height;
117 }
118 return ret;
119 }
120 }
|