All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.opensagres.odfdom.converter.pdf.internal.StyleEngineForIText Maven / Gradle / Ivy

There is a newer version: 2.0.6
Show newest version
/**
 * Copyright (C) 2011-2015 The XDocReport Team 
 *
 * All rights reserved.
 *
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package fr.opensagres.odfdom.converter.pdf.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.dom.attribute.fo.FoBreakBeforeAttribute;
import org.odftoolkit.odfdom.dom.attribute.fo.FoFontStyleAttribute;
import org.odftoolkit.odfdom.dom.attribute.fo.FoFontWeightAttribute;
import org.odftoolkit.odfdom.dom.attribute.fo.FoKeepTogetherAttribute;
import org.odftoolkit.odfdom.dom.attribute.fo.FoTextAlignAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleFontStyleAsianAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleFontStyleComplexAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleFontWeightAsianAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleFontWeightComplexAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleNumFormatAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StylePrintOrientationAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleTextLineThroughStyleAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleTextUnderlineStyleAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleTypeAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleVerticalAlignAttribute;
import org.odftoolkit.odfdom.dom.attribute.style.StyleWrapAttribute;
import org.odftoolkit.odfdom.dom.attribute.table.TableAlignAttribute;
import org.odftoolkit.odfdom.dom.element.OdfStyleBase;
import org.odftoolkit.odfdom.dom.element.office.OfficeAutomaticStylesElement;
import org.odftoolkit.odfdom.dom.element.office.OfficeMasterStylesElement;
import org.odftoolkit.odfdom.dom.element.office.OfficeStylesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleBackgroundImageElement;
import org.odftoolkit.odfdom.dom.element.style.StyleColumnElement;
import org.odftoolkit.odfdom.dom.element.style.StyleColumnsElement;
import org.odftoolkit.odfdom.dom.element.style.StyleDefaultStyleElement;
import org.odftoolkit.odfdom.dom.element.style.StyleFooterStyleElement;
import org.odftoolkit.odfdom.dom.element.style.StyleGraphicPropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleHeaderFooterPropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleListLevelLabelAlignmentElement;
import org.odftoolkit.odfdom.dom.element.style.StyleListLevelPropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StylePageLayoutElement;
import org.odftoolkit.odfdom.dom.element.style.StylePageLayoutPropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleParagraphPropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleSectionPropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleStyleElement;
import org.odftoolkit.odfdom.dom.element.style.StyleTabStopElement;
import org.odftoolkit.odfdom.dom.element.style.StyleTabStopsElement;
import org.odftoolkit.odfdom.dom.element.style.StyleTableCellPropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleTablePropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleTableRowPropertiesElement;
import org.odftoolkit.odfdom.dom.element.style.StyleTextPropertiesElement;
import org.odftoolkit.odfdom.dom.element.text.TextListLevelStyleBulletElement;
import org.odftoolkit.odfdom.dom.element.text.TextListLevelStyleImageElement;
import org.odftoolkit.odfdom.dom.element.text.TextListLevelStyleNumberElement;
import org.odftoolkit.odfdom.dom.element.text.TextListStyleElement;
import org.odftoolkit.odfdom.dom.element.text.TextOutlineLevelStyleElement;
import org.odftoolkit.odfdom.dom.element.text.TextOutlineStyleElement;
import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
import org.odftoolkit.odfdom.pkg.OdfElement;
import org.w3c.dom.Node;

import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;

import fr.opensagres.odfdom.converter.core.AbstractStyleEngine;
import fr.opensagres.odfdom.converter.core.utils.ODFUtils;
import fr.opensagres.odfdom.converter.pdf.PdfOptions;
import fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableImage;
import fr.opensagres.odfdom.converter.pdf.internal.styles.Style;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleBorder;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleBreak;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleColumnProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleGraphicProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleHeaderFooterProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleLineHeight;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleListProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleNumFormat;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StylePageLayoutProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleParagraphProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleSectionProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleTabStopProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleTableCellProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleTableProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleTableRowProperties;
import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleTextProperties;
import fr.opensagres.xdocreport.itext.extension.PageOrientation;
import fr.opensagres.xdocreport.utils.BorderType;
import fr.opensagres.xdocreport.utils.StringUtils;

/**
 * fixes for pdf conversion by Leszek Piotrowicz 
 */
