Examples of RichTextRun


Examples of org.apache.poi.hslf.usermodel.RichTextRun

    TextRun trB = textRuns[1];
    assertEquals(3, trB.getRichTextRuns().length);

    // We start with 3 text runs, each with their own set of styles,
    //  but all sharing the same paragraph styles
    RichTextRun rtrB = trB.getRichTextRuns()[0];
    RichTextRun rtrC = trB.getRichTextRuns()[1];
    RichTextRun rtrD = trB.getRichTextRuns()[2];
    TextPropCollection tpBP = rtrB._getRawParagraphStyle();
    TextPropCollection tpBC = rtrB._getRawCharacterStyle();
    TextPropCollection tpCP = rtrC._getRawParagraphStyle();
    TextPropCollection tpCC = rtrC._getRawCharacterStyle();
    TextPropCollection tpDP = rtrD._getRawParagraphStyle();
    TextPropCollection tpDC = rtrD._getRawCharacterStyle();

    // Check text and stylings
    assertEquals(trB.getText().substring(0, 30), rtrB.getText());
    assertNotNull(tpBP);
    assertNotNull(tpBC);
    assertNotNull(tpCP);
    assertNotNull(tpCC);
    assertNotNull(tpDP);
    assertNotNull(tpDC);
    assertTrue(tpBP.equals(tpCP));
    assertTrue(tpBP.equals(tpDP));
    assertTrue(tpCP.equals(tpDP));
    assertFalse(tpBC.equals(tpCC));
    assertFalse(tpBC.equals(tpDC));
    assertFalse(tpCC.equals(tpDC));

    // Check text in the rich runs
    assertEquals("This is the subtitle, in bold\n", rtrB.getText());
    assertEquals("This bit is blue and italic\n", rtrC.getText());
    assertEquals("This bit is red (normal)", rtrD.getText());

    String newBText = "New Subtitle, will still be bold\n";
    String newCText = "New blue and italic text\n";
    String newDText = "Funky new normal red text";
    rtrB.setText(newBText);
    rtrC.setText(newCText);
    rtrD.setText(newDText);
    assertEquals(newBText, rtrB.getText());
    assertEquals(newCText, rtrC.getText());
    assertEquals(newDText, rtrD.getText());

    assertEquals(newBText + newCText + newDText, trB.getText());

    // The styles should have been updated for the new sizes
    assertEquals(newBText.length(), tpBC.getCharactersCovered());
    assertEquals(newCText.length(), tpCC.getCharactersCovered());
    assertEquals(newDText.length()+1, tpDC.getCharactersCovered()); // Last one is always one larger

    assertEquals(
        newBText.length() + newCText.length() + newDText.length(),
        tpBP.getCharactersCovered()
    );

    // Paragraph style should be sum of text length
    assertEquals(newBText.length() + newCText.length() + newDText.length(), tpBP.getCharactersCovered());

    // Check stylings still as expected
    TextPropCollection ntpBC = rtrB._getRawCharacterStyle();
    TextPropCollection ntpCC = rtrC._getRawCharacterStyle();
    TextPropCollection ntpDC = rtrD._getRawCharacterStyle();
    assertEquals(tpBC.getTextPropList(), ntpBC.getTextPropList());
    assertEquals(tpCC.getTextPropList(), ntpCC.getTextPropList());
    assertEquals(tpDC.getTextPropList(), ntpDC.getTextPropList());
    }
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

        //String filename = dirname + "/with_textbox.ppt";
        //new SlideShow(new HSLFSlideShow(filename));

        SlideShow ppt = new SlideShow();
        Slide sl = ppt.createSlide();
        RichTextRun rt;

        String val = "Hello, World!";

        // Create a new textbox, and give it lots of properties
        TextBox txtbox = new TextBox();
        rt = txtbox.getTextRun().getRichTextRuns()[0];
        txtbox.setText(val);
        rt.setFontSize(42);
        rt.setBold(true);
        rt.setItalic(true);
        rt.setUnderlined(false);
        sl.addShape(txtbox);

        // Check it before save
        rt = txtbox.getTextRun().getRichTextRuns()[0];
        assertEquals(val, rt.getText());
        assertEquals(42, rt.getFontSize());
        assertTrue(rt.isBold());
        assertTrue(rt.isItalic());

        // Serialize and read again
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ppt.write(out);
        out.close();

        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));
        sl = ppt.getSlides()[0];

        txtbox = (TextBox)sl.getShapes()[0];
        rt = txtbox.getTextRun().getRichTextRuns()[0];

        // Check after save
        assertEquals(val, rt.getText());
        assertEquals(42, rt.getFontSize());
        assertTrue(rt.isBold());
        assertTrue(rt.isItalic());
        assertFalse(rt.isUnderlined());
    }
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

            int nextBreak = text.indexOf('\n', measurer.getPosition() + 1);

            boolean prStart = text.charAt(startIndex) == '\n';
            if(prStart) measurer.setPosition(startIndex++);

            RichTextRun rt = run.getRichTextRunAt(startIndex == text.length() ? (startIndex-1) : startIndex);
            if(rt == null) {
                logger.log(POILogger.WARN,  "RichTextRun not found at pos" + startIndex + "; text.length: " + text.length());
                break;
            }

            float wrappingWidth = textWidth - _shape.getMarginLeft() - _shape.getMarginRight();
            int bulletOffset = rt.getBulletOffset();
            int textOffset = rt.getTextOffset();
            int indent = rt.getIndentLevel();

            TextRulerAtom ruler = run.getTextRuler();
            if(ruler != null) {
                int bullet_val = ruler.getBulletOffsets()[indent]*Shape.POINT_DPI/Shape.MASTER_DPI;
                int text_val = ruler.getTextOffsets()[indent]*Shape.POINT_DPI/Shape.MASTER_DPI;
                if(bullet_val > text_val){
                    int a = bullet_val;
                    bullet_val = text_val;
                    text_val = a;
                }
                if(bullet_val != 0 ) bulletOffset = bullet_val;
                if(text_val != 0) textOffset = text_val;
            }

            if(bulletOffset > 0 || prStart || startIndex == 0) wrappingWidth -= textOffset;

            if (_shape.getWordWrap() == TextShape.WrapNone) {
                wrappingWidth = _shape.getSheet().getSlideShow().getPageSize().width;
            }

            TextLayout textLayout = measurer.nextLayout(wrappingWidth + 1,
                    nextBreak == -1 ? paragraphEnd : nextBreak, true);
            if (textLayout == null) {
                textLayout = measurer.nextLayout(textWidth,
                    nextBreak == -1 ? paragraphEnd : nextBreak, false);
            }
            if(textLayout == null){
                logger.log(POILogger.WARN, "Failed to break text into lines: wrappingWidth: "+wrappingWidth+
                        "; text: " + rt.getText());
                measurer.setPosition(rt.getEndIndex());
                continue;
            }
            int endIndex = measurer.getPosition();

            float lineHeight = (float)textLayout.getBounds().getHeight();
            int linespacing = rt.getLineSpacing();
            if(linespacing == 0) linespacing = 100;

            TextElement el = new TextElement();
            if(linespacing >= 0){
                el.ascent = textLayout.getAscent()*linespacing/100;
            } else {
                el.ascent = -linespacing*Shape.POINT_DPI/Shape.MASTER_DPI;
            }

            el._align = rt.getAlignment();
            el.advance = textLayout.getAdvance();
            el._textOffset = textOffset;
            el._text = new AttributedString(it, startIndex, endIndex);
            el.textStartIndex = startIndex;
            el.textEndIndex = endIndex;

            if (prStart){
                int sp = rt.getSpaceBefore();
                float spaceBefore;
                if(sp >= 0){
                    spaceBefore = lineHeight * sp/100;
                } else {
                    spaceBefore = -sp*Shape.POINT_DPI/Shape.MASTER_DPI;
                }
                el.ascent += spaceBefore;
            }

            float descent;
            if(linespacing >= 0){
                descent = (textLayout.getDescent() + textLayout.getLeading())*linespacing/100;
            } else {
                descent = -linespacing*Shape.POINT_DPI/Shape.MASTER_DPI;
            }
            if (prStart){
                int sp = rt.getSpaceAfter();
                float spaceAfter;
                if(sp >= 0){
                    spaceAfter = lineHeight * sp/100;
                } else {
                    spaceAfter = -sp*Shape.POINT_DPI/Shape.MASTER_DPI;
                }
                el.ascent += spaceAfter;
            }
            el.descent = descent;

            if(rt.isBullet() && (prStart || startIndex == 0)){
                it.setIndex(startIndex);

                AttributedString bat = new AttributedString(Character.toString(rt.getBulletChar()));
                Color clr = rt.getBulletColor();
                if (clr != null) bat.addAttribute(TextAttribute.FOREGROUND, clr);
                else bat.addAttribute(TextAttribute.FOREGROUND, it.getAttribute(TextAttribute.FOREGROUND));

                int fontIdx = rt.getBulletFont();
                if(fontIdx == -1) fontIdx = rt.getFontIndex();
                PPFont bulletFont = _shape.getSheet().getSlideShow().getFont(fontIdx);
                bat.addAttribute(TextAttribute.FAMILY, bulletFont.getFontName());

                int bulletSize = rt.getBulletSize();
                int fontSize = rt.getFontSize();
                if(bulletSize != -1) fontSize = Math.round(fontSize*bulletSize*0.01f);
                bat.addAttribute(TextAttribute.SIZE, new Float(fontSize));

                if(!new Font(bulletFont.getFontName(), Font.PLAIN, 1).canDisplay(rt.getBulletChar())){
                    bat.addAttribute(TextAttribute.FAMILY, "Arial");
                    bat = new AttributedString("" + DEFAULT_BULLET_CHAR, bat.getIterator().getAttributes());
                }

                if(text.substring(startIndex, endIndex).length() > 1){
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

            int nextBreak = text.indexOf('\n', measurer.getPosition() + 1);

            boolean prStart = text.charAt(startIndex) == '\n';
            if(prStart) measurer.setPosition(startIndex++);

            RichTextRun rt = run.getRichTextRunAt(startIndex == text.length() ? (startIndex-1) : startIndex);
            if(rt == null) {
                logger.log(POILogger.WARN,  "RichTextRun not found at pos" + startIndex + "; text.length: " + text.length());
                break;
            }

            float wrappingWidth = textWidth - _shape.getMarginLeft() - _shape.getMarginRight();
            int bulletOffset = rt.getBulletOffset();
            int textOffset = rt.getTextOffset();
            int indent = rt.getIndentLevel();

            TextRulerAtom ruler = run.getTextRuler();
            if(ruler != null) {
                int bullet_val = ruler.getBulletOffsets()[indent]*Shape.POINT_DPI/Shape.MASTER_DPI;
                int text_val = ruler.getTextOffsets()[indent]*Shape.POINT_DPI/Shape.MASTER_DPI;
                if(bullet_val > text_val){
                    int a = bullet_val;
                    bullet_val = text_val;
                    text_val = a;
                }
                if(bullet_val != 0 ) bulletOffset = bullet_val;
                if(text_val != 0) textOffset = text_val;
            }

            if(bulletOffset > 0 || prStart || startIndex == 0) wrappingWidth -= textOffset;

            if (_shape.getWordWrap() == TextShape.WrapNone) {
                wrappingWidth = _shape.getSheet().getSlideShow().getPageSize().width;
            }

            TextLayout textLayout = measurer.nextLayout(wrappingWidth + 1,
                    nextBreak == -1 ? paragraphEnd : nextBreak, true);
            if (textLayout == null) {
                textLayout = measurer.nextLayout(textWidth,
                    nextBreak == -1 ? paragraphEnd : nextBreak, false);
            }
            if(textLayout == null){
                logger.log(POILogger.WARN, "Failed to break text into lines: wrappingWidth: "+wrappingWidth+
                        "; text: " + rt.getText());
                measurer.setPosition(rt.getEndIndex());
                continue;
            }
            int endIndex = measurer.getPosition();

            float lineHeight = (float)textLayout.getBounds().getHeight();
            int linespacing = rt.getLineSpacing();
            if(linespacing == 0) linespacing = 100;

            TextElement el = new TextElement();
            if(linespacing >= 0){
                el.ascent = textLayout.getAscent()*linespacing/100;
            } else {
                el.ascent = -linespacing*Shape.POINT_DPI/Shape.MASTER_DPI;
            }

            el._align = rt.getAlignment();
            el.advance = textLayout.getAdvance();
            el._textOffset = textOffset;
            el._text = new AttributedString(it, startIndex, endIndex);
            el.textStartIndex = startIndex;
            el.textEndIndex = endIndex;

            if (prStart){
                int sp = rt.getSpaceBefore();
                float spaceBefore;
                if(sp >= 0){
                    spaceBefore = lineHeight * sp/100;
                } else {
                    spaceBefore = -sp*Shape.POINT_DPI/Shape.MASTER_DPI;
                }
                el.ascent += spaceBefore;
            }

            float descent;
            if(linespacing >= 0){
                descent = (textLayout.getDescent() + textLayout.getLeading())*linespacing/100;
            } else {
                descent = -linespacing*Shape.POINT_DPI/Shape.MASTER_DPI;
            }
            if (prStart){
                int sp = rt.getSpaceAfter();
                float spaceAfter;
                if(sp >= 0){
                    spaceAfter = lineHeight * sp/100;
                } else {
                    spaceAfter = -sp*Shape.POINT_DPI/Shape.MASTER_DPI;
                }
                el.ascent += spaceAfter;
            }
            el.descent = descent;

            if(rt.isBullet() && (prStart || startIndex == 0)){
                it.setIndex(startIndex);

                AttributedString bat = new AttributedString(Character.toString(rt.getBulletChar()));
                Color clr = rt.getBulletColor();
                if (clr != null) bat.addAttribute(TextAttribute.FOREGROUND, clr);
                else bat.addAttribute(TextAttribute.FOREGROUND, it.getAttribute(TextAttribute.FOREGROUND));

                int fontIdx = rt.getBulletFont();
                if(fontIdx == -1) fontIdx = rt.getFontIndex();
                PPFont bulletFont = _shape.getSheet().getSlideShow().getFont(fontIdx);
                bat.addAttribute(TextAttribute.FAMILY, bulletFont.getFontName());

                int bulletSize = rt.getBulletSize();
                int fontSize = rt.getFontSize();
                if(bulletSize != -1) fontSize = Math.round(fontSize*bulletSize*0.01f);
                bat.addAttribute(TextAttribute.SIZE, new Float(fontSize));

                if(!new Font(bulletFont.getFontName(), Font.PLAIN, 1).canDisplay(rt.getBulletChar())){
                    bat.addAttribute(TextAttribute.FAMILY, "Arial");
                    bat = new AttributedString("" + DEFAULT_BULLET_CHAR, bat.getIterator().getAttributes());
                }

                if(text.substring(startIndex, endIndex).length() > 1){
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

     */
    public Rectangle2D resizeToFitText(){
        String txt = getText();
        if(txt == null || txt.length() == 0) return new Rectangle2D.Float();

        RichTextRun rt = getTextRun().getRichTextRuns()[0];
        int size = rt.getFontSize();
        int style = 0;
        if (rt.isBold()) style |= Font.BOLD;
        if (rt.isItalic()) style |= Font.ITALIC;
        String fntname = rt.getFontName();
        Font font = new Font(fntname, style, size);

        float width = 0, height = 0, leading = 0;
        String[] lines = txt.split("\n");
        for (int i = 0; i < lines.length; i++) {
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

        TextBox txt = new TextBox(_group);
        txt.getTextRun().supplySlideShow(_group.getSheet().getSlideShow());
        txt.getTextRun().setSheet(_group.getSheet());
        txt.setText(s);

        RichTextRun rt = txt.getTextRun().getRichTextRuns()[0];
        rt.setFontSize(_font.getSize());
        rt.setFontName(_font.getFamily());

        if (getColor() != null) rt.setFontColor(getColor());
        if (_font.isBold()) rt.setBold(true);
        if (_font.isItalic()) rt.setItalic(true);

        txt.setMarginBottom(0);
        txt.setMarginTop(0);
        txt.setMarginLeft(0);
        txt.setMarginRight(0);
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

     */
    public Rectangle2D resizeToFitText(){
        String txt = getText();
        if(txt == null || txt.length() == 0) return new Rectangle2D.Float();

        RichTextRun rt = getTextRun().getRichTextRuns()[0];
        int size = rt.getFontSize();
        int style = 0;
        if (rt.isBold()) style |= Font.BOLD;
        if (rt.isItalic()) style |= Font.ITALIC;
        String fntname = rt.getFontName();
        Font font = new Font(fntname, style, size);

        float width = 0, height = 0, leading = 0;
        String[] lines = txt.split("\n");
        for (int i = 0; i < lines.length; i++) {
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

  public void buildRichTextRuns(LinkedList<TextPropCollection> pStyles, LinkedList<TextPropCollection> cStyles, String runRawText){

        // Handle case of no current style, with a default
        if(pStyles.size() == 0 || cStyles.size() == 0) {
            _rtRuns = new RichTextRun[1];
            _rtRuns[0] = new RichTextRun(this, 0, runRawText.length());
        } else {
            // Build up Rich Text Runs, one for each
            //  character/paragraph style pair
            List<RichTextRun> rtrs = new ArrayList<RichTextRun>();

            int pos = 0;

            int curP = 0;
            int curC = 0;
            int pLenRemain = -1;
            int cLenRemain = -1;

            // Build one for each run with the same style
            while(pos <= runRawText.length() && curP < pStyles.size() && curC < cStyles.size()) {
                // Get the Props to use
                TextPropCollection pProps = (TextPropCollection)pStyles.get(curP);
                TextPropCollection cProps = (TextPropCollection)cStyles.get(curC);

                int pLen = pProps.getCharactersCovered();
                int cLen = cProps.getCharactersCovered();

                // Handle new pass
                boolean freshSet = false;
                if(pLenRemain == -1 && cLenRemain == -1) { freshSet = true; }
                if(pLenRemain == -1) { pLenRemain = pLen; }
                if(cLenRemain == -1) { cLenRemain = cLen; }

                // So we know how to build the eventual run
                int runLen = -1;
                boolean pShared = false;
                boolean cShared = false;

                // Same size, new styles - neither shared
                if(pLen == cLen && freshSet) {
                    runLen = cLen;
                    pShared = false;
                    cShared = false;
                    curP++;
                    curC++;
                    pLenRemain = -1;
                    cLenRemain = -1;
                } else {
                    // Some sharing

                    // See if we are already in a shared block
                    if(pLenRemain < pLen) {
                        // Existing shared p block
                        pShared = true;

                        // Do we end with the c block, or either side of it?
                        if(pLenRemain == cLenRemain) {
                            // We end at the same time
                            cShared = false;
                            runLen = pLenRemain;
                            curP++;
                            curC++;
                            pLenRemain = -1;
                            cLenRemain = -1;
                        } else if(pLenRemain < cLenRemain) {
                            // We end before the c block
                            cShared = true;
                            runLen = pLenRemain;
                            curP++;
                            cLenRemain -= pLenRemain;
                            pLenRemain = -1;
                        } else {
                            // We end after the c block
                            cShared = false;
                            runLen = cLenRemain;
                            curC++;
                            pLenRemain -= cLenRemain;
                            cLenRemain = -1;
                        }
                    } else if(cLenRemain < cLen) {
                        // Existing shared c block
                        cShared = true;

                        // Do we end with the p block, or either side of it?
                        if(pLenRemain == cLenRemain) {
                            // We end at the same time
                            pShared = false;
                            runLen = cLenRemain;
                            curP++;
                            curC++;
                            pLenRemain = -1;
                            cLenRemain = -1;
                        } else if(cLenRemain < pLenRemain) {
                            // We end before the p block
                            pShared = true;
                            runLen = cLenRemain;
                            curC++;
                            pLenRemain -= cLenRemain;
                            cLenRemain = -1;
                        } else {
                            // We end after the p block
                            pShared = false;
                            runLen = pLenRemain;
                            curP++;
                            cLenRemain -= pLenRemain;
                            pLenRemain = -1;
                        }
                    } else {
                        // Start of a shared block
                        if(pLenRemain < cLenRemain) {
                            // Shared c block
                            pShared = false;
                            cShared = true;
                            runLen = pLenRemain;
                            curP++;
                            cLenRemain -= pLenRemain;
                            pLenRemain = -1;
                        } else {
                            // Shared p block
                            pShared = true;
                            cShared = false;
                            runLen = cLenRemain;
                            curC++;
                            pLenRemain -= cLenRemain;
                            cLenRemain = -1;
                        }
                    }
                }

                // Wind on
                int prevPos = pos;
                pos += runLen;
                // Adjust for end-of-run extra 1 length
                if(pos > runRawText.length()) {
                    runLen--;
                }

                // Save
                RichTextRun rtr = new RichTextRun(this, prevPos, runLen, pProps, cProps, pShared, cShared);
                rtrs.add(rtr);
            }

            // Build the array
            _rtRuns = rtrs.toArray(new RichTextRun[rtrs.size()]);
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

      _styleAtom.addParagraphTextPropCollection(s.length()+pOverRun);
    TextPropCollection newCTP =
      _styleAtom.addCharacterTextPropCollection(s.length()+cOverRun);

    // Now, create the new RichTextRun
    RichTextRun nr = new RichTextRun(
        this, oldSize, s.length(),
        newPTP, newCTP, false, false
    );

    // Add the new RichTextRun onto our list
View Full Code Here

Examples of org.apache.poi.hslf.usermodel.RichTextRun

   * If you care about styling, do setText on a RichTextRun instead
   */
  public void setRawText(String s) {
    // Save the new text to the atoms
    storeText(s);
    RichTextRun fst = _rtRuns[0];

    // Finally, zap and re-do the RichTextRuns
    for(int i=0; i<_rtRuns.length; i++) { _rtRuns[i] = null; }
    _rtRuns = new RichTextRun[1];
        _rtRuns[0] = fst;

    // Now handle record stylings:
    // If there isn't styling
    //  no change, stays with no styling
    // If there is styling:
    //  everthing gets the same style that the first block has
        // Update the lengths +1 for since these will be the only runs
    if(_styleAtom != null) {
      LinkedList<TextPropCollection> pStyles = _styleAtom.getParagraphStyles();
      while(pStyles.size() > 1) { pStyles.removeLast(); }

            if (!pStyles.isEmpty()) {
                pStyles.getFirst().updateTextSize( s.length()+1 );
            }

      LinkedList<TextPropCollection> cStyles = _styleAtom.getCharacterStyles();
      while(cStyles.size() > 1) { cStyles.removeLast(); }
           
            if (!cStyles.isEmpty()) {
                cStyles.getFirst().updateTextSize( s.length()+1 );
            }
     
      _rtRuns[0].setText(s);
    } else {
      // Recreate rich text run with no styling
      _rtRuns[0] = new RichTextRun(this,0,s.length());
    }

  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.