⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customfont.java

📁 anyview手机小说阅览器的开源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ismyway.anyview;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.io.*;

/**
 * <p>Title: AnyView</p>
 *
 * <p>Description: E680(I) Reader</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: www.ismyway.com</p>
 *
 * @author ZhangJian
 * @version 1.0
 */
public class CustomFont {
    private byte[] un2gbmap;
    private char[] unicode;
    private byte[] charset;
    public static int GBWidth = 16;
    public static int AsciiWidth = 8;
    public static int FontHeight = 16;
    public static int AsciiOffset = 4096;
    public static int AsciiPerByte = 16;
    public static int GBPerByte = 32;
    public static int CurrentFontSize = 1;
    private Image fontImage;
    private int[] colors = new int[24];
    private boolean gbk = false;

    public final static int GBK16B = 8;
    public final static int GBK16 = 7;
    public final static int FONT24 = 6;
    public final static int FONT20 = 5;
    public final static int FONT18 = 4;
    public final static int FONT16B = 3;
    public final static int FONT16 = 2;
    public final static int FONT14 = 1;
    public final static int FONT12 = 0;
    public final static int ALPHA = 0XFF000000;
    public final static int[] verify = {128, 64, 32, 16, 8, 4, 2, 1};

    public CustomFont() {
        this(FONT16);
    }

    public CustomFont(int fontsize) {
        String root = FileSystemReader.listRoots()[1];
        FileSystemReader fsa = new FileSystemReader(root + "anyview/uc2gb");
        un2gbmap = null;
        un2gbmap = new byte[(int) fsa.fileSize()];
        un2gbmap = fsa.read(un2gbmap.length);
        fsa.close();
        fsa = null;

        fsa = new FileSystemReader(root + "anyview/unicode.map");
        unicode = null;
        byte[] b = new byte[(int) fsa.fileSize()];
        b = fsa.read(b.length);
        try {
            unicode = (new String(b, "UTF-8")).toCharArray();
        } catch (UnsupportedEncodingException ex) {
        }
        b = null;
        fsa.close();
        fsa = null;

        setFontSize(fontsize);
    }

    public void setColor(int c) {
        for (int i = 0; i < colors.length; i++) {
            colors[i] = c;
        }
    }

    public void setColor(int[] c) {
        for (int i = 0; i < colors.length && i < c.length; i++) {
            colors[i] = c[i];
        }
    }

    public void setColor(int c1, int c2) {
        int r1, g1, b1, r2, g2, b2, sr, sg, sb, r, g, b;
        r1 = c1 >> 16;
        g1 = (c1 >> 8) & 0xff;
        b1 = c1 & 0xff;

        r2 = c2 >> 16;
        g2 = (c2 >> 8) & 0xff;
        b2 = c2 & 0xff;

        sr = (r1 - r2) / (FontHeight - 1);
        sg = (g1 - g2) / (FontHeight - 1);
        sb = (b1 - b2) / (FontHeight - 1);

        for (int i = 1; i < FontHeight - 1; i++) {
            r = r1 - sr * i;
            g = g1 - sg * i;
            b = b1 - sb * i;

            colors[i] = r << 16 | g << 8 | b;
        }
        colors[0] = c1;
        colors[FontHeight - 1] = c2;
    }

