Search in sources :

Example 36 with Font

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

the class DefaultLookAndFeel method getTextFieldCursorX.

/**
 * Calculates the position of the text field cursor within the string
 */
private int getTextFieldCursorX(TextArea ta) {
    Style style = ta.getStyle();
    Font f = style.getFont();
    // display ******** if it is a password field
    String displayText = getTextFieldString(ta);
    String inputMode = ta.getInputMode();
    int inputModeWidth = f.stringWidth(inputMode);
    // QWERTY devices don't quite have an input mode hide it also when we have a VK
    if (ta.isQwertyInput() || Display.getInstance().isVirtualKeyboardShowing()) {
        inputMode = "";
        inputModeWidth = 0;
    }
    int xPos = 0;
    int cursorCharPosition = ta.getCursorX();
    int cursorX = 0;
    int x = 0;
    if (reverseAlignForBidi(ta) == Component.RIGHT) {
        if (Display.getInstance().isBidiAlgorithm()) {
            // char[] dest = displayText.toCharArray();
            cursorCharPosition = Display.getInstance().getCharLocation(displayText, cursorCharPosition - 1);
            if (cursorCharPosition == -1) {
                xPos = f.stringWidth(displayText);
            } else {
                displayText = Display.getInstance().convertBidiLogicalToVisual(displayText);
                if (!isRTLOrWhitespace((displayText.charAt(cursorCharPosition)))) {
                    cursorCharPosition++;
                }
                xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
            }
        }
        int displayX = ta.getX() + ta.getWidth() - style.getPaddingLeft(ta.isRTL()) - f.stringWidth(displayText);
        cursorX = displayX + xPos;
        x = 0;
    } else {
        if (cursorCharPosition > 0) {
            cursorCharPosition = Math.min(displayText.length(), cursorCharPosition);
            xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
        }
        cursorX = ta.getX() + style.getPaddingLeft(ta.isRTL()) + xPos;
        if (ta.isSingleLineTextArea() && ta.getWidth() > (f.getHeight() * 2) && cursorX >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL())) {
            if (x + xPos >= ta.getWidth() - inputModeWidth - style.getPaddingLeftNoRTL() - style.getPaddingRightNoRTL()) {
                x = ta.getWidth() - inputModeWidth - style.getPaddingLeftNoRTL() - style.getPaddingRightNoRTL() - xPos - 1;
            }
        }
    }
    return cursorX + x;
}
Also used : Font(com.codename1.ui.Font)

Example 37 with Font

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

the class UIManager method parseStyle.

/**
 * Creates a style by providing style strings in a specific format. This method allows for the use of inline styles
 * to override the styles in {@link com.codename1.ui.Component}
 * @param theme Theme used to retrieve images referenced in the style strings.
 * @param id The style ID (UIID) to use to cache the style inside the theme.
 * @param prefix Prefix to use for styles.  Corresponds to the {@literal prefix} argument in {@link #getComponentStyleImpl(java.lang.String, boolean, java.lang.String)
 * @param baseStyle The style class from which this new style should derive.
 * @param selected True if this is for a selected style.
 * @param styleString Array of style strings to be parsed.  The format is {@literal key1:value1; key2:value2; etc...}.  While this looks similar to CSS, it is important to note that it is NOT
 * CSS.  The keys and values correspond to the properties of {@link com.codename1.ui.plaf.Style} and their associated values.
 * @return A style object representing the styles that were provided in the styleString.
 *
 * <h3>Example Usage</h3>
 *
 * {@code
 * Style s = parseStyle(theme, "Button[MyCustomButton]", "", "Button", false,
 *     "fgColor:ff0000; font:18mm; border: 1px solid ff0000; bgType:none; padding: 3mm; margin: 1mm");
 *
 * // Create a 9-piece image border on the fly:
 * Style s = parseStyle(theme, "Button[MyCustomButton]", "", "Button", false,
 *      "border:splicedImage /notes.png 0.3 0.4 0.3 0.4");
 *      // This splices the image found at /notes.png into 9 pieces.  Splice insets are specified by the 4 floating point values
 *      // at the end of the border directive:  [top] [right] [bottom] [left].
 * }
 */
