Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poi-ooxml: add new API to add text transparency on XLSX #214

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.xssf.model.ParagraphPropertyFetcher;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import org.openxmlformats.schemas.drawingml.x2006.main.impl.STPositiveFixedPercentageImpl;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;

/**
Expand Down Expand Up @@ -863,6 +864,41 @@ private boolean fetchParagraphProperty(ParagraphPropertyFetcher visitor){
return ok;
}

public void setTextFillColor(XSSFColor color) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a getTextFillColor?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must I return null if no fill colour is found?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably best to return null if no value is set

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will try to do so. But translating CTColors into XSSFColor may be hard, Is there any helper to do that in the project?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XSSFColor has public static XSSFColor from(CTColor color, IndexedColorMap map) - you can use new DefaultIndexedColorMap()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will try to build org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor from CTSolidColorFillProperties.

I'm wondering if we cannot create a sub interface for all those colors instead of CTColor

if (getTextRuns().isEmpty()) {
addNewTextRun();
}
XSSFTextRun run = getTextRuns().get(getTextRuns().size() - 1);
if (!run.getRPr().isSetSolidFill()) {
run.getRPr().addNewSolidFill();
} else if (run.getRPr().getSolidFill().isSetSrgbClr()){
run.getRPr().getSolidFill().unsetSrgbClr();
} else if (run.getRPr().getSolidFill().isSetScrgbClr()){
run.getRPr().getSolidFill().unsetScrgbClr();
} else if (run.getRPr().getSolidFill().isSetSchemeClr()){
run.getRPr().getSolidFill().unsetSchemeClr();
} else if (run.getRPr().getSolidFill().isSetSysClr()){
run.getRPr().getSolidFill().unsetSysClr();
} else if (run.getRPr().getSolidFill().isSetHslClr()){
run.getRPr().getSolidFill().unsetHslClr();
} else if (run.getRPr().getSolidFill().isSetPrstClr()){
run.getRPr().getSolidFill().unsetPrstClr();
}
CTSRgbColor srgbClr = run.getRPr().getSolidFill().addNewSrgbClr();
srgbClr.setVal(color.getRGB());
byte[] argbArray = color.getARGB();
if (argbArray[0] != -1) {
int unsignedTransparency;
if (argbArray[0] >= 0) {
unsignedTransparency = argbArray[0];
} else {
unsignedTransparency = 255 & argbArray[0];
}
int alphaInTenthOfPercent = 100_000 * unsignedTransparency / 255;
srgbClr.addNewAlpha().setVal(alphaInTenthOfPercent);
}
}

@Override
public String toString(){
return "[" + getClass() + "]" + getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import static org.junit.jupiter.api.Assertions.*;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;

public class TestXSSFTextParagraph {
@Test
Expand Down Expand Up @@ -192,4 +194,105 @@ public void testXSSFTextParagraph() throws IOException {
new XSSFTextParagraph(text.getXmlObject(), shape.getCTShape());
}
}
@Test
public void testXSSFTextParagraphWithAlpha() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
XSSFRichTextString rt = new XSSFRichTextString("Test String");

XSSFFont font = wb.createFont();
Color color = new Color(0, 255, 255, 10);
XSSFColor textColor = new XSSFColor(color, wb.getStylesSource().getIndexedColors());
textColor.setARGBHex("0A00FFFF");
font.setColor(textColor);
font.setFontName("Arial");
rt.applyFont(font);
shape.setText(rt);

List<XSSFTextParagraph> paras = shape.getTextParagraphs();
assertEquals(1, paras.size());
paras.get(0).setTextFillColor(textColor);
XSSFTextParagraph testedParagraph = paras.get(0);
CTSolidColorFillProperties properties = testedParagraph.getTextRuns()
.get(testedParagraph.getTextRuns().size() - 1)
.getRPr()
.getSolidFill();
assertNotNull(properties);
assertNotNull(properties.getSrgbClr());
assertEquals(textColor.getRGB()[0], properties.getSrgbClr().getVal()[0]);
assertEquals(textColor.getRGB()[1], properties.getSrgbClr().getVal()[1]);
assertEquals(textColor.getRGB()[2], properties.getSrgbClr().getVal()[2]);
// 10 is 3.921% of transparency therefore 3912 1000th% of transparency
assertEquals(3921, properties.getSrgbClr().getAlphaArray()[0].getVal());
}
}
@Test
public void testXSSFTextParagraphWithoutAlpha() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
XSSFRichTextString rt = new XSSFRichTextString("Test String");

XSSFFont font = wb.createFont();
Color color = new Color(0, 255, 255, 10);
XSSFColor textColor = new XSSFColor(color, wb.getStylesSource().getIndexedColors());
font.setColor(textColor);
font.setFontName("Arial");
rt.applyFont(font);
shape.setText(rt);

List<XSSFTextParagraph> paras = shape.getTextParagraphs();
assertEquals(1, paras.size());
paras.get(0).setTextFillColor(textColor);
XSSFTextParagraph testedParagraph = paras.get(0);
CTSolidColorFillProperties properties = testedParagraph.getTextRuns()
.get(testedParagraph.getTextRuns().size() - 1)
.getRPr()
.getSolidFill();
assertNotNull(properties);
assertNotNull(properties.getSrgbClr());
assertEquals(textColor.getRGB()[0], properties.getSrgbClr().getVal()[0]);
assertEquals(textColor.getRGB()[1], properties.getSrgbClr().getVal()[1]);
assertEquals(textColor.getRGB()[2], properties.getSrgbClr().getVal()[2]);
// 10 is 3.921% of transparency therefore 3912 1000th% of transparency
assertEquals(0, properties.getSrgbClr().getAlphaArray().length);
}
}
@Test
public void testXSSFTextParagraphNegativeByteAlpha() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
XSSFRichTextString rt = new XSSFRichTextString("Test String");

XSSFFont font = wb.createFont();
Color color = new Color(0, 255, 255, 10);
XSSFColor textColor = new XSSFColor(color, wb.getStylesSource().getIndexedColors());
textColor.setARGBHex("FA00FFFF");
//font.setColor(textColor);
font.setFontName("Arial");
rt.applyFont(font);
shape.setText(rt);

List<XSSFTextParagraph> paras = shape.getTextParagraphs();
assertEquals(1, paras.size());
paras.get(0).setTextFillColor(textColor);
XSSFTextParagraph testedParagraph = paras.get(0);
CTSolidColorFillProperties properties = testedParagraph.getTextRuns()
.get(testedParagraph.getTextRuns().size() - 1)
.getRPr()
.getSolidFill();
assertNotNull(properties);
assertNotNull(properties.getSrgbClr());
assertEquals(textColor.getRGB()[0], properties.getSrgbClr().getVal()[0]);
assertEquals(textColor.getRGB()[1], properties.getSrgbClr().getVal()[1]);
assertEquals(textColor.getRGB()[2], properties.getSrgbClr().getVal()[2]);
// 250 is 3.921% of transparency therefore 3912 1000th% of transparency
assertEquals(98039, properties.getSrgbClr().getAlphaArray()[0].getVal());
}
}
}