java.awt.font.TextAttribute Java Examples

The following examples show how to use java.awt.font.TextAttribute. 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: KerningLeak.java    From jdk8u-jdk with GNU General Public License v2.0 7 votes vote down vote up
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example #2
Source File: LogAxis.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a tick label for the specified value based on the current
 * tick unit (used for formatting the exponent).
 *
 * @param value  the value.
 *
 * @return The label.
 *
 * @since 1.0.18
 */
protected AttributedString createTickLabel(double value) {
    if (this.numberFormatOverride != null) {
        return new AttributedString(
                this.numberFormatOverride.format(value));
    } else {
        String baseStr = this.baseSymbol;
        if (baseStr == null) {
            baseStr = this.baseFormatter.format(this.base);
        }
        double logy = calculateLog(value);
        String exponentStr = getTickUnit().valueToString(logy);
        AttributedString as = new AttributedString(baseStr + exponentStr);
        as.addAttributes(getTickLabelFont().getAttributes(), 0, (baseStr 
                + exponentStr).length());
        as.addAttribute(TextAttribute.SUPERSCRIPT, 
                TextAttribute.SUPERSCRIPT_SUPER, baseStr.length(), 
                baseStr.length() + exponentStr.length());
        return as;
    }
}
 
Example #3
Source File: ParagraphView.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set the cached properties from the attributes.
 */
protected void setPropertiesFromAttributes() {
    AttributeSet attr = getAttributes();
    if (attr != null) {
        setParagraphInsets(attr);
        Integer a = (Integer)attr.getAttribute(StyleConstants.Alignment);
        int alignment;
        if (a == null) {
            Document doc = getElement().getDocument();
            Object o = doc.getProperty(TextAttribute.RUN_DIRECTION);
            if ((o != null) && o.equals(TextAttribute.RUN_DIRECTION_RTL)) {
                alignment = StyleConstants.ALIGN_RIGHT;
            } else {
                alignment = StyleConstants.ALIGN_LEFT;
            }
        } else {
            alignment = a.intValue();
        }
        setJustification(alignment);
        setLineSpacing(StyleConstants.getLineSpacing(attr));
        setFirstLineIndent(StyleConstants.getFirstLineIndent(attr));
    }
}
 
Example #4
Source File: JRXlsMetadataExporter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont, Locale locale) {
	String text = styledText.getText();
	HSSFRichTextString richTextStr = new HSSFRichTextString(text);
	int runLimit = 0;
	AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

	while(runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
		Map<Attribute,Object> attributes = iterator.getAttributes();
		JRFont runFont = attributes.isEmpty()? defaultFont : new JRBaseFont(attributes);
		short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null  
			? getWorkbookColor((Color)attributes.get(TextAttribute.FOREGROUND)).getIndex() 
			: forecolor;
		HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
		richTextStr.applyFont(iterator.getIndex(), runLimit, font);
		iterator.setIndex(runLimit);
	}
	return richTextStr;
}
 
Example #5
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the width of the passed in String.
 * If the passed String is {@code null}, returns zero.
 *
 * @param c JComponent that will display the string, may be null
 * @param fm FontMetrics used to measure the String width
 * @param string String to get the width of
 * @param useFPAPI use floating point API
 */
public static float stringWidth(JComponent c, FontMetrics fm, String string,
        boolean useFPAPI){
    if (string == null || string.isEmpty()) {
        return 0;
    }
    boolean needsTextLayout = ((c != null) &&
            (c.getClientProperty(TextAttribute.NUMERIC_SHAPING) != null));
    if (needsTextLayout) {
        synchronized(charsBufferLock) {
            int length = syncCharsBuffer(string);
            needsTextLayout = isComplexLayout(charsBuffer, 0, length);
        }
    }
    if (needsTextLayout) {
        TextLayout layout = createTextLayout(c, string,
                                fm.getFont(), fm.getFontRenderContext());
        return layout.getAdvance();
    } else {
        return getFontStringWidth(string, fm, useFPAPI);
    }
}
 
