Color result = color.getColor();
- double alpha = getAlpha(result, color);
+ final double alpha = getAlpha(result, color);
+
+ final double[] scRGB = RGB2SCRGB(result);
+ applyShade(scRGB, color);
+ applyTint(scRGB, color);
+ result = SCRGB2RGB(scRGB);
+
// values are in the range [0..100] (usually ...)
double[] hsl = RGB2HSL(result);
applyHslModOff(hsl, 0, color.getHueMod(), color.getHueOff());
applyHslModOff(hsl, 1, color.getSatMod(), color.getSatOff());
applyHslModOff(hsl, 2, color.getLumMod(), color.getLumOff());
- applyShade(hsl, color);
- applyTint(hsl, color);
result = HSL2RGB(hsl[0], hsl[1], hsl[2], alpha);
*
* For a shade, the equation is luminance * %tint.
*/
- private static void applyShade(double[] hsl, ColorStyle fc) {
+ private static void applyShade(double[] scRGB, ColorStyle fc) {
int shade = fc.getShade();
if (shade == -1) {
return;
}
- double shadePct = shade / 100_000.;
- hsl[2] *= shadePct;
+ final double shadePct = shade / 100_000.;
+ for (int i=0; i<3; i++) {
+ scRGB[i] = Math.max(0, Math.min(1, scRGB[i]*shadePct));
+ }
}
/**
* Apply the tint
- *
- * For a tint, the equation is luminance * %tint + (1-%tint).
- * (Note that 1-%tint is equal to the lumOff value in DrawingML.)
*/
- private static void applyTint(double[] hsl, ColorStyle fc) {
+ private static void applyTint(double[] scRGB, ColorStyle fc) {
int tint = fc.getTint();
if (tint == -1 || tint == 0) {
return;
// see 18.8.19 fgColor (Foreground Color)
double tintPct = tint / 100_000.;
-
- // The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken
- // and 1.0 means 100% lighten. Also, 0.0 means no change.
- if (tintPct < 0) {
- // Lum’ = Lum * (1.0 + tint)
- hsl[2] *= (1 + tintPct);
- } else {
- // Lum‘ = Lum * (1.0-tint) + (HLSMAX – HLSMAX * (1.0-tint))
- hsl[2] = hsl[2]*(1-tintPct) + (100-100*(1-tintPct));
+ for (int i=0; i<3; i++) {
+ scRGB[i] = 1 - (1 - scRGB[i]) * tintPct;
}
}
}
/**
- * Convert sRGB float component [0..1] from sRGB to linear RGB [0..100000]
+ * Convert sRGB Color to scRGB [0..1] (0:red,1:green,2:blue).
+ * Alpha needs to be handled separately.
*
- * @see Color#getRGBColorComponents(float[])
+ * @see <a href="https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/Color.cs,1048">.Net implementation sRgbToScRgb</a>
*/
- public static int srgb2lin(float sRGB) {
- // scRGB has a linear gamma of 1.0, scale the AWT-Color which is in sRGB to linear RGB
- // see https://en.wikipedia.org/wiki/SRGB (the reverse transformation)
- if (sRGB <= 0.04045d) {
- return (int)Math.rint(100000d * sRGB / 12.92d);
- } else {
- return (int)Math.rint(100000d * Math.pow((sRGB + 0.055d) / 1.055d, 2.4d));
+ public static double[] RGB2SCRGB(Color color) {
+ float[] rgb = color.getColorComponents(null);
+ double[] scRGB = new double[3];
+ for (int i=0; i<3; i++) {
+ if (rgb[i] < 0) {
+ scRGB[i] = 0;
+ } else if (rgb[i] <= 0.04045) {
+ scRGB[i] = rgb[i] / 12.92;
+ } else if (rgb[i] <= 1) {
+ scRGB[i] = Math.pow((rgb[i] + 0.055) / 1.055, 2.4);
+ } else {
+ scRGB[i] = 1;
+ }
}
+ return scRGB;
}
/**
- * Convert linear RGB [0..100000] to sRGB float component [0..1]
+ * Convert scRGB [0..1] components (0:red,1:green,2:blue) to sRGB Color.
+ * Alpha needs to be handled separately.
*
- * @see Color#getRGBColorComponents(float[])
+ * @see <a href="https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/Color.cs,1075">.Net implementation ScRgbTosRgb</a>
*/
- public static float lin2srgb(int linRGB) {
- // color in percentage is in linear RGB color space, i.e. needs to be gamma corrected for AWT color
- // see https://en.wikipedia.org/wiki/SRGB (The forward transformation)
- if (linRGB <= 0.0031308d) {
- return (float)(linRGB / 100000d * 12.92d);
- } else {
- return (float)(1.055d * Math.pow(linRGB / 100000d, 1.0d/2.4d) - 0.055d);
+ public static Color SCRGB2RGB(double... scRGB) {
+ final double[] rgb = new double[3];
+ for (int i=0; i<3; i++) {
+ if (scRGB[i] < 0) {
+ rgb[i] = 0;
+ } else if (scRGB[i] <= 0.0031308) {
+ rgb[i] = scRGB[i] * 12.92;
+ } else if (scRGB[i] < 1) {
+ rgb[i] = 1.055 * Math.pow(scRGB[i], 1.0 / 2.4) - 0.055;
+ } else {
+ rgb[i] = 1;
+ }
}
+ return new Color((float)rgb[0],(float)rgb[1],(float)rgb[2]);
}
-
static void fillPaintWorkaround(Graphics2D graphics, Shape shape) {
// the ibm jdk has a rendering/JIT bug, which throws an AIOOBE in
// TexturePaintContext$Int.setRaster(TexturePaintContext.java:476)
}
private Color toColor(CTScRgbColor scrgb) {
- // color in percentage is in linear RGB color space, i.e. needs to be gamma corrected for AWT color
- return new Color(DrawPaint.lin2srgb(scrgb.getR()), DrawPaint.lin2srgb(scrgb.getG()), DrawPaint.lin2srgb(scrgb.getB()));
+ // percental [0..100000] scRGB color space needs to be gamma corrected for AWT/sRGB colorspace
+ return DrawPaint.SCRGB2RGB(scrgb.getR()/100_000d,scrgb.getG()/100_000d,scrgb.getB()/100_000d);
}
private Color toColor(CTSRgbColor srgb) {
alphaPct = (addAlpha) ? rgb.addNewAlpha() : null;
} else {
CTScRgbColor rgb = fill.addNewScrgbClr();
- rgb.setR(DrawPaint.srgb2lin(rgbaf[0]));
- rgb.setG(DrawPaint.srgb2lin(rgbaf[1]));
- rgb.setB(DrawPaint.srgb2lin(rgbaf[2]));
+ double[] scRGB = DrawPaint.RGB2SCRGB(color);
+ rgb.setR((int)Math.rint(scRGB[0]*100_000d));
+ rgb.setG((int)Math.rint(scRGB[1]*100_000d));
+ rgb.setB((int)Math.rint(scRGB[2]*100_000d));
alphaPct = (addAlpha) ? rgb.addNewAlpha() : null;
}
// alpha (%)
if (alphaPct != null) {
- alphaPct.setVal((int)(100000 * rgbaf[3]));
+ alphaPct.setVal((int)Math.rint(rgbaf[3]*100_000));
}
}
==================================================================== */
package org.apache.poi.xslf.usermodel;
-import static org.apache.poi.sl.TestCommonSL.sameColor;
+import static org.apache.poi.sl.TestCommonSL.getColor;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
assertEquals(40.0, r1.getFontSize(), 0);
assertTrue(r1.isBold());
assertTrue(r1.isItalic());
- assertTrue(sameColor(new Color(148, 198, 0), r1.getFontColor()));
+ assertEquals(new Color(148, 198, 0), getColor(r1.getFontColor()));
assertNull(sh1.getFillColor());
assertNull(sh1.getLineColor());
assertEquals(18.0, r2.getFontSize(), 0);
assertFalse(r2.isBold());
assertFalse(r2.isItalic());
- assertTrue(sameColor(Color.white, r2.getFontColor()));
+ assertEquals(Color.white, getColor(r2.getFontColor()));
assertEquals(new Color(148, 198, 0), sh2.getFillColor());
assertEquals(new Color(148, 198, 0), sh2.getLineColor()); // slightly different from PowerPoint!
//assertEquals(32.4.0, r3.getFontSize());
assertTrue(r3.isBold());
assertTrue(r3.isItalic());
- assertTrue(sameColor(new Color(148, 198, 0), r3.getFontColor()));
+ assertEquals(new Color(148, 198, 0), getColor(r3.getFontColor()));
assertNull(sh3.getFillColor());
assertNull(sh3.getLineColor());
==================================================================== */
package org.apache.poi.xslf.usermodel;
-import static org.apache.poi.sl.TestCommonSL.sameColor;
+import static org.apache.poi.sl.TestCommonSL.getColor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
assertNull(p.getBulletFontColor());
p.setBulletFontColor(Color.red);
- assertTrue(sameColor(Color.red, p.getBulletFontColor()));
+ assertEquals(Color.red, getColor(p.getBulletFontColor()));
assertNull(p.getBulletFontSize());
p.setBulletFontSize(200.);
*/
package org.apache.poi.xslf.usermodel;
-import static org.apache.poi.sl.TestCommonSL.sameColor;
+import static org.apache.poi.sl.TestCommonSL.getColor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
assertEquals(0., r.getCharacterSpacing(), 0);
assertFalse(r.getRPr(true).isSetSpc());
- assertTrue(sameColor(Color.black, r.getFontColor()));
+ assertEquals(Color.black, getColor(r.getFontColor()));
r.setFontColor(Color.red);
- assertTrue(sameColor(Color.red, r.getFontColor()));
+ assertEquals(Color.red, getColor(r.getFontColor()));
assertEquals("Calibri", r.getFontFamily());
r.setFontFamily("Arial");
assertTrue(r.isSubscript());
r.setSubscript(false);
assertFalse(r.isSubscript());
-
+
ppt.close();
}
==================================================================== */
package org.apache.poi.xslf.usermodel;
-import static org.apache.poi.sl.TestCommonSL.sameColor;
+import static org.apache.poi.sl.TestCommonSL.getColor;
import static org.apache.poi.xslf.usermodel.TestXSLFSimpleShape.getSpPr;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xddf.usermodel.text.XDDFBodyProperties;
import org.apache.poi.xddf.usermodel.text.XDDFTextBody;
+import org.apache.poi.xddf.usermodel.text.XDDFTextParagraph;
import org.apache.poi.xslf.XSLFTestDataSamples;
import org.junit.BeforeClass;
import org.junit.Test;
verifySlide7(slide.get(6));
verifySlide8(slide.get(7));
verifySlide10(slide.get(9));
-
+
ppt.close();
}
// now check text properties
assertEquals("Centered Title", shape1.getText());
assertEquals("Centered Title",
- tb1.getParagraphs().stream().map(p -> p.getText()).collect(Collectors.joining("\n")));
+ tb1.getParagraphs().stream().map(XDDFTextParagraph::getText).collect(Collectors.joining("\n")));
XSLFTextRun r1 = shape1.getTextParagraphs().get(0).getTextRuns().get(0);
assertEquals("Calibri", r1.getFontFamily());
assertEquals(44.0, r1.getFontSize(), 0);
- assertTrue(sameColor(Color.black, r1.getFontColor()));
+ assertEquals(Color.black, getColor(r1.getFontColor()));
XSLFTextShape shape2 = (XSLFTextShape)shapes.get(1);
XDDFTextBody tb2 = shape2.getTextBody();
assertNull(tbp2.getAnchoring());
assertEquals("subtitle", shape2.getText());
- assertEquals("subtitle", tb2.getParagraphs().stream().map(p -> p.getText()).collect(Collectors.joining("\n")));
+ assertEquals("subtitle", tb2.getParagraphs().stream().map(XDDFTextParagraph::getText).collect(Collectors.joining("\n")));
XSLFTextRun r2 = shape2.getTextParagraphs().get(0).getTextRuns().get(0);
assertEquals("Calibri", r2.getFontFamily());
assertEquals(32.0, r2.getFontSize(), 0);
XSLFTextRun r1 = shape1.getTextParagraphs().get(0).getTextRuns().get(0);
assertEquals("Calibri", r1.getFontFamily());
assertEquals(44.0, r1.getFontSize(), 0);
- assertTrue(sameColor(Color.black, r1.getFontColor()));
+ assertEquals(Color.black, getColor(r1.getFontColor()));
XSLFTextShape shape2 = (XSLFTextShape)shapes.get(1);
CTPlaceholder ph2 = shape2.getPlaceholderDetails().getCTPlaceholder(false);
assertEquals("Content", pr1.getRawText());
assertEquals("Calibri", pr1.getFontFamily());
assertEquals(32.0, pr1.getFontSize(), 0);
- assertEquals(27.0, pr1.getParentParagraph().getLeftMargin(), 0);
- assertEquals("\u2022", pr1.getParentParagraph().getBulletCharacter());
+ assertEquals(27.0, pr1.getParentParagraph().getLeftMargin(), 0);
+ assertEquals("\u2022", pr1.getParentParagraph().getBulletCharacter());
assertEquals("Arial", pr1.getParentParagraph().getBulletFont());
XSLFTextRun pr2 = shape2.getTextParagraphs().get(1).getTextRuns().get(0);
assertEquals(TextAlign.LEFT, r1.getParentParagraph().getTextAlign());
assertEquals("Calibri", r1.getFontFamily());
assertEquals(40.0, r1.getFontSize(), 0);
- assertTrue(sameColor(Color.black, r1.getFontColor()));
+ assertEquals(Color.black, getColor(r1.getFontColor()));
assertTrue(r1.isBold());
assertFalse(r1.isItalic());
assertFalse(r1.isUnderlined());
assertEquals(TextAlign.CENTER, r1.getParentParagraph().getTextAlign());
assertEquals("Calibri", r1.getFontFamily());
assertEquals(44.0, r1.getFontSize(), 0);
- assertTrue(sameColor(Color.black, r1.getFontColor()));
+ assertEquals(Color.black, getColor(r1.getFontColor()));
XSLFTextShape shape2 = (XSLFTextShape)shapes.get(1);
CTPlaceholder ph2 = shape2.getPlaceholderDetails().getCTPlaceholder(false);
assertEquals(0, pr5.getParentParagraph().getIndentLevel());
assertEquals("Right", pr5.getRawText());
assertEquals("Calibri", pr5.getFontFamily());
- assertTrue(sameColor(Color.black, pr5.getFontColor()));
+ assertEquals(Color.black, getColor(pr5.getFontColor()));
}
@SuppressWarnings("unused")
XSLFSlideLayout layout = slide.getSlideLayout();
List<XSLFShape> shapes = slide.getShapes();
// TODO
- }
+ }
void verifySlide7(XSLFSlide slide){
XSLFSlideLayout layout = slide.getSlideLayout();
assertEquals(TextAlign.CENTER, r1.getParentParagraph().getTextAlign());
assertEquals("Calibri", r1.getFontFamily());
assertEquals(44.0, r1.getFontSize(), 0);
- assertTrue(sameColor(Color.black, r1.getFontColor()));
+ assertEquals(Color.black, getColor(r1.getFontColor()));
assertFalse(r1.isBold());
XSLFTextShape shape2 = (XSLFTextShape)shapes.get(1);
assertEquals(TextAlign.LEFT, r1.getParentParagraph().getTextAlign());
assertEquals("Calibri", r1.getFontFamily());
assertEquals(20.0, r1.getFontSize(), 0);
- assertTrue(sameColor(Color.black, r1.getFontColor()));
+ assertEquals(Color.black, getColor(r1.getFontColor()));
assertTrue(r1.isBold());
XSLFTextShape shape2 = (XSLFTextShape)shapes.get(1);
assertEquals(TextAlign.CENTER, r1.getParentParagraph().getTextAlign());
assertEquals("Calibri", r1.getFontFamily());
assertEquals(12.0, r1.getFontSize(), 0);
- // TODO calculation of tint might be incorrect
- assertTrue(sameColor(new Color(191,191,191), r1.getFontColor()));
+ assertEquals(new Color(0x898989), getColor(r1.getFontColor()));
XSLFTextShape dt = (XSLFTextShape)slide.getPlaceholderByType(STPlaceholderType.INT_DT);
assertEquals("Friday, October 21, 2011", dt.getText());
assertEquals("Calibri", textRun.getFontFamily());
lv5PPr.setAlgn(STTextAlignType.CTR);
assertEquals(TextAlign.CENTER, paragraph.getTextAlign());
-
+
ppt.close();
}
assertEquals("Calibri", r3.getFontFamily());
lv3PPr.setAlgn(STTextAlignType.CTR);
assertEquals(TextAlign.CENTER, p3.getTextAlign());
-
+
ppt.close();
}
-
+
@Test
public void metroBlob() throws IOException, ReflectiveOperationException {
assumeFalse(xslfOnly);
==================================================================== */
package org.apache.poi.xslf.usermodel;
-import static org.apache.poi.sl.TestCommonSL.sameColor;
+import static org.apache.poi.sl.TestCommonSL.getColor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
XSLFTextShape sh1 = (XSLFTextShape)getShape(slide, "Rectangle 3");
XSLFTextRun run1 = sh1.getTextParagraphs().get(0).getTextRuns().get(0);
- assertTrue(sameColor(Color.white, run1.getFontColor()));
+ assertEquals(Color.white, getColor(run1.getFontColor()));
assertEquals(new Color(79, 129, 189), sh1.getFillColor());
assertTrue(sh1.getFillStyle().getPaint() instanceof SolidPaint) ; // solid fill
XSLFTextShape sh1 = (XSLFTextShape)getShape(slide, "Rectangle 4");
XSLFTextRun run1 = sh1.getTextParagraphs().get(0).getTextRuns().get(0);
- assertTrue(sameColor(Color.white, run1.getFontColor()));
+ assertEquals(Color.white, getColor(run1.getFontColor()));
assertEquals(new Color(148, 198, 0), sh1.getFillColor());
assertTrue(sh1.getFillStyle().getPaint() instanceof SolidPaint) ; // solid fill
XSLFTextShape sh2 = (XSLFTextShape)getShape(slide, "Title 3");
XSLFTextRun run2 = sh2.getTextParagraphs().get(0).getTextRuns().get(0);
- assertTrue(sameColor(new Color(148, 198, 0), run2.getFontColor()));
+ assertEquals(new Color(148, 198, 0), getColor(run2.getFontColor()));
assertNull(sh2.getFillColor()); // no fill
assertTrue(slide.getSlideLayout().getFollowMasterGraphics());
XSLFTextShape sh2 = (XSLFTextShape)getShape(slide, "Title 1");
XSLFTextRun run2 = sh2.getTextParagraphs().get(0).getTextRuns().get(0);
- assertTrue(sameColor(new Color(148, 198, 0), run2.getFontColor()));
+ assertEquals(new Color(148, 198, 0), getColor(run2.getFontColor()));
assertNull(sh2.getFillColor()); // no fill
// font size is 40pt and scale factor is 90%
assertEquals(36.0, run2.getFontSize(), 0);
XSLFTextShape sh1 = (XSLFTextShape)getShape(slide, "Subtitle 3");
XSLFTextRun run1 = sh1.getTextParagraphs().get(0).getTextRuns().get(0);
- assertTrue(sameColor(new Color(66, 66, 66), run1.getFontColor()));
+ assertEquals(new Color(66, 66, 66), getColor(run1.getFontColor()));
assertNull(sh1.getFillColor()); // no fill
XSLFTextShape sh2 = (XSLFTextShape)getShape(slide, "Title 2");
XSLFTextRun run2 = sh2.getTextParagraphs().get(0).getTextRuns().get(0);
- assertTrue(sameColor(new Color(148, 198, 0), run2.getFontColor()));
+ assertEquals(new Color(148, 198, 0), getColor(run2.getFontColor()));
assertNull(sh2.getFillColor()); // no fill
assertFalse(slide.getSlideLayout().getFollowMasterGraphics());
XSLFTextShape sh1 = (XSLFTextShape)getShape(slide, "Title 3");
XSLFTextRun run1 = sh1.getTextParagraphs().get(0).getTextRuns().get(0);
- assertTrue(sameColor(Color.white, run1.getFontColor()));
+ assertEquals(Color.white, getColor(run1.getFontColor()));
assertNull(sh1.getFillColor()); // no fill
XSLFTextShape sh2 = (XSLFTextShape)getShape(slide, "Subtitle 4");
XSLFTextRun run2 = sh2.getTextParagraphs().get(0).getTextRuns().get(0);
- assertTrue(sameColor(Color.white, run2.getFontColor()));
+ assertEquals(Color.white, getColor(run2.getFontColor()));
assertNull(sh2.getFillColor()); // no fill
}
}
package org.apache.poi.hslf.model;
-import static org.apache.poi.sl.TestCommonSL.sameColor;
+import static org.apache.poi.sl.TestCommonSL.getColor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@Before
public void setUp() throws Exception {
- InputStream is1 = null, is2 = null;
- try {
- is1 = _slTests.openResourceAsStream("empty.ppt");
+ try (InputStream is1 = _slTests.openResourceAsStream("empty.ppt");
+ InputStream is2 = _slTests.openResourceAsStream("empty_textbox.ppt")) {
ppt = new HSLFSlideShow(is1);
- is2 = _slTests.openResourceAsStream("empty_textbox.ppt");
pptB = new HSLFSlideShow(is2);
- } finally {
- is1.close();
- is2.close();
}
}
assertTrue(shape.get(1) instanceof HSLFAutoShape); //group shape
assertEquals(ellipseAnchor, shape.get(1).getAnchor()); //group shape
-
+
ppt2.close();
}
/**
* Verify that we can read TextBox shapes
- * @throws Exception
*/
@Test
public void textBoxRead() throws IOException {
assertEquals(txtbox.getTextParagraphs().get(0).getTextRuns().size(), 1);
HSLFTextRun rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
- if (text.equals("Hello, World!!!")){
- assertEquals(32, rt.getFontSize(), 0);
- assertTrue(rt.isBold());
- assertTrue(rt.isItalic());
- } else if (text.equals("I am just a poor boy")){
- assertEquals(44, rt.getFontSize(), 0);
- assertTrue(rt.isBold());
- } else if (text.equals("This is Times New Roman")){
- assertEquals(16, rt.getFontSize(), 0);
- assertTrue(rt.isBold());
- assertTrue(rt.isItalic());
- assertTrue(rt.isUnderlined());
- } else if (text.equals("Plain Text")){
- assertEquals(18, rt.getFontSize(), 0);
+ switch (text) {
+ case "Hello, World!!!":
+ assertNotNull(rt.getFontSize());
+ assertEquals(32, rt.getFontSize(), 0);
+ assertTrue(rt.isBold());
+ assertTrue(rt.isItalic());
+ break;
+ case "I am just a poor boy":
+ assertNotNull(rt.getFontSize());
+ assertEquals(44, rt.getFontSize(), 0);
+ assertTrue(rt.isBold());
+ break;
+ case "This is Times New Roman":
+ assertNotNull(rt.getFontSize());
+ assertEquals(16, rt.getFontSize(), 0);
+ assertTrue(rt.isBold());
+ assertTrue(rt.isItalic());
+ assertTrue(rt.isUnderlined());
+ break;
+ case "Plain Text":
+ assertNotNull(rt.getFontSize());
+ assertEquals(18, rt.getFontSize(), 0);
+ break;
}
}
}
shape.setAnchor(new Rectangle2D.Double(100,100,100,10));
slide.addShape(shape);
shape.resizeToFitText();
-
+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ss.write(bos);
-
+
ss = new HSLFSlideShow(new ByteArrayInputStream(bos.toByteArray()));
slide = ss.getSlides().get(0);
HSLFTextBox tb = (HSLFTextBox)slide.getShapes().get(0);
List<HSLFTextParagraph> para = tb.getTextParagraphs();
HSLFTextRun tr = para.get(0).getTextRuns().get(0);
assertEquals("para 1 run 1. ", tr.getRawText());
- assertTrue(sameColor(Color.black, tr.getFontColor()));
+ assertEquals(Color.black, getColor(tr.getFontColor()));
tr = para.get(0).getTextRuns().get(1);
assertEquals("para 1 run 2.\r", tr.getRawText());
- assertTrue(sameColor(Color.red, tr.getFontColor()));
+ assertEquals(Color.red, getColor(tr.getFontColor()));
tr = para.get(1).getTextRuns().get(0);
assertEquals("para 2 run 1. ", tr.getRawText());
- assertTrue(sameColor(Color.yellow, tr.getFontColor()));
+ assertEquals(Color.yellow, getColor(tr.getFontColor()));
tr = para.get(1).getTextRuns().get(1);
assertEquals("para 2 run 2. para 2 run 3.", tr.getRawText());
- assertTrue(sameColor(Color.black, tr.getFontColor()));
+ assertEquals(Color.black, getColor(tr.getFontColor()));
assertTrue(tr.isStrikethrough());
}
-
-
+
+
/**
* Verify that we can add TextBox shapes to a slide
* and set some of the style attributes
// Check it before save
rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
assertEquals(val, rt.getRawText());
+ assertNotNull(rt.getFontSize());
assertEquals(42, rt.getFontSize(), 0);
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontFamily());
- assertTrue(sameColor(Color.red, rt.getFontColor()));
+ assertEquals(Color.red, getColor(rt.getFontColor()));
// Serialize and read again
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Check after save
assertEquals(val, rt.getRawText());
+ assertNotNull(rt.getFontSize());
assertEquals(42, rt.getFontSize(), 0);
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontFamily());
- assertTrue(sameColor(Color.red, rt.getFontColor()));
-
+ assertEquals(Color.red, getColor(rt.getFontColor()));
+
ppt2.close();
}
line = (HSLFLine)grshape.get(1);
assertEquals(new Rectangle2D.Double(300, 300, 500, 0), line.getAnchor());
-
+
ss.close();
}
public void shapeId() throws IOException {
HSLFSlideShow ss = new HSLFSlideShow();
HSLFSlide slide = ss.createSlide();
- HSLFShape shape = null;
//EscherDgg is a document-level record which keeps track of the drawing groups
EscherDggRecord dgg = ss.getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
int dgShapesUsed = dg.getNumShapes(); // number of shapes in the slide
//insert 3 shapes and make sure the Ids are properly incremented
for (int i = 0; i < 3; i++) {
- shape = new HSLFLine();
+ HSLFShape shape = new HSLFLine();
assertEquals(0, shape.getShapeId());
slide.addShape(shape);
assertTrue(shape.getShapeId() > 0);
//make sure it is so
int numClusters = dgg.getNumIdClusters();
for (int i = 0; i < 1025; i++) {
- shape = new HSLFLine();
+ HSLFShape shape = new HSLFLine();
slide.addShape(shape);
}
assertEquals(numClusters + 1, dgg.getNumIdClusters());
assertEquals("Border width is 5 pt", sh4.getText());
assertEquals(Color.black, sh4.getLineColor());
assertEquals(5.0, sh4.getLineWidth(), 0);
-
+
ss.close();
}
}
package org.apache.poi.hslf.usermodel;
-import static org.apache.poi.sl.TestCommonSL.sameColor;
+import static org.apache.poi.sl.TestCommonSL.getColor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import org.apache.poi.hslf.HSLFTestDataSamples;
import org.apache.poi.hslf.model.textproperties.TextPropCollection;
-import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.TextBytesAtom;
import org.apache.poi.hslf.record.TextCharsAtom;
import org.apache.poi.hslf.record.TextHeaderAtom;
/**
* Tests for TextRuns
*/
+@SuppressWarnings("UnusedAssignment")
public final class TestTextRun {
// SlideShow primed on the test data
private HSLFSlideShow ss;
ssRich.close();
ss.close();
}
-
+
/**
* Test to ensure that getting the text works correctly
*/
HSLFSlide slideOne = ss.getSlides().get(0);
List<HSLFTextParagraph> paras = slideOne.getTextParagraphs().get(0);
HSLFTextParagraph para = paras.get(0);
-
+
TextHeaderAtom tha = null;
TextBytesAtom tba = null;
TextCharsAtom tca = null;
else if (r instanceof TextBytesAtom) tba = (TextBytesAtom)r;
else if (r instanceof TextCharsAtom) tca = (TextCharsAtom)r;
}
-
+
assertEquals(changeBytesOnly, HSLFTextParagraph.getRawText(paras));
assertNull(tca);
assertNotNull(tba);
// Bytes -> Chars
- assertNull(tca);
- assertNotNull(tba);
assertEquals(changeBytesOnly, HSLFTextParagraph.getRawText(paras));
String changeByteChar = "This is a test title with a '\u0121' g with a dot";
if (r instanceof TextHeaderAtom) tha = (TextHeaderAtom)r;
else if (r instanceof TextBytesAtom) tba = (TextBytesAtom)r;
else if (r instanceof TextCharsAtom) tca = (TextCharsAtom)r;
- }
+ }
assertEquals(changeByteChar, HSLFTextParagraph.getRawText(paras));
assertNotNull(tca);
assertNull(tba);
// Chars -> Chars
- assertNull(tba);
assertNotNull(tca);
assertEquals(changeByteChar, HSLFTextParagraph.getRawText(paras));
if (r instanceof TextHeaderAtom) tha = (TextHeaderAtom)r;
else if (r instanceof TextBytesAtom) tba = (TextBytesAtom)r;
else if (r instanceof TextCharsAtom) tca = (TextCharsAtom)r;
- }
+ }
assertEquals(changeCharChar, HSLFTextParagraph.getRawText(paras));
assertNotNull(tca);
HSLFSlide sl = ppt.getSlides().get(0);
List<List<HSLFTextParagraph>> textParass = sl.getTextParagraphs();
assertEquals(2, textParass.size());
-
+
List<HSLFTextParagraph> textParas = textParass.get(0);
rt = textParass.get(0).get(0).getTextRuns();
assertEquals(1, rt.size());
// tx.storeText();
}
}
-
+
HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
for(HSLFSlide slide : ppt2.getSlides()){
for(HSLFShape sh : slide.getShapes()){
List<HSLFTextParagraph> run = tx.getTextParagraphs();
HSLFTextRun rt = run.get(0).getTextRuns().get(0);
assertTrue(rt.isBold());
- assertTrue(sameColor(Color.RED, rt.getFontColor()));
+ assertEquals(Color.RED, getColor(rt.getFontColor()));
}
}
}
HSLFSlide slide = ppt.getSlides().get(0);
int[] sizes = {36, 24, 12, 32, 12, 12};
-
+
int i=0;
for (List<HSLFTextParagraph> textParas : slide.getTextParagraphs()) {
- assertEquals("Arial", textParas.get(0).getTextRuns().get(0).getFontFamily());
- assertEquals(sizes[i++], textParas.get(0).getTextRuns().get(0).getFontSize().intValue());
+ HSLFTextRun first = textParas.get(0).getTextRuns().get(0);
+ assertEquals("Arial", first.getFontFamily());
+ assertNotNull(first.getFontSize());
+ assertEquals(sizes[i++], first.getFontSize().intValue());
}
ppt.close();
}
@Ignore
public class TestCommonSL {
- public static boolean sameColor(Color colorExpected, PaintStyle paintActual) {
- if (!(paintActual instanceof SolidPaint)) return false;
- Color thisC = DrawPaint.applyColorTransform(((SolidPaint)paintActual).getSolidColor());
- return thisC.equals(colorExpected);
+ public static Color getColor(PaintStyle paintActual) {
+ return (paintActual instanceof SolidPaint)
+ ? DrawPaint.applyColorTransform(((SolidPaint)paintActual).getSolidColor())
+ : null;
}
}