Style parseStyle(Resources theme, String id, String prefix, String baseStyle, boolean selected, String... styleString) {
    String cacheKey = selected ? id + ".sel" : id + "." + prefix;
    String originalId = id;
    if (id == null || id.length() == 0) {
        // if no id return the default style
        id = "";
    } else {
        id = id + ".";
    }
    if (Arrays.toString(styleString).equals(parseCache().get(cacheKey)) && ((selected && selectedStyles.containsKey(id)) || (!selected && this.styles.containsKey(id)))) {
        return getComponentStyleImpl(originalId, selected, prefix);
    }
    parseCache().put(cacheKey, Arrays.toString(styleString));
    Style base = baseStyle != null ? getComponentStyleImpl(baseStyle, selected, prefix) : null;
    Map<String, String> styles = new HashMap<String, String>();
    for (String str : styleString) {
        StyleParser.parseString(styles, str);
    }
    StyleInfo styleInfo = new StyleInfo(styles);
    if (prefix != null && prefix.length() > 0) {
        id += prefix;
    }
    if (themeProps == null) {
        resetThemeProps(null);
    }
    if (baseStyle != null) {
        themeProps.put(id + "derive", baseStyle);
    } else {
        themeProps.remove(id + "derive");
    }
    String val = null;
    Integer bgColor = styleInfo.getBgColor();
    if (bgColor != null) {
        themeProps.put(id + Style.BG_COLOR, Integer.toHexString(bgColor));
    } else {
        themeProps.remove(id + Style.BG_COLOR);
    }
    Integer fgColor = styleInfo.getFgColor();
    if (fgColor != null) {
        themeProps.put(id + Style.FG_COLOR, Integer.toHexString(fgColor));
    } else {
        themeProps.remove(id + Style.FG_COLOR);
    }
    BorderInfo border = styleInfo.getBorder();
    if (border != null) {
        themeProps.put(id + Style.BORDER, border.createBorder(theme));
    } else {
        themeProps.remove(id + Style.BORDER);
    }
    Integer bgType = styleInfo.getBgType();
    if (bgType != null) {
        themeProps.put(id + Style.BACKGROUND_TYPE, bgType.byteValue());
    } else {
        themeProps.remove(id + Style.BACKGROUND_TYPE);
    }
    ImageInfo bgImage = styleInfo.getBgImage();
    if (bgImage != null) {
        themeProps.put(id + Style.BG_IMAGE, bgImage.getImage(theme));
    } else {
        themeProps.remove(id + Style.BG_IMAGE);
    }
    MarginInfo margin = styleInfo.getMargin();
    if (margin != null) {
        float[] marginArr = margin.createMargin(base);
        themeProps.put(id + Style.MARGIN, marginArr[Component.TOP] + "," + marginArr[Component.BOTTOM] + "," + marginArr[Component.LEFT] + "," + marginArr[Component.RIGHT]);
        byte[] unitArr = margin.createMarginUnit(base);
        themeProps.put(id + Style.MARGIN_UNIT, new byte[] { unitArr[Component.TOP], unitArr[Component.BOTTOM], unitArr[Component.LEFT], unitArr[Component.RIGHT] });
    } else {
        themeProps.remove(id + Style.MARGIN);
        themeProps.remove(id + Style.MARGIN_UNIT);
    }
    PaddingInfo padding = styleInfo.getPadding();
    if (padding != null) {
        float[] paddingArr = padding.createPadding(base);
        themeProps.put(id + Style.PADDING, paddingArr[Component.TOP] + "," + paddingArr[Component.BOTTOM] + "," + paddingArr[Component.LEFT] + "," + paddingArr[Component.RIGHT]);
        byte[] unitArr = padding.createPaddingUnit(base);
        themeProps.put(id + Style.PADDING_UNIT, new byte[] { unitArr[Component.TOP], unitArr[Component.BOTTOM], unitArr[Component.LEFT], unitArr[Component.RIGHT] });
    } else {
        themeProps.remove(id + Style.PADDING);
        themeProps.remove(id + Style.PADDING_UNIT);
    }
    Integer transparency = styleInfo.getTransparency();
    if (transparency != null) {
        themeProps.put(id + Style.TRANSPARENCY, String.valueOf(transparency.intValue()));
    } else {
        themeProps.remove(id + Style.TRANSPARENCY);
    }
    Integer opacity = styleInfo.getOpacity();
    if (opacity != null) {
        themeProps.put(id + Style.OPACITY, String.valueOf(opacity.intValue()));
    } else {
        themeProps.remove(id + Style.OPACITY);
    }
    Integer alignment = styleInfo.getAlignment();
    if (alignment != null) {
        themeProps.put(id + Style.ALIGNMENT, alignment);
    } else {
        themeProps.remove(id + Style.ALIGNMENT);
    }
    Integer textDecoration = styleInfo.getTextDecoration();
    if (textDecoration != null) {
        themeProps.put(id + Style.TEXT_DECORATION, textDecoration);
    } else {
        themeProps.remove(id + Style.TEXT_DECORATION);
    }
    FontInfo font = styleInfo.getFont();
    if (font != null) {
        themeProps.put(id + Style.FONT, font.createFont(base));
    } else {
        themeProps.remove(id + Style.FONT);
    }
    if (selected)
        selectedStyles.remove(id);
    else
        this.styles.remove(id);
    return getComponentStyleImpl(originalId, selected, prefix);
}
Also used : HashMap(java.util.HashMap) BorderInfo(com.codename1.ui.plaf.StyleParser.BorderInfo) PaddingInfo(com.codename1.ui.plaf.StyleParser.PaddingInfo) MarginInfo(com.codename1.ui.plaf.StyleParser.MarginInfo) StyleInfo(com.codename1.ui.plaf.StyleParser.StyleInfo) ImageInfo(com.codename1.ui.plaf.StyleParser.ImageInfo) FontInfo(com.codename1.ui.plaf.StyleParser.FontInfo)