Example #6
Source File: GraphicsVisualContext.java    From CSSBox with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected Font createFont(String family, int size, CSSProperty.FontWeight weight,
        CSSProperty.FontStyle style, float spacing)
{
    Font base = createBaseFont(family, size, weight, style);
    Map<TextAttribute, Object> attributes = new HashMap<>(defaultFontAttributes);
    // add tracking when needed
    if (spacing >= 0.0001)
    {
        // TRACKING value is multiplied by font size in AWT. 
        // (0.75 has been empiricaly determined by comparing with other browsers) 
        final float tracking = spacing / getFontSize() * 0.75f;
        
        attributes.put(TextAttribute.TRACKING, tracking);
    }
    // derive the font when some attributes have been set
    if (attributes.isEmpty())
        return base;
    else
        return base.deriveFont(attributes);
}
 
Example #7
Source File: OptionsMap.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * If the object stored under key is a Font then returns its value
 * otherwise returns null.
 * @param key key associated to the value to retrieve
 * @return the associated font
 */
public Font getFont(Object key) {
  try {
    String stringValue = (String) get(key);
    if (stringValue == null) { return null; }
    StringTokenizer strTok = new StringTokenizer(stringValue, "#", false);
    String family = strTok.nextToken();
    int size = Integer.parseInt(strTok.nextToken());
    boolean italic = Boolean.valueOf(strTok.nextToken());
    boolean bold = Boolean.valueOf(strTok.nextToken());
    HashMap<TextAttribute, Serializable> fontAttrs =
      new HashMap<TextAttribute, Serializable>();
    fontAttrs.put(TextAttribute.FAMILY, family);
    fontAttrs.put(TextAttribute.SIZE, (float) size);
    if(bold) fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
    else fontAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
    if(italic) fontAttrs.put(
      TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
    else fontAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
    return new Font(fontAttrs);
  } catch (Exception e) {
    return null;
  }
}
 
Example #8
Source File: ParagraphView.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set the cached properties from the attributes.
 */
protected void setPropertiesFromAttributes() {
    AttributeSet attr = getAttributes();
    if (attr != null) {
        setParagraphInsets(attr);
        Integer a = (Integer)attr.getAttribute(StyleConstants.Alignment);
        int alignment;
        if (a == null) {
            Document doc = getElement().getDocument();
            Object o = doc.getProperty(TextAttribute.RUN_DIRECTION);
            if ((o != null) && o.equals(TextAttribute.RUN_DIRECTION_RTL)) {
                alignment = StyleConstants.ALIGN_RIGHT;
            } else {
                alignment = StyleConstants.ALIGN_LEFT;
            }
        } else {
            alignment = a.intValue();
        }
        setJustification(alignment);
        setLineSpacing(StyleConstants.getLineSpacing(attr));
        setFirstLineIndent(StyleConstants.getFirstLineIndent(attr));
    }
}
 
Example #9
Source File: AbstractDocument.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * A convenience method for storing up a property value.  It is
 * equivalent to:
 * <pre>
 * getDocumentProperties().put(key, value);
 * </pre>
 * If <code>value</code> is <code>null</code> this method will
 * remove the property.
 *
 * @param key the non-<code>null</code> key
 * @param value the property value
 * @see #getDocumentProperties
 */
public final void putProperty(Object key, Object value) {
    if (value != null) {
        getDocumentProperties().put(key, value);
    } else {
        getDocumentProperties().remove(key);
    }
    if( key == TextAttribute.RUN_DIRECTION
        && Boolean.TRUE.equals(getProperty(I18NProperty)) )
    {
        //REMIND - this needs to flip on the i18n property if run dir
        //is rtl and the i18n property is not already on.
        writeLock();
        try {
            DefaultDocumentEvent e
                = new DefaultDocumentEvent(0, getLength(),
                                           DocumentEvent.EventType.INSERT);
            updateBidi( e );
        } finally {
            writeUnlock();
        }
    }
}
 
Example #10
Source File: KerningLeak.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example #11
Source File: RendererSettings.java    From mil-sym-java with Apache License 2.0 6 votes vote down vote up
/**
     * get font object used for labels
     * @return Font object
     */
    public Font getLabelFont()
    {
        try
        {
            Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
//            map.put(TextAttribute.FONT, _ModifierFontName);
//            map.put(TextAttribute.SIZE, _ModifierFontSize);
//            map.put(TextAttribute.WEIGHT, _ModifierFontType);
            map.put(TextAttribute.KERNING, _ModifierFontKerning);
            map.put(TextAttribute.TRACKING, _ModifierFontTracking);
            
            Font temp = new Font(_ModifierFontName, _ModifierFontType, _ModifierFontSize);
                    
            return temp.deriveFont(map);
        }
        catch(Exception exc)
        {
            String message = "font creation error, returning \"" + _ModifierFontName + "\" font, " + _ModifierFontSize + "pt. Check font name and type.";
            ErrorLogger.LogMessage("RendererSettings", "getLabelFont", message);
            ErrorLogger.LogMessage("RendererSettings", "getLabelFont", exc.getMessage());
            return new Font("arial", Font.BOLD, 12);
        }
    }
 
Example #12
Source File: ParagraphView.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set the cached properties from the attributes.
 */
protected void setPropertiesFromAttributes() {
    AttributeSet attr = getAttributes();
    if (attr != null) {
        setParagraphInsets(attr);
        Integer a = (Integer)attr.getAttribute(StyleConstants.Alignment);
        int alignment;
        if (a == null) {
            Document doc = getElement().getDocument();
            Object o = doc.getProperty(TextAttribute.RUN_DIRECTION);
            if ((o != null) && o.equals(TextAttribute.RUN_DIRECTION_RTL)) {
                alignment = StyleConstants.ALIGN_RIGHT;
            } else {
                alignment = StyleConstants.ALIGN_LEFT;
            }
        } else {
            alignment = a.intValue();
        }
        setJustification(alignment);
        setLineSpacing(StyleConstants.getLineSpacing(attr));
        setFirstLineIndent(StyleConstants.getFirstLineIndent(attr));
    }
}
 
Example #13
Source File: Utilities.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Draws the given composed text passed from an input method.
 *
 * @param view View hosting text
 * @param attr the attributes containing the composed text
 * @param g  the graphics context
 * @param x  the X origin
 * @param y  the Y origin
 * @param p0 starting offset in the composed text to be rendered
 * @param p1 ending offset in the composed text to be rendered
 * @return  the new insertion position
 */
static int drawComposedText(View view, AttributeSet attr, Graphics g,
                            int x, int y, int p0, int p1)
                                 throws BadLocationException {
    Graphics2D g2d = (Graphics2D)g;
    AttributedString as = (AttributedString)attr.getAttribute(
        StyleConstants.ComposedTextAttribute);
    as.addAttribute(TextAttribute.FONT, g.getFont());

    if (p0 >= p1)
        return x;

    AttributedCharacterIterator aci = as.getIterator(null, p0, p1);
    return x + (int)SwingUtilities2.drawString(
                         getJComponent(view), g2d,aci,x,y);
}
 
Example #14
Source File: KerningLeak.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example #15
Source File: ParagraphView.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Set the cached properties from the attributes.
 */
protected void setPropertiesFromAttributes() {
    AttributeSet attr = getAttributes();
    if (attr != null) {
        setParagraphInsets(attr);
        Integer a = (Integer)attr.getAttribute(StyleConstants.Alignment);
        int alignment;
        if (a == null) {
            Document doc = getElement().getDocument();
            Object o = doc.getProperty(TextAttribute.RUN_DIRECTION);
            if ((o != null) && o.equals(TextAttribute.RUN_DIRECTION_RTL)) {
                alignment = StyleConstants.ALIGN_RIGHT;
            } else {
                alignment = StyleConstants.ALIGN_LEFT;
            }
        } else {
            alignment = a.intValue();
        }
        setJustification(alignment);
        setLineSpacing(StyleConstants.getLineSpacing(attr));
        setFirstLineIndent(StyleConstants.getFirstLineIndent(attr));
    }
}
 
Example #16
Source File: KerningLeak.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example #17
Source File: Font.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the keys of all the attributes supported by this
 * <code>Font</code>.  These attributes can be used to derive other
 * fonts.
 * @return an array containing the keys of all the attributes
 *          supported by this <code>Font</code>.
 * @since 1.2
 */
public Attribute[] getAvailableAttributes() {
    // FONT is not supported by Font

    Attribute attributes[] = {
        TextAttribute.FAMILY,
        TextAttribute.WEIGHT,
        TextAttribute.WIDTH,
        TextAttribute.POSTURE,
        TextAttribute.SIZE,
        TextAttribute.TRANSFORM,
        TextAttribute.SUPERSCRIPT,
        TextAttribute.CHAR_REPLACEMENT,
        TextAttribute.FOREGROUND,
        TextAttribute.BACKGROUND,
        TextAttribute.UNDERLINE,
        TextAttribute.STRIKETHROUGH,
        TextAttribute.RUN_DIRECTION,
        TextAttribute.BIDI_EMBEDDING,
        TextAttribute.JUSTIFICATION,
        TextAttribute.INPUT_METHOD_HIGHLIGHT,
        TextAttribute.INPUT_METHOD_UNDERLINE,
        TextAttribute.SWAP_COLORS,
        TextAttribute.NUMERIC_SHAPING,
        TextAttribute.KERNING,
        TextAttribute.LIGATURES,
        TextAttribute.TRACKING,
    };

    return attributes;
}
 
Example #18
Source File: StringFormatTest.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Prepare the test parser.
 */
public StringFormatTest() {
	// The usual client definitions. Could be something else
	blue = new TextAttributeSet();
	blue.setAttribute(TextAttribute.FOREGROUND, Color.blue);
	blue.setAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
	f.addStyle('#', blue);

	underline = new TextAttributeSet();
	underline.setAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
	f.addStyle('§', underline);
}
 
Example #19
Source File: AttributeValues.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static AffineTransform getCharTransform(Map<?, ?> map) {
    if (map != null) {
        AttributeValues av = null;
        if (map instanceof AttributeMap &&
            ((AttributeMap) map).getValues() != null) {
            av = ((AttributeMap)map).getValues();
        } else if (map.get(TextAttribute.TRANSFORM) != null) {
            av = AttributeValues.fromMap((Map<Attribute, ?>)map); // yuck
        }
        if (av != null) {
            return av.charTransform;
        }
    }
    return null;
}
 
Example #20
Source File: RichTextSpecProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Map<Attribute, Object> computeStyle( final StyleSheet layoutContext ) {
  Map<Attribute, Object> result = new HashMap<Attribute, Object>();
  // Determine font style
  if ( layoutContext.getBooleanStyleProperty( TextStyleKeys.ITALIC ) ) {
    result.put( TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE );
  } else {
    result.put( TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR );
  }
  if ( layoutContext.getBooleanStyleProperty( TextStyleKeys.BOLD ) ) {
    result.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD );
  } else {
    result.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR );
  }

  final String fontNameRaw = (String) layoutContext.getStyleProperty( TextStyleKeys.FONT );
  final String fontName = metaData.getNormalizedFontFamilyName( fontNameRaw );
  result.put( TextAttribute.FAMILY, fontName );
  result.put( TextAttribute.SIZE, layoutContext.getIntStyleProperty( TextStyleKeys.FONTSIZE, 12 ) );
  if ( layoutContext.getBooleanStyleProperty( TextStyleKeys.UNDERLINED ) ) {
    result.put( TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON );
  }
  if ( layoutContext.getBooleanStyleProperty( TextStyleKeys.STRIKETHROUGH ) ) {
    result.put( TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON );
  }

  // character spacing
  result.put( TextAttribute.TRACKING,
      layoutContext.getDoubleStyleProperty( TextStyleKeys.X_MIN_LETTER_SPACING, 0 ) / 10 );

  return result;
}
 
