Java Code Examples for java.awt.font.TextAttribute

The following examples show how to use java.awt.font.TextAttribute. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: jdk8u-jdk   Source File: KerningLeak.java    License: 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 2
Source Project: openstock   Source File: LogAxis.java    License: 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 Project: openjdk-8   Source File: ParagraphView.java    License: 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
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 Project: Bytecoder   Source File: SwingUtilities2.java    License: 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
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 Project: gate-core   Source File: OptionsMap.java    License: 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 Project: jdk8u_jdk   Source File: ParagraphView.java    License: 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
/**
 * 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 Project: openjdk-8-source   Source File: KerningLeak.java    License: 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 Project: mil-sym-java   Source File: RendererSettings.java    License: 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
/**
 * 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 Project: hottub   Source File: Utilities.java    License: 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 Project: openjdk-8   Source File: KerningLeak.java    License: 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 Project: TencentKona-8   Source File: ParagraphView.java    License: 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 Project: jdk8u-jdk   Source File: KerningLeak.java    License: 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 Project: Java8CN   Source File: Font.java    License: 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 Project: stendhal   Source File: StringFormatTest.java    License: 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
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
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 Project: openjdk-jdk8u   Source File: ReadResolve.java    License: 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 Project: jdk8u60   Source File: JTextComponent.java    License: 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
protected FontUIResource getObject() {
    return new FontUIResource(
            new Font(
                    Collections.singletonMap(
                            TextAttribute.STRIKETHROUGH,
                            TextAttribute.STRIKETHROUGH_ON)));
}
 
Example 24
Source Project: Bytecoder   Source File: AttributeValues.java    License: 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
/**
 * 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 Project: jdk8u_jdk   Source File: BidiEmbeddingTest.java    License: 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
@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 Project: netbeans   Source File: CCPaintComponent.java    License: 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 Project: jdk8u_jdk   Source File: AttributeValues.java    License: 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 Project: openjdk-8   Source File: AttributeValues.java    License: 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();
    }
}
 
Example 31
protected FontUIResource getObject() {
    return new FontUIResource(
            new Font(
                    Collections.singletonMap(
                            TextAttribute.STRIKETHROUGH,
                            TextAttribute.STRIKETHROUGH_ON)));
}
 
Example 32
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();
    }
}
 
Example 33
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 34
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 35
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 36
Source Project: jdk8u-jdk   Source File: AttributeValues.java    License: 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();
    }
}
 
Example 37
Source Project: openjdk-jdk9   Source File: AttributeValues.java    License: GNU General Public License v2.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 38
/**
 * Sets attributes for a paragraph.
 * <p>
 * This method is thread safe, although most Swing methods
 * are not. Please see
 * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 * in Swing</A> for more information.
 *
 * @param offset the offset into the paragraph &gt;= 0
 * @param length the number of characters affected &gt;= 0
 * @param s the attributes
 * @param replace whether to replace existing attributes, or merge them
 */
public void setParagraphAttributes(int offset, int length, AttributeSet s,
                                   boolean replace) {
    try {
        writeLock();
        DefaultDocumentEvent changes =
            new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.CHANGE);

        AttributeSet sCopy = s.copyAttributes();

        // PENDING(prinz) - this assumes a particular element structure
        Element section = getDefaultRootElement();
        int index0 = section.getElementIndex(offset);
        int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0));
        boolean isI18N = Boolean.TRUE.equals(getProperty(I18NProperty));
        boolean hasRuns = false;
        for (int i = index0; i <= index1; i++) {
            Element paragraph = section.getElement(i);
            MutableAttributeSet attr = (MutableAttributeSet) paragraph.getAttributes();
            changes.addEdit(new AttributeUndoableEdit(paragraph, sCopy, replace));
            if (replace) {
                attr.removeAttributes(attr);
            }
            attr.addAttributes(s);
            if (isI18N && !hasRuns) {
                hasRuns = (attr.getAttribute(TextAttribute.RUN_DIRECTION) != null);
            }
        }

        if (hasRuns) {
            updateBidi( changes );
        }

        changes.end();
        fireChangedUpdate(changes);
        fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
    } finally {
        writeUnlock();
    }
}
 
Example 39
Source Project: netbeans   Source File: AttributedCharacters.java    License: Apache License 2.0 5 votes vote down vote up
public int getRunStart(Set<? extends AttributedCharacterIterator.Attribute> attributes) {
    if ((attributes.contains(TextAttribute.FONT)) || attributes.contains(TextAttribute.FOREGROUND)) {
        return getRunStart();
    } else {
        return 0;
    }
}
 
Example 40
Source Project: hottub   Source File: AttributeMap.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Map<TextAttribute, Object> delegate() {
    if (delegateMap == null) {
        if (first) {
            first = false;
            Thread.dumpStack();
        }
        delegateMap = values.toMap(new HashMap<TextAttribute, Object>(27));

        // nuke values, once map is accessible it might be mutated and values would
        // no longer reflect its contents
        values = null;
    }

    return delegateMap;
}