Java Code Examples for java.text.AttributedCharacterIterator#current()

The following examples show how to use java.text.AttributedCharacterIterator#current() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DecimalFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_formatToCharacterIterator_veryLarge() throws Exception {
    AttributedCharacterIterator iterator;
    int[] runStarts;
    int[] runLimits;
    String result;
    char current;

    Number number = new BigDecimal("1.23456789E1234");
    assertEquals("1.23456789E+1234", number.toString());
    iterator = new DecimalFormat().formatToCharacterIterator(number);
    runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 };
    runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 };
    result = "12,345,678,900,"; // 000,000,000,000....
    current = iterator.current();
    for (int i = 0; i < runStarts.length; i++) {
        assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
        assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
        assertEquals("wrong char @" + i, result.charAt(i), current);
        current = iterator.next();
    }
    assertEquals(0, iterator.getBeginIndex());
    assertEquals(1646, iterator.getEndIndex());
}
 
Example 2
Source File: ScientificNumberFormatter.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
@Override
String format(
        AttributedCharacterIterator iterator,
        String preExponent) {
    int copyFromOffset = 0;
    StringBuilder result = new StringBuilder();
    for (
            iterator.first();
            iterator.current() != CharacterIterator.DONE;
        ) {
        Map<Attribute, Object> attributeSet = iterator.getAttributes();
        if (attributeSet.containsKey(NumberFormat.Field.EXPONENT_SYMBOL)) {
            append(
                    iterator,
                    copyFromOffset,
                    iterator.getRunStart(NumberFormat.Field.EXPONENT_SYMBOL),
                    result);
            copyFromOffset = iterator.getRunLimit(NumberFormat.Field.EXPONENT_SYMBOL);
            iterator.setIndex(copyFromOffset);
            result.append(preExponent);
            result.append(beginMarkup);
        } else if (attributeSet.containsKey(NumberFormat.Field.EXPONENT)) {
            int limit = iterator.getRunLimit(NumberFormat.Field.EXPONENT);
            append(
                    iterator,
                    copyFromOffset,
                    limit,
                    result);
            copyFromOffset = limit;
            iterator.setIndex(copyFromOffset);
            result.append(endMarkup);
        } else {
            iterator.next();
        }
    }
    append(iterator, copyFromOffset, iterator.getEndIndex(), result);
    return result.toString();
}
 
Example 3
Source File: ScientificNumberFormatter.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
private static int char32AtAndAdvance(AttributedCharacterIterator iterator) {
    char c1 = iterator.current();
    char c2 = iterator.next();
    if (UCharacter.isHighSurrogate(c1)) {
        // If c2 is DONE, it will fail the low surrogate test and we
        // skip this block.
        if (UCharacter.isLowSurrogate(c2)) {
            iterator.next();
            return UCharacter.toCodePoint(c1, c2);
        }
    }
    return c1;
}
 
Example 4
Source File: TTML2ResourceConverterState.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void populateText(Paragraph p, AttributedString as, boolean insertBreakBefore, Direction blockDirection) {
    if (as != null) {
        List<Serializable> content = p.getContent();
        if (insertBreakBefore)
            content.add(ttmlFactory.createBr(ttmlFactory.createBreak()));
        AttributedCharacterIterator aci = as.getIterator();
        aci.first();
        StringBuffer sb = new StringBuffer();
        while (aci.current() != CharacterIterator.DONE) {
            int i = aci.getRunStart();
            int e = aci.getRunLimit();
            Map<AttributedCharacterIterator.Attribute,Object> attributes = aci.getAttributes();
            while (i < e) {
                sb.append(aci.setIndex(i++));
            }
            String text = sb.toString();
            if (!text.isEmpty()) {
                if (!attributes.isEmpty()) {
                    populateAttributedText(content, text, attributes, blockDirection);
                } else {
                    content.add(text);
                }
            }
            sb.setLength(0);
            aci.setIndex(e);
        }
    }
}
 
Example 5
Source File: IMSC11ResourceConverterState.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void populateText(Paragraph p, AttributedString as, boolean insertBreakBefore, Direction blockDirection) {
    if (as != null) {
        List<Serializable> content = p.getContent();
        if (insertBreakBefore)
            content.add(ttmlFactory.createBr(ttmlFactory.createBreak()));
        AttributedCharacterIterator aci = as.getIterator();
        aci.first();
        StringBuffer sb = new StringBuffer();
        while (aci.current() != CharacterIterator.DONE) {
            int i = aci.getRunStart();
            int e = aci.getRunLimit();
            Map<AttributedCharacterIterator.Attribute,Object> attributes = aci.getAttributes();
            while (i < e) {
                sb.append(aci.setIndex(i++));
            }
            String text = sb.toString();
            if (!text.isEmpty()) {
                if (!attributes.isEmpty()) {
                    populateAttributedText(content, text, attributes, blockDirection);
                } else {
                    content.add(text);
                }
            }
            sb.setLength(0);
            aci.setIndex(e);
        }
    }
}
 