Example 38 with Font

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

the class EditableResources method saveTheme.

private void saveTheme(DataOutputStream output, Hashtable theme, boolean newVersion) throws IOException {
    theme.remove("name");
    output.writeShort(theme.size());
    for (Object currentKey : theme.keySet()) {
        String key = (String) currentKey;
        output.writeUTF(key);
        if (key.startsWith("@")) {
            if (key.endsWith("Image")) {
                output.writeUTF(findId(theme.get(key), true));
            } else {
                output.writeUTF((String) theme.get(key));
            }
            continue;
        }
        // if this is a simple numeric value
        if (key.endsWith("Color")) {
            output.writeInt(Integer.decode("0x" + theme.get(key)));
            continue;
        }
        if (key.endsWith("align") || key.endsWith("textDecoration")) {
            output.writeShort(((Number) theme.get(key)).shortValue());
            continue;
        }
        // if this is a short numeric value
        if (key.endsWith("transparency")) {
            output.writeByte(Integer.parseInt((String) theme.get(key)));
            continue;
        }
        if (key.endsWith("opacity")) {
            output.writeInt(Integer.parseInt((String) theme.get(key)));
            continue;
        }
        // if this is a padding or margin then we will have the 4 values as bytes
        if (key.endsWith("padding") || key.endsWith("margin")) {
            String[] arr = ((String) theme.get(key)).split(",");
            output.writeFloat(Float.parseFloat(arr[0]));
            output.writeFloat(Float.parseFloat(arr[1]));
            output.writeFloat(Float.parseFloat(arr[2]));
            output.writeFloat(Float.parseFloat(arr[3]));
            continue;
        }
        // padding and or margin type
        if (key.endsWith("Unit")) {
            for (byte b : (byte[]) theme.get(key)) {
                output.writeByte(b);
            }
            continue;
        }
        if (key.endsWith("border")) {
            Border border = (Border) theme.get(key);
            writeBorder(output, border, newVersion);
            continue;
        }
        // if this is a font
        if (key.endsWith("font")) {
            com.codename1.ui.Font f = (com.codename1.ui.Font) theme.get(key);
            // is this a new font?
            boolean newFont = f instanceof EditorFont;
            output.writeBoolean(newFont);
            if (newFont) {
                String fontId = findId(f);
                output.writeUTF(fontId);
            } else {
                output.writeByte(f.getFace());
                output.writeByte(f.getStyle());
                output.writeByte(f.getSize());
                if (f instanceof EditorTTFFont && (((EditorTTFFont) f).getFontFile() != null || ((EditorTTFFont) f).getNativeFontName() != null)) {
                    output.writeBoolean(true);
                    EditorTTFFont ed = (EditorTTFFont) f;
                    if (ed.getNativeFontName() != null) {
                        output.writeUTF(ed.getNativeFontName());
                        output.writeUTF(ed.getNativeFontName());
                    } else {
                        output.writeUTF(ed.getFontFile().getName());
                        output.writeUTF(((java.awt.Font) ed.getNativeFont()).getPSName());
                    }
                    output.writeInt(ed.getSizeSetting());
                    output.writeFloat(ed.getActualSize());
                } else {
                    output.writeBoolean(false);
                }
            }
            continue;
        }
        // if this is a background image
        if (key.endsWith("bgImage")) {
            String imageId = findId(theme.get(key), true);
            if (imageId == null) {
                imageId = "";
            }
            output.writeUTF(imageId);
            continue;
        }
        if (key.endsWith("scaledImage")) {
            output.writeBoolean(theme.get(key).equals("true"));
            continue;
        }
        if (key.endsWith("derive")) {
            output.writeUTF((String) theme.get(key));
            continue;
        }
        // if this is a background gradient
        if (key.endsWith("bgGradient")) {
            Object[] gradient = (Object[]) theme.get(key);
            output.writeInt(((Integer) gradient[0]).intValue());
            output.writeInt(((Integer) gradient[1]).intValue());
            output.writeFloat(((Float) gradient[2]).floatValue());
            output.writeFloat(((Float) gradient[3]).floatValue());
            output.writeFloat(((Float) gradient[4]).floatValue());
            continue;
        }
        if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
            output.writeByte(((Number) theme.get(key)).intValue());
            continue;
        }
        // thow an exception no idea what this is
        throw new IOException("Error while trying to read theme property: " + key);
    }
}
Also used : IOException(java.io.IOException) LegacyFont(com.codename1.ui.util.xml.LegacyFont) EditorTTFFont(com.codename1.ui.EditorTTFFont) EditorFont(com.codename1.ui.EditorFont) EditorTTFFont(com.codename1.ui.EditorTTFFont) AnimationObject(com.codename1.ui.animations.AnimationObject) EditorFont(com.codename1.ui.EditorFont) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) RoundBorder(com.codename1.ui.plaf.RoundBorder) Border(com.codename1.ui.plaf.Border)

