com.hfg.xml.msofficexml.docx.wordprocessingml.WmlTextRunProperties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.msofficexml.docx.wordprocessingml;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import com.hfg.css.CSSDeclaration;
import com.hfg.graphics.units.GfxSize;
import com.hfg.graphics.units.GfxUnits;
import com.hfg.graphics.units.Points;
import com.hfg.graphics.ColorUtil;
import com.hfg.xml.XMLName;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlHighlightColor;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlShading;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlSpacing;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTextBorder;
//------------------------------------------------------------------------------
/**
Text run properties for Docx's Office Open XML documents.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
//
// Note: In section 2.4.1 it states "It should also be noted that a pPr element
// may contain a set of run properties within a rPr element - these properties
// are applied to the run which contains the glyph which represents the paragraph
// mark and not the entire paragraph." It seems very inefficient to structure things
// this way but the format seems to force you to duplicate the run properties for every text run in a paragraph.
public class WmlTextRunProperties extends WmlXMLTag
{
private XMLTag mStyle;
private XMLTag mUnderlineTag;
private XMLTag mColorTag;
private XMLTag mRunFontsTag;
private XMLTag mShadowTag;
private XMLTag mHighlightTag;
private WmlShading mShadingTag;
private WmlSpacing mSpacingTag;
private XMLTag mSmallCapsTag;
private XMLTag mSizeTag;
private XMLTag mCsSizeTag;
private XMLTag mStrikeTag;
private XMLTag mVanishTag;
private XMLTag mVertAlignTag;
private WmlTextBorder mBorder;
private XMLTag mNoProof;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public WmlTextRunProperties(Docx inDocx)
{
super(WmlXML.RUN_PROPS, inDocx);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
// For now we will just remove the subtags being overridden
public WmlTextRunProperties combine(WmlTextRunProperties inPropertiesToOverlay)
{
if (inPropertiesToOverlay != null)
{
Map subtagMap = new HashMap<>(15);
for (XMLizable subtag : getSubtags())
{
subtagMap.put(((XMLTag)subtag).getTagName(), subtag);
}
for (XMLizable subtag : inPropertiesToOverlay.getSubtags())
{
XMLizable existingSubtag = subtagMap.get(((XMLTag)subtag).getTagName());
if (existingSubtag != null)
{
removeSubtag(existingSubtag);
}
addSubtag(subtag);
}
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setStyle(String inStyleId)
{
if (null == mStyle)
{
mStyle = new XMLTag(WmlXML.RUN_STYLE);
addSubtag(0, mStyle);
}
mStyle.setAttribute(WmlXML.VALUE_ATT, inStyleId);
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setBold()
{
if (null == getOptionalSubtagByName(WmlXML.BOLD))
{
addSubtag(new XMLTag(WmlXML.BOLD));
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setComplexScriptBold()
{
if (null == getOptionalSubtagByName(WmlXML.COMPLEX_SCRIPT_BOLD))
{
addSubtag(new XMLTag(WmlXML.COMPLEX_SCRIPT_BOLD));
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setItalics()
{
if (null == getOptionalSubtagByName(WmlXML.ITALICS))
{
addSubtag(new XMLTag(WmlXML.ITALICS));
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setComplexScriptItalics()
{
if (null == getOptionalSubtagByName(WmlXML.COMPLEX_SCRIPT_ITALICS))
{
addSubtag(new XMLTag(WmlXML.COMPLEX_SCRIPT_ITALICS));
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setUnderline()
{
if (null == mUnderlineTag)
{
mUnderlineTag = new XMLTag(WmlXML.UNDERLINE);
addSubtag(mUnderlineTag);
}
mUnderlineTag.setAttribute(WmlXML.VALUE_ATT, "single");
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setSuperscript()
{
if (null == mVertAlignTag)
{
mVertAlignTag = new XMLTag(WmlXML.VERT_ALIGN);
addSubtag(mVertAlignTag);
}
mVertAlignTag.setAttribute(WmlXML.VALUE_ATT, "superscript");
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setSubscript()
{
if (null == mVertAlignTag)
{
mVertAlignTag = new XMLTag(WmlXML.VERT_ALIGN);
addSubtag(mVertAlignTag);
}
mVertAlignTag.setAttribute(WmlXML.VALUE_ATT, "subscript");
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setShadow()
{
if (null == mShadowTag)
{
mShadowTag = new XMLTag(WmlXML.SHADOW);
addSubtag(mShadowTag);
}
mShadowTag.setAttribute(WmlXML.VALUE_ATT, "true");
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setSmallCaps()
{
if (null == mSmallCapsTag)
{
mSmallCapsTag = new XMLTag(WmlXML.SMALL_CAPS);
addSubtag(mSmallCapsTag);
}
mSmallCapsTag.setAttribute(WmlXML.VALUE_ATT, "true");
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setColor(Color inColor)
{
return setColor(WmlXML.VALUE_ATT, ColorUtil.colorToHex(inColor));
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setAutoColor()
{
return setColor(WmlXML.VALUE_ATT, "auto");
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setThemeColor(String inValue)
{
return setColor(WmlXML.THEME_COLOR_ATT, inValue);
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setThemeShade(String inValue)
{
return setColor(WmlXML.THEME_SHADE_ATT, inValue);
}
//---------------------------------------------------------------------------
private WmlTextRunProperties setColor(XMLName inXMLAttrName, String inValue)
{
if (null == mColorTag)
{
// Check if it has been added via addSubtag()...
mColorTag = getOptionalSubtagByName(WmlXML.COLOR);
if (null == mColorTag)
{
mColorTag = new XMLTag(WmlXML.COLOR);
addSubtag(mColorTag);
}
}
mColorTag.setAttribute(inXMLAttrName, inValue);
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setFont(Font inFont)
{
setFont(inFont.getName());
setSize(new Points(inFont.getSize()));
if (inFont.isBold())
{
setBold();
}
if (inFont.isItalic())
{
setItalics();
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setFont(String inFont)
{
setFont(WmlXML.ASCII_ATT, inFont);
return setFont(WmlXML.HIGH_ANSI_ATT, inFont);
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setAnsiThemeFont(String inValue)
{
return setFont(WmlXML.ANSI_THEME_ATT, inValue);
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setAsciiThemeFont(String inValue)
{
return setFont(WmlXML.ASCII_THEME_ATT, inValue);
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setComplexScriptThemeFont(String inValue)
{
return setFont(WmlXML.COMPLEX_SCRIPT_THEME_ATT, inValue);
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setEastAsiaThemeFont(String inValue)
{
return setFont(WmlXML.EAST_ASIA_THEME_ATT, inValue);
}
//---------------------------------------------------------------------------
private WmlTextRunProperties setFont(XMLName inXMLAttrName, String inValue)
{
if (null == mRunFontsTag)
{
// Check if it has been added via addSubtag()...
mRunFontsTag = getOptionalSubtagByName(WmlXML.RUN_FONTS);
if (null == mRunFontsTag)
{
mRunFontsTag = new XMLTag(WmlXML.RUN_FONTS);
addSubtag(mRunFontsTag);
}
}
mRunFontsTag.setAttribute(inXMLAttrName, inValue);
return this;
}
//---------------------------------------------------------------------------
/**
* Specifies a positive measurement of font size.
* @param inValue the font size expressed in any units that extend GfxSize
* @return this text run properties object to enable method chaining
*/
public WmlTextRunProperties setSize(GfxSize inValue)
{
if (null == mSizeTag)
{
mSizeTag = new XMLTag(WmlXML.SIZE);
addSubtag(mSizeTag);
}
mSizeTag.setAttribute(WmlXML.VALUE_ATT, inValue.to(GfxUnits.half_points));
return this;
}
//---------------------------------------------------------------------------
/**
* Specifies a positive measurement of complex script font size.
* @param inValue the complex script font size expressed in any units that extend GfxSize
* @return this text run properties object to enable method chaining
*/
public WmlTextRunProperties setComplexScriptSize(GfxSize inValue)
{
if (null == mCsSizeTag)
{
mCsSizeTag = new XMLTag(WmlXML.COMPLEX_SCRIPT_FONT_SIZE);
addSubtag(mCsSizeTag);
}
mSizeTag.setAttribute(WmlXML.VALUE_ATT, inValue.to(GfxUnits.half_points));
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setStrikeThrough()
{
if (null == mStrikeTag)
{
mStrikeTag = new XMLTag(WmlXML.STRIKE);
addSubtag(mStrikeTag);
}
mStrikeTag.setAttribute(WmlXML.VALUE_ATT, "true");
return this;
}
//---------------------------------------------------------------------------
/**
Specifies a highlighting color to be applied as background behind the contents:
<w:highlight w:val="red"/> This element will supersede any background shading
specified in shd.
* @param inColor Only the colors specified by this enum are allowed
* @return this text run properties object (facilitates chaining)
*/
public WmlTextRunProperties setHighlight(WmlHighlightColor inColor)
{
if (null == inColor)
{
// Clear the highlight tag if present
// Check it it has been added via addSubtag()...
XMLTag highlightTag = getOptionalSubtagByName(WmlXML.HIGHLIGHT);
if (highlightTag != null)
{
removeSubtag(highlightTag);
mHighlightTag = null;
}
}
else
{
if (null == mHighlightTag)
{
// Check it it has been added via addSubtag()...
mHighlightTag = getOptionalSubtagByName(WmlXML.HIGHLIGHT);
if (null == mHighlightTag)
{
mHighlightTag = new XMLTag(WmlXML.HIGHLIGHT);
addSubtag(mHighlightTag);
}
}
mHighlightTag.setAttribute(WmlXML.VALUE_ATT, inColor.name());
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties disableSpellingAndGrammarChecks()
{
if (null == mNoProof)
{
// Check if it has been added via addSubtag()...
mNoProof = getOptionalSubtagByName(WmlXML.NO_PROOF);
if (null == mNoProof)
{
mNoProof = new XMLTag(WmlXML.NO_PROOF);
addSubtag(mNoProof);
}
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties vanish()
{
if (null == mVanishTag)
{
// Check if it has been added via addSubtag()...
mVanishTag = getOptionalSubtagByName(WmlXML.VANISH);
if (null == mVanishTag)
{
mVanishTag = new XMLTag(WmlXML.VANISH);
addSubtag(mVanishTag);
}
}
return this;
}
//---------------------------------------------------------------------------
public WmlTextBorder getBorder()
{
if (null == mBorder)
{
// Check if it has been added via addSubtag()...
mBorder = getOptionalSubtagByName(WmlXML.BORDER);
if (null == mBorder)
{
mBorder = new WmlTextBorder(getParentDoc());
addSubtag(mBorder);
}
}
return mBorder;
}
//---------------------------------------------------------------------------
public WmlTextBorder getBorder(CSSDeclaration inCSSDeclaration)
{
if (null == mBorder)
{
// Check if it has been added via addSubtag()...
mBorder = getOptionalSubtagByName(WmlXML.BORDER);
if (null == mBorder)
{
mBorder = new WmlTextBorder(getParentDoc(), inCSSDeclaration);
addSubtag(mBorder);
}
}
return mBorder;
}
//---------------------------------------------------------------------------
public WmlShading getShading()
{
if (null == mShadingTag)
{
// Check if it has been added via addSubtag()...
mShadingTag = getOptionalSubtagByName(WmlXML.SHADING);
if (null == mShadingTag)
{
mShadingTag = new WmlShading();
addSubtag(mShadingTag);
}
}
return mShadingTag;
}
//---------------------------------------------------------------------------
public WmlSpacing getSpacing()
{
if (null == mSpacingTag)
{
// Check if it has been added via addSubtag()...
mSpacingTag = getOptionalSubtagByName(WmlXML.SPACING);
if (null == mSpacingTag)
{
mSpacingTag = new WmlSpacing();
addSubtag(mSpacingTag);
}
}
return mSpacingTag;
}
}