Search in sources :

Example 11 with PaintStyle

use of org.apache.poi.sl.usermodel.PaintStyle in project poi by apache.

the class DrawTextParagraph method getAttributedString.

protected AttributedString getAttributedString(Graphics2D graphics, StringBuilder text) {
    List<AttributedStringData> attList = new ArrayList<AttributedStringData>();
    if (text == null) {
        text = new StringBuilder();
    }
    PlaceableShape<?, ?> ps = getParagraphShape();
    DrawFontManager fontHandler = (DrawFontManager) graphics.getRenderingHint(Drawable.FONT_HANDLER);
    @SuppressWarnings("unchecked") Map<String, String> fontMap = (Map<String, String>) graphics.getRenderingHint(Drawable.FONT_MAP);
    @SuppressWarnings("unchecked") Map<String, String> fallbackMap = (Map<String, String>) graphics.getRenderingHint(Drawable.FONT_FALLBACK);
    for (TextRun run : paragraph) {
        String runText = getRenderableText(graphics, run);
        // skip empty runs
        if (runText.isEmpty()) {
            continue;
        }
        // user can pass an custom object to convert fonts
        String mappedFont = run.getFontFamily();
        String fallbackFont = Font.SANS_SERIF;
        if (mappedFont == null) {
            mappedFont = paragraph.getDefaultFontFamily();
        }
        if (mappedFont == null) {
            mappedFont = Font.SANS_SERIF;
        }
        if (fontHandler != null) {
            String font = fontHandler.getRendererableFont(mappedFont, run.getPitchAndFamily());
            if (font != null) {
                mappedFont = font;
            }
            font = fontHandler.getFallbackFont(mappedFont, run.getPitchAndFamily());
            if (font != null) {
                fallbackFont = font;
            }
        } else {
            mappedFont = getFontWithFallback(fontMap, mappedFont);
            fallbackFont = getFontWithFallback(fallbackMap, mappedFont);
        }
        runText = mapFontCharset(runText, mappedFont);
        int beginIndex = text.length();
        text.append(runText);
        int endIndex = text.length();
        attList.add(new AttributedStringData(TextAttribute.FAMILY, mappedFont, beginIndex, endIndex));
        PaintStyle fgPaintStyle = run.getFontColor();
        Paint fgPaint = new DrawPaint(ps).getPaint(graphics, fgPaintStyle);
        attList.add(new AttributedStringData(TextAttribute.FOREGROUND, fgPaint, beginIndex, endIndex));
        Double fontSz = run.getFontSize();
        if (fontSz == null) {
            fontSz = paragraph.getDefaultFontSize();
        }
        attList.add(new AttributedStringData(TextAttribute.SIZE, fontSz.floatValue(), beginIndex, endIndex));
        if (run.isBold()) {
            attList.add(new AttributedStringData(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, beginIndex, endIndex));
        }
        if (run.isItalic()) {
            attList.add(new AttributedStringData(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, beginIndex, endIndex));
        }
        if (run.isUnderlined()) {
            attList.add(new AttributedStringData(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, beginIndex, endIndex));
            attList.add(new AttributedStringData(TextAttribute.INPUT_METHOD_UNDERLINE, TextAttribute.UNDERLINE_LOW_TWO_PIXEL, beginIndex, endIndex));
        }
        if (run.isStrikethrough()) {
            attList.add(new AttributedStringData(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, beginIndex, endIndex));
        }
        if (run.isSubscript()) {
            attList.add(new AttributedStringData(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB, beginIndex, endIndex));
        }
        if (run.isSuperscript()) {
            attList.add(new AttributedStringData(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, beginIndex, endIndex));
        }
        Hyperlink<?, ?> hl = run.getHyperlink();
        if (hl != null) {
            attList.add(new AttributedStringData(HYPERLINK_HREF, hl.getAddress(), beginIndex, endIndex));
            attList.add(new AttributedStringData(HYPERLINK_LABEL, hl.getLabel(), beginIndex, endIndex));
        }
        int style = (run.isBold() ? Font.BOLD : 0) | (run.isItalic() ? Font.ITALIC : 0);
        Font f = new Font(mappedFont, style, (int) Math.rint(fontSz));
        // check for unsupported characters and add a fallback font for these
        char[] textChr = runText.toCharArray();
        int nextEnd = canDisplayUpTo(f, textChr, 0, textChr.length);
        int last = nextEnd;
        boolean isNextValid = (nextEnd == 0);
        while (nextEnd != -1 && nextEnd <= textChr.length) {
            if (isNextValid) {
                nextEnd = canDisplayUpTo(f, textChr, nextEnd, textChr.length);
                isNextValid = false;
            } else {
                if (nextEnd >= textChr.length || f.canDisplay(Character.codePointAt(textChr, nextEnd, textChr.length))) {
                    attList.add(new AttributedStringData(TextAttribute.FAMILY, fallbackFont, beginIndex + last, beginIndex + Math.min(nextEnd, textChr.length)));
                    if (nextEnd >= textChr.length) {
                        break;
                    }
                    last = nextEnd;
                    isNextValid = true;
                } else {
                    boolean isHS = Character.isHighSurrogate(textChr[nextEnd]);
                    nextEnd += (isHS ? 2 : 1);
                }
            }
        }
    }
    // We need this trick to correctly measure text
    if (text.length() == 0) {
        Double fontSz = paragraph.getDefaultFontSize();
        text.append(" ");
        attList.add(new AttributedStringData(TextAttribute.SIZE, fontSz.floatValue(), 0, 1));
    }
    AttributedString string = new AttributedString(text.toString());
    for (AttributedStringData asd : attList) {
        string.addAttribute(asd.attribute, asd.value, asd.beginIndex, asd.endIndex);
    }
    return string;
}
Also used : ArrayList(java.util.ArrayList) AttributedString(java.text.AttributedString) TextRun(org.apache.poi.sl.usermodel.TextRun) Paint(java.awt.Paint) Paint(java.awt.Paint) Font(java.awt.Font) AttributedString(java.text.AttributedString) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) Map(java.util.Map)

