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

📄 customfont.java

📁 很好的基于java的手机文本阅读器anyview 2.0源码,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    }

                }
            }

            byte abyte2[] = new byte[k];
            for (int j1 = 0; j1 < k; j1++) {
                abyte2[j1] = abyte1[j1];
            }

            ac = null;
            abyte0 = null;
            abyte1 = null;
            return abyte2;
        } catch (Exception _ex) {
            return (new String(s)).getBytes();
        }
    }

    public final void drawString(Graphics g, String str, int x, int y,
                                 int anchor) {
        buildImage(str);
        g.drawImage(fontImage, x, y, anchor);
    }

    public final Image buildImage(String str) {
        fontImage = null;
        System.gc();
        if (null == str || "".equals(str)) {
            str = " ";
        }
        byte[] b = str2bytes(str);
        return buildImage(b);
    }

    public final Image buildImage(byte[] b) {
        if (b == null) {
            return null;
        }
        int width = AsciiWidth * b.length;
        int[] image = new int[FontHeight * width];
        int x = 0;
        int i = 0;
        while (i < b.length - 1) {
            if ((b[i] & 0xff) > 0x7F) { //汉字
                int skip = 0;
                if (gbk) {
                    skip = drawGBK(b[i], b[i + 1], image, x, width);
                } else {
                    skip = drawGB(b[i], b[i + 1], image, x, width);
                }
                if (skip == 1) {
                    x += AsciiWidth;
                } else {
                    x += GBWidth;
                }
                i += skip;
            } else { //英文
                drawAscii(b[i], image, x, width);
                x += AsciiWidth;
                i++;
            }
        }
        if (i == b.length - 1) { //最后一个是英文
            drawAscii(b[b.length - 1], image, x, width);
        }
        fontImage = Image.createRGBImage(image, width, FontHeight, true);
        return fontImage;
    }

    public final String byte2unicode(byte[] abyte) {
        StringBuffer sb = new StringBuffer();
        int i3 = unicode.length - 1;
        for (int i = 0; i < abyte.length; i++) {
            if (abyte[i] < 0) {
                int j;
                if ((j = (((abyte[i] + 256) * 256 + (abyte[i + 1] + 256)) -
                          0x10000) + 24159) > 0 && j < i3) {
                    sb.append(unicode[j]);
                }
                if (j == 0) {
                    sb.append("  ");
                }
                i++;
            } else {
                sb.append((char) abyte[i]);
            }
        }
        return sb.toString();
    }

    //ascii显示代码
    private void drawAscii(byte bt, int[] image, int x, int width) {
        int index = bt & 0xff;
        if (index < 33 || index > 126) {
            return;
        }
        int offset = index * AsciiPerByte;
        int pos = x;
        if (this.AsciiWidth > 8) { //一字节显示不下
            for (int h = 0; h < FontHeight; h++) {
                byte b = charset[offset++];
                for (int w = 0; w < 8; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + w] = ALPHA | colors[h];
                    } else {
                        image[pos + w] = 0;
                    }
                }

                b = charset[offset++];
                for (int w = 0; w < AsciiWidth - 8; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + 8 + w] = ALPHA | colors[h];
                    } else {
                        image[pos + 8 + w] = 0;
                    }
                }
                pos += width;
            }
        } else {
            for (int h = 0; h < FontHeight; h++) {
                byte b = charset[offset++];
                for (int w = 0; w < AsciiWidth; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + w] = ALPHA | colors[h];
                    } else {
                        image[pos + w] = 0;
                    }
                }
                pos += width;
            }
        }
    }

    //GB2312显示代码
    private int drawGB(byte bt1, byte bt2, int[] image, int x, int width) {
        int q1 = bt1 & 0xff;
        int q2 = bt2 & 0xff;
        int offset = (q1 - 0xa1) * 94 * GBPerByte;
        q2 -= 0xa1;
        offset += q2 * GBPerByte;
        offset += AsciiOffset;
        if (offset > charset.length - 1) { //超过范围,处理下一个
            return 1;
        }

        if (offset < 0) { //使用GB库绘制GBK字符
            return 2;
        }

        int pos = x;
        if (this.GBWidth > 16) {
            for (int h = 0; h < FontHeight; h++) {
                byte b = charset[offset++];
                for (int w = 0; w < 8; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + w] = ALPHA | colors[h];
                    } else {
                        image[pos + w] = 0;
                    }
                }
                b = charset[offset++];
                for (int w = 0; w < 8; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + 8 + w] = ALPHA | colors[h];
                    } else {
                        image[pos + 8 + w] = 0;
                    }
                }
                b = charset[offset++];
                for (int w = 0; w < (GBWidth - 16); w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + 16 + w] = ALPHA | colors[h];
                    } else {
                        image[pos + 16 + w] = 0;
                    }
                }

                pos += width;
            }
        } else {
            for (int h = 0; h < FontHeight; h++) {
                byte b = charset[offset++];
                for (int w = 0; w < 8; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + w] = ALPHA | colors[h];
                    } else {
                        image[pos + w] = 0;
                    }
                }
                b = charset[offset++];
                for (int w = 0; w < (GBWidth - 8); w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + 8 + w] = ALPHA | colors[h];
                    } else {
                        image[pos + 8 + w] = 0;
                    }
                }
                pos += width;
            }
        }
        return 2;
    }

    private int drawGBK(byte bt1, byte bt2, int[] image, int x, int width) {
        int q1 = bt1 & 0xff;
        int q2 = bt2 & 0xff;
        int offset = (q1 - 0x81) * 190 * GBPerByte;
        if (q2 >= 0x80) {
            q2 -= 0x41;
        } else {
            q2 -= 0x40;
        }
        offset += q2 * GBPerByte;

        offset += AsciiOffset;
        if (offset > charset.length - 1) { //超过范围,处理下一个
            return 1;
        }
        int pos = x;
        if (this.GBWidth > 16) {
            for (int h = 0; h < FontHeight; h++) {
                byte b = charset[offset++];
                for (int w = 0; w < 8; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + w] = ALPHA | colors[h];
                    } else {
                        image[pos + w] = 0;
                    }
                }
                b = charset[offset++];
                for (int w = 0; w < 8; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + 8 + w] = ALPHA | colors[h];
                    } else {
                        image[pos + 8 + w] = 0;
                    }
                }
                b = charset[offset++];
                for (int w = 0; w < (GBWidth - 16); w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + 16 + w] = ALPHA | colors[h];
                    } else {
                        image[pos + 16 + w] = 0;
                    }
                }

                pos += width;
            }
        } else {
            for (int h = 0; h < FontHeight; h++) {
                byte b = charset[offset++];
                for (int w = 0; w < 8; w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + w] = ALPHA | colors[h];
                    } else {
                        image[pos + w] = 0;
                    }
                }
                b = charset[offset++];
                for (int w = 0; w < (GBWidth - 8); w++) {
                    if ((b & verify[w]) == verify[w]) {
                        image[pos + 8 + w] = ALPHA | colors[h];
                    } else {
                        image[pos + 8 + w] = 0;
                    }
                }
                pos += width;
            }
        }
        return 2;
    }

    public int stringWidth(String str) {
        return stringWidth(str2bytes(str));
    }

    public int stringWidth(byte[] b) {
        int width = 0;
        if (null == b || b.length == 0) {
            return width;
        }
        for (int i = 0; i < b.length; i++) {
            if ((b[i] & 0xff) < 128) {
                width += AsciiWidth;
            } else {
                width += GBWidth;
                i++;
            }
        }
        return width;
    }

}

⌨️ 快捷键说明

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