Search in sources :

Example 26 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 27 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 28 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 29 with AttributedCharacterIterator

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;
    }
}
Also used : KeyEvent(java.awt.event.KeyEvent) AttributedString(java.text.AttributedString) InputMethodRequests(java.awt.im.InputMethodRequests) InputMethodEvent(java.awt.event.InputMethodEvent) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 30 with AttributedCharacterIterator

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;
}
Also used : PrintColorUIResource(sun.swing.PrintColorUIResource) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Aggregations

AttributedCharacterIterator (java.text.AttributedCharacterIterator)52 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