001    /*
002     * Copyright 2007 - 2009 JEuclid, http://jeuclid.sf.net
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: MathRenderer.java,v d5b416878097 2009/08/28 07:30:22 max $ */
018    
019    package net.sourceforge.jeuclid.swt;
020    
021    import java.awt.image.BufferedImage;
022    import java.awt.image.DataBuffer;
023    import java.awt.image.DataBufferByte;
024    import java.awt.image.Raster;
025    import java.io.IOException;
026    
027    import net.sourceforge.jeuclid.LayoutContext;
028    import net.sourceforge.jeuclid.converter.Converter;
029    
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    import org.eclipse.swt.graphics.ImageData;
033    import org.eclipse.swt.graphics.PaletteData;
034    import org.w3c.dom.Node;
035    
036    /**
037     * Renders MathML to SWT ImageData.
038     * 
039     * @version $Revision: d5b416878097 $
040     */
041    public final class MathRenderer {
042    
043        private static final int COLOR_ENTRIES = 3;
044    
045        private static final int BITS_PER_PIXEL = MathRenderer.COLOR_ENTRIES * 8;
046    
047        private static final PaletteData PALETTE_BGR = new PaletteData(0xff,
048                0xff00, 0xff0000);
049    
050        private static final class SingletonHolder {
051            private static final MathRenderer INSTANCE = new MathRenderer();
052    
053            private SingletonHolder() {
054            }
055        }
056    
057        /**
058         * Logger for this class
059         */
060        private static final Log LOGGER = LogFactory.getLog(MathRenderer.class);
061    
062        private final Converter converter = Converter.getInstance();
063    
064        /**
065         * Default constructor.
066         */
067        private MathRenderer() {
068            // Empty on purpose.
069        }
070    
071        /**
072         * Retrieve an instance of the converter singleton class.
073         * 
074         * @return a Converter object.
075         */
076        public static MathRenderer getInstance() {
077            return MathRenderer.SingletonHolder.INSTANCE;
078        }
079    
080        /**
081         * Renders MathML into ImageData.
082         * 
083         * @param document
084         *            The MathML Document to render
085         * @param layoutContext
086         *            LayoutContext to use
087         * @return an ImageData instance or null if an error occurred.
088         */
089        public ImageData render(final Node document,
090                final LayoutContext layoutContext) {
091            ImageData renderedFormula;
092            if (document == null) {
093                renderedFormula = null;
094            } else {
095                try {
096                    final BufferedImage bi = this.converter.render(document,
097                            layoutContext, BufferedImage.TYPE_3BYTE_BGR);
098                    final Raster r = bi.getRaster();
099                    final DataBuffer b = r.getDataBuffer();
100                    final DataBufferByte db = (DataBufferByte) b;
101                    final byte[] data = db.getData();
102                    final int w = bi.getWidth();
103                    renderedFormula = new ImageData(w, bi.getHeight(),
104                            MathRenderer.BITS_PER_PIXEL, MathRenderer.PALETTE_BGR,
105                            MathRenderer.COLOR_ENTRIES * w, data);
106                } catch (IOException io) {
107                    MathRenderer.LOGGER.warn(io.getMessage(), io);
108                    renderedFormula = null;
109                }
110            }
111            return renderedFormula;
112        }
113    
114    }