You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by ph...@apache.org on 2005/09/25 19:32:31 UTC

svn commit: r291449 - in /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf: RTFHandler.java rtflib/rtfdoc/RtfExternalGraphic.java rtflib/tools/ImageConstants.java

Author: pherweg
Date: Sun Sep 25 10:32:24 2005
New Revision: 291449

URL: http://svn.apache.org/viewcvs?rev=291449&view=rev
Log:
added support for external-graphic (width and height attributes should be specified)

Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/RTFHandler.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageConstants.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/RTFHandler.java
URL: http://svn.apache.org/viewcvs/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/RTFHandler.java?rev=291449&r1=291448&r2=291449&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/RTFHandler.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/RTFHandler.java Sun Sep 25 10:32:24 2005
@@ -1170,6 +1170,10 @@
             } else {
                 endStatic();
             }
+        } else if (foNode instanceof ExternalGraphic) {
+            if (bStart) {
+                image( (ExternalGraphic) foNode );
+            }
         } else if (foNode instanceof Block) {
             if (bStart) {
                 startBlock( (Block) foNode);

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java
URL: http://svn.apache.org/viewcvs/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java?rev=291449&r1=291448&r2=291449&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfExternalGraphic.java Sun Sep 25 10:32:24 2005
@@ -67,7 +67,156 @@
             super(reason);
         }
     }
+    
+    //////////////////////////////////////////////////
+    // Supported Formats
+    //////////////////////////////////////////////////
+    private static class FormatBase {
+
+        /**
+         * Determines whether the image is in the according format.
+         *
+         * @param data Image
+         *
+         * @return
+         * true    If according type\n
+         * false   Other type
+         */
+        public static boolean isFormat(byte[] data) {
+            return false;
+        }
+        
+        /**
+         * Convert image data if necessary - for example when format is not supported by rtf.
+         *
+         * @param data Image
+         * @param type Format type
+         */
+        public FormatBase convert(FormatBase format, byte[] data) {
+            return format;
+        }
+        
+        /**
+         * Determine image file format.
+         *
+         * @param data Image
+         *
+         * @return Image format class
+         */
+
+        public static FormatBase determineFormat(byte[] data) {
+            int type = ImageConstants.I_NOT_SUPPORTED;
 
+            if (FormatPNG.isFormat(data)) {
+                return new FormatPNG();
+            } else if (FormatJPG.isFormat(data)) {
+                return new FormatJPG();
+            } else if (FormatEMF.isFormat(data)) {
+                return new FormatEMF();
+            } else if (FormatGIF.isFormat(data)) {
+                return new FormatGIF();
+            } else if (FormatBMP.isFormat(data)) {
+                return new FormatBMP();
+            } else {
+                return null;
+            }
+        }
+        
+        /**
+         * Get image type.
+         *
+         * @return Image format class
+         */
+        public int getType() {
+            return ImageConstants.I_NOT_SUPPORTED;
+        }
+        
+        /**
+         * Get rtf tag.
+         *
+         * @return Rtf tag for image format.
+         */
+        public String getRtfTag() {
+            return "";
+        }
+    }
+    
+    private static class FormatGIF extends FormatBase {
+        public static boolean isFormat(byte[] data) {
+            // Indentifier "GIF8" on position 0
+            byte [] pattern = new byte [] {(byte) 0x47, (byte) 0x49, (byte) 0x46, (byte) 0x38};
+
+            return ImageUtil.compareHexValues(pattern, data, 0, true);
+        }
+        
+        public int getType() {
+            return ImageConstants.I_GIF;
+        }
+    }
+    
+    private static class FormatEMF extends FormatBase {
+        public static boolean isFormat(byte[] data) {
+            // No offical Indentifier known
+            byte [] pattern = new byte [] {(byte) 0x01, (byte) 0x00, (byte) 0x00};
+
+            return ImageUtil.compareHexValues(pattern, data, 0, true);
+        }
+        
+        public int getType() {
+            return ImageConstants.I_EMF;
+        }
+        
+        public String getRtfTag() {
+            return "emfblip";
+        }
+    }
+    
+    private  static class FormatBMP extends FormatBase {
+        public static boolean isFormat(byte[] data) {
+            byte [] pattern = new byte [] {(byte) 0x42, (byte) 0x4D};
+
+            return ImageUtil.compareHexValues(pattern, data, 0, true);
+        }
+        
+        public int getType() {
+            return ImageConstants.I_BMP;
+        }
+    }
+    
+    private static class FormatJPG extends FormatBase {
+        public static boolean isFormat(byte[] data) {
+            // Indentifier "0xFFD8" on position 0
+            byte [] pattern = new byte [] {(byte) 0xFF, (byte) 0xD8};
+
+            return ImageUtil.compareHexValues(pattern, data, 0, true);
+        }
+      
+        public int getType() {
+            return ImageConstants.I_JPG;
+        }
+        
+        public String getRtfTag() {
+            return "jpegblip";
+        }
+    }
+    
+    private static class FormatPNG extends FormatBase {
+        public static boolean isFormat(byte[] data) {
+            // Indentifier "PNG" on position 1
+            byte [] pattern = new byte [] {(byte) 0x50, (byte) 0x4E, (byte) 0x47};
+
+            return ImageUtil.compareHexValues(pattern, data, 1, true);
+        }
+        
+        public int getType() {
+            return ImageConstants.I_PNG;
+        }
+        
+        public String getRtfTag() {
+            return "pngblip";
+        }
+    }
+    
     //////////////////////////////////////////////////
     // @@ Members
     //////////////////////////////////////////////////
