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

org.pptx4j.model.TextStyles Maven / Gradle / Ivy

/*
 *  Copyright 2010, Plutext Pty Ltd.
 *   
 *  This file is part of docx4j.

    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 

    You may obtain a copy of the License at 

        http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License.

 */

package org.pptx4j.model;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.docx4j.XmlUtils;
import org.docx4j.dml.BaseStyles.FontScheme;
import org.docx4j.dml.CTTextCharacterProperties;
import org.docx4j.dml.CTTextListStyle;
import org.docx4j.dml.CTTextParagraphProperties;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.PresentationMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.ThemePart;
import org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart;
import org.docx4j.openpackaging.parts.PresentationML.SlideMasterPart;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.DocDefaults;
import org.docx4j.wml.DocDefaults.RPrDefault;
import org.docx4j.wml.HpsMeasure;
import org.docx4j.wml.Jc;
import org.docx4j.wml.JcEnumeration;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.PPr;
import org.docx4j.wml.RFonts;
import org.docx4j.wml.RPr;
import org.docx4j.wml.Style;
import org.docx4j.wml.Style.Name;
import org.docx4j.wml.U;
import org.docx4j.wml.UnderlineEnumeration;
import org.pptx4j.pml.CTSlideMasterTextStyles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TextStyles {
	
	private final static Logger log = LoggerFactory.getLogger(TextStyles.class);
	
	
	/*
	 * Given:
	 * 
	 *  [insert example]
	 * 
	 * we're aiming to create something like:
	 * 
//			
//				
//				
//				
//				
//				
//				
//				
//					
//					
//					
//						
//					
//					
//					
//				
//				
//					
//					
//					
//					
//					
//					
//				
//			
	 * 
	 * We'll also do something like 
	 * StyleDefinitionsPart.createVirtualStylesForDocDefaults()
	 * 
	 */
	
	public static Style convertToWordStyle(CTTextParagraphProperties lvlPPr, 
			String id, String name,
			String basedOn,
			FontScheme fontScheme) {
		
		ObjectFactory factory = Context.getWmlObjectFactory();
		Style style = factory.createStyle();
		
//		
		style.setType("paragraph");
		style.setStyleId(id);
		
		System.out.println("created " + id);
		
//		
		Name styleName = factory.createStyleName();
		styleName.setVal(name);
		style.setName(styleName);
		
//		
		Style.BasedOn basedon = factory.createStyleBasedOn();
		basedon.setVal(basedOn);
		style.setBasedOn(basedon);
		
//		
		if (lvlPPr ==null) {
			log.warn("Empty style: " + id);
			if (log.isDebugEnabled() ) {
				
				log.debug( XmlUtils.marshaltoString(lvlPPr, true, true, Context.jc, 
						"URI", "lvl1pPr", // BEWARE: could be lvl2 etc
						CTTextParagraphProperties.class));
				
				log.debug("Converted to: " +  XmlUtils.marshaltoString(style, true, true));
			}
			return style; 
		}
		
		style.setPPr(
				getWmlPPr(lvlPPr) );
		
		style.setRPr(
				getWmlRPr(lvlPPr, fontScheme) );
				
		
		
		if (log.isDebugEnabled() ) {
			
			log.debug( XmlUtils.marshaltoString(lvlPPr, true, true, Context.jc, 
					"URI", "lvl1pPr", 
					CTTextParagraphProperties.class));
			
			log.debug("Converted to: " +  XmlUtils.marshaltoString(style, true, true));
		}
		
		return style;
	}
	
	public static PPr getWmlPPr(CTTextParagraphProperties lvlPPr ) {
		
		ObjectFactory factory = Context.getWmlObjectFactory();
		PPr pPr = factory.createPPr();		
		
//		*marL eg 342900 EMU
//		*indent eg -342900
		
//		*algn l|ctr|r
		if (lvlPPr.getAlgn()!=null) {
			Jc jc = factory.createJc();
			String algn = lvlPPr.getAlgn().value();
			log.debug("algn: " + algn);
			if (  algn.equals("l")) {
				jc.setVal(JcEnumeration.LEFT);
			} else if (algn.equals("ctr")) {
				jc.setVal(JcEnumeration.CENTER);
			} else if (algn.equals("r")) {
				jc.setVal(JcEnumeration.RIGHT);
	//		} else if (value.getCssText().toLowerCase().equals("justify")) {
	//			jc.setVal(JcEnumeration.BOTH);
			} else {
				log.warn("How to handle algn: " + algn);
			}		
			pPr.setJc( jc );
		}
		
//		defTabSz eg 914400 EMU = 1 inch
//		hangingPunct?

		// spcBef %
//		
			
	//		*buFont, buChar
//	    
//	    

		return pPr;
	}
	
	public static RPr getWmlRPr(CTTextParagraphProperties lvlPPr,
		FontScheme fontScheme) {
		
		ObjectFactory factory = Context.getWmlObjectFactory();

//		
		RPr rPr = factory.createRPr();

		if (lvlPPr.getDefRPr()!=null) {  
		
//		*sz hundreds of pt
//		
			if (lvlPPr.getDefRPr().getSz()!=null) {				
				rPr.setSz( convertFontSize(lvlPPr.getDefRPr().getSz()) );
			}
			
//		kern
//		solidFill
		
//		*latin				
//		
			if (lvlPPr.getDefRPr().getLatin()!=null ) {
				RFonts rFonts = factory.createRFonts();
				if (lvlPPr.getDefRPr().getLatin().getTypeface().startsWith("+mj") ) {					
					rFonts.setAscii( fontScheme.getMajorFont().getLatin().getTypeface() );
				} else if (lvlPPr.getDefRPr().getLatin().getTypeface().startsWith("+mn") ) {					
					rFonts.setAscii( fontScheme.getMinorFont().getLatin().getTypeface() );
				}  
				rPr.setRFonts(rFonts);
			}			
		}
	
		return rPr;
	}	
	
	private static HpsMeasure convertFontSize(Integer in) {
		ObjectFactory factory = Context.getWmlObjectFactory();
		HpsMeasure sz = factory.createHpsMeasure();
		int halfPts = Math.round(in/50); 
		sz.setVal( BigInteger.valueOf(halfPts) );
		return sz;
	}
	
	// From Main Presentation Part
	public static List