Example #21
Source File: ReadResolve.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    testSerializationCycle(Attribute.LANGUAGE);
    testSerializationCycle(TextAttribute.INPUT_METHOD_HIGHLIGHT);

    boolean gotException = false;
    Attribute result = null;
    try {
        result = doSerializationCycle(FakeAttribute.LANGUAGE);
    } catch (Throwable e) {
        gotException = true;
    }
    if (!gotException) {
        throw new RuntimeException("Attribute should throw an exception when given a fake \"language\" attribute. Deserialized object: " + result);
    }
}
 
Example #22
Source File: JTextComponent.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void setComponentOrientation( ComponentOrientation o ) {
    // Set the document's run direction property to match the
    // ComponentOrientation property.
    Document doc = getDocument();
    if( doc !=  null ) {
        Boolean runDir = o.isLeftToRight()
                         ? TextAttribute.RUN_DIRECTION_LTR
                         : TextAttribute.RUN_DIRECTION_RTL;
        doc.putProperty( TextAttribute.RUN_DIRECTION, runDir );
    }
    super.setComponentOrientation( o );
}
 
Example #23
Source File: javax_swing_plaf_FontUIResource.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected FontUIResource getObject() {
    return new FontUIResource(
            new Font(
                    Collections.singletonMap(
                            TextAttribute.STRIKETHROUGH,
                            TextAttribute.STRIKETHROUGH_ON)));
}
 
