Skip to content

Commit c543379

Browse files
committed
Add Hanse-Web
1 parent a44f2f0 commit c543379

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

AnsiGraphics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def cga_colour(self, pal_idx):
100100
"""
101101
Returns the VGA palette colour with the given index as an RGP triplet.
102102
"""
103-
return AnsiGraphics.CGA_PAL[idx]
103+
return AnsiGraphics.CGA_PAL[pal_idx]
104104

105105
def char_bitmap(self, char_idx):
106106
"""

MainWindow.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ def openFile(self):
740740

741741
if len(loadFileName) != 0:
742742
self.currentFileName = loadFileName
743-
self.ansiImage.load_ans(self.currentFileName, wideMode)
743+
self.ansiImage.load_ans(self.currentFileName, wideMode)
744744
self.previewBuffer = None
745745

746746
self.redisplayAnsi()
@@ -932,7 +932,7 @@ def loadReferenceImage(self):
932932
"""
933933
Sets up a reference image
934934
"""
935-
refFileName = QtWidgets.QFileDialog.getOpenFileName(self, caption = "Open reference image", filter="Image Files (*.png)")[0]
935+
refFileName = QtWidgets.QFileDialog.getOpenFileName(self, caption = "Open reference image", filter="Image Files (*.png *.jpg)")[0]
936936
try:
937937
self.refImage.load(refFileName)
938938
self.redisplayAnsi()

hanse_web.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from flask import Flask, request, send_from_directory, render_template, send_file
2+
app = Flask(__name__, static_url_path='')
3+
4+
import numpy as np
5+
from io import BytesIO
6+
import glob
7+
8+
from AnsiGraphics import AnsiGraphics
9+
from AnsiImage import AnsiImage
10+
from AnsiPalette import AnsiPalette
11+
12+
ansi_graphics = AnsiGraphics('config/cp866_8x16.fnt', 8, 16)
13+
14+
pal_styles = ""
15+
for i in range(16):
16+
col = np.floor(np.array(ansi_graphics.cga_colour(i)) * 255.0)
17+
pal_entry = ".fg" + str(i) + "{\n"
18+
pal_entry += " color: rgb(" + str(col[0]) + ", " + str(col[1]) + ", " + str(col[2]) + ");\n"
19+
pal_entry += "}\n\n"
20+
pal_entry += ".bg" + str(i) + "{\n"
21+
pal_entry += " background: rgb(" + str(col[0]) + ", " + str(col[1]) + ", " + str(col[2]) + ");\n"
22+
pal_entry += "}\n\n"
23+
pal_styles += pal_entry
24+
25+
26+
@app.route('/')
27+
def file_list():
28+
file_list = list(glob.glob('*.ans'))
29+
list_html = '<h1>Files</h1><div style="text-align: left; font-size: 28px; width: 600px;">'
30+
for ansi_file in file_list:
31+
list_html += '--> <a href="/view/' + ansi_file + '">' + ansi_file + '</a><br/>'
32+
list_html += "</div>"
33+
return(render_template("default.html", title="files", content=list_html))
34+
35+
@app.route('/view/<path:path>')
36+
def render_ansi(path):
37+
try:
38+
ansi_image = AnsiImage(ansi_graphics)
39+
ansi_image.clear_image(80, 24)
40+
ansi_image.load_ans(path, False)
41+
width, height = ansi_image.get_size()
42+
except:
43+
return(render_template("default.html", title="Loading error, sorry."))
44+
45+
html_ansi = ""
46+
for y in range(height):
47+
for x in range(width):
48+
char = ansi_image.get_cell(x, y)
49+
html_ansi += '<span class="fg' + str(char[1]) + ' bg' + str(char[2]) + '">'
50+
html_ansi += chr(char[0])
51+
html_ansi += '</span>'
52+
html_ansi += "\n"
53+
return(render_template("default.html", title=path, styles=pal_styles, content=html_ansi, show_dl=True))
54+
55+
@app.route('/image/<path:path>')
56+
def render_ansi_png(path):
57+
transparent = False
58+
if request.args.get('transparent', False) != False:
59+
transparent = True
60+
try:
61+
ansi_image = AnsiImage(ansi_graphics)
62+
ansi_image.clear_image(80, 24)
63+
ansi_image.load_ans(path, False)
64+
bitmap = ansi_image.to_bitmap(transparent = transparent)
65+
except:
66+
return(render_template("default.html", title="Loading error, sorry."))
67+
68+
img_io = BytesIO()
69+
bitmap.save(img_io, 'PNG')
70+
img_io.seek(0)
71+
72+
return send_file(img_io, mimetype='image/png')
73+
74+
@app.route('/webfont/<path:path>')
75+
def webfont(path):
76+
return send_from_directory('webfont', path)
77+
78+
if __name__ == "__main__":
79+
port = 5000
80+
app.run(host='0.0.0.0', port=port)
81+

xmas2019.ans

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)