001package com.hfg.xml.msofficexml.docx.wordprocessingml; 002 003 004import com.hfg.graphics.PaperSize; 005import com.hfg.graphics.units.GfxSize2D; 006import com.hfg.graphics.units.Twips; 007import com.hfg.graphics.units.GfxSize; 008import com.hfg.graphics.units.GfxUnits; 009import com.hfg.util.Orientation; 010import com.hfg.util.StringUtil; 011import com.hfg.xml.XMLTag; 012 013public class WmlPageSize extends XMLTag 014{ 015 //--------------------------------------------------------------------------- 016 public WmlPageSize() 017 { 018 super(WmlXML.PAGE_SIZE); 019 } 020 021 //--------------------------------------------------------------------------- 022 public WmlPageSize(PaperSize inSize) 023 { 024 this(); 025 setWidth(inSize.getDimensions().getWidth()); 026 setHeight(inSize.getDimensions().getHeight()); 027 } 028 029 030 //--------------------------------------------------------------------------- 031 public WmlPageSize setHeight(GfxSize inValue) 032 { 033 setAttribute(WmlXML.HEIGHT_ATT, inValue.toInt(GfxUnits.dxa)); 034 return this; 035 } 036 037 //--------------------------------------------------------------------------- 038 public GfxSize getHeight() 039 { 040 String stringValue = getAttributeValue(WmlXML.HEIGHT_ATT); 041 GfxSize height = null; 042 if (StringUtil.isSet(stringValue)) 043 { 044 height = new Twips(Integer.parseInt(stringValue)); 045 } 046 047 return height; 048 } 049 050 //--------------------------------------------------------------------------- 051 public WmlPageSize setWidth(GfxSize inValue) 052 { 053 setAttribute(WmlXML.WIDTH_ATT, inValue.toInt(GfxUnits.dxa)); 054 return this; 055 } 056 057 //--------------------------------------------------------------------------- 058 public GfxSize getWidth() 059 { 060 String stringValue = getAttributeValue(WmlXML.WIDTH_ATT); 061 GfxSize width = null; 062 if (StringUtil.isSet(stringValue)) 063 { 064 width = new Twips(Integer.parseInt(stringValue)); 065 } 066 067 return width; 068 } 069 070 //--------------------------------------------------------------------------- 071 public GfxSize2D getDimensions() 072 { 073 return new GfxSize2D().setWidth(getWidth()).setHeight(getHeight()); 074 } 075 076 //--------------------------------------------------------------------------- 077 public WmlPageSize setOrientation(Orientation inValue) 078 { 079 setAttribute(WmlXML.ORIENTATION_ATT, inValue == Orientation.HORIZONAL ? "landscape" : "portrait"); 080 adjustDimensionsToMatchOrientation(); 081 return this; 082 } 083 084 //--------------------------------------------------------------------------- 085 private void adjustDimensionsToMatchOrientation() 086 { 087 GfxSize width = getWidth(); 088 GfxSize height = getHeight(); 089 090 if (width != null 091 && height != null) 092 { 093 String orientationString = getAttributeValue(WmlXML.ORIENTATION_ATT); 094 Orientation orientation = (null == orientationString || orientationString.equals("portrait") ? Orientation.VERTICAL : Orientation.HORIZONAL); 095 096 // Should we swap the width and height to match the orientation? 097 if ((width.to(GfxUnits.dxa) < height.to(GfxUnits.dxa) 098 && orientation.equals(Orientation.HORIZONAL)) 099 || (width.to(GfxUnits.dxa) > height.to(GfxUnits.dxa) 100 && orientation.equals(Orientation.VERTICAL))) 101 { 102 setWidth(height); 103 setHeight(width); 104 } 105 } 106 } 107 108}