Search in sources :

Example 56 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class Paint method breakText.

public int breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth) {
    char[] chars = text.toCharArray();
    Font f = getTypeface();
    float tmp = 0;
    if (f != null) {
        int start = measureForwards ? 0 : chars.length - 1;
        int inc = measureForwards ? 1 : -1;
        float currWidth = 0f;
        int clen = chars.length;
        int wlen = measuredWidth != null ? measuredWidth.length : -1;
        for (int i = start; (measureForwards && i < clen) || (!measureForwards && i >= 0); i += inc) {
            tmp = f.charWidth(chars[i]);
            if (currWidth + tmp > maxWidth) {
                return i;
            }
            if (i < wlen) {
                measuredWidth[i] = tmp;
            }
            currWidth += tmp;
        }
    } else {
        throw new RuntimeException("Failed to get font");
    }
    return chars.length;
}
Also used : Font(com.codename1.ui.Font)

Example 57 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class Paint method getCN1TextBounds.

void getCN1TextBounds(String string, int start, int count, Rectangle2D rect) {
    Font f = getTypeface();
    if (f != null) {
        int w = f.substringWidth(string, start, count);
        int h = f.getHeight();
        rect.setBounds(0, 0, w, h);
    }
}
Also used : Font(com.codename1.ui.Font)

Example 58 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class Paint method measureText.

public float measureText(char[] chars, int start, int count) {
    float out = 0f;
    Font f = getTypeface();
    if (f != null) {
        int clen = chars.length;
        for (int i = start; i < clen && i < start + count; i++) {
            out += f.charWidth(chars[i]);
        }
    } else {
        throw new RuntimeException("Failed to get font");
    }
    return out;
}
Also used : Font(com.codename1.ui.Font)

Example 59 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class Paint method measureTextHeight.

float measureTextHeight(char[] chars, int start, int count) {
    Font f = getTypeface();
    float h = 0f;
    if (f != null) {
        int clen = chars.length;
        for (int i = start; i < clen && i < start + count; i++) {
            float nh = f.getHeight();
            h = nh > h ? nh : h;
        }
    } else {
        throw new RuntimeException("Failed to get font");
    }
    return h;
}
Also used : Font(com.codename1.ui.Font)

Example 60 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class ComponentSelector method setFontSizePercent.

/**
 * Sets the fonts size of all components in the found set as a percentage of the font
 * size of the components' respective parents.
 * @param sizePercentage Font size as a percentage of parent font size.
 * @return
 */
public ComponentSelector setFontSizePercent(double sizePercentage) {
    for (Component c : this) {
        Component parent = c.getParent();
        if (parent != null) {
            float size = (float) (getEffectiveFontSize(parent) * sizePercentage / 100.0);
            Style style = getStyle(c);
            Font curr = style.getFont();
            if (curr == null || !curr.isTTFNativeFont()) {
                curr = c.getStyle().getFont();
            }
            if (curr == null || !curr.isTTFNativeFont()) {
                parent = c.getParent();
                while (parent != null && (curr == null || !curr.isTTFNativeFont())) {
                    curr = parent.getStyle().getFont();
                    parent = parent.getParent();
                }
            }
            if (curr == null || !curr.isTTFNativeFont()) {
                curr = Font.create("native:MainRegular");
            }
            if (curr != null && curr.isTTFNativeFont()) {
                curr = curr.derive(size, 0);
                style.setFont(curr);
            }
        }
    }
    return this;
}
Also used : Style(com.codename1.ui.plaf.Style)

Aggregations

Font (com.codename1.ui.Font)30 Image (com.codename1.ui.Image)12 Hashtable (java.util.Hashtable)10 EditorFont (com.codename1.ui.EditorFont)8 Border (com.codename1.ui.plaf.Border)8 Component (com.codename1.ui.Component)7 Style (com.codename1.ui.plaf.Style)7 EncodedImage (com.codename1.ui.EncodedImage)6 TextArea (com.codename1.ui.TextArea)6 AnimationObject (com.codename1.ui.animations.AnimationObject)6 BufferedImage (java.awt.image.BufferedImage)6 IOException (java.io.IOException)6 java.awt (java.awt)5 Container (com.codename1.ui.Container)4 Dimension (com.codename1.ui.geom.Dimension)4 RoundBorder (com.codename1.ui.plaf.RoundBorder)4 RoundRectBorder (com.codename1.ui.plaf.RoundRectBorder)4 ParseException (java.text.ParseException)4 Paint (android.graphics.Paint)3 EditorTTFFont (com.codename1.ui.EditorTTFFont)3