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

The following examples show how to use java.text.AttributedCharacterIterator#getIndex() . 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: Support_Format.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * finds attributes with regards to char index in this
 * AttributedCharacterIterator, and puts them in a vector
 *
 * @param iterator
 * @return a vector, each entry in this vector are of type FieldContainer,
 *       which stores start and end indexes and an attribute this range has
 */
protected static Vector<FieldContainer> findFields(AttributedCharacterIterator iterator) {
  Vector<FieldContainer> result = new Vector<FieldContainer>();
  while (iterator.getIndex() != iterator.getEndIndex()) {
    int start = iterator.getRunStart();
    int end = iterator.getRunLimit();

    Iterator<Attribute> it = iterator.getAttributes().keySet().iterator();
    while (it.hasNext()) {
      AttributedCharacterIterator.Attribute attribute = it.next();
      Object value = iterator.getAttribute(attribute);
      result.add(new FieldContainer(start, end, attribute, value));
      // System.out.println(start + " " + end + ": " + attribute + ",
      // " + value );
      // System.out.println("v.add(new FieldContainer(" + start +"," +
      // end +"," + attribute+ "," + value+ "));");
    }
    iterator.setIndex(end);
  }
  return result;
}
 
Example 2
Source File: SwingUtilities2.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator
        (AttributedCharacterIterator iterator) {
    int curIdx = iterator.getIndex();

    char c = iterator.last();
    while(c != CharacterIterator.DONE && Character.isWhitespace(c)) {
        c = iterator.previous();
    }

    if (c != CharacterIterator.DONE) {
        int endIdx = iterator.getIndex();

        if (endIdx == iterator.getEndIndex() - 1) {
            iterator.setIndex(curIdx);
            return iterator;
        } else {
            AttributedString trimmedText = new AttributedString(iterator,
                    iterator.getBeginIndex(), endIdx + 1);
            return trimmedText.getIterator();
        }
    } else {
        return null;
    }
}
 
Example 3
Source File: SwingUtilities2.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator
        (AttributedCharacterIterator iterator) {
    int curIdx = iterator.getIndex();

    char c = iterator.last();
    while(c != CharacterIterator.DONE && Character.isWhitespace(c)) {
        c = iterator.previous();
    }

    if (c != CharacterIterator.DONE) {
        int endIdx = iterator.getIndex();

        if (endIdx == iterator.getEndIndex() - 1) {
            iterator.setIndex(curIdx);
            return iterator;
        } else {
            AttributedString trimmedText = new AttributedString(iterator,
                    iterator.getBeginIndex(), endIdx + 1);
            return trimmedText.getIterator();
        }
    } else {
        return null;
    }
}
 
Example 4
Source File: SwingUtilities2.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator
        (AttributedCharacterIterator iterator) {
    int curIdx = iterator.getIndex();

    char c = iterator.last();
    while(c != CharacterIterator.DONE && Character.isWhitespace(c)) {
        c = iterator.previous();
    }

    if (c != CharacterIterator.DONE) {
        int endIdx = iterator.getIndex();

        if (endIdx == iterator.getEndIndex() - 1) {
            iterator.setIndex(curIdx);
            return iterator;
        } else {
            AttributedString trimmedText = new AttributedString(iterator,
                    iterator.getBeginIndex(), endIdx + 1);
            return trimmedText.getIterator();
        }
    } else {
        return null;
    }
}
 
Example 5
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator
        (AttributedCharacterIterator iterator) {
    int curIdx = iterator.getIndex();

    char c = iterator.last();
    while(c != CharacterIterator.DONE && Character.isWhitespace(c)) {
        c = iterator.previous();
    }

    if (c != CharacterIterator.DONE) {
        int endIdx = iterator.getIndex();

        if (endIdx == iterator.getEndIndex() - 1) {
            iterator.setIndex(curIdx);
            return iterator;
        } else {
            AttributedString trimmedText = new AttributedString(iterator,
                    iterator.getBeginIndex(), endIdx + 1);
            return trimmedText.getIterator();
        }
    } else {
        return null;
    }
}
 
Example 6
Source File: SwingUtilities2.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator
        (AttributedCharacterIterator iterator) {
    int curIdx = iterator.getIndex();

    char c = iterator.last();
    while(c != CharacterIterator.DONE && Character.isWhitespace(c)) {
        c = iterator.previous();
    }

    if (c != CharacterIterator.DONE) {
        int endIdx = iterator.getIndex();

        if (endIdx == iterator.getEndIndex() - 1) {
            iterator.setIndex(curIdx);
            return iterator;
        } else {
            AttributedString trimmedText = new AttributedString(iterator,
                    iterator.getBeginIndex(), endIdx + 1);
            return trimmedText.getIterator();
        }
    } else {
        return null;
    }
}
 