Example #24
Source File: AttributeValues.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public static float getJustification(Map<?, ?> map) {
    if (map != null) {
        if (map instanceof AttributeMap &&
            ((AttributeMap) map).getValues() != null) {
            return ((AttributeMap)map).getValues().justification;
        }
        Object obj = map.get(TextAttribute.JUSTIFICATION);
        if (obj != null && obj instanceof Number) {
            return max(0, min(1, ((Number)obj).floatValue()));
        }
    }
    return DEFAULT.justification;
}
 
Example #25
Source File: AbstractDocument.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs the actual work of inserting the text; it is assumed the
 * caller has obtained a write lock before invoking this.
 */
private void handleInsertString(int offs, String str, AttributeSet a)
        throws BadLocationException {
    if ((str == null) || (str.length() == 0)) {
        return;
    }
    UndoableEdit u = data.insertString(offs, str);
    DefaultDocumentEvent e =
        new DefaultDocumentEvent(offs, str.length(), DocumentEvent.EventType.INSERT);
    if (u != null) {
        e.addEdit(u);
    }

    // see if complex glyph layout support is needed
    if( getProperty(I18NProperty).equals( Boolean.FALSE ) ) {
        // if a default direction of right-to-left has been specified,
        // we want complex layout even if the text is all left to right.
        Object d = getProperty(TextAttribute.RUN_DIRECTION);
        if ((d != null) && (d.equals(TextAttribute.RUN_DIRECTION_RTL))) {
            putProperty( I18NProperty, Boolean.TRUE);
        } else {
            char[] chars = str.toCharArray();
            if (SwingUtilities2.isComplexLayout(chars, 0, chars.length)) {
                putProperty( I18NProperty, Boolean.TRUE);
            }
        }
    }

    insertUpdate(e, a);
    // Mark the edit as done.
    e.end();
    fireInsertUpdate(e);
    // only fire undo if Content implementation supports it
    // undo for the composed text is not supported for now
    if (u != null && (a == null || !a.isDefined(StyleConstants.ComposedTextAttribute))) {
        fireUndoableEditUpdate(new UndoableEditEvent(this, e));
    }
}
 
