use of java.text.AttributedCharacterIterator in project poi by apache.
the class CellDateFormatter method formatValue.
/** {@inheritDoc} */
public void formatValue(StringBuffer toAppendTo, Object value) {
if (value == null)
value = 0.0;
if (value instanceof Number) {
Number num = (Number) value;
long v = num.longValue();
if (v == 0L) {
value = EXCEL_EPOCH_CAL.getTime();
} else {
Calendar c = (Calendar) EXCEL_EPOCH_CAL.clone();
c.add(Calendar.SECOND, (int) (v / 1000));
c.add(Calendar.MILLISECOND, (int) (v % 1000));
value = c.getTime();
}
}
AttributedCharacterIterator it = dateFmt.formatToCharacterIterator(value);
boolean doneAm = false;
boolean doneMillis = false;
it.first();
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
if (it.getAttribute(DateFormat.Field.MILLISECOND) != null) {
if (!doneMillis) {
Date dateObj = (Date) value;
int pos = toAppendTo.length();
Formatter formatter = new Formatter(toAppendTo, Locale.ROOT);
try {
long msecs = dateObj.getTime() % 1000;
formatter.format(LocaleUtil.getUserLocale(), sFmt, msecs / 1000.0);
} finally {
formatter.close();
}
toAppendTo.delete(pos, pos + 2);
doneMillis = true;
}
} else if (it.getAttribute(DateFormat.Field.AM_PM) != null) {
if (!doneAm) {
if (showAmPm) {
if (amPmUpper) {
toAppendTo.append(Character.toUpperCase(ch));
if (showM)
toAppendTo.append('M');
} else {
toAppendTo.append(Character.toLowerCase(ch));
if (showM)
toAppendTo.append('m');
}
}
doneAm = true;
}
} else {
toAppendTo.append(ch);
}
}
}
use of java.text.AttributedCharacterIterator 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();
}
use of java.text.AttributedCharacterIterator in project intellij-community by JetBrains.
the class SimpleColoredComponent method createAndCacheTextLayout.
private TextLayout createAndCacheTextLayout(int fragmentIndex, Font basefont, FontRenderContext fontRenderContext) {
String text = myFragments.get(fragmentIndex);
AttributedString string = new AttributedString(text);
int start = 0;
int end = text.length();
AttributedCharacterIterator it = string.getIterator(new AttributedCharacterIterator.Attribute[0], start, end);
Font currentFont = basefont;
int currentIndex = start;
for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
Font font = basefont;
if (!font.canDisplay(c)) {
for (SuitableFontProvider provider : SuitableFontProvider.EP_NAME.getExtensions()) {
font = provider.getFontAbleToDisplay(c, basefont.getSize(), basefont.getStyle(), basefont.getFamily());
if (font != null)
break;
}
}
int i = it.getIndex();
if (!Comparing.equal(currentFont, font)) {
if (i > currentIndex) {
string.addAttribute(TextAttribute.FONT, currentFont, currentIndex, i);
}
currentFont = font;
currentIndex = i;
}
}
if (currentIndex < end) {
string.addAttribute(TextAttribute.FONT, currentFont, currentIndex, end);
}
TextLayout layout = new TextLayout(string.getIterator(), fontRenderContext);
if (fragmentIndex >= myLayouts.size()) {
myLayouts.addAll(Collections.nCopies(fragmentIndex - myLayouts.size() + 1, null));
}
myLayouts.set(fragmentIndex, layout);
myLayoutFont = getBaseFont();
return layout;
}
use of java.text.AttributedCharacterIterator in project jdk8u_jdk by JetBrains.
the class InputMethodContext method dispatchCommittedText.
/**
* Dispatches committed text to a client component.
* Called by composition window.
*
* @param client The component that the text should get dispatched to.
* @param text The iterator providing access to the committed
* (and possible composed) text.
* @param committedCharacterCount The number of committed characters in the text.
*/
synchronized void dispatchCommittedText(Component client, AttributedCharacterIterator text, int committedCharacterCount) {
// the event was in the queue.
if (committedCharacterCount == 0 || text.getEndIndex() <= text.getBeginIndex()) {
return;
}
long time = System.currentTimeMillis();
dispatchingCommittedText = true;
try {
InputMethodRequests req = client.getInputMethodRequests();
if (req != null) {
// active client -> send text as InputMethodEvent
int beginIndex = text.getBeginIndex();
AttributedCharacterIterator toBeCommitted = (new AttributedString(text, beginIndex, beginIndex + committedCharacterCount)).getIterator();
InputMethodEvent inputEvent = new InputMethodEvent(client, InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, toBeCommitted, committedCharacterCount, null, null);
client.dispatchEvent(inputEvent);
} else {
// passive client -> send text as KeyEvents
char keyChar = text.first();
while (committedCharacterCount-- > 0 && keyChar != CharacterIterator.DONE) {
KeyEvent keyEvent = new KeyEvent(client, KeyEvent.KEY_TYPED, time, 0, KeyEvent.VK_UNDEFINED, keyChar);
client.dispatchEvent(keyEvent);
keyChar = text.next();
}
}
} finally {
dispatchingCommittedText = false;
}
}
use of java.text.AttributedCharacterIterator in project jdk8u_jdk by JetBrains.
the class SwingUtilities2 method drawString.
/*
* see documentation for drawChars
* returns the advance
*/
public static float drawString(JComponent c, Graphics g, AttributedCharacterIterator iterator, int x, int y) {
float retVal;
boolean isPrinting = isPrinting(g);
Color col = g.getColor();
if (isPrinting) {
/* Use alternate print color if specified */
if (col instanceof PrintColorUIResource) {
g.setColor(((PrintColorUIResource) col).getPrintColor());
}
}
Graphics2D g2d = getGraphics2D(g);
if (g2d == null) {
//for the cases where advance
g.drawString(iterator, x, y);
//matters it should not happen
retVal = x;
} else {
FontRenderContext frc;
if (isPrinting) {
frc = getFontRenderContext(c);
if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
frc = new FontRenderContext(frc.getTransform(), false, false);
}
} else if ((frc = getFRCProperty(c)) != null) {
/* frc = frc; ! */
} else {
frc = g2d.getFontRenderContext();
}
TextLayout layout;
if (isPrinting) {
FontRenderContext deviceFRC = g2d.getFontRenderContext();
if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
layout = new TextLayout(iterator, deviceFRC);
AttributedCharacterIterator trimmedIt = getTrimmedTrailingSpacesIterator(iterator);
if (trimmedIt != null) {
float screenWidth = new TextLayout(trimmedIt, frc).getAdvance();
layout = layout.getJustifiedLayout(screenWidth);
}
} else {
layout = new TextLayout(iterator, frc);
}
} else {
layout = new TextLayout(iterator, frc);
}
layout.draw(g2d, x, y);
retVal = layout.getAdvance();
}
if (isPrinting) {
g.setColor(col);
}
return retVal;
}
Aggregations