public class StyleEngineForIText
    extends AbstractStyleEngine
{
    private static final String OUTLINE = "outline";

    private Style currentStyle = null;

    private Map currentListPropertiesMap;

    private StyleListProperties currentListProperties;

    private List currentTabStopPropertiesList;

    private List currentColumnPropertiesList;

    private BackgroundImage backgroundImage = null;

    private final PdfOptions options;

    private final Map stylesMap = new HashMap();

    public StyleEngineForIText( OdfDocument odfDocument, PdfOptions options )
    {
        super(odfDocument);
        this.options = options != null ? options : PdfOptions.getDefault();
    }

    public BackgroundImage getBackgroundImage() {
    	return backgroundImage;
    }

    public void visit(StyleBackgroundImageElement ele) {

    	String href = ele.getXlinkHrefAttribute();
        if (StringUtils.isNotEmpty(href)) {
            byte[] imageStream = odfDocument.getPackage().getBytes(href);
	         try {
		        if (imageStream != null) {
		        	BackgroundImage.Builder builder = new BackgroundImage.Builder(imageStream);
		            Node parentNode = ele.getParentNode();
		            builder.setRepeat(BackgroundImage.Repeat.fromODT(ele.getStyleRepeatAttribute()));
		            builder.setPosition(BackgroundImage.Position.fromODT(ele.getStylePositionAttribute()));
		            if (parentNode instanceof StylePageLayoutPropertiesElement) {
		            	StylePageLayoutPropertiesElement layout = (StylePageLayoutPropertiesElement) parentNode;
		                String svgWidth = layout.getFoPageWidthAttribute();
		                if (StringUtils.isNotEmpty(svgWidth)) builder.setPageWidth(ODFUtils.getDimensionAsPoint(svgWidth));
		                String svgHeight = layout.getFoPageHeightAttribute();
		                if (StringUtils.isNotEmpty(svgHeight)) builder.setPageHeight(ODFUtils.getDimensionAsPoint(svgHeight));
		                String leftMargin = layout.getFoMarginLeftAttribute();
	                	if (StringUtils.isNotEmpty(leftMargin)) builder.setLeftMargin(ODFUtils.getDimensionAsPoint(leftMargin));
	                	String rightMargin = layout.getFoMarginRightAttribute();
	                	if (StringUtils.isNotEmpty(rightMargin)) builder.setRightMargin(ODFUtils.getDimensionAsPoint(rightMargin));
	                	String topMargin = layout.getFoMarginTopAttribute();
	                	if (StringUtils.isNotEmpty(topMargin)) builder.setTopMargin(ODFUtils.getDimensionAsPoint(topMargin));
	                	String bottomMargin = layout.getFoMarginBottomAttribute();
	                	if (StringUtils.isNotEmpty(bottomMargin)) builder.setBottomMargin(ODFUtils.getDimensionAsPoint(bottomMargin));
		            }
		            backgroundImage = builder.build();
		        }
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
        }
    }

    public void visit( OfficeStylesElement ele )
    {
        super.visit( ele );
    }

    public void visit( OfficeAutomaticStylesElement ele )
    {
        super.visit( ele );
    }

    public void visit( OfficeMasterStylesElement ele )
    {
        super.visit( ele );
    }

    public void visit( StyleDefaultStyleElement ele )
    {
        computeStyle( ele, true );
    }

    public void visit( StyleStyleElement ele )
    {
        computeStyle( ele, false );
    }

    public void visit( StylePageLayoutElement ele )
    {
        computeStyle( ele, false );
    }

    private Style computeStyle( OdfElement odfElement, boolean isDefaultStyle )
    {
        if ( odfElement == null )
        {
            return null;
        }
        OdfStyleBase styleBase = odfElement instanceof OdfStyleBase ? (OdfStyleBase) odfElement : null;

        // 1) Check if style is in the cache
        String familyName = styleBase != null ? styleBase.getFamilyName() : OUTLINE;
        String styleName = null;
        String masterPageName = null;
        if ( !isDefaultStyle )
        {
            if ( styleBase instanceof StyleStyleElement )
            {
                StyleStyleElement styleElement = (StyleStyleElement) styleBase;
                styleName = styleElement.getStyleNameAttribute();
                masterPageName = styleElement.getStyleMasterPageNameAttribute();
            }
            else if ( styleBase instanceof StylePageLayoutElement )
            {
                styleName = ( (StylePageLayoutElement) styleBase ).getStyleNameAttribute();
            }
            else if ( styleBase instanceof TextListStyleElement )
            {
                styleName = ( (TextListStyleElement) styleBase ).getStyleNameAttribute();
            }
        }

        String styleId = getStyleId( familyName, styleName );
        Style style = stylesMap.get( styleId );
        if ( style != null )
        {
            // style already computed
            return style;
        }

        // 3) Create style
        style = new Style( options.getFontProvider(), styleName, familyName, masterPageName );

        // 4) Apply default style if needed
        // Style defaultStyle = null;
        // if (!isDefaultStyle) {
        // OdfDefaultStyle odfDefaultStyle = odfDocument.getDocumentStyles()
        // .getDefaultStyle(styleBase.getFamily());
        // if (odfDefaultStyle != null) {
        // defaultStyle = computeStyle(odfDefaultStyle, true);
        // }
        // if (defaultStyle != null) {
        // style.merge(defaultStyle);
        // }
        // }

        // 5) Apply Parent style
        List