Java Code Examples for java.text.AttributedCharacterIterator#Attribute

The following examples show how to use java.text.AttributedCharacterIterator#Attribute . 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: RichTextSpecTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private RichTextSpec createText() {
  Map<AttributedCharacterIterator.Attribute, Object> attrs =
      new HashMap<AttributedCharacterIterator.Attribute, Object>();
  List<RichTextSpec.StyledChunk> chunks = new ArrayList<RichTextSpec.StyledChunk>();
  InstanceID id = new InstanceID();
  chunks.add( new RichTextSpec.StyledChunk( 0, 3, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
      ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "ABC" ) );
  chunks.add( new RichTextSpec.StyledChunk( 3, 6, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
      ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "def" ) );
  chunks.add( new RichTextSpec.StyledChunk( 6, 9, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
      ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "GHI" ) );
  chunks.add( new RichTextSpec.StyledChunk( 9, 12, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
      ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "jkl" ) );
  chunks.add( new RichTextSpec.StyledChunk( 12, 15, new SpacerRenderNode(), attrs, new ReportAttributeMap<Object>(),
      ElementDefaultStyleSheet.getDefaultStyle(), new InstanceID(), "MNO" ) );
  return new RichTextSpec( "ABCdefGHIjklMNO", TextDirection.LTR, chunks );
}
 
Example 2
Source File: AttributedStringSerializer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Reads the object from the object input stream.
 *
 * @param stream the object input stream from where to read the serialized data.
 * @return the generated object.
 * @throws IOException            if reading the stream failed.
 * @throws ClassNotFoundException if serialized object class cannot be found.
 */
public Object readObject( final ObjectInputStream stream )
  throws IOException, ClassNotFoundException {
  // read string and attributes then create result
  final String plainStr = (String) stream.readObject();
  final AttributedString result = new AttributedString( plainStr );
  char c = stream.readChar();
  int start = 0;
  while ( c != CharacterIterator.DONE ) {
    final int limit = stream.readInt();
    final Map<AttributedCharacterIterator.Attribute, Object> atts =
      (Map<AttributedCharacterIterator.Attribute, Object>) stream.readObject();
    result.addAttributes( atts, start, limit );
    start = limit;
    c = stream.readChar();
  }
  return result;
}
 
Example 3
Source File: RichTextSpec.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public StyledChunk( final int start, final int end, final RenderNode originatingTextNode,
    final Map<AttributedCharacterIterator.Attribute, Object> attributes,
    final ReportAttributeMap<Object> originalAttributes, final StyleSheet styleSheet, final InstanceID instanceID,
    final String text ) {
  ArgumentNullException.validate( "originatingTextNode", originatingTextNode );
  ArgumentNullException.validate( "attributes", attributes );
  ArgumentNullException.validate( "text", text );
  ArgumentNullException.validate( "originalAttributes", originalAttributes );
  ArgumentNullException.validate( "styleSheet", styleSheet );
  ArgumentNullException.validate( "instanceID", instanceID );

  this.instanceID = instanceID;
  this.start = start;
  this.end = end;
  this.originatingTextNode = originatingTextNode;
  this.attributes = Collections.unmodifiableMap( attributes );
  this.originalAttributes = originalAttributes;
  this.styleSheet = styleSheet;
  this.text = text;
}
 
Example 4
Source File: FXGraphics2D.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws a string of attributed characters at {@code (x, y)}. 
 * 
 * @param iterator  an iterator over the characters ({@code null} not 
 *     permitted).
 * @param x  the x-coordinate.
 * @param y  the y-coordinate.
 */
@Override
public void drawString(AttributedCharacterIterator iterator, float x, 
        float y) {
    Set<AttributedCharacterIterator.Attribute> 
            s = iterator.getAllAttributeKeys();
    if (!s.isEmpty()) {
        TextLayout layout = new TextLayout(iterator, 
                getFontRenderContext());
        layout.draw(this, x, y);
    } else {
        StringBuilder strb = new StringBuilder();
        iterator.first();
        for (int i = iterator.getBeginIndex(); i < iterator.getEndIndex(); 
                i++) {
            strb.append(iterator.current());
            iterator.next();
        }
        drawString(strb.toString(), x, y);
    }
}
 