    /**
     * 从在储卡中读取字库
     * @param fontsize int
     */
    public final void setFontSize(int fontsize) { //synchronized
        String root = FileSystemReader.listRoots()[1];
        FileSystemReader fsa;

        CurrentFontSize = fontsize;

        if (fontsize == GBK16B) {
            gbk = true;
            GBWidth = 16;
            AsciiWidth = 8;
            FontHeight = 16;
            AsciiOffset = 4080;
            AsciiPerByte = 16;
            GBPerByte = 32;

            fsa = new FileSystemReader(root + "anyview/df16gbkb.avf");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        } else if (fontsize == GBK16) {
            gbk = true;
            GBWidth = 16;
            AsciiWidth = 8;
            FontHeight = 16;
            AsciiOffset = 4080;
            AsciiPerByte = 16;
            GBPerByte = 32;

            fsa = new FileSystemReader(root + "anyview/df16gbk.avf");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        } else if (fontsize == FONT24) {
            gbk = false;
            GBWidth = 24;
            AsciiWidth = 12;
            FontHeight = 24;
            AsciiOffset = 12240;
            AsciiPerByte = 48;
            GBPerByte = 72;

            fsa = new FileSystemReader(root + "anyview/df24");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        } else if (fontsize == FONT20) {
            gbk = false;
            GBWidth = 20;
            AsciiWidth = 10;
            FontHeight = 20;
            AsciiOffset = 10200;
            AsciiPerByte = 40;
            GBPerByte = 60;

            fsa = new FileSystemReader(root + "anyview/df20");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        } else if (fontsize == FONT18) {
            gbk = false;
            GBWidth = 18;
            AsciiWidth = 9;
            FontHeight = 18;
            AsciiOffset = 9180;
            AsciiPerByte = 36;
            GBPerByte = 54;

            fsa = new FileSystemReader(root + "anyview/df18");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        } else if (fontsize == FONT16B) {
            gbk = false;
            GBWidth = 16;
            AsciiWidth = 8;
            FontHeight = 16;
            AsciiOffset = 4096;
            AsciiPerByte = 16;
            GBPerByte = 32;

            fsa = new FileSystemReader(root + "anyview/df16b");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        } else if (fontsize == FONT16) {
            gbk = false;
            GBWidth = 16;
            AsciiWidth = 8;
            FontHeight = 16;
            AsciiOffset = 4096;
            AsciiPerByte = 16;
            GBPerByte = 32;

            fsa = new FileSystemReader(root + "anyview/df16");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        } else if (fontsize == FONT14) {
            gbk = false;
            GBWidth = 16;
            AsciiWidth = 12;
            FontHeight = 24;
            AsciiOffset = 12240;
            AsciiPerByte = 48;
            GBPerByte = 48;

            fsa = new FileSystemReader(root + "anyview/yao16.24.font");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        } else {
            gbk = false;
            GBWidth = 12;
            AsciiWidth = 6;
            FontHeight = 12;
            AsciiOffset = 3060;
            AsciiPerByte = 12;
            GBPerByte = 24;

            fsa = new FileSystemReader(root + "anyview/df12");
            charset = null;
            charset = new byte[(int) fsa.fileSize()];
            charset = fsa.read(charset.length);
            fsa.close();
            fsa = null;
        }
        System.gc();
    }

    public final byte[] str2bytes(String s) {
        if (s == null || s.equals("")) {
            s = " ";
        }
        try {
            char ac[] = new char[256];
            byte abyte0[] = new byte[768];
            System.arraycopy(un2gbmap, 0, abyte0, 0, 512);
            int i = 0;
            for (int j = 0; i < 256; j += 2) {
                ac[i] = (char) (((abyte0[j] & 0xff) << 8) +
                                (abyte0[j + 1] & 0xff));
                i++;
            }

            byte abyte1[] = new byte[(i = s.length()) * 2];
            int k = 0;
            for (int i1 = 0; i1 < i; i1++) {
                char c;
                char c1 = (char) ((c = s.charAt(i1)) >> 8);
                byte byte0 = (byte) (c & 0xff);
                if (c1 == 0 && (byte0 & 0x80) == 0) {
                    abyte1[k++] = byte0;
                    if (byte0 == 10) {
                        abyte1[k++] = byte0;
                    }
                } else {
                    char c2 = ac[c1];
                    System.arraycopy(un2gbmap, c2, abyte0, 0, abyte0.length);
                    int l1 = (abyte0[0] & 0xff) * 3 + 1;
                    for (int i2 = 1; i2 < l1; i2 += 3) {
                        if (abyte0[i2] != byte0) {
                            continue;
                        }
                        abyte1[k++] = abyte0[++i2];
                        abyte1[k++] = abyte0[++i2];
                        break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -