2019-09-01 14:34:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
2019-09-09 10:46:52 +00:00
|
|
|
# CanvasExportStore.py
|
2019-09-04 14:23:59 +00:00
|
|
|
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
2019-09-01 14:34:00 +00:00
|
|
|
#
|
|
|
|
|
2019-09-06 08:32:54 +00:00
|
|
|
from CanvasColours import ColourMapBold, ColourMapNormal, MiRCARTToAnsiColours
|
2019-09-01 14:34:00 +00:00
|
|
|
import io, os, tempfile
|
|
|
|
|
|
|
|
try:
|
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
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
havePIL = True
|
2019-09-01 14:34:00 +00:00
|
|
|
except ImportError:
|
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
|
|
|
havePIL = False
|
2019-09-01 14:34:00 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import base64, json, requests, urllib.request
|
|
|
|
haveUrllib = True
|
|
|
|
except ImportError:
|
|
|
|
haveUrllib = False
|
|
|
|
|
2019-09-03 16:58:50 +00:00
|
|
|
class CanvasExportStore():
|
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
|
|
|
|
class _CellState():
|
|
|
|
CS_NONE = 0x00
|
|
|
|
CS_BOLD = 0x01
|
|
|
|
CS_ITALIC = 0x02
|
|
|
|
CS_UNDERLINE = 0x04
|
|
|
|
# }}}
|
2019-09-04 13:52:54 +00:00
|
|
|
ImgurUploadUrl = "https://api.imgur.com/3/upload.json"
|
|
|
|
PastebinPostUrl = "https://pastebin.com/api/api_post.php"
|
2019-09-01 14:34:00 +00:00
|
|
|
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ _drawUnderline(self, curPos, fillColour, fontSize, imgDraw)
|
2019-09-06 08:29:45 +00:00
|
|
|
def _drawUnderLine(self, curPos, fillColour, fontSize, imgDraw):
|
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
|
|
|
imgDraw.line( \
|
|
|
|
xy=(curPos[0], curPos[1] + (fontSize[1] - 2), \
|
|
|
|
curPos[0] + fontSize[0], curPos[1] + (fontSize[1] - 2)), \
|
|
|
|
fill=fillColour)
|
|
|
|
# }}}
|
2019-09-01 14:34:00 +00:00
|
|
|
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ exportAnsiFile(self, canvasMap, canvasSize, outFile)
|
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
|
|
|
def exportAnsiFile(self, canvasMap, canvasSize, outFile):
|
|
|
|
outBuffer = ""
|
|
|
|
for inCurRow in range(len(canvasMap)):
|
2019-09-06 08:29:45 +00:00
|
|
|
lastAttribs, lastColours = self._CellState.CS_NONE, None
|
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
|
|
|
for inCurCol in range(len(canvasMap[inCurRow])):
|
|
|
|
inCurCell = canvasMap[inCurRow][inCurCol]
|
|
|
|
if lastAttribs != inCurCell[2]:
|
|
|
|
if inCurCell[2] & self._CellState.CS_BOLD:
|
|
|
|
outBuffer += "\u001b[1m"
|
|
|
|
if inCurCell[2] & self._CellState.CS_UNDERLINE:
|
|
|
|
outBuffer += "\u001b[4m"
|
|
|
|
lastAttribs = inCurCell[2]
|
|
|
|
if lastColours == None or lastColours != inCurCell[:2]:
|
2019-09-08 14:57:45 +00:00
|
|
|
if (inCurCell[0] == -1) \
|
|
|
|
and (inCurCell[1] == -1):
|
|
|
|
outBuffer += "\u001b[39;49m{}".format(" ")
|
|
|
|
elif inCurCell[1] == -1:
|
|
|
|
ansiFg = MiRCARTToAnsiColours[inCurCell[0]]
|
|
|
|
outBuffer += "\u001b[49;{:02d}m{}".format(ansiFg, inCurCell[3])
|
|
|
|
else:
|
|
|
|
ansiBg = MiRCARTToAnsiColours[inCurCell[1]] + 10
|
|
|
|
ansiFg = MiRCARTToAnsiColours[inCurCell[0] if inCurCell[0] != -1 else inCurCell[1]]
|
|
|
|
outBuffer += "\u001b[{:02d};{:02d}m{}".format(ansiBg, ansiFg, inCurCell[3])
|
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
|
|
|
lastColours = inCurCell[:2]
|
|
|
|
else:
|
|
|
|
outBuffer += inCurCell[3]
|
|
|
|
outBuffer += "\u001b[0m\n"
|
2019-09-06 08:29:45 +00:00
|
|
|
if len(outBuffer):
|
|
|
|
outFile.write(outBuffer)
|
|
|
|
return (True, None)
|
|
|
|
else:
|
|
|
|
return (False, "empty buffer generated")
|
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-08 16:08:04 +00:00
|
|
|
# {{{ exportBitmapToImgur(self, apiKey, canvasBitmap, imgName, imgTitle, imgType)
|
2019-09-01 14:34:00 +00:00
|
|
|
def exportBitmapToImgur(self, apiKey, canvasBitmap, imgName, imgTitle, imgType):
|
|
|
|
tmpPathName = tempfile.mkstemp()
|
|
|
|
os.close(tmpPathName[0])
|
|
|
|
canvasBitmap.ConvertToImage().SaveFile(tmpPathName[1], imgType)
|
2019-09-06 08:29:45 +00:00
|
|
|
with open(tmpPathName[1], "rb") as requestImage:
|
|
|
|
requestImageData = requestImage.read()
|
|
|
|
requestData = { \
|
|
|
|
"image": base64.b64encode(requestImageData), \
|
|
|
|
"key": apiKey, \
|
|
|
|
"name": imgName, \
|
|
|
|
"title": imgTitle, \
|
|
|
|
"type": "base64"}
|
|
|
|
requestHeaders = {"Authorization": "Client-ID " + apiKey}
|
|
|
|
responseHttp = requests.post(self.ImgurUploadUrl, data=requestData, headers=requestHeaders)
|
|
|
|
responseDict = json.loads(responseHttp.text)
|
|
|
|
if responseHttp.status_code == 200:
|
|
|
|
imgurResult = (True, responseHttp.status_code, responseDict.get("data").get("link"))
|
|
|
|
else:
|
|
|
|
imgurResult = (False, responseHttp.status_code, responseDict.get("data"))
|
2019-09-01 14:34:00 +00:00
|
|
|
os.remove(tmpPathName[1])
|
|
|
|
return imgurResult
|
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ exportBitmapToPngFile(self, canvasBitmap, outPathName, outType)
|
2019-09-04 13:52:54 +00:00
|
|
|
def exportBitmapToPngFile(self, canvasBitmap, outPathName, outType):
|
|
|
|
return canvasBitmap.ConvertToImage().SaveFile(outPathName, outType)
|
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ exportPastebin(self, apiDevKey, canvasMap, canvasSize, pasteName="", pastePrivate=0)
|
2019-09-01 14:34:00 +00:00
|
|
|
def exportPastebin(self, apiDevKey, canvasMap, canvasSize, pasteName="", pastePrivate=0):
|
|
|
|
if haveUrllib:
|
|
|
|
outFile = io.StringIO()
|
|
|
|
self.exportTextFile(canvasMap, canvasSize, outFile)
|
2019-09-04 13:52:54 +00:00
|
|
|
requestData = { \
|
|
|
|
"api_dev_key": apiDevKey, \
|
|
|
|
"api_option": "paste", \
|
|
|
|
"api_paste_code": outFile.getvalue().encode(), \
|
|
|
|
"api_paste_name": pasteName, \
|
2019-09-01 14:34:00 +00:00
|
|
|
"api_paste_private": pastePrivate}
|
2019-09-05 15:13:35 +00:00
|
|
|
responseHttp = requests.post(self.PastebinPostUrl, data=requestData)
|
2019-09-01 14:34:00 +00:00
|
|
|
if responseHttp.status_code == 200:
|
2019-09-06 08:29:45 +00:00
|
|
|
return (True if responseHttp.text.startswith("http") else False, responseHttp.text)
|
2019-09-01 14:34:00 +00:00
|
|
|
else:
|
|
|
|
return (False, str(responseHttp.status_code))
|
|
|
|
else:
|
|
|
|
return (False, "missing requests and/or urllib3 module(s)")
|
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ exportPngFile(self, canvasMap, fontFilePath, fontSize, outPathName)
|
2019-09-06 08:29:45 +00:00
|
|
|
def exportPngFile(self, canvasMap, fontFilePath, fontSize, outPathName):
|
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 havePIL:
|
|
|
|
inSize = (len(canvasMap[0]), len(canvasMap))
|
|
|
|
outCurPos = [0, 0]
|
2019-09-06 08:29:45 +00:00
|
|
|
outFontFilePath, outFontSize = fontFilePath, fontSize
|
|
|
|
outImgFont = ImageFont.truetype(outFontFilePath, outFontSize)
|
|
|
|
outImgFontSize = [*outImgFont.getsize(" ")]; outImgFontSize[1] += 3;
|
|
|
|
outSize = [a * b for a, b in zip(inSize, outImgFontSize)]
|
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
|
|
|
outImg = Image.new("RGBA", outSize, (*ColourMapNormal[1], 255))
|
2019-09-06 08:29:45 +00:00
|
|
|
outImgDraw = ImageDraw.Draw(outImg); outImgDraw.fontmode = "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
|
|
|
for inCurRow in range(len(canvasMap)):
|
|
|
|
for inCurCol in range(len(canvasMap[inCurRow])):
|
2019-09-07 15:38:28 +00:00
|
|
|
inCurCell = canvasMap[inCurRow][inCurCol]; outColours = [0, 0];
|
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 inCurCell[2] & self._CellState.CS_BOLD:
|
|
|
|
if inCurCell[3] != " ":
|
|
|
|
if inCurCell[3] == "█":
|
|
|
|
outColours[1] = ColourMapNormal[inCurCell[0]]
|
|
|
|
else:
|
2019-09-06 08:29:45 +00:00
|
|
|
outColours = (ColourMapBold[inCurCell[0]], ColourMapNormal[inCurCell[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:
|
|
|
|
outColours[1] = ColourMapNormal[inCurCell[1]]
|
|
|
|
else:
|
|
|
|
if inCurCell[3] != " ":
|
|
|
|
if inCurCell[3] == "█":
|
|
|
|
outColours[1] = ColourMapNormal[inCurCell[0]]
|
|
|
|
else:
|
2019-09-06 08:29:45 +00:00
|
|
|
outColours = (ColourMapNormal[inCurCell[0]], ColourMapNormal[inCurCell[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:
|
|
|
|
outColours[1] = ColourMapNormal[inCurCell[1]]
|
|
|
|
outImgDraw.rectangle((*outCurPos, outCurPos[0] + outImgFontSize[0], outCurPos[1] + outImgFontSize[1]), fill=(*outColours[1], 255))
|
|
|
|
if not inCurCell[3] in " █" \
|
|
|
|
and outColours[0] != outColours[1]:
|
|
|
|
outImgDraw.text(outCurPos, inCurCell[3], (*outColours[0], 255), outImgFont)
|
|
|
|
if inCurCell[2] & self._CellState.CS_UNDERLINE:
|
|
|
|
outColours[0] = ColourMapNormal[inCurCell[0]]
|
2019-09-06 08:29:45 +00:00
|
|
|
self._drawUnderLine(outCurPos, (*outColours[0], 255), outImgFontSize, outImgDraw)
|
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
|
|
|
outCurPos[0] += outImgFontSize[0];
|
2019-09-06 08:29:45 +00:00
|
|
|
outCurPos[0] = 0; outCurPos[1] += outImgFontSize[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
|
|
|
outImg.save(outPathName);
|
2019-09-06 08:29:45 +00:00
|
|
|
return (True, None)
|
2019-09-01 14:34:00 +00:00
|
|
|
else:
|
2019-09-06 08:29:45 +00:00
|
|
|
return (False, "missing PIL modules")
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ exportTextBuffer(self, canvasMap, canvasSize)
|
2019-09-04 10:50:21 +00:00
|
|
|
def exportTextBuffer(self, canvasMap, canvasSize):
|
|
|
|
outBuffer = ""
|
|
|
|
for canvasRow in range(canvasSize[1]):
|
2019-09-04 13:52:54 +00:00
|
|
|
canvasLastColours = [15, 1]
|
2019-09-04 10:50:21 +00:00
|
|
|
for canvasCol in range(canvasSize[0]):
|
|
|
|
canvasColColours = canvasMap[canvasRow][canvasCol][0:2]
|
|
|
|
canvasColText = canvasMap[canvasRow][canvasCol][3]
|
2019-09-08 14:57:45 +00:00
|
|
|
if canvasColColours[0] == -1:
|
|
|
|
canvasColColours[0] = canvasColColours[1]
|
|
|
|
if (canvasColColours[0] != canvasLastColours[0]) \
|
|
|
|
and (canvasColColours[1] != canvasLastColours[1]):
|
|
|
|
if (canvasColColours[0] == -1) \
|
|
|
|
and (canvasColColours[1] == -1):
|
|
|
|
outBuffer += "\u0003 "
|
|
|
|
elif canvasColColours[1] == -1:
|
|
|
|
outBuffer += "\u0003\u0003{}".format(canvasColColours[0])
|
|
|
|
elif (canvasColColours[0] == canvasLastColours[1]) \
|
|
|
|
and (canvasColColours[1] == canvasLastColours[0]):
|
2019-09-04 13:52:54 +00:00
|
|
|
outBuffer += "\u0016"
|
|
|
|
else:
|
|
|
|
outBuffer += "\u0003{},{}".format(canvasColColours[0], canvasColColours[1])
|
2019-09-04 10:50:21 +00:00
|
|
|
canvasLastColours = canvasColColours
|
2019-09-04 13:52:54 +00:00
|
|
|
elif canvasColColours[1] != canvasLastColours[1]:
|
2019-09-08 14:57:45 +00:00
|
|
|
if canvasColColours[1] == -1:
|
|
|
|
outBuffer += "\u0003\u0003{}".format(canvasLastColours[0])
|
|
|
|
else:
|
|
|
|
outBuffer += "\u0003{},{}".format(canvasLastColours[0], canvasColColours[1])
|
2019-09-04 13:52:54 +00:00
|
|
|
canvasLastColours[1] = canvasColColours[1]
|
|
|
|
elif canvasColColours[0] != canvasLastColours[0]:
|
|
|
|
outBuffer += "\u0003{}".format(canvasColColours[0])
|
|
|
|
canvasLastColours[0] = canvasColColours[0]
|
2019-09-04 10:50:21 +00:00
|
|
|
outBuffer += canvasColText
|
|
|
|
outBuffer += "\n"
|
2019-09-06 08:29:45 +00:00
|
|
|
if len(outBuffer):
|
|
|
|
return (True, outBuffer)
|
|
|
|
else:
|
|
|
|
return (False, "empty buffer generated")
|
2019-09-04 10:50:21 +00:00
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ exportTextFile(self, canvasMap, canvasSize, outFile)
|
2019-09-01 14:34:00 +00:00
|
|
|
def exportTextFile(self, canvasMap, canvasSize, outFile):
|
2019-09-06 08:29:45 +00:00
|
|
|
rc, outBuffer = self.exportTextBuffer(canvasMap, canvasSize)
|
|
|
|
return outFile.write(outBuffer) if rc else (rc, outBuffer)
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|