Example 5
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 6
Source File: AttributedCharacters.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public int getRunLimit(Set<? extends AttributedCharacterIterator.Attribute> attributes) {
    if (attributes.contains(TextAttribute.FONT) || attributes.contains(TextAttribute.FOREGROUND)) {
        return getRunLimit();
    } else {
        return getEndIndex();
    }
}
 
Example 7
Source File: TextLayoutStrategy.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the index of the first character of the run
 * with respect to the given attribute containing the current character.
 */
public int getRunStart(AttributedCharacterIterator.Attribute attribute) {
    if (attribute instanceof TextAttribute) {
        int pos = toModelPosition(getIndex());
        int i = v.getViewIndex(pos, Position.Bias.Forward);
        if (attribute == TextAttribute.FONT) {
            return toIteratorIndex(getFontBoundary(i, -1));
        }
    }
    return getBeginIndex();
}
 
Example 8
Source File: TextLayoutStrategy.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the value of the named attribute for the current character.
 * Returns null if the attribute is not defined.
 * @param attribute the key of the attribute whose value is requested.
 */
public Object getAttribute(AttributedCharacterIterator.Attribute attribute) {
    int pos = toModelPosition(getIndex());
    int childIndex = v.getViewIndex(pos, Position.Bias.Forward);
    if (attribute == TextAttribute.FONT) {
        return getFont(childIndex);
    } else if( attribute == TextAttribute.RUN_DIRECTION ) {
        return
            v.getDocument().getProperty(TextAttribute.RUN_DIRECTION);
    } else if (attribute == TextAttribute.NUMERIC_SHAPING) {
        return shaper;
    }
    return null;
}
 
Example 9
Source File: SwingFont.java    From CrossMobile with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("UseSpecificCatch")
static SwingFont getFont(String name, float size, boolean bold, boolean italic) {
    if (!Theme.Font.FONTNAME.equals(name))
        DesktopGraphicsBridge.loadFonts();
    Map<AttributedCharacterIterator.Attribute, Object> attributes = new HashMap<>();
    attributes.put(FAMILY, name);
    attributes.put(WEIGHT, bold ? WEIGHT_BOLD : WEIGHT_REGULAR);
    attributes.put(POSTURE, italic ? POSTURE_OBLIQUE : POSTURE_REGULAR);
    attributes.put(SIZE, size);
    return new SwingFont(new Font(attributes));
}
 
Example 10
Source File: TextLayoutStrategy.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the value of the named attribute for the current character.
 * Returns null if the attribute is not defined.
 * @param attribute the key of the attribute whose value is requested.
 */
public Object getAttribute(AttributedCharacterIterator.Attribute attribute) {
    int pos = toModelPosition(getIndex());
    int childIndex = v.getViewIndex(pos, Position.Bias.Forward);
    if (attribute == TextAttribute.FONT) {
        return getFont(childIndex);
    } else if( attribute == TextAttribute.RUN_DIRECTION ) {
        return
            v.getDocument().getProperty(TextAttribute.RUN_DIRECTION);
    } else if (attribute == TextAttribute.NUMERIC_SHAPING) {
        return shaper;
    }
    return null;
}
 
Example 11
Source File: TextLayoutStrategy.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the index of the first character following the run
 * with respect to the given attribute containing the current character.
 */
public int getRunLimit(AttributedCharacterIterator.Attribute attribute) {
    if (attribute instanceof TextAttribute) {
        int pos = toModelPosition(getIndex());
        int i = v.getViewIndex(pos, Position.Bias.Forward);
        if (attribute == TextAttribute.FONT) {
            return toIteratorIndex(getFontBoundary(i, 1));
        }
    }
    return getEndIndex();
}
 
Example 12
Source File: TextLayoutStrategy.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the value of the named attribute for the current character.
 * Returns null if the attribute is not defined.
 * @param attribute the key of the attribute whose value is requested.
 */
public Object getAttribute(AttributedCharacterIterator.Attribute attribute) {
    int pos = toModelPosition(getIndex());
    int childIndex = v.getViewIndex(pos, Position.Bias.Forward);
    if (attribute == TextAttribute.FONT) {
        return getFont(childIndex);
    } else if( attribute == TextAttribute.RUN_DIRECTION ) {
        return
            v.getDocument().getProperty(TextAttribute.RUN_DIRECTION);
    } else if (attribute == TextAttribute.NUMERIC_SHAPING) {
        return shaper;
    }
    return null;
}
 
