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

org.odftoolkit.odfdom.converter.pdf.internal.StyleEngineForIText Maven / Gradle / Ivy

/**
 * Copyright (C) 2011-2012 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 org.odftoolkit.odfdom.converter.pdf.internal;

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

import org.odftoolkit.odfdom.converter.core.AbstractStyleEngine;
import org.odftoolkit.odfdom.converter.core.utils.ODFUtils;
import org.odftoolkit.odfdom.converter.pdf.PdfOptions;
import org.odftoolkit.odfdom.converter.pdf.internal.stylable.StylableImage;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.Style;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleBorder;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleBreak;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleColumnProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleGraphicProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleHeaderFooterProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleLineHeight;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleListProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleNumFormat;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StylePageLayoutProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleParagraphProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleSectionProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleTabStopProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleTableCellProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleTableProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleTableRowProperties;
import org.odftoolkit.odfdom.converter.pdf.internal.styles.StyleTextProperties;
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.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.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 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 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