2018-01-07 01:08:35 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
2019-09-09 10:46:52 +00:00
|
|
|
# CanvasImportStore.py
|
2019-09-04 14:23:59 +00:00
|
|
|
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
2018-11-26 09:35:48 +00:00
|
|
|
# This project is licensed under the terms of the MIT licence.
|
2018-01-07 01:08:35 +00:00
|
|
|
#
|
|
|
|
|
2019-09-06 08:32:54 +00:00
|
|
|
from CanvasColours import AnsiBgToMiRCARTColours, AnsiFgToMiRCARTColours, AnsiFgBoldToMiRCARTColours
|
2019-09-07 08:39:26 +00:00
|
|
|
import io, os, re, struct, sys
|
Implements importing from {ANSI,SAUCE} and exporting to ANSI files.
assets/tools/{MiRCARTTo{Ansi,PngFile},SAUCETo{Ansi,MiRCART}}.py: reimplemented using Canvas{Ex,Im}portStore.
libcanvas/Canvas.py: minor cleanup.
libcanvas/CanvasExportStore.py:export{Ansi,Png}File(): merged from assets/tools/MiRCARTTo{Ansi,PngFile}.py.
libcanvas/CanvasImportStore.py:import{Ansi,Sauce}File(): merged from assets/tools/SAUCETo{Ansi,MiRCART}.py.
libcanvas/Colours.py:{AnsiBgToMiRCARTColours,AnsiFgToMiRCARTColours,AnsiFgBoldToMiRCARTColours}{}: merged from assets/tools/SAUCEToMiRCART.py.
libcanvas/Colours.py:{ColourMap{Bold,Normal}}[]: merged from assets/tools/MiRCARTToPngFile.py.
libcanvas/Colours.py: minor cleanup.
libgui/GuiCanvasInterface.py:canvas{ExportAsAnsi,Import{Ansi,Sauce}}(): implemented.
libgui/GuiFrame.py: adds CID_{EXPORT_AS_{ANSI,SAUCE},IMPORT_{ANSI,SAUCE}}.
assets/text/TODO: updated.
2019-09-05 14:55:07 +00:00
|
|
|
|
2019-09-03 16:58:50 +00:00
|
|
|
class CanvasImportStore():
|
Implements importing from {ANSI,SAUCE} and exporting to ANSI files.
assets/tools/{MiRCARTTo{Ansi,PngFile},SAUCETo{Ansi,MiRCART}}.py: reimplemented using Canvas{Ex,Im}portStore.
libcanvas/Canvas.py: minor cleanup.
libcanvas/CanvasExportStore.py:export{Ansi,Png}File(): merged from assets/tools/MiRCARTTo{Ansi,PngFile}.py.
libcanvas/CanvasImportStore.py:import{Ansi,Sauce}File(): merged from assets/tools/SAUCETo{Ansi,MiRCART}.py.
libcanvas/Colours.py:{AnsiBgToMiRCARTColours,AnsiFgToMiRCARTColours,AnsiFgBoldToMiRCARTColours}{}: merged from assets/tools/SAUCEToMiRCART.py.
libcanvas/Colours.py:{ColourMap{Bold,Normal}}[]: merged from assets/tools/MiRCARTToPngFile.py.
libcanvas/Colours.py: minor cleanup.
libgui/GuiCanvasInterface.py:canvas{ExportAsAnsi,Import{Ansi,Sauce}}(): implemented.
libgui/GuiFrame.py: adds CID_{EXPORT_AS_{ANSI,SAUCE},IMPORT_{ANSI,SAUCE}}.
assets/text/TODO: updated.
2019-09-05 14:55:07 +00:00
|
|
|
# {{{ _CellState(): Cell state
|
2018-01-07 01:08:35 +00:00
|
|
|
class _CellState():
|
|
|
|
CS_NONE = 0x00
|
|
|
|
CS_BOLD = 0x01
|
|
|
|
CS_ITALIC = 0x02
|
|
|
|
CS_UNDERLINE = 0x04
|
Implements importing from {ANSI,SAUCE} and exporting to ANSI files.
assets/tools/{MiRCARTTo{Ansi,PngFile},SAUCETo{Ansi,MiRCART}}.py: reimplemented using Canvas{Ex,Im}portStore.
libcanvas/Canvas.py: minor cleanup.
libcanvas/CanvasExportStore.py:export{Ansi,Png}File(): merged from assets/tools/MiRCARTTo{Ansi,PngFile}.py.
libcanvas/CanvasImportStore.py:import{Ansi,Sauce}File(): merged from assets/tools/SAUCETo{Ansi,MiRCART}.py.
libcanvas/Colours.py:{AnsiBgToMiRCARTColours,AnsiFgToMiRCARTColours,AnsiFgBoldToMiRCARTColours}{}: merged from assets/tools/SAUCEToMiRCART.py.
libcanvas/Colours.py:{ColourMap{Bold,Normal}}[]: merged from assets/tools/MiRCARTToPngFile.py.
libcanvas/Colours.py: minor cleanup.
libgui/GuiCanvasInterface.py:canvas{ExportAsAnsi,Import{Ansi,Sauce}}(): implemented.
libgui/GuiFrame.py: adds CID_{EXPORT_AS_{ANSI,SAUCE},IMPORT_{ANSI,SAUCE}}.
assets/text/TODO: updated.
2019-09-05 14:55:07 +00:00
|
|
|
# }}}
|
2018-01-07 01:08:35 +00:00
|
|
|
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ _flipCellStateBit(self, bit, cellState)
|
2019-09-06 08:29:45 +00:00
|
|
|
def _flipCellStateBit(self, bit, cellState):
|
2019-09-04 13:52:54 +00:00
|
|
|
return cellState & ~bit if cellState & bit else cellState | bit
|
2018-01-07 22:49:57 +00:00
|
|
|
# }}}
|
2018-01-07 03:19:38 +00:00
|
|
|
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ importAnsiBuffer(self, inBuffer, encoding="cp437", width=None)
|
2019-09-07 08:39:26 +00:00
|
|
|
def importAnsiBuffer(self, inBuffer, encoding="cp437", width=None):
|
2019-09-06 08:29:45 +00:00
|
|
|
curBg, curBgAnsi, curBoldAnsi, curFg, curFgAnsi = 1, 30, False, 15, 37
|
|
|
|
done, outMap, outMaxCols = False, [[]], 0
|
2019-09-07 08:39:26 +00:00
|
|
|
inBufferData = inBuffer.decode(encoding)
|
|
|
|
inBufferChar, inBufferCharMax = 0, len(inBufferData)
|
Implements importing from {ANSI,SAUCE} and exporting to ANSI files.
assets/tools/{MiRCARTTo{Ansi,PngFile},SAUCETo{Ansi,MiRCART}}.py: reimplemented using Canvas{Ex,Im}portStore.
libcanvas/Canvas.py: minor cleanup.
libcanvas/CanvasExportStore.py:export{Ansi,Png}File(): merged from assets/tools/MiRCARTTo{Ansi,PngFile}.py.
libcanvas/CanvasImportStore.py:import{Ansi,Sauce}File(): merged from assets/tools/SAUCETo{Ansi,MiRCART}.py.
libcanvas/Colours.py:{AnsiBgToMiRCARTColours,AnsiFgToMiRCARTColours,AnsiFgBoldToMiRCARTColours}{}: merged from assets/tools/SAUCEToMiRCART.py.
libcanvas/Colours.py:{ColourMap{Bold,Normal}}[]: merged from assets/tools/MiRCARTToPngFile.py.
libcanvas/Colours.py: minor cleanup.
libgui/GuiCanvasInterface.py:canvas{ExportAsAnsi,Import{Ansi,Sauce}}(): implemented.
libgui/GuiFrame.py: adds CID_{EXPORT_AS_{ANSI,SAUCE},IMPORT_{ANSI,SAUCE}}.
assets/text/TODO: updated.
2019-09-05 14:55:07 +00:00
|
|
|
while True:
|
2019-09-07 08:39:26 +00:00
|
|
|
if inBufferChar >= inBufferCharMax:
|
Implements importing from {ANSI,SAUCE} and exporting to ANSI files.
assets/tools/{MiRCARTTo{Ansi,PngFile},SAUCETo{Ansi,MiRCART}}.py: reimplemented using Canvas{Ex,Im}portStore.
libcanvas/Canvas.py: minor cleanup.
libcanvas/CanvasExportStore.py:export{Ansi,Png}File(): merged from assets/tools/MiRCARTTo{Ansi,PngFile}.py.
libcanvas/CanvasImportStore.py:import{Ansi,Sauce}File(): merged from assets/tools/SAUCETo{Ansi,MiRCART}.py.
libcanvas/Colours.py:{AnsiBgToMiRCARTColours,AnsiFgToMiRCARTColours,AnsiFgBoldToMiRCARTColours}{}: merged from assets/tools/SAUCEToMiRCART.py.
libcanvas/Colours.py:{ColourMap{Bold,Normal}}[]: merged from assets/tools/MiRCARTToPngFile.py.
libcanvas/Colours.py: minor cleanup.
libgui/GuiCanvasInterface.py:canvas{ExportAsAnsi,Import{Ansi,Sauce}}(): implemented.
libgui/GuiFrame.py: adds CID_{EXPORT_AS_{ANSI,SAUCE},IMPORT_{ANSI,SAUCE}}.
assets/text/TODO: updated.
2019-09-05 14:55:07 +00:00
|
|
|
break
|
|
|
|
else:
|
2019-09-07 08:39:26 +00:00
|
|
|
m = re.match("\x1b\[((?:\d{1,3};?)+m|\d+[ABCDEFG])", inBufferData[inBufferChar:])
|
Implements importing from {ANSI,SAUCE} and exporting to ANSI files.
assets/tools/{MiRCARTTo{Ansi,PngFile},SAUCETo{Ansi,MiRCART}}.py: reimplemented using Canvas{Ex,Im}portStore.
libcanvas/Canvas.py: minor cleanup.
libcanvas/CanvasExportStore.py:export{Ansi,Png}File(): merged from assets/tools/MiRCARTTo{Ansi,PngFile}.py.
libcanvas/CanvasImportStore.py:import{Ansi,Sauce}File(): merged from assets/tools/SAUCETo{Ansi,MiRCART}.py.
libcanvas/Colours.py:{AnsiBgToMiRCARTColours,AnsiFgToMiRCARTColours,AnsiFgBoldToMiRCARTColours}{}: merged from assets/tools/SAUCEToMiRCART.py.
libcanvas/Colours.py:{ColourMap{Bold,Normal}}[]: merged from assets/tools/MiRCARTToPngFile.py.
libcanvas/Colours.py: minor cleanup.
libgui/GuiCanvasInterface.py:canvas{ExportAsAnsi,Import{Ansi,Sauce}}(): implemented.
libgui/GuiFrame.py: adds CID_{EXPORT_AS_{ANSI,SAUCE},IMPORT_{ANSI,SAUCE}}.
assets/text/TODO: updated.
2019-09-05 14:55:07 +00:00
|
|
|
if m:
|
2019-09-06 08:29:45 +00:00
|
|
|
if m[1][-1] == "C":
|
|
|
|
outMap[-1] += [[curFg, curBg, self._CellState.CS_NONE, " "]] * int(m[1][:-1])
|
|
|
|
elif m[1][-1] == "m":
|
|
|
|
newBg, newFg = -1, -1
|
|
|
|
for ansiCode in [int(c) for c in m[1][:-1].split(";")]:
|
|
|
|
if ansiCode == 0:
|
|
|
|
curBgAnsi, curBoldAnsi, curFgAnsi, newBg, newFg = 30, False, 37, 1, 15
|
|
|
|
elif ansiCode == 1:
|
|
|
|
curBoldAnsi, newFg = True, AnsiFgBoldToMiRCARTColours[curFgAnsi]
|
|
|
|
elif ansiCode == 2:
|
|
|
|
curBoldAnsi, newFg = False, AnsiFgToMiRCARTColours[curFgAnsi]
|
|
|
|
elif ansiCode == 7:
|
|
|
|
curBgAnsi, curFgAnsi, newBg, newFg = curFgAnsi, curBgAnsi, curFg, curBg
|
2019-09-07 08:39:26 +00:00
|
|
|
elif (not curBoldAnsi) and (ansiCode in AnsiBgToMiRCARTColours):
|
2019-09-06 08:29:45 +00:00
|
|
|
curBgAnsi, newBg = ansiCode, AnsiBgToMiRCARTColours[ansiCode]
|
2019-09-07 08:39:26 +00:00
|
|
|
elif curBoldAnsi and (ansiCode in AnsiFgBoldToMiRCARTColours):
|
2019-09-06 08:29:45 +00:00
|
|
|
curFgAnsi, newFg = ansiCode, AnsiFgBoldToMiRCARTColours[ansiCode]
|
|
|
|
elif ansiCode in AnsiFgToMiRCARTColours:
|
|
|
|
newFg = AnsiFgBoldToMiRCARTColours[ansiCode] if curBoldAnsi else AnsiFgToMiRCARTColours[ansiCode]
|
|
|
|
curFgAnsi = ansiCode
|
|
|
|
curBg = newBg if newBg != -1 else curBg; curFg = newFg if newFg != -1 else curFg;
|
2019-09-07 08:39:26 +00:00
|
|
|
inBufferChar += len(m[0])
|
|
|
|
elif inBufferData[inBufferChar:inBufferChar + 2] == "\r\n":
|
|
|
|
done = True; inBufferChar += 2;
|
|
|
|
elif inBufferData[inBufferChar] in set("\r\n"):
|
|
|
|
done = True; inBufferChar += 1;
|
Implements importing from {ANSI,SAUCE} and exporting to ANSI files.
assets/tools/{MiRCARTTo{Ansi,PngFile},SAUCETo{Ansi,MiRCART}}.py: reimplemented using Canvas{Ex,Im}portStore.
libcanvas/Canvas.py: minor cleanup.
libcanvas/CanvasExportStore.py:export{Ansi,Png}File(): merged from assets/tools/MiRCARTTo{Ansi,PngFile}.py.
libcanvas/CanvasImportStore.py:import{Ansi,Sauce}File(): merged from assets/tools/SAUCETo{Ansi,MiRCART}.py.
libcanvas/Colours.py:{AnsiBgToMiRCARTColours,AnsiFgToMiRCARTColours,AnsiFgBoldToMiRCARTColours}{}: merged from assets/tools/SAUCEToMiRCART.py.
libcanvas/Colours.py:{ColourMap{Bold,Normal}}[]: merged from assets/tools/MiRCARTToPngFile.py.
libcanvas/Colours.py: minor cleanup.
libgui/GuiCanvasInterface.py:canvas{ExportAsAnsi,Import{Ansi,Sauce}}(): implemented.
libgui/GuiFrame.py: adds CID_{EXPORT_AS_{ANSI,SAUCE},IMPORT_{ANSI,SAUCE}}.
assets/text/TODO: updated.
2019-09-05 14:55:07 +00:00
|
|
|
else:
|
2019-09-07 08:39:26 +00:00
|
|
|
outMap[-1].append([curFg, curBg, self._CellState.CS_NONE, inBufferData[inBufferChar]])
|
|
|
|
inBufferChar += 1
|
2019-09-06 08:29:45 +00:00
|
|
|
if done or (width == len(outMap[-1])):
|
|
|
|
done, outMaxCols, = False, max(outMaxCols, len(outMap[-1])); outMap.append([]);
|
2019-09-07 08:39:26 +00:00
|
|
|
if (len(outMap) > 1) \
|
|
|
|
or ((len(outMap) == 1) and len(outMap[0])):
|
2019-09-06 08:29:45 +00:00
|
|
|
for numRow in range(len(outMap)):
|
|
|
|
for numCol in range(len(outMap[numRow]), outMaxCols):
|
|
|
|
outMap[numRow].append([curFg, curBg, self._CellState.CS_NONE, " "])
|
|
|
|
self.inSize, self.outMap = [outMaxCols, len(outMap)], outMap
|
|
|
|
return (True, None)
|
|
|
|
else:
|
|
|
|
return (False, "empty output map")
|
2018-01-07 03:19:38 +00:00
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ importAnsiFile(self, inPathName, encoding="cp437")
|
2019-09-06 08:29:45 +00:00
|
|
|
def importAnsiFile(self, inPathName, encoding="cp437"):
|
2019-09-07 08:39:26 +00:00
|
|
|
return self.importAnsiBuffer(open(inPathName, "rb").read(), encoding)
|
2019-09-04 13:52:54 +00:00
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ importSauceFile(self, inPathName, encoding="cp437")
|
2019-09-07 08:39:26 +00:00
|
|
|
def importSauceFile(self, inPathName, encoding="cp437"):
|
Implements importing from {ANSI,SAUCE} and exporting to ANSI files.
assets/tools/{MiRCARTTo{Ansi,PngFile},SAUCETo{Ansi,MiRCART}}.py: reimplemented using Canvas{Ex,Im}portStore.
libcanvas/Canvas.py: minor cleanup.
libcanvas/CanvasExportStore.py:export{Ansi,Png}File(): merged from assets/tools/MiRCARTTo{Ansi,PngFile}.py.
libcanvas/CanvasImportStore.py:import{Ansi,Sauce}File(): merged from assets/tools/SAUCETo{Ansi,MiRCART}.py.
libcanvas/Colours.py:{AnsiBgToMiRCARTColours,AnsiFgToMiRCARTColours,AnsiFgBoldToMiRCARTColours}{}: merged from assets/tools/SAUCEToMiRCART.py.
libcanvas/Colours.py:{ColourMap{Bold,Normal}}[]: merged from assets/tools/MiRCARTToPngFile.py.
libcanvas/Colours.py: minor cleanup.
libgui/GuiCanvasInterface.py:canvas{ExportAsAnsi,Import{Ansi,Sauce}}(): implemented.
libgui/GuiFrame.py: adds CID_{EXPORT_AS_{ANSI,SAUCE},IMPORT_{ANSI,SAUCE}}.
assets/text/TODO: updated.
2019-09-05 14:55:07 +00:00
|
|
|
with open(inPathName, "rb") as inFile:
|
2019-09-07 08:39:26 +00:00
|
|
|
inFileStat = os.stat(inPathName)
|
|
|
|
inFile.seek(inFileStat.st_size - 128, os.SEEK_SET); inFile.seek(94, os.SEEK_CUR);
|
2019-09-06 08:29:45 +00:00
|
|
|
if inFile.read(2) == b'\x01\x01':
|
|
|
|
width = struct.unpack("H", inFile.read(2))[0]
|
|
|
|
inFile.seek(0, 0); inFileData = inFile.read(inFileStat.st_size - 128);
|
2019-09-07 08:39:26 +00:00
|
|
|
return self.importAnsiBuffer(inFileData, encoding, width)
|
2019-09-06 08:29:45 +00:00
|
|
|
else:
|
|
|
|
return (False, "only character based ANSi SAUCE files are supported")
|
2019-09-04 10:50:21 +00:00
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ importTextBuffer(self, inFile)
|
2019-09-06 08:29:45 +00:00
|
|
|
def importTextBuffer(self, inFile):
|
|
|
|
inLine, outMap, outMaxCols = inFile.readline(), [], 0
|
2018-01-07 01:08:35 +00:00
|
|
|
while inLine:
|
2019-09-06 08:29:45 +00:00
|
|
|
inCellState, inCurCol, inCurColours, inMaxCol = self._CellState.CS_NONE, 0, (15, 1), len(inLine); outMap.append([]);
|
2018-01-07 01:08:35 +00:00
|
|
|
while inCurCol < inMaxCol:
|
|
|
|
inChar = inLine[inCurCol]
|
2019-09-06 08:29:45 +00:00
|
|
|
if inChar in set("\r\n"):
|
2018-01-07 01:08:35 +00:00
|
|
|
inCurCol += 1
|
2019-09-06 08:29:45 +00:00
|
|
|
elif inChar == "\u0002":
|
|
|
|
inCellState = self._flipCellStateBit(self._CellState.CS_BOLD, inCellState); inCurCol += 1;
|
|
|
|
elif inChar == "\u0003":
|
|
|
|
m = re.match("\u0003((1[0-5]|0?[0-9])?(?:,(1[0-5]|0?[0-9]))?)", inLine[inCurCol:])
|
|
|
|
if m:
|
|
|
|
if (m[2] != None) and (m[3] != None):
|
|
|
|
inCurColours = (int(m[2]), int(m[3]))
|
2019-09-09 17:07:27 +00:00
|
|
|
elif (m[2] != None) and (m[3] == None):
|
|
|
|
inCurColours = (int(m[2]), int(inCurColours[1]))
|
2018-01-20 08:17:52 +00:00
|
|
|
else:
|
2019-09-06 08:29:45 +00:00
|
|
|
inCurColours = (15, 1)
|
|
|
|
inCurCol += len(m[0])
|
2018-01-07 01:08:35 +00:00
|
|
|
else:
|
2019-09-06 08:29:45 +00:00
|
|
|
inCurColours = (15, 1); inCurCol += 1;
|
|
|
|
elif inChar == "\u0006":
|
|
|
|
inCellState = self._flipCellStateBit(self._CellState.CS_ITALIC, inCellState); inCurCol += 1;
|
|
|
|
elif inChar == "\u000f":
|
|
|
|
inCellState |= self._CellState.CS_NONE; inCurColours = (15, 1); inCurCol += 1;
|
|
|
|
elif inChar == "\u0016":
|
|
|
|
inCurColours = (inCurColours[1], inCurColours[0]); inCurCol += 1;
|
|
|
|
elif inChar == "\u001f":
|
|
|
|
inCellState = self._flipCellStateBit(self._CellState.CS_UNDERLINE, inCellState); inCurCol += 1;
|
|
|
|
else:
|
|
|
|
outMap[-1].append([*inCurColours, inCellState, inChar]); inCurCol += 1;
|
|
|
|
inLine, outMaxCols = inFile.readline(), max(outMaxCols, len(outMap[-1]))
|
2019-09-07 08:39:26 +00:00
|
|
|
if (len(outMap) > 1) \
|
|
|
|
or ((len(outMap) == 1) and len(outMap[0])):
|
2019-09-06 08:29:45 +00:00
|
|
|
self.inSize, self.outMap = [outMaxCols, len(outMap)], outMap
|
|
|
|
return (True, None)
|
|
|
|
else:
|
|
|
|
return (False, "empty output map")
|
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ importTextFile(self, pathName)
|
2019-09-06 08:29:45 +00:00
|
|
|
def importTextFile(self, pathName):
|
|
|
|
with open(pathName, "r", encoding="utf-8-sig") as inFile:
|
|
|
|
return self.importTextBuffer(inFile)
|
2018-01-07 01:08:35 +00:00
|
|
|
# }}}
|
|
|
|
|
|
|
|
#
|
2019-09-06 08:29:45 +00:00
|
|
|
# __init__(self, inFile=None): initialisation method
|
|
|
|
def __init__(self, inFile=None):
|
|
|
|
self.inSize, self.outMap = None, None
|
2018-01-07 03:19:38 +00:00
|
|
|
if inFile != None:
|
|
|
|
self.importTextFile(inFile)
|
2018-01-07 01:08:35 +00:00
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|