Example 39 with Font

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

the class EditableResources method saveFont.

private void saveFont(DataOutputStream output, boolean packed, String id) throws IOException {
    EditorFont f = (EditorFont) getFont(id);
    // write the system font fallback
    output.writeByte(f.getSystemFallback().getFace() | f.getSystemFallback().getSize() | f.getSystemFallback().getStyle());
    // do we have an emedded truetype font? Do we support embedded fonts?
    boolean trueTypeIncluded = f.getTruetypeFont() != null;
    output.writeBoolean(trueTypeIncluded);
    if (trueTypeIncluded) {
        output.writeInt(f.getTruetypeFont().length);
        output.write(f.getTruetypeFont());
    }
    boolean lookupIncluded = f.getLookupFont() != null;
    output.writeBoolean(lookupIncluded);
    if (lookupIncluded) {
        output.writeUTF(f.getLookupFont());
    }
    boolean bitmapIncluded = f.isIncludesBitmap();
    output.writeBoolean(bitmapIncluded);
    if (bitmapIncluded) {
        com.codename1.ui.Font bitmapFont = f.getBitmapFont();
        com.codename1.ui.Image cache = CodenameOneAccessor.getImage(bitmapFont);
        int[] imageArray = cache.getRGB();
        for (int iter = 0; iter < imageArray.length; iter++) {
            imageArray[iter] = (imageArray[iter] >> 8) & 0xff0000;
        }
        saveImage(output, com.codename1.ui.Image.createImage(imageArray, cache.getWidth(), cache.getHeight()), null, BufferedImage.TYPE_INT_RGB);
        String charset = bitmapFont.getCharset();
        int charCount = charset.length();
        output.writeShort(charCount);
        int[] cutOffsets = CodenameOneAccessor.getOffsets(bitmapFont);
        int[] charWidth = CodenameOneAccessor.getWidths(bitmapFont);
        for (int iter = 0; iter < charCount; iter++) {
            output.writeShort(cutOffsets[iter]);
        }
        for (int iter = 0; iter < charCount; iter++) {
            output.writeByte(charWidth[iter]);
        }
        output.writeUTF(charset);
        output.writeByte(f.getRenderingHint());
    }
}
Also used : Image(com.codename1.ui.Image) EditorFont(com.codename1.ui.EditorFont)

