Search in sources :

Example 16 with AttributedCharacterIterator

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);
        }
    }
}
Also used : Formatter(java.util.Formatter) Calendar(java.util.Calendar) Date(java.util.Date) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 17 with AttributedCharacterIterator

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();
}
Also used : PrintStream(java.io.PrintStream) Attribute(java.text.AttributedCharacterIterator.Attribute) OutputStream(java.io.OutputStream) AttributedCharacterIterator(java.text.AttributedCharacterIterator) DummyGraphics2d(org.apache.poi.hssf.usermodel.DummyGraphics2d) Test(org.junit.Test)

Example 18 with AttributedCharacterIterator

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;
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 19 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project robovm by robovm.

the class OldAttributedCharacterIteratorTest method test_getRunLimit.

public void test_getRunLimit() {
    int limit = it.getRunLimit();
    assertEquals(string.length(), limit);
    AttributedString as = new AttributedString("");
    assertEquals(0, as.getIterator().getRunLimit());
    as = new AttributedString(new AttributedString("test text").getIterator(), 2, 7);
    AttributedCharacterIterator it = as.getIterator();
    assertEquals(5, it.getRunLimit());
}
Also used : AttributedString(java.text.AttributedString) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 20 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project limelight by slagyr.

the class TextPanel method addLines.

private synchronized void addLines() {
    AttributedString aText = prepareAttributedString();
    AttributedCharacterIterator styledTextIterator = aText.getIterator();
    List<Integer> newlineLocations = getNewlineLocations(styledTextIterator);
    LineBreakMeasurer lbm = new LineBreakMeasurer(styledTextIterator, getRenderContext());
    float width = (float) consumableArea.width;
    if (width <= 0)
        return;
    TextLayout layout;
    int startOfNextLayout;
    int currentLine = 0;
    int endIndex = styledTextIterator.getEndIndex();
    do {
        if (currentLine < newlineLocations.size())
            startOfNextLayout = newlineLocations.get(currentLine) + 1;
        else
            startOfNextLayout = endIndex + 1;
        layout = lbm.nextLayout(width, startOfNextLayout, false);
        lines.add(layout);
        if (lbm.getPosition() == startOfNextLayout)
            currentLine += 1;
    } while (layout != null && lbm.getPosition() < endIndex);
}
Also used : AttributedString(java.text.AttributedString) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Aggregations

AttributedCharacterIterator (java.text.AttributedCharacterIterator)57 AttributedString (java.text.AttributedString)35 TextLayout (java.awt.font.TextLayout)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)15 Point (java.awt.Point)8 Font (java.awt.Font)7 Paint (java.awt.Paint)7 FontRenderContext (java.awt.font.FontRenderContext)7 Graphics2D (java.awt.Graphics2D)6 Color (java.awt.Color)5 ArrayList (java.util.ArrayList)5 WeakHashMap (java.util.WeakHashMap)5 Rectangle (java.awt.Rectangle)4 Attribute (java.text.AttributedCharacterIterator.Attribute)4 DecimalFormat (java.text.DecimalFormat)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 HashSet (java.util.HashSet)3 Dimension (java.awt.Dimension)2 Image (java.awt.Image)2