Example #26
Source File: BidiEmbeddingTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void test2() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int length = str.length();
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);

    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-3),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi + "\n");

    if (bidi.getRunCount() != 6) { // runs of spaces and angles at embedding bound,s and final period, each get level 1
        throw new Error("Bidi embedding processing failed");
    } else {
        System.out.println("test2() passed.\n");
    }
}
 
Example #27
Source File: DecksListCellRenderer.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    final MagicDeck deck = (MagicDeck) value;
    final Component c = super.getListCellRendererComponent(list, deck.getName(), index, isSelected, cellHasFocus);
    if (!deck.isValid()) {
        if (invalidDeckFont == null) {
            final Map<TextAttribute, Object> attributes = new HashMap<>();
            attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
            invalidDeckFont = c.getFont().deriveFont(attributes);
        }
        c.setFont(invalidDeckFont);
        c.setForeground(isSelected ? list.getSelectionForeground() : Color.RED.darker());
    }
    return c;
}
 
Example #28
Source File: CCPaintComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void drawStringToGraphics(Graphics g, String s, Font font, boolean strike) {
    if (g != null) {
        if (!strike){
            g.drawString(s, drawX, drawY);
        }else{
            Graphics2D g2 = ((Graphics2D)g);
            AttributedString strikeText = new AttributedString(s);
            strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
            strikeText.addAttribute(TextAttribute.FONT, g.getFont());
            g2.drawString(strikeText.getIterator(), drawX, drawY);
        }
    }
    drawX += getWidth(s, font);
}
 