Example 13
Source File: BidiBase.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("serial")
private static AttributedCharacterIterator.Attribute
    getTextAttribute(String name)
{
    if (clazz == null) {
        // fake attribute
        return new AttributedCharacterIterator.Attribute(name) { };
    } else {
        return (AttributedCharacterIterator.Attribute)getStaticField(clazz, name);
    }
}
 
Example 14
Source File: BidiBase.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("serial")
private static AttributedCharacterIterator.Attribute
    getTextAttribute(String name)
{
    if (clazz == null) {
        // fake attribute
        return new AttributedCharacterIterator.Attribute(name) { };
    } else {
        return (AttributedCharacterIterator.Attribute)getStaticField(clazz, name);
    }
}
 
Example 15
Source File: AttributedCharacters.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public int getRunStart(AttributedCharacterIterator.Attribute att) {
    if ((att != TextAttribute.FONT) && (att != TextAttribute.FOREGROUND)) {
        return 0; // undefined attribute
    }

    return getRunStart();
}
 
Example 16
Source File: TextLayoutStrategy.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the index of the first character following the run
 * with respect to the given attribute containing the current character.
 */
public int getRunLimit(AttributedCharacterIterator.Attribute attribute) {
    if (attribute instanceof TextAttribute) {
        int pos = toModelPosition(getIndex());
        int i = v.getViewIndex(pos, Position.Bias.Forward);
        if (attribute == TextAttribute.FONT) {
            return toIteratorIndex(getFontBoundary(i, 1));
        }
    }
    return getEndIndex();
}
 
Example 17
Source File: RichTextSpec.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Map<AttributedCharacterIterator.Attribute, Object> getAttributes() {
  return attributes;
}
 
Example 18
Source File: IMSC11ResourceConverterState.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void populateAttributedText(List<Serializable> content, String text, Map<AttributedCharacterIterator.Attribute,Object> attributes, Direction blockDirection) {
    Span sEmphasis      = null;
    Span sRuby          = null;
    Span sCombine       = null;
    Span sWideBar       = null;
    Span sOuter         = null;
    int numExclusive    = 0;
    for (Map.Entry<AttributedCharacterIterator.Attribute,Object> e : attributes.entrySet()) {
        Attribute a = (Attribute) e.getValue();
        if (a.isEmphasis()) {
            sEmphasis = createEmphasis(text, a, blockDirection);
            ++numExclusive;
        } else if (a.isRuby()) {
            sRuby = createRuby(text, a, blockDirection);
            ++numExclusive;
        } else if (a.isCombine()) {
            sCombine = createCombine(text, a, blockDirection);
            ++numExclusive;
        } else if (a.isStretchWide() && isHorizontalBar(text)) {
            sWideBar = createWideBar(text, a, blockDirection);
            ++numExclusive;
        } else {
            if (sOuter == null)
                sOuter = createStyledSpan(null, a);
            else
                sOuter = augmentStyledSpan(sOuter, a);
        }
    }
    if (numExclusive > 1)
        throw new IllegalStateException();
    if (sOuter == null) {
        if (sEmphasis != null)
            sOuter = sEmphasis;
        else if (sRuby != null)
            sOuter = sRuby;
        else if (sCombine != null)
            sOuter = sCombine;
        else if (sWideBar != null)
            sOuter = sWideBar;
    } else {
        Span sInner = null;
        if (sEmphasis != null)
            sInner = sEmphasis;
        else if (sRuby != null)
            sInner = sRuby;
        else if (sCombine != null)
            sInner = sCombine;
        else if (sWideBar != null)
            sInner = sWideBar;
        if (sInner != null) {
            sOuter.getContent().add(ttmlFactory.createSpan(sInner));
        } else {
            sOuter.getContent().add(text);
        }
    }
    if (sOuter != null)
        content.add(ttmlFactory.createSpan(sOuter));
}
 
Example 19
Source File: IntlTestDecimalFormatAPIC.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public FieldContainer(int start, int end, AttributedCharacterIterator.Attribute attribute, int value) {
this(start, end, attribute, new Integer(value));
}
 
Example 20
Source File: FontSets.java    From radiance with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Font deriveFont(final Map<? extends AttributedCharacterIterator.Attribute, ?> attributes) {
	return new DefaultUIResourceFont(super.deriveFont(attributes));
}