Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
327 lines (251 loc) · 8.18 KB

File metadata and controls

327 lines (251 loc) · 8.18 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package JSSHTerminal;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* Terminal component: Use bitmap font to render
*
* @author JL PONS
*/
public class GraphicTerminal extends TerminalEvent {
private BufferedImage charSetA;
private BufferedImage charSetB;
private BufferedImage charSet0;
private BufferedImage screen;
private BufferedImage tmpImg;
private int[] tmpBuffer;
private int lastChx=-1;
private int lastChy=-1;
private int lastBg=-1;
private int lastFg=-1;
private int lastCharSet=-1;
private Graphics tmpImgG;
private AffineTransform identityTr;
public GraphicTerminal(MainPanel parent,int width,int height) {
setBorder(null);
setOpaque(false);
setDoubleBuffered(false);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
init(parent,width,height,8,16);
tmpImg = new BufferedImage(charWidth,charHeight,BufferedImage.TYPE_INT_RGB);
tmpImgG = tmpImg.getGraphics();
tmpBuffer = new int[charWidth*charHeight];
identityTr = new AffineTransform();
loadFont();
}
private void loadFont() {
try {
charSetA = ImageIO.read(getClass().getClassLoader().getResource("JSSHTerminal/fontPA.png"));
charSetB = ImageIO.read(getClass().getClassLoader().getResource("JSSHTerminal/fontPB.png"));
charSet0 = ImageIO.read(getClass().getClassLoader().getResource("JSSHTerminal/fontP0.png"));
} catch (IOException e) {
System.out.println("Cannot load font " + e.getMessage());
}
}
@Override
public void dispose() {
tmpImgG.dispose();
tmpImg = null;
screen = null;
terminal = null;
}
@Override
public void sizeComponent(int width,int height) {
//GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
//GraphicsDevice device = env.getDefaultScreenDevice();
//GraphicsConfiguration config = device.getDefaultConfiguration();
//screen = config.createCompatibleImage(termWidth*charWidth,termHeight*charHeight,Transparency.OPAQUE);
screen = new BufferedImage(termWidth*charWidth,termHeight*charHeight, BufferedImage.TYPE_INT_RGB);
}
@Override
public synchronized void drawComponent(Graphics g) {
if (terminal == null) {
g.setColor(Color.BLACK);
g.drawRect(0, 0, termWidth * charWidth, termHeight * charHeight);
return;
}
Graphics scrG = screen.getGraphics();
// Reset transformation to identity
((Graphics2D)g).setTransform(identityTr);
paintTo(scrG);
scrG.dispose();
g.drawImage(screen,0,0,null);
}
public void paintTo(Graphics g) {
int[] scr = terminal.getScreen(scrollPos);
int i = 0;
for (int y = 0; y < termHeight; y++) {
for (int x = 0; x < termWidth; x++) {
int c = scr[i] & 0xFFFF;
int sgr = scr[i] >> 16;
int bg = (sgr & 0x70) >> 4;
int fg = sgr & 0x7;
boolean bold = (sgr & 0x8) != 0;
boolean underline = (sgr & 0x80) != 0;
boolean reverse = (sgr & 0x400) != 0;
int charSet = (sgr & 0x300) >> 8;
boolean isDefault = (fg == 7 && bg == 0) && !reverse;
Color fgColor;
Color bgColor;
if (reverse) {
bgColor = defaultColors[fg];
if(bold)
fgColor = defaultBrightColors[bg];
else
fgColor = defaultColors[bg];
} else {
bgColor = defaultColors[bg];
if(bold)
fgColor = defaultBrightColors[fg];
else
fgColor = defaultColors[fg];
}
if (i - scrollPos * termWidth >= terminal.getStartSelection() &&
i - scrollPos * termWidth <= terminal.getEndSelection()) {
// Selected text
bgColor = Color.WHITE;
fgColor = Color.BLACK;
isDefault = false;
}
if (terminal.isCursor(x, y - scrollPos)) {
// Cursor
bgColor = cursorBackground;
fgColor = Color.BLACK;
isDefault = false;
}
if (c < 256) {
getChar(c, fgColor, bgColor, charSet, bold, isDefault);
} else {
switch (c) {
case 0x2500: // hline
c = 'q';
charSet = 2;
break;
case 0x2502: // vline
c = 'x';
charSet = 2;
break;
case 0x250C: // LU corner
c = 'l';
charSet = 2;
break;
case 0x2510: // RU corner
c = 'k';
charSet = 2;
break;
case 0x2514: // LD corner
c = 'm';
charSet = 2;
break;
case 0x2518: // LD corner
c = 'j';
charSet = 2;
break;
case 0x2524: // T left
c = 'u';
charSet = 2;
break;
case 0x251C: // T right
c = 't';
charSet = 2;
break;
case 0x2592: // Dotted fill
c = 'a';
charSet = 2;
break;
case 0x2010: // Dash
c = '-';
break;
default:
// Not handled unicode
System.out.println("Warning, unhandled unicode " + String.format("%04X", c));
c = 0;
}
}
getChar(c, fgColor, bgColor, charSet, bold, isDefault);
g.drawImage(tmpImg, x * charWidth, y * charHeight, charWidth * (x + 1), charHeight * (y + 1),
0, 0, charWidth, charHeight, null);
if (underline) {
g.setColor(Color.WHITE);
g.drawLine(x * charWidth, (y + 1) * charHeight - 1, (x + 1) * charWidth, (y + 1) * charHeight - 1);
}
i++;
}
}
}
BufferedImage getChar(int c,Color fgColor,Color bgColor,int charSet,boolean isBold,boolean isDefault) {
// Character coordinates
int chx = 0;
int chy = 0;
if(c>=32 && c<127) {
chx = c % 16;
chy = c / 16 - 2;
if( isBold ) chy += 6;
}
// Detect context change
int bg = bgColor.getRGB();
int fg = fgColor.getRGB();
if(lastBg==bg && lastFg==fg && lastChx==chx && lastChy==chy && lastCharSet==charSet) {
// Last image is the same
return tmpImg;
}
lastBg = bg;
lastFg = fg;
lastChx = chx;
lastChy = chy;
lastCharSet = charSet;
BufferedImage src;
switch (charSet) {
case 1:
src = charSetA;
break;
case 2:
src = charSet0;
break;
default:
src = charSetB;
}
if( isDefault ) {
// Default white on black
tmpImgG.drawImage(src, 0, 0, charWidth, charHeight,
chx * charWidth, chy * charHeight, charWidth * (chx + 1), charHeight * (chy + 1), null);
} else {
if (c <= 32) {
// Space (only paint background)
tmpImgG.setColor(bgColor);
tmpImgG.fillRect(0, 0, charWidth, charHeight);
} else {
// Colorize character
double bgR = (double) bgColor.getRed();
double bgG = (double) bgColor.getGreen();
double bgB = (double) bgColor.getBlue();
double fgR = (double) fgColor.getRed();
double fgG = (double) fgColor.getGreen();
double fgB = (double) fgColor.getBlue();
double i255 = 1/255.0;
src.getRGB(chx * charWidth, chy * charHeight, charWidth, charHeight, tmpBuffer, 0, charWidth);
for (int i = 0; i < tmpBuffer.length; i++) {
if ((tmpBuffer[i] & 0xFFFFFF) == 0) {
tmpBuffer[i] = bg;
} else if (tmpBuffer[i] == 0xFFFFFF) {
tmpBuffer[i] = fg;
} else {
double factor = (double) (tmpBuffer[i] & 0xFF) * i255;
double ofactor = 1.0 - factor;
int nRed = (int) (factor * fgR + ofactor * bgR) << 16;
int nGreen = (int) (factor * fgG + ofactor * bgG) << 8;
int nBlue = (int) (factor * fgB + ofactor * bgB);
tmpBuffer[i] = nRed | nGreen | nBlue;
}
}
tmpImg.setRGB(0, 0, charWidth, charHeight, tmpBuffer, 0, charWidth);
}
}
return tmpImg;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.