创建本机位图库

时间:2012-05-31 21:09:43

标签: java-me fonts bitmap

我是波斯语,j2me对波斯语字体没有很好的支持。

我将创建一个本地字体库,用于获取位图字体并在desplay中绘制我的波斯文本。但我有一个问题。

在英语中,每个字母都是一个字母组合和解码。与(a , U+0061)

一样

但在波斯语中,char可能有几种形状。例如,波斯字母表中的字母'ب'可以是:

آب - 当它是单词中的单独字母时 به - 当它是一个单词的起始字母时 ...

如何从字体文件中获取其他形式的信件?

2 个答案:

答案 0 :(得分:2)

我是一名波斯开发人员,大约4年前我遇到了同样的问题。你有办法解决这个问题:
1 - 使用自定义字体 2,重塑您的文字,然后再显示 关于第一个的好文章是“MIDP Terminal Emulation, Part 3: Custom Fonts for MIDP”。但对于阿拉伯字母,我认为这并不简单。 大约在第二种方式,假设你要用正确的字符替换文本中的任何字符。这意味着当你有:

String str = "به";

如果获得str字符,它们将如下所示:
{1576,1607}就像“به”而不是“به”。所以你要用正确的Unicode代码替换不正确的Unicode(在这种情况下,正确的字符是:{65169,65258})。你可以使用“阿拉伯语重塑者” “甚至为Android设计的重塑器!我看到了这个重塑器的2个链接:1 - github 2 - Arabic Android(我是波斯开发者,所以我不尝试它们,而是我用和他们一样的想法。) 使用好的整形器也可能会出现从左到右而不是从右到左排列字符的问题。(有些手机从左到右和从右到左绘制字符)。我在下面使用用于检测排序是否为真的类(从右到左):

public class DetectOrdering{   
    public static boolean hasTrueOrdering()
    {
        boolean b = false;
        try {
        char[] chArr = {65169, 65258};
        String str = new String(chArr);
        System.out.println(str);
        int width = f1.charWidth(chArr[1]) / 2;
        int height = f1.getHeight();
        image1 = Image.createImage(width, height);
        image2 = Image.createImage(width, height);
        Graphics g1 = image1.getGraphics();
        Graphics g2 = image2.getGraphics();
        g1.drawString(str, 0, 0, 0);
        g2.drawChar(chArr[1], 0, 0, 0);
        int[] im1 = new int[width * height];
        int[] im2 = new int[width * height];

        image1.getRGB(im1, 0, width, 0, 0, width, height);
        image2.getRGB(im2, 0, width, 0, 0, width, height);
        if (areEqualIntArrrays(im1, im2)) {
            b = true;
        } else {
            b = false;
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

private static boolean areEqualIntArrrays(int[] i1, int[] i2) {
    if (i1.length != i2.length) {
        return false;
    } else {
        for (int i = 0; i < i1.length; i++) {
            if (i1[i] != i2[i]) {
                return false;
            }
        }
    }
    return true;
    }
}    

如果DetectOrdering.hasTrueOrdering()返回true,请确保手机从右向左绘制阿拉伯字符并显示您的String.If返回false,从左到右绘制。如果手机从左到右绘制阿拉伯字符,您将反转重塑它后的字符串然后你可以显示它。

答案 1 :(得分:1)

您可以将一个alphabet.png用于直接unicode映射(由于邻居字符而导致波斯字符不会更改的映射)。如果您的字符是等宽字符,则可以从下面的课程开始,如http://smallandadaptive.blogspot.com.br/2008/12/custom-monospaced-font.html所示:


    public class MonospacedFont {

    private Image image;
    private char firstChar;
    private int numChars;
    private int charWidth;

    public MonospacedFont(Image image, char firstChar, int numChars) {

        if (image == null) {
            throw new IllegalArgumentException("image == null");
        }

        // the first visible Unicode character is '!' (value 33)
        if (firstChar <= 33) {
            throw new IllegalArgumentException("firstChar <= 33");
        }

        // there must be at lease one character on the image
        if (numChars <= 0) {
            throw new IllegalArgumentException("numChars <= 0");
        }

        this.image = image;
        this.firstChar = firstChar;
        this.numChars = numChars;
        this.charWidth = image.getWidth() / this.numChars;
    }

    public void drawString(Graphics g, String text, int x, int y) {

        // store current Graphics clip area to restore later
        int clipX = g.getClipX();
        int clipY = g.getClipY();
        int clipWidth = g.getClipWidth();
        int clipHeight = g.getClipHeight();
        char[] chars = text.toCharArray();

        for (int i = 0; i < chars.length; i++) {

            int charIndex = chars[i] - this.firstChar;

            // current char exists on the image
            if (charIndex >= 0 && charIndex <= this.numChars) {
                g.setClip(x, y, this.charWidth, this.image.getHeight());
                g.drawImage(image, x - (charIndex * this.charWidth), y,
                Graphics.TOP | Graphics.LEFT);
                x += this.charWidth;
            }
        }

        // restore initial clip area
        g.setClip(clipX, clipY, clipWidth, clipHeight);
    }

}

并将其更改为对每个因邻居字符而更改的波斯字符使用不同的char_uxxxx.png文件。

在解析字符串时,在绘制之前,必须检查哪个png文件适合使用。希望这是一个好的开始。