Example 7
Source File: FormattedCharacterIterator.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Appends all characters and attributes from the given iterator.
 *
 * @param  toAppendTo  shall be the same instance than {@link #text}.
 */
final void append(final AttributedCharacterIterator it, final StringBuffer toAppendTo) {
    final int offset = toAppendTo.length();
    int currentRunLimit = 0;                                        // Next index where to check for attributes.
    for (char c=it.first(); c!=DONE; c=it.next()) {
        toAppendTo.append(c);
        if (it.getIndex() == currentRunLimit) {
            currentRunLimit = it.getRunLimit();
            for (final Map.Entry<Attribute,Object> entry : it.getAttributes().entrySet()) {
                final Attribute attribute = entry.getKey();
                if (it.getRunLimit(attribute) == currentRunLimit) {
                    final Entry e = new Entry(attribute, entry.getValue(),  // Constructeur adds itself to the map.
                                              offset + it.getRunStart(attribute),
                                              offset + currentRunLimit, attributes);
                }
            }
        }
    }
    upper = toAppendTo.length();
}
 
Example 8
Source File: DateTimeFormatConstructor.java    From es6draft with MIT License 6 votes vote down vote up
/**
 * 12.1.6 PartitionDateTimePattern ( dateTimeFormat, x )
 * 
 * @param dateTimeFormat
 *            the date format object
 * @param date
 *            the date object
 * @return the formatted date-time object
 */
private static List<Map.Entry<String, String>> PartitionDateTimePattern(DateTimeFormatObject dateTimeFormat,
        Date date) {
    ArrayList<Map.Entry<String, String>> parts = new ArrayList<>();
    DateFormat dateFormat = dateTimeFormat.getDateFormat();
    AttributedCharacterIterator iterator = dateFormat.formatToCharacterIterator(date);
    StringBuilder sb = new StringBuilder();
    for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
        sb.append(ch);
        if (iterator.getIndex() + 1 == iterator.getRunLimit()) {
            Iterator<Attribute> keyIterator = iterator.getAttributes().keySet().iterator();
            String key;
            if (keyIterator.hasNext()) {
                key = fieldToString((DateFormat.Field) keyIterator.next());
            } else {
                key = "literal";
            }
            String value = sb.toString();
            sb.setLength(0);
            parts.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
        }
    }
    return parts;
}
 
Example 9
Source File: SwingUtilities2.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator
        (AttributedCharacterIterator iterator) {
    int curIdx = iterator.getIndex();

    char c = iterator.last();
    while(c != CharacterIterator.DONE && Character.isWhitespace(c)) {
        c = iterator.previous();
    }

    if (c != CharacterIterator.DONE) {
        int endIdx = iterator.getIndex();

        if (endIdx == iterator.getEndIndex() - 1) {
            iterator.setIndex(curIdx);
            return iterator;
        } else {
            AttributedString trimmedText = new AttributedString(iterator,
                    iterator.getBeginIndex(), endIdx + 1);
            return trimmedText.getIterator();
        }
    } else {
        return null;
    }
}
 
Example 10
Source File: DualGraphics2D.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public void drawString(AttributedCharacterIterator iterator, int x, int y) {
	int index = iterator.getIndex();
	g1.drawString(iterator, x, y);
	iterator.setIndex(index);
	g2.drawString(iterator, x, y);
}
 
Example 11
Source File: LineLayout.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static boolean hasTextRunBreakingAttribute(AttributedCharacterIterator iterator, int index) {
    int s = iterator.getIndex();
    if (index != s)
        iterator.setIndex(index);
    int k = iterator.getRunStart(textRunBreakingAttributes);
    if (index != s)
        iterator.setIndex(s);
    return k == index;
}
 
Example 12
Source File: ScientificNumberFormatter.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
private static void copyAsSuperscript(
        AttributedCharacterIterator iterator, int start, int limit, StringBuilder result) {
    int oldIndex = iterator.getIndex();
    iterator.setIndex(start);
    while (iterator.getIndex() < limit) {
        int aChar = char32AtAndAdvance(iterator);
        int digit = UCharacter.digit(aChar);
        if (digit < 0) {
            throw new IllegalArgumentException();
        }
        result.append(SUPERSCRIPT_DIGITS[digit]);
    }
    iterator.setIndex(oldIndex);
}
 
Example 13
Source File: ScientificNumberFormatter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
static void append(
        AttributedCharacterIterator iterator,
        int start,
        int limit,
        StringBuilder result) {
    int oldIndex = iterator.getIndex();
    iterator.setIndex(start);
    for (int i = start; i < limit; i++) {
        result.append(iterator.current());
        iterator.next();
    }
    iterator.setIndex(oldIndex);
}
 
