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.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;
/**
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 static XMLTag BOLD = new XMLTag(WmlXML.BOLD);
private static XMLTag ITALICS = new XMLTag(WmlXML.ITALICS);
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 mStrikeTag;
private XMLTag mVanishTag;
private XMLTag mVertAlignTag;
private WmlTextBorder mBorder;
private XMLTag mNoProof;
//---------------------------------------------------------------------------
public WmlTextRunProperties(Docx inDocx)
{
super(WmlXML.RUN_PROPS, inDocx);
}
//---------------------------------------------------------------------------
// 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()
{
addSubtag(BOLD);
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setItalics()
{
addSubtag(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)
{
if (null == mColorTag)
{
mColorTag = new XMLTag(WmlXML.COLOR);
addSubtag(mColorTag);
}
mColorTag.setAttribute(WmlXML.VALUE_ATT, ColorUtil.colorToHex(inColor));
return this;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setFont(String inFont)
{
if (null == mRunFontsTag)
{
mRunFontsTag = new XMLTag(WmlXML.RUN_FONTS);
addSubtag(mRunFontsTag);
}
mRunFontsTag.setAttribute(WmlXML.ASCII_ATT, inFont);
mRunFontsTag.setAttribute(WmlXML.HIGH_ANSI_ATT, inFont);
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;
}
//---------------------------------------------------------------------------
/**
* 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;
}
//---------------------------------------------------------------------------
public WmlTextRunProperties setStrikeThrough()
{
if (null == mStrikeTag)
{
mStrikeTag = new XMLTag(WmlXML.STRIKE);
addSubtag(mSizeTag);
}
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;
}
}