Search in sources :

Example 1 with DummyGraphics2d

use of org.apache.poi.hssf.usermodel.DummyGraphics2d in project poi by apache.

the class TestBugs method bug57796.

@Test
public void bug57796() throws IOException {
    HSLFSlideShow ppt = open("WithLinks.ppt");
    HSLFSlide slide = ppt.getSlides().get(0);
    HSLFTextShape shape = (HSLFTextShape) slide.getShapes().get(1);
    List<HSLFHyperlink> hlList = HSLFHyperlink.find(shape);
    HSLFHyperlink hlShape = hlList.get(0);
    HSLFTextRun r = shape.getTextParagraphs().get(1).getTextRuns().get(0);
    HSLFHyperlink hlRun = r.getHyperlink();
    assertEquals(hlRun.getId(), hlShape.getId());
    assertEquals(hlRun.getAddress(), hlShape.getAddress());
    assertEquals(hlRun.getLabel(), hlShape.getLabel());
    assertEquals(hlRun.getTypeEnum(), hlShape.getTypeEnum());
    assertEquals(hlRun.getStartIndex(), hlShape.getStartIndex());
    assertEquals(hlRun.getEndIndex(), hlShape.getEndIndex());
    OutputStream nullOutput = new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        }
    };
    final boolean[] found = { false };
    DummyGraphics2d dgfx = new DummyGraphics2d(new PrintStream(nullOutput)) {

        @Override
        public void drawString(AttributedCharacterIterator iterator, float x, float y) {
            // For the test file, common sl draws textruns one by one and not mixed
            // so we evaluate the whole iterator
            Map<Attribute, Object> attributes = null;
            StringBuffer sb = new StringBuffer();
            for (char c = iterator.first(); c != CharacterIterator.DONE; c = iterator.next()) {
                sb.append(c);
                attributes = iterator.getAttributes();
            }
            if ("Jakarta HSSF".equals(sb.toString())) {
                // this is a test for a manually modified ppt, for real hyperlink label
                // one would need to access the screen tip record
                String href = (String) attributes.get(DrawTextParagraph.HYPERLINK_HREF);
                String label = (String) attributes.get(DrawTextParagraph.HYPERLINK_LABEL);
                assertEquals("http://jakarta.apache.org/poi/hssf/", href);
                assertEquals("Open Jakarta POI HSSF module test  ", label);
                found[0] = true;
            }
        }
    };
    ppt.getSlides().get(1).draw(dgfx);
    assertTrue(found[0]);
    ppt.close();
}
Also used : PrintStream(java.io.PrintStream) Attribute(java.text.AttributedCharacterIterator.Attribute) OutputStream(java.io.OutputStream) AttributedCharacterIterator(java.text.AttributedCharacterIterator) DummyGraphics2d(org.apache.poi.hssf.usermodel.DummyGraphics2d) Test(org.junit.Test)

Example 2 with DummyGraphics2d

use of org.apache.poi.hssf.usermodel.DummyGraphics2d in project poi by apache.

the class TestPicture method bug54541.

@Test
@Ignore("Just for visual validation - antialiasing is different on various systems")
public void bug54541() throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
    String[] files = { //            "54541_cropped_bitmap2.ppt",
    "alterman_security.ppt" };
    BitSet pages = new BitSet();
    pages.set(2);
    for (String file : files) {
        InputStream is = _slTests.openResourceAsStream(file);
        SlideShow<?, ?> ss;
        if (file.endsWith("pptx")) {
            Class<?> cls = Class.forName("org.apache.poi.xslf.usermodel.XMLSlideShow");
            Constructor<?> ct = cls.getDeclaredConstructor(InputStream.class);
            ss = (SlideShow<?, ?>) ct.newInstance(is);
        } else {
            ss = new HSLFSlideShow(is);
        }
        is.close();
        boolean debugOut = false;
        Dimension pg = ss.getPageSize();
        for (Slide<?, ?> slide : ss.getSlides()) {
            int slideNo = slide.getSlideNumber();
            if (!pages.get(slideNo - 1)) {
                if (pages.nextSetBit(slideNo - 1) == -1)
                    break;
                else
                    continue;
            }
            if (debugOut) {
                DummyGraphics2d graphics = new DummyGraphics2d();
                slide.draw(graphics);
            } else {
                BufferedImage img = new BufferedImage(pg.width, pg.height, BufferedImage.TYPE_INT_ARGB);
                Graphics2D graphics = img.createGraphics();
                DrawFactory.getInstance(graphics).fixFonts(graphics);
                slide.draw(graphics);
                graphics.setColor(Color.BLACK);
                graphics.setStroke(new BasicStroke(1));
                graphics.drawRect(0, 0, (int) pg.getWidth() - 1, (int) pg.getHeight() - 1);
                ImageIO.write(img, "PNG", new File(file.replaceFirst(".pptx?", "-") + slideNo + ".png"));
            }
        }
        ss.close();
    }
}
Also used : BasicStroke(java.awt.BasicStroke) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BitSet(java.util.BitSet) Dimension(java.awt.Dimension) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) DummyGraphics2d(org.apache.poi.hssf.usermodel.DummyGraphics2d) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DummyGraphics2d (org.apache.poi.hssf.usermodel.DummyGraphics2d)2 Test (org.junit.Test)2 BasicStroke (java.awt.BasicStroke)1 Dimension (java.awt.Dimension)1 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 Attribute (java.text.AttributedCharacterIterator.Attribute)1 BitSet (java.util.BitSet)1 Ignore (org.junit.Ignore)1