Example 6
Source File: ScientificNumberFormatter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
String format(
        AttributedCharacterIterator iterator,
        String preExponent) {
    int copyFromOffset = 0;
    StringBuilder result = new StringBuilder();
    for (
            iterator.first();
            iterator.current() != CharacterIterator.DONE;
        ) {
        Map<Attribute, Object> attributeSet = iterator.getAttributes();
        if (attributeSet.containsKey(NumberFormat.Field.EXPONENT_SYMBOL)) {
            append(
                    iterator,
                    copyFromOffset,
                    iterator.getRunStart(NumberFormat.Field.EXPONENT_SYMBOL),
                    result);
            copyFromOffset = iterator.getRunLimit(NumberFormat.Field.EXPONENT_SYMBOL);
            iterator.setIndex(copyFromOffset);
            result.append(preExponent);
            result.append(beginMarkup);
        } else if (attributeSet.containsKey(NumberFormat.Field.EXPONENT)) {
            int limit = iterator.getRunLimit(NumberFormat.Field.EXPONENT);
            append(
                    iterator,
                    copyFromOffset,
                    limit,
                    result);
            copyFromOffset = limit;
            iterator.setIndex(copyFromOffset);
            result.append(endMarkup);
        } else {
            iterator.next();
        }
    }
    append(iterator, copyFromOffset, iterator.getEndIndex(), result);
    return result.toString();
}
 
Example 7
Source File: ScientificNumberFormatter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static int char32AtAndAdvance(AttributedCharacterIterator iterator) {
    char c1 = iterator.current();
    char c2 = iterator.next();
    if (UCharacter.isHighSurrogate(c1)) {
        // If c2 is DONE, it will fail the low surrogate test and we
        // skip this block.
        if (UCharacter.isLowSurrogate(c2)) {
            iterator.next();
            return UCharacter.toCodePoint(c1, c2);
        }
    }
    return c1;
}
 
Example 8
Source File: AttributedStringSerializationWrapper.java    From pumpernickel with MIT License 4 votes vote down vote up
public AttributedStringSerializationWrapper(AttributedString as) {
	AttributedCharacterIterator iter = as.getIterator();
	Set<Attribute> allAttributes = iter.getAllAttributeKeys();
	StringBuilder sb = new StringBuilder();
	while (true) {
		char ch = iter.current();
		if (ch == CharacterIterator.DONE)
			break;
		sb.append(ch);
		iter.next();
	}

	Map<Attribute, List<Run>> runMap = new HashMap<>();
	for (Attribute attribute : allAttributes) {
		AttributedCharacterIterator iter2 = as
				.getIterator(new Attribute[] { attribute });

		List<Run> runs = new ArrayList<>();
		int index = 0;
		while (true) {
			if (iter2.current() == CharacterIterator.DONE)
				break;

			Run lastRun = runs.isEmpty() ? null : runs.get(runs.size() - 1);
			Object value = iter2.getAttribute(attribute);

			if (lastRun != null && Objects.equals(lastRun.value, value)) {
				lastRun.endIndex++;
			} else {
				Run newRun = new Run(value, index);
				runs.add(newRun);
			}

			index++;
			iter2.next();
		}

		for (Run run : runs) {
			for (SerializationFilter filter : AWTSerializationUtils.FILTERS) {
				Object filteredValue = filter.filter(run.value);
				if (filteredValue != null)
					run.value = filteredValue;
			}
		}

		runMap.put(attribute, runs);
	}

	map.put(KEY_STRING, sb.toString());
	map.put(KEY_RUN_MAP, runMap);
}
 
Example 9
Source File: DesktopEditorImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void replaceInputMethodText(@Nonnull InputMethodEvent e) {
  if (myNeedToSelectPreviousChar && SystemInfo.isMac &&
      (Registry.is("ide.mac.pressAndHold.brute.workaround") || Registry.is("ide.mac.pressAndHold.workaround") && (hasRelevantCommittedText(e) || e.getCaret() == null))) {
    // This is required to support input of accented characters using press-and-hold method (http://support.apple.com/kb/PH11264).
    // JDK currently properly supports this functionality only for TextComponent/JTextComponent descendants.
    // For our editor component we need this workaround.
    // After https://bugs.openjdk.java.net/browse/JDK-8074882 is fixed, this workaround should be replaced with a proper solution.
    myNeedToSelectPreviousChar = false;
    getCaretModel().runForEachCaret(caret -> {
      int caretOffset = caret.getOffset();
      if (caretOffset > 0) {
        caret.setSelection(caretOffset - 1, caretOffset);
      }
    });
  }

  int commitCount = e.getCommittedCharacterCount();
  AttributedCharacterIterator text = e.getText();

  // old composed text deletion
  final Document doc = getDocument();

  if (composedText != null) {
    if (!isViewer() && doc.isWritable()) {
      runUndoTransparent(() -> {
        int docLength = doc.getTextLength();
        ProperTextRange range = composedTextRange.intersection(new TextRange(0, docLength));
        if (range != null) {
          doc.deleteString(range.getStartOffset(), range.getEndOffset());
        }
      });
    }
    composedText = null;
  }

  if (text != null) {
    text.first();

    // committed text insertion
    if (commitCount > 0) {
      for (char c = text.current(); commitCount > 0; c = text.next(), commitCount--) {
        if (c >= 0x20 && c != 0x7F) { // Hack just like in javax.swing.text.DefaultEditorKit.DefaultKeyTypedAction
          processKeyTyped(c);
        }
      }
    }

    // new composed text insertion
    if (!isViewer() && doc.isWritable()) {
      int composedTextIndex = text.getIndex();
      if (composedTextIndex < text.getEndIndex()) {
        createComposedString(composedTextIndex, text);

        runUndoTransparent(() -> EditorModificationUtil.insertStringAtCaret(DesktopEditorImpl.this, composedText, false, false));

        composedTextRange = ProperTextRange.from(getCaretModel().getOffset(), composedText.length());
      }
    }
  }
}