Example 12 with PaintStyle

use of org.apache.poi.sl.usermodel.PaintStyle in project poi by apache.

the class TestXSLFTheme method slide5.

void slide5(XSLFSlide slide) {
    PaintStyle fs = slide.getBackground().getFillStyle().getPaint();
    assertTrue(fs instanceof TexturePaint);
    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()));
    // no fill
    assertNull(sh2.getFillColor());
    // font size is 40pt and scale factor is 90%
    assertEquals(36.0, run2.getFontSize(), 0);
    assertTrue(slide.getSlideLayout().getFollowMasterGraphics());
}
Also used : TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) Color(java.awt.Color) TestCommonSL.sameColor(org.apache.poi.sl.TestCommonSL.sameColor)

Example 13 with PaintStyle

use of org.apache.poi.sl.usermodel.PaintStyle in project poi by apache.

the class TestXSLFTheme method slide10.

void slide10(XSLFSlide slide) {
    PaintStyle fs = slide.getBackground().getFillStyle().getPaint();
    assertTrue(fs instanceof GradientPaint);
    XSLFTextShape sh1 = (XSLFTextShape) getShape(slide, "Title 3");
    XSLFTextRun run1 = sh1.getTextParagraphs().get(0).getTextRuns().get(0);
    assertTrue(sameColor(Color.white, run1.getFontColor()));
    // no fill
    assertNull(sh1.getFillColor());
    XSLFTextShape sh2 = (XSLFTextShape) getShape(slide, "Subtitle 4");
    XSLFTextRun run2 = sh2.getTextParagraphs().get(0).getTextRuns().get(0);
    assertTrue(sameColor(Color.white, run2.getFontColor()));
    // no fill
    assertNull(sh2.getFillColor());
}
Also used : PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)

Example 14 with PaintStyle

use of org.apache.poi.sl.usermodel.PaintStyle in project poi by apache.

the class TestXSLFTheme method slide3.

void slide3(XSLFSlide slide) {
    PaintStyle fs = slide.getBackground().getFillStyle().getPaint();
    assertTrue(fs instanceof GradientPaint);
}
Also used : PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)

Example 15 with PaintStyle

use of org.apache.poi.sl.usermodel.PaintStyle in project poi by apache.

the class TestXSLFTheme method slide9.

void slide9(XSLFSlide slide) {
    PaintStyle fs = slide.getBackground().getFillStyle().getPaint();
    assertTrue(fs instanceof TexturePaint);
}
Also used : TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle)

Aggregations

PaintStyle (org.apache.poi.sl.usermodel.PaintStyle)20 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)6 Color (java.awt.Color)5 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)5 XSLFFillProperties (org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties)5 GradientPaint (org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)4 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)3 DrawPaint (org.apache.poi.sl.draw.DrawPaint)3 Test (org.junit.Test)3 CTSchemeColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor)3 CTShapeStyle (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeStyle)3 Paint (java.awt.Paint)2 AttributedString (java.text.AttributedString)2 TestCommonSL.sameColor (org.apache.poi.sl.TestCommonSL.sameColor)2 TextRun (org.apache.poi.sl.usermodel.TextRun)2 PropertyFetcher (org.apache.poi.xslf.model.PropertyFetcher)2 XmlCursor (org.apache.xmlbeans.XmlCursor)2 XmlObject (org.apache.xmlbeans.XmlObject)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Font (java.awt.Font)1