Example 14
Source File: SimpleColoredComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private TextLayout createTextLayout(String text, Font basefont, FontRenderContext fontRenderContext) {
  if (StringUtil.isEmpty(text)) return null;
  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);
  }
  return new TextLayout(string.getIterator(), fontRenderContext);
}
 
Example 15
Source File: Phrase.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean isEmbedding() {
    boolean rv = true;
    AttributedCharacterIterator aci = getIterator();
    int savedIndex = aci.getIndex();
    int b = aci.getBeginIndex();
    int e = aci.getEndIndex();
    if ((e - b) != 1)
        rv = false;
    else if (aci.setIndex(b) != Characters.UC_OBJECT)
        rv = false;
    aci.setIndex(savedIndex);
    return rv;
}
 
Example 16
Source File: MultiIconSimpleColoredComponent.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
    // TODO(jacobr): SuitableFontProvider is a private class so we can't
    // easily use it. How important is supporting this use case?
    /*
    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;
}
 
Example 17
Source File: PdfGraphics2D.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
     * @see Graphics2D#drawString(AttributedCharacterIterator, float, float)
     */
    public void drawString(AttributedCharacterIterator iter, float x, float y) {
/*
        StringBuffer sb = new StringBuffer();
        for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {
            sb.append(c);
        }
        drawString(sb.toString(),x,y);
*/
        StringBuffer stringbuffer = new StringBuffer(iter.getEndIndex());
        for(char c = iter.first(); c != '\uFFFF'; c = iter.next())
        {
            if(iter.getIndex() == iter.getRunStart())
            {
                if(stringbuffer.length() > 0)
                {
                    drawString(stringbuffer.toString(), x, y);
                    FontMetrics fontmetrics = getFontMetrics();
                    x = (float)((double)x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth());
                    stringbuffer.delete(0, stringbuffer.length());
                }
                doAttributes(iter);
            }
            stringbuffer.append(c);
        }
        
        drawString(stringbuffer.toString(), x, y);
        underline = false;
    }
 
Example 18
Source File: PdfGraphics2D.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @see Graphics2D#drawString(AttributedCharacterIterator, float, float)
 */
@Override
public void drawString(AttributedCharacterIterator iter, float x, float y) {
	/*
	StringBuffer sb = new StringBuffer();
	for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {
	    sb.append(c);
	}
	drawString(sb.toString(),x,y);
	*/
	StringBuffer stringbuffer = new StringBuffer(iter.getEndIndex());
	for (char c = iter.first(); c != '\uFFFF'; c = iter.next()) {
		if (iter.getIndex() == iter.getRunStart()) {
			if (stringbuffer.length() > 0) {
				drawString(stringbuffer.toString(), x, y);
				FontMetrics fontmetrics = getFontMetrics();
				x = (float) (x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth());
				stringbuffer.delete(0, stringbuffer.length());
			}
			doAttributes(iter);
		}
		stringbuffer.append(c);
	}

	drawString(stringbuffer.toString(), x, y);
	underline = false;
}
 
Example 19
Source File: PdfGraphics2D.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see Graphics2D#drawString(AttributedCharacterIterator, float, float)
 */
public void drawString(AttributedCharacterIterator iter, float x, float y) {

	if(onlyShapes) {
		/*
		 * When drawing shapes, always use TextLayout, because it will
		 * ensure all chars are correctly layouted. With this even Bidi Texts work
		 * perfect.
		 */
		TextLayout textLayout = new TextLayout(iter, getFontRenderContext());
		textLayout.draw(this, x, y);
		return;
		
	}
    StringBuffer stringbuffer = new StringBuffer(iter.getEndIndex());
    for(char c = iter.first(); c != '\uFFFF'; c = iter.next())
    {
        if(iter.getIndex() == iter.getRunStart())
        {
            if(stringbuffer.length() > 0)
            {
                drawString(stringbuffer.toString(), x, y);
                FontMetrics fontmetrics = getFontMetrics();
                x = (float)(x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth());
                stringbuffer.delete(0, stringbuffer.length());
            }
            doAttributes(iter);
        }
        stringbuffer.append(c);
    }
    
    drawString(stringbuffer.toString(), x, y);
    underline = false;
}
 
Example 20
Source File: DualGraphics2D.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public void drawString(AttributedCharacterIterator iterator, float x,
		float y) {
	int index = iterator.getIndex();
	g1.drawString(iterator, x, y);
	iterator.setIndex(index);
	g2.drawString(iterator, x, y);
}