Example #29
Source File: AttributeValues.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static NumericShaper getNumericShaping(Map<?, ?> map) {
    if (map != null) {
        if (map instanceof AttributeMap &&
            ((AttributeMap) map).getValues() != null) {
            return ((AttributeMap)map).getValues().numericShaping;
        }
        Object obj = map.get(TextAttribute.NUMERIC_SHAPING);
        if (obj != null && obj instanceof NumericShaper) {
            return (NumericShaper)obj;
        }
    }
    return DEFAULT.numericShaping;
}
 
Example #30
Source File: AttributeValues.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private Object i_get(EAttribute a) {
    switch (a) {
    case EFAMILY: return family;
    case EWEIGHT: return Float.valueOf(weight);
    case EWIDTH: return Float.valueOf(width);
    case EPOSTURE: return Float.valueOf(posture);
    case ESIZE: return Float.valueOf(size);
    case ETRANSFORM:
        return transform == null
            ? TransformAttribute.IDENTITY
            : new TransformAttribute(transform);
    case ESUPERSCRIPT: return Integer.valueOf(superscript);
    case EFONT: return font;
    case ECHAR_REPLACEMENT: return charReplacement;
    case EFOREGROUND: return foreground;
    case EBACKGROUND: return background;
    case EUNDERLINE: return Integer.valueOf(underline);
    case ESTRIKETHROUGH: return Boolean.valueOf(strikethrough);
    case ERUN_DIRECTION: {
        switch (runDirection) {
            // todo: figure out a way to indicate this value
            // case -1: return Integer.valueOf(runDirection);
        case 0: return TextAttribute.RUN_DIRECTION_LTR;
        case 1: return TextAttribute.RUN_DIRECTION_RTL;
        default: return null;
        }
    } // not reachable
    case EBIDI_EMBEDDING: return Integer.valueOf(bidiEmbedding);
    case EJUSTIFICATION: return Float.valueOf(justification);
    case EINPUT_METHOD_HIGHLIGHT: return imHighlight;
    case EINPUT_METHOD_UNDERLINE: return Integer.valueOf(imUnderline);
    case ESWAP_COLORS: return Boolean.valueOf(swapColors);
    case ENUMERIC_SHAPING: return numericShaping;
    case EKERNING: return Integer.valueOf(kerning);
    case ELIGATURES: return Integer.valueOf(ligatures);
    case ETRACKING: return Float.valueOf(tracking);
    default: throw new InternalError();
    }
}