1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
private BufferedImage generateTextImage(String text) throws IOException {
int fontWidth = this.fontWidth; int fontHeight = this.fontHeight; int byteCount = this.byteCount;
int textLength = text.length();
ArrayList<char[][]> dotMatList = new ArrayList<>(textLength);
for (int i = 0; i < textLength; i++) { char word = text.charAt(i);
int codePoint = this.getCodePoint(String.valueOf(word)); codePoint = this.fixCodePointWhenFontWidth24(codePoint); int position = codePoint * byteCount;
byte[] fontData = this.readFontData(this.fontFileName, position, byteCount); char[][] dotMat = this.convertBytesToDotMat(fontData, fontHeight, fontWidth);
int realWidth = codePoint < 700 ? this.fontEn : this.fontZh; for (int j = 0; j < fontHeight; j++) { realWidth = this.autoWidth ? this.getCharWidth(dotMat, this.fontZh, this.fontWidth) : realWidth; realWidth = codePoint == this.SPACE_CODE_POINT ? this.SPACE_WIDTH : realWidth; char[] row = dotMat[j]; char[] newRow = new char[realWidth]; System.arraycopy(row, 0, newRow, 0, realWidth); newRow = this.setBold(newRow, this.bold); newRow = this.setSpacing(newRow, this.fontSpace);
dotMat[j] = newRow; } this.padDotMat(dotMat);
dotMatList.add(dotMat); }
char[][] finalDotMat = this.mergeDotMat(dotMatList);
this.textBitWidth = finalDotMat[0].length; this.textBitHeight = finalDotMat.length;
BufferedImage textImage = new BufferedImage(finalDotMat[0].length, fontHeight, BufferedImage.TYPE_BYTE_BINARY); this.renderDotMatToImage(finalDotMat, textImage, 0, 0);
return textImage; }
private int getCodePoint(String str) { int codePoint = 0; try { codePoint = str.codePointAt(0); } catch (IndexOutOfBoundsException e) { e.printStackTrace(); } if (codePoint > 65535) { codePoint = 0; } return codePoint; }
private int fixCodePointWhenFontWidth24(int codePoint) { if (this.fontWidth == 24) { if (codePoint <= this.SPACE_CODE_POINT || codePoint > this.fontMaxPosition) { codePoint = this.SPACE_CODE_POINT; } } return codePoint; }
private char[][] convertBytesToDotMat(byte[] fontData, int fontHeight, int fontWidth) { char[] dots = new char[fontHeight * fontWidth]; for (int i = 0; i < fontData.length; i++) { byte b = fontData[i]; for (int bit = 7; bit >= 0; bit--) { int bitValue = (b >> bit) & 1; dots[i * 8 + (7 - bit)] = bitValue == 1 ? '1' : '0'; } } char[][] dotMat = new char[fontHeight][fontWidth]; for (int i = 0; i < fontHeight; i++) { System.arraycopy(dots, i * fontWidth, dotMat[i], 0, fontWidth); } return dotMat; }
private char[] setBold(char[] dot, boolean fontBold) { if (!fontBold) { return dot; }
char[] result = new char[dot.length]; for (int i = 0; i < dot.length; i++) { char current = dot[i]; char shifted = i == 0 ? '0' : dot[i - 1]; result[i] = (current == '1' || shifted == '1') ? '1' : '0'; }
return result; }
private char[] setSpacing(char[] dotArray, int spacing) { if (spacing <= 0) { return dotArray; }
char[] result = new char[dotArray.length + spacing]; int index = 0;
for (char c : dotArray) { result[index++] = c; }
for (int i = 0; i < spacing; i++) { result[index++] = '0'; }
return result; }
private void padDotMat(char[][] dotMat) { int maxRowLength = 0; for (char[] row : dotMat) { maxRowLength = Math.max(maxRowLength, row.length); } for (int k = 0; k < dotMat.length; k++) { char[] row = dotMat[k]; char[] newRow = new char[maxRowLength]; System.arraycopy(row, 0, newRow, 0, row.length); for (int j = row.length; j < maxRowLength; j++) { newRow[j] = '0'; } dotMat[k] = newRow; } }
private char[][] mergeDotMat(ArrayList<char[][]> dotMatList) { int finalWidth = this.calcFinalWidth(dotMatList); int fontHeight = this.fontHeight; char[][] finalDotMat = new char[fontHeight][finalWidth]; int columnIndex = 0; for (char[][] chars : dotMatList) { int rowIndex = 0; for (char[] aChar : chars) { System.arraycopy(aChar, 0, finalDotMat[rowIndex++], columnIndex, aChar.length); } columnIndex += chars[0].length; } return finalDotMat; }
private int calcFinalWidth(ArrayList<char[][]> dotMatList) { int finalWidth = 0; for (char[][] dotMat : dotMatList) { finalWidth += dotMat[0].length; } return finalWidth; }
private void renderDotMatToImage(char[][] dotMat, BufferedImage image, int x, int y) { for (int j = 0; j < dotMat.length; j++) { char[] row = dotMat[j]; for (int k = 0; k < row.length; k++) { char bitValue = dotMat[j][k]; image.setRGB(x + k, y + j, bitValue == '1' ? Color.BLACK.getRGB() : Color.WHITE.getRGB()); } } }
|