Example usage for org.apache.poi.ss.usermodel RichTextString numFormattingRuns

List of usage examples for org.apache.poi.ss.usermodel RichTextString numFormattingRuns

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel RichTextString numFormattingRuns.

Prototype

int numFormattingRuns();

Source Link

Usage

From source file:com.dua3.meja.model.poi.PoiCell.java

License:Apache License

public RichText toRichText(RichTextString rts) {
    String text = rts.getString();
    //TODO: properly process tabs
    text = text.replace('\t', ' '); // tab
    text = text.replace((char) 160, ' '); // non-breaking space

    RichTextBuilder rtb = new RichTextBuilder();
    int start = 0;
    for (int i = 0; i < rts.numFormattingRuns(); i++) {
        start = rts.getIndexOfFormattingRun(i);
        int end = i + 1 < rts.numFormattingRuns() ? rts.getIndexOfFormattingRun(i + 1) : rts.length();

        if (start == end) {
            // skip empty
            continue;
        }//  w  w  w.  jav  a2  s. com

        // apply font attributes for formatting run
        PoiFont runFont = getFontForFormattingRun(rts, i);
        rtb.push(Style.FONT_FAMILY, runFont.getFamily());
        rtb.push(Style.FONT_SIZE, runFont.getSizeInPoints() + "pt");
        rtb.push(Style.COLOR, runFont.getColor().toString());
        if (runFont.isBold()) {
            rtb.push(Style.FONT_WEIGHT, "bold");
        }
        if (runFont.isItalic()) {
            rtb.push(Style.FONT_STYLE, "italic");
        }
        if (runFont.isUnderlined()) {
            rtb.push(Style.TEXT_DECORATION, "underline");
        }
        if (runFont.isStrikeThrough()) {
            rtb.push(Style.TEXT_DECORATION, "line-through");
        }

        rtb.append(text, start, end);
        start = end;
    }
    rtb.append(text, start, text.length());

    return rtb.toRichText();
}