本文整理汇总了Java中org.apache.poi.ss.usermodel.RichTextString.numFormattingRuns方法的典型用法代码示例。如果您正苦于以下问题:Java RichTextString.numFormattingRuns方法的具体用法?Java RichTextString.numFormattingRuns怎么用?Java RichTextString.numFormattingRuns使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.ss.usermodel.RichTextString
的用法示例。
在下文中一共展示了RichTextString.numFormattingRuns方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toRichText
import org.apache.poi.ss.usermodel.RichTextString; //导入方法依赖的package包/类
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;
}
Map<String, Object> properties = new HashMap<>();
// apply font attributes for formatting run
Font runFont = getFontForFormattingRun(rts, i);
properties.put(TextAttributes.FONT_FAMILY, runFont.getFamily());
properties.put(TextAttributes.FONT_SIZE, runFont.getSizeInPoints() + "pt");
properties.put(TextAttributes.COLOR, runFont.getColor().toString());
if (runFont.isBold()) {
properties.put(TextAttributes.FONT_WEIGHT, "bold");
}
if (runFont.isItalic()) {
properties.put(TextAttributes.FONT_STYLE, "italic");
}
if (runFont.isUnderlined()) {
properties.put(TextAttributes.TEXT_DECORATION, "underline");
}
if (runFont.isStrikeThrough()) {
properties.put(TextAttributes.TEXT_DECORATION, "line-through");
}
properties.put(TextAttributes.COLOR, runFont.getColor());
Style attr = Style.create("style", properties);
push(rtb, TextAttributes.STYLE_START_RUN, attr );
rtb.append(text, start, end);
push(rtb, TextAttributes.STYLE_END_RUN, attr );
start = end;
}
rtb.append(text, start, text.length());
return rtb.toRichText();
}
示例2: substring
import org.apache.poi.ss.usermodel.RichTextString; //导入方法依赖的package包/类
/**
* Extracts a substring of a <code>RichTextString</code> as another
* <code>RichTextString</code>. Preserves the formatting that is in place
* from the given string.
* @param richTextString The <code>RichTextString</code> of which to take a
* substring.
* @param helper A <code>CreationHelper</code> that can create the proper
* <code>RichTextString</code>.
* @param beginIndex The beginning index, inclusive.
* @param endIndex The ending index, exclusive.
* @return The specified substring as a <code>RichTextString</code>, with
* the original formatting from the original string intact.
* @since 0.2.0
*/
public static RichTextString substring(RichTextString richTextString,
CreationHelper helper, int beginIndex, int endIndex)
{
int numFormattingRuns = richTextString.numFormattingRuns();
String value = richTextString.getString();
if (DEBUG)
System.err.println("RTSU.substring: \"" + value + "\" (" + value.length() +
"): numFormattingRuns=" + numFormattingRuns + ", beginIndex: " + beginIndex +
", endIndex: " + endIndex);
List<FormattingRun> formattingRuns = determineFormattingRunStats(richTextString);
// Determine which runs apply in the new substring's range.
List<FormattingRun> substrFormattingRuns = new ArrayList<FormattingRun>();
int begin, end;
for (int i = 0; i < numFormattingRuns; i++)
{
FormattingRun run = formattingRuns.get(i);
begin = run.getBegin();
end = begin + run.getLength();
if ((begin < beginIndex && end < beginIndex) ||
(begin >= endIndex && end >= endIndex))
{
// Not copied to the new substring.
continue;
}
if (begin < beginIndex && end >= beginIndex)
{
// Partial cover at beginning.
begin = beginIndex;
}
if (begin < endIndex && end >= endIndex)
{
// Partial cover at end.
end = endIndex;
}
substrFormattingRuns.add(new FormattingRun(begin - beginIndex, end - begin, run.getFont()));
}
return createFormattedString(substrFormattingRuns.size(), helper, value.substring(beginIndex, endIndex),
substrFormattingRuns);
}