support colors as byte[]

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1925496 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2025-05-11 10:20:27 +00:00
parent 783ebda7f3
commit 1b0632f3f1

View File

@ -58,7 +58,7 @@ public class XSSFTextRun {
return _r;
}
public void setFontColor(Color color){
public void setFontColor(Color color) {
CTTextCharacterProperties rPr = getRPr();
CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();
CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
@ -72,24 +72,49 @@ public class XSSFTextRun {
}
public Color getFontColor(){
/**
* @param rgb
* @since POI 5.4.2
*/
public void setFontColor(byte[] rgb) {
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetSolidFill()){
CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();
CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
clr.setVal(rgb);
if(fill.isSetHslClr()) fill.unsetHslClr();
if(fill.isSetPrstClr()) fill.unsetPrstClr();
if(fill.isSetSchemeClr()) fill.unsetSchemeClr();
if(fill.isSetScrgbClr()) fill.unsetScrgbClr();
if(fill.isSetSysClr()) fill.unsetSysClr();
}
public Color getFontColor() {
final byte[] rgb = getFontColorAsBytes();
if (rgb.length == 3) {
return new Color(0xFF & rgb[0], 0xFF & rgb[1], 0xFF & rgb[2]);
} else {
return new Color(0xFF & rgb[1], 0xFF & rgb[2], 0xFF & rgb[3], 0xFF & rgb[0]);
}
}
/**
* @return the font color as a byte array.
* @since POI 5.4.2
*/
public byte[] getFontColorAsBytes() {
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetSolidFill()) {
CTSolidColorFillProperties fill = rPr.getSolidFill();
if(fill.isSetSrgbClr()){
CTSRgbColor clr = fill.getSrgbClr();
byte[] rgb = clr.getVal();
if (rgb.length == 3) {
return new Color(0xFF & rgb[0], 0xFF & rgb[1], 0xFF & rgb[2]);
} else {
return new Color(0xFF & rgb[1], 0xFF & rgb[2], 0xFF & rgb[3], 0xFF & rgb[0]);
}
return clr.getVal();
}
}
return new Color(0, 0, 0);
return new byte[]{0, 0, 0};
}
/**
@ -97,7 +122,7 @@ public class XSSFTextRun {
* @param fontSize font size in points.
* The value of {@code -1} unsets the Sz attribute from the underlying xml bean
*/
public void setFontSize(double fontSize){
public void setFontSize(double fontSize) {
CTTextCharacterProperties rPr = getRPr();
if(fontSize == -1.0) {
if(rPr.isSetSz()) rPr.unsetSz();
@ -113,14 +138,14 @@ public class XSSFTextRun {
/**
* @return font size in points or -1 if font size is not set.
*/
public double getFontSize(){
public double getFontSize() {
double scale = 1;
double size = XSSFFont.DEFAULT_FONT_SIZE; // default font size
CTTextNormalAutofit afit = getParentParagraph().getParentShape().getTxBody().getBodyPr().getNormAutofit();
if(afit != null) scale = (double)afit.getFontScale() / 100000;
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetSz()){
CTTextCharacterProperties rPr = getRPrOrNull();
if(rPr != null && rPr.isSetSz()){
size = rPr.getSz()*0.01;
}
@ -133,8 +158,8 @@ public class XSSFTextRun {
* If this attribute is omitted then a value of 0 or no adjustment is assumed.
*/
public double getCharacterSpacing(){
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetSpc()){
CTTextCharacterProperties rPr = getRPrOrNull();
if(rPr != null && rPr.isSetSpc()){
return Units.toPoints(POIXMLUnits.parseLength(rPr.xgetSpc()));
}
return 0;
@ -192,19 +217,23 @@ public class XSSFTextRun {
* @return font family or null if not set
*/
public String getFontFamily(){
CTTextCharacterProperties rPr = getRPr();
CTTextFont font = rPr.getLatin();
if(font != null){
return font.getTypeface();
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null) {
CTTextFont font = rPr.getLatin();
if(font != null) {
return font.getTypeface();
}
}
return XSSFFont.DEFAULT_FONT_NAME;
}
public byte getPitchAndFamily(){
CTTextCharacterProperties rPr = getRPr();
CTTextFont font = rPr.getLatin();
if(font != null){
return font.getPitchFamily();
public byte getPitchAndFamily() {
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null) {
CTTextFont font = rPr.getLatin();
if(font != null) {
return font.getPitchFamily();
}
}
return 0;
}
@ -222,8 +251,8 @@ public class XSSFTextRun {
* @return whether a run of text will be formatted as strikethrough text. Default is false.
*/
public boolean isStrikethrough() {
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetStrike()){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetStrike()){
return rPr.getStrike() != STTextStrikeType.NO_STRIKE;
}
return false;
@ -233,8 +262,8 @@ public class XSSFTextRun {
* @return whether a run of text will be formatted as a superscript text. Default is false.
*/
public boolean isSuperscript() {
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetBaseline()){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetBaseline()){
return POIXMLUnits.parsePercent(rPr.xgetBaseline()) > 0;
}
return false;
@ -274,8 +303,8 @@ public class XSSFTextRun {
* @return whether a run of text will be formatted as a superscript text. Default is false.
*/
public boolean isSubscript() {
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetBaseline()){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetBaseline()){
return POIXMLUnits.parsePercent(rPr.xgetBaseline()) < 0;
}
return false;
@ -285,8 +314,8 @@ public class XSSFTextRun {
* @return whether a run of text will be formatted as a superscript text. Default is false.
*/
public TextCap getTextCap() {
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetCap()){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetCap()) {
return TextCap.values()[rPr.getCap().intValue() - 1];
}
return TextCap.NONE;
@ -305,8 +334,8 @@ public class XSSFTextRun {
* @return whether this run of text is formatted as bold text
*/
public boolean isBold(){
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetB()){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetB()){
return rPr.getB();
}
return false;
@ -323,8 +352,8 @@ public class XSSFTextRun {
* @return whether this run of text is formatted as italic text
*/
public boolean isItalic(){
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetI()){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetI()){
return rPr.getI();
}
return false;
@ -341,17 +370,21 @@ public class XSSFTextRun {
* @return whether this run of text is formatted as underlined text
*/
public boolean isUnderline(){
CTTextCharacterProperties rPr = getRPr();
if(rPr.isSetU()){
CTTextCharacterProperties rPr = getRPrOrNull();
if (rPr != null && rPr.isSetU()){
return rPr.getU() != STTextUnderlineType.NONE;
}
return false;
}
protected CTTextCharacterProperties getRPr(){
protected CTTextCharacterProperties getRPr() {
return _r.isSetRPr() ? _r.getRPr() : _r.addNewRPr();
}
private CTTextCharacterProperties getRPrOrNull() {
return _r.isSetRPr() ? _r.getRPr() : null;
}
@Override
public String toString(){
return "[" + getClass() + "]" + getText();