Example 40 with Font

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

the class ResourceEditorView method importResourceStream.

public void importResourceStream(InputStream is) throws IOException {
    EditableResources r = new EditableResources();
    r.openFile(is);
    checkDuplicateResourcesLoop(r, loadedResources.getThemeResourceNames(), r.getThemeResourceNames(), "Rename Theme", "Theme ");
    // load all the themes so rename will work properly on images and won't conflict
    for (String t : r.getThemeResourceNames()) {
        r.getTheme(t);
    }
    checkDuplicateResourcesLoop(r, loadedResources.getImageResourceNames(), r.getImageResourceNames(), "Rename Image", "Image ");
    checkDuplicateResourcesLoop(r, loadedResources.getL10NResourceNames(), r.getL10NResourceNames(), "Rename Localization", "Localization ");
    checkDuplicateResourcesLoop(r, loadedResources.getDataResourceNames(), r.getDataResourceNames(), "Rename Data", "Data ");
    checkDuplicateResourcesLoop(r, loadedResources.getUIResourceNames(), r.getUIResourceNames(), "Rename GUI", "GUI ");
    checkDuplicateResourcesLoop(r, loadedResources.getFontResourceNames(), r.getFontResourceNames(), "Rename Font", "Font ");
    for (String s : r.getImageResourceNames()) {
        if (r.isMultiImage(s)) {
            loadedResources.setMultiImage(s, (EditableResources.MultiImage) r.getResourceObject(s));
        } else {
            loadedResources.setImage(s, r.getImage(s));
        }
    }
    for (String s : r.getL10NResourceNames()) {
        loadedResources.setL10N(s, (Hashtable) r.getResourceObject(s));
    }
    for (String s : r.getDataResourceNames()) {
        loadedResources.setData(s, (byte[]) r.getResourceObject(s));
    }
    for (String s : r.getUIResourceNames()) {
        loadedResources.setUi(s, (byte[]) r.getResourceObject(s));
    }
    for (String s : r.getFontResourceNames()) {
        loadedResources.setFont(s, r.getFont(s));
    }
    for (String s : r.getThemeResourceNames()) {
        loadedResources.setTheme(s, r.getTheme(s));
    }
}
Also used : EditableResources(com.codename1.ui.util.EditableResources)

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