@@ -131,8 +280,8 @@
      /** The image data */
      private byte[] imagedata = null;
 
-     /** The image type */
-     private int imagetype;
+     /** The image format */
+     FormatBase imageformat;
 
     //////////////////////////////////////////////////
     // @@ Construction
@@ -232,44 +381,17 @@
 
         // Determine image file format
         String file = url.getFile ();
-        imagetype = determineImageType(imagedata, file.substring(file.lastIndexOf(".") + 1));
-
-        if (imagetype >= ImageConstants.I_TO_CONVERT_BASIS) {
-            // convert
-            int to = ImageConstants.CONVERT_TO[imagetype - ImageConstants.I_TO_CONVERT_BASIS];
-
-//            if (to == ImageConstants.I_JPG) {
-//                ByteArrayOutputStream out = null;
-//                try {
-//                    //convert to jpeg
-//                    out = new ByteArrayOutputStream();
-//                    Encoder jpgEncoder = new Encoder(graphicCompressionRate, out);
-//                    jpgEncoder.encodeJPEG(imagedata);
-//                    imagedata = out.toByteArray();
-//                    type = to;
-//                }
-//                catch (JPEGException e) {
-//                    e.setMessage("Image from tag <fo:external-graphic> could "
-//                            + "not be created (src = '" + url + "'");
-//                }
-//                finally {
-//                    out.close();
-//                }
-//            } else {
-                imagetype = ImageConstants.I_NOT_SUPPORTED;
-//            }
-        }
-
-
-        if (imagetype == ImageConstants.I_NOT_SUPPORTED) {
+        imageformat = FormatBase.determineFormat(imagedata);
+        imageformat = imageformat.convert(imageformat, imagedata);
+        
+        if (imageformat.getType() == ImageConstants.I_NOT_SUPPORTED
+                | imageformat.getRtfTag() == "") {
             throw new ExternalGraphicException("The tag <fo:external-graphic> "
                     + "does not support "
                     + file.substring(file.lastIndexOf(".") + 1)
                     + " - image type.");
         }
 
-        String rtfImageCode = ImageConstants.RTF_TAGS[imagetype];
-
         // Writes the beginning of the rtf image
 
         writeGroupMark(true);
@@ -279,7 +401,7 @@
 
         StringBuffer buf = new StringBuffer(imagedata.length * 3);
 
-        writeControlWord(rtfImageCode);
+        writeControlWord(imageformat.getRtfTag());
 
         computeImageSize();
         writeSizeInfo();
@@ -313,10 +435,10 @@
     }
 
     private void computeImageSize () {
-        if (imagetype == ImageConstants.I_PNG) {
+        if (imageformat.getType() == ImageConstants.I_PNG) {
             width = ImageUtil.getIntFromByteArray(imagedata, 16, 4, true);
             height = ImageUtil.getIntFromByteArray(imagedata, 20, 4, true);
-        } else if (imagetype == ImageConstants.I_JPG) {
+        } else if (imageformat.getType() == ImageConstants.I_JPG) {
             int basis = -1;
             byte ff = (byte) 0xff;
             byte c0 = (byte) 0xc0;
@@ -340,7 +462,7 @@
                 width = ImageUtil.getIntFromByteArray(imagedata, basis + 2, 2, true);
                 height = ImageUtil.getIntFromByteArray(imagedata, basis, 2, true);
             }
-        } else if (imagetype == ImageConstants.I_EMF) {
+        } else if (imageformat.getType() == ImageConstants.I_EMF) {
             width = ImageUtil.getIntFromByteArray(imagedata, 151, 4, false);
             height = ImageUtil.getIntFromByteArray(imagedata, 155, 4, false);
         }
@@ -474,116 +596,6 @@
     //////////////////////////////////////////////////
     // @@ Helpers
     //////////////////////////////////////////////////
-
-
-    /**
-     * Determines wheter the image is a jpeg.
-     *
-     * @param imagedata Image
-     *
-     * @return
-     * true    If JPEG type\n
-     * false   Other type
-     */
-    private boolean isJPEG(byte[] data) {
-        // Indentifier "0xFFD8" on position 0
-        byte [] pattern = new byte [] {(byte) 0xFF, (byte) 0xD8};
-
-        return ImageUtil.compareHexValues(pattern, data, 0, true);
-    }
-
-    /**
-     * Determines wheter the image is a png.
-     *
-     * @param data Image
-     *
-     * @return
-     * true    If PNG type\n
-     * false   Other type
-     */
-    private boolean isPNG(byte[] data) {
-        // Indentifier "PNG" on position 1
-        byte [] pattern = new byte [] {(byte) 0x50, (byte) 0x4E, (byte) 0x47};
-
-        return ImageUtil.compareHexValues(pattern, data, 1, true);
-    }
-
-    /**
-     * Determines wheter the image is a emf.
-     *
-     * @param data Image
-     *
-     * @return
-     * true    If EMF type\n
-     * false   Other type
-     */
-    private boolean isEMF(byte[] data) {
-        // No offical Indentifier known
-        byte [] pattern = new byte [] {(byte) 0x01, (byte) 0x00, (byte) 0x00};
-
-        return ImageUtil.compareHexValues(pattern, data, 0, true);
-    }
-
-    /**
-     * Determines wheter the image is a gif.
-     *
-     * @param data Image
-     *
-     * @return
-     * true    If GIF type\n
-     * false   Other type
-     */
-    private boolean isGIF(byte[] data) {
-        // Indentifier "GIF8" on position 0
-        byte [] pattern = new byte [] {(byte) 0x47, (byte) 0x49, (byte) 0x46, (byte) 0x38};
-
-        return ImageUtil.compareHexValues(pattern, data, 0, true);
-    }
-
-    /**
-     * Determines wheter the image is a gif.
-     *
-     * @param data Image
-     *
-     * @return
-     * true    If BMP type\n
-     * false   Other type
-     */
-    private boolean isBMP(byte[] data) {
-        // Indentifier "BM" on position 0
-        byte [] pattern = new byte [] {(byte) 0x42, (byte) 0x4D};
-
-        return ImageUtil.compareHexValues(pattern, data, 0, true);
-    }
-
-    /**
-     * Determine image file format.
-     *
-     * @param data Image
-     * @param ext Image extension
-     *
-     * @return Image type by ImageConstants.java
-     */
-    private int determineImageType(byte [] data, String ext) {
-        int type = ImageConstants.I_NOT_SUPPORTED;
-
-        if (isPNG(data)) {
-            type = ImageConstants.I_PNG;
-        } else if (isJPEG(data)) {
-            type = ImageConstants.I_JPG_C;
-        } else if (isEMF(data)) {
-            type = ImageConstants.I_EMF;
-        } else if (isGIF(data)) {
-            type = ImageConstants.I_GIF;
-        } else {
-            Object tmp = ImageConstants.SUPPORTED_IMAGE_TYPES.get(ext.toLowerCase());
-            if (tmp != null) {
-                type = ((Integer) tmp).intValue();
-            }
-        }
-
-        return type;
-    }
 
     /**
      * @return true if this element would generate no "useful" RTF content

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageConstants.java
URL: http://svn.apache.org/viewcvs/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageConstants.java?rev=291449&r1=291448&r2=291449&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageConstants.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/rtf/rtflib/tools/ImageConstants.java Sun Sep 25 10:32:24 2005
@@ -26,7 +26,6 @@
 
 package org.apache.fop.render.rtf.rtflib.tools;
 
-import java.util.Hashtable;
 
 /** Here will be defined all supported image formats.
  *  This class belongs to the <fo:external-graphic> tag processing.
@@ -46,26 +45,14 @@
     public static final int I_PNG = 1;
     /** Integer equivalent for JPG */
     public static final int I_JPG = 2;
+    /** Integer equivalent for BMP */
+    public static final int I_BMP = 3;
 
-    /** Defines the RTF properties */
-    public static final String [] RTF_TAGS = new String []
-        {
-            "emfblip", "pngblip", "jpegblip"
-        };
-
-    /** constant for image conversion basis (??) */
-    public static final int I_TO_CONVERT_BASIS = 50;
     /** Integer equivalent for GIF */
     public static final int I_GIF = 50;
     /** Integer equivalent for JPEG C (??) */
     public static final int I_JPG_C = 51;
 
-    /** Defines the types for converting rtf supported image types */
-    public static final int [] CONVERT_TO = new int []
-        {
-            I_JPG, I_JPG
-        };
-
     /** EMF file extension */
     public static final String EMF_EXT = "emf";
     /** PNG file extension */
@@ -76,16 +63,6 @@
     public static final String JPEG_EXT = "jpeg";
     /** GIF file extension */
     public static final String GIF_EXT = "gif";
-
-    /** Defines the file extensions and the RTF property belongs to */
-    public static final Hashtable SUPPORTED_IMAGE_TYPES = new Hashtable ();
-    static {
-        SUPPORTED_IMAGE_TYPES.put (EMF_EXT, new Integer (I_EMF));
-        SUPPORTED_IMAGE_TYPES.put (PNG_EXT, new Integer (I_PNG));
-        SUPPORTED_IMAGE_TYPES.put (JPG_EXT, new Integer (I_JPG_C));
-        SUPPORTED_IMAGE_TYPES.put (JPEG_EXT, new Integer (I_JPG_C));
-        SUPPORTED_IMAGE_TYPES.put (GIF_EXT, new Integer (I_GIF));
-    }
 
     //////////////////////////////////////////////////
     // @@ Construction



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org