2018-01-06 20:08:35 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# MiRCARTToPngFile.py -- convert ASCII w/ mIRC control codes to monospaced PNG (for EFnet #MiRCART)
|
2019-09-04 10:23:59 -04:00
|
|
|
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
2018-11-26 04:35:48 -05:00
|
|
|
# This project is licensed under the terms of the MIT licence.
|
2018-01-06 20:08:35 -05:00
|
|
|
#
|
|
|
|
|
2019-09-03 12:58:50 -04:00
|
|
|
import os, sys
|
|
|
|
[sys.path.append(os.path.join(os.getcwd(), "..", "..", path)) for path in ["libcanvas", "librtl", "libtools"]]
|
|
|
|
|
|
|
|
from CanvasImportStore import CanvasImportStore
|
|
|
|
from getopt import getopt, GetoptError
|
2018-01-06 20:08:35 -05:00
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
|
class MiRCARTToPngFile:
|
|
|
|
"""XXX"""
|
2018-01-07 10:43:41 -05:00
|
|
|
inFile = inFromTextFile = None
|
2018-01-06 20:08:35 -05:00
|
|
|
outFontFilePath = outFontSize = None
|
|
|
|
|
|
|
|
# {{{ _ColourMapBold: mIRC colour number to RGBA map given ^B (bold)
|
|
|
|
_ColourMapBold = [
|
2019-01-21 04:23:19 -05:00
|
|
|
[255, 255, 255], # Bright White
|
|
|
|
[85, 85, 85], # Black
|
2018-01-06 20:08:35 -05:00
|
|
|
[85, 85, 255], # Light Blue
|
2019-01-21 04:23:19 -05:00
|
|
|
[85, 255, 85], # Green
|
|
|
|
[255, 85, 85], # Red
|
2018-01-06 20:08:35 -05:00
|
|
|
[255, 85, 85], # Light Red
|
|
|
|
[255, 85, 255], # Pink
|
2019-01-21 04:23:19 -05:00
|
|
|
[255, 255, 85], # Yellow
|
2018-01-06 20:08:35 -05:00
|
|
|
[255, 255, 85], # Light Yellow
|
|
|
|
[85, 255, 85], # Light Green
|
2019-01-21 04:23:19 -05:00
|
|
|
[85, 255, 255], # Cyan
|
2018-01-06 20:08:35 -05:00
|
|
|
[85, 255, 255], # Light Cyan
|
2019-01-21 04:23:19 -05:00
|
|
|
[85, 85, 255], # Blue
|
|
|
|
[255, 85, 255], # Light Pink
|
2018-01-06 20:08:35 -05:00
|
|
|
[85, 85, 85], # Grey
|
2019-01-21 04:23:19 -05:00
|
|
|
[255, 255, 255], # Light Grey
|
2018-01-06 20:08:35 -05:00
|
|
|
]
|
|
|
|
# }}}
|
|
|
|
# {{{ _ColourMapNormal: mIRC colour number to RGBA map given none of ^[BFV_] (bold, italic, reverse, underline)
|
|
|
|
_ColourMapNormal = [
|
2019-01-21 04:23:19 -05:00
|
|
|
[255, 255, 255], # Bright White
|
2018-01-06 20:08:35 -05:00
|
|
|
[0, 0, 0], # Black
|
2019-01-21 04:23:19 -05:00
|
|
|
[0, 0, 187], # Light Blue
|
2018-01-06 20:08:35 -05:00
|
|
|
[0, 187, 0], # Green
|
2019-01-21 04:23:19 -05:00
|
|
|
[255, 85, 85], # Red
|
|
|
|
[187, 0, 0], # Light Red
|
|
|
|
[187, 0, 187], # Pink
|
2018-01-06 20:08:35 -05:00
|
|
|
[187, 187, 0], # Yellow
|
|
|
|
[255, 255, 85], # Light Yellow
|
|
|
|
[85, 255, 85], # Light Green
|
|
|
|
[0, 187, 187], # Cyan
|
|
|
|
[85, 255, 255], # Light Cyan
|
2019-01-21 04:23:19 -05:00
|
|
|
[85, 85, 255], # Blue
|
|
|
|
[255, 85, 255], # Light Pink
|
2018-01-06 20:08:35 -05:00
|
|
|
[85, 85, 85], # Grey
|
|
|
|
[187, 187, 187], # Light Grey
|
|
|
|
]
|
|
|
|
# }}}
|
2018-01-20 03:17:52 -05:00
|
|
|
# {{{ _drawUnderline(self, curPos, fontSize, imgDraw, fillColour): XXX
|
|
|
|
def _drawUnderLine(self, curPos, fontSize, imgDraw, fillColour):
|
|
|
|
imgDraw.line( \
|
|
|
|
xy=(curPos[0], curPos[1] + (fontSize[1] - 2), \
|
|
|
|
curPos[0] + fontSize[0], curPos[1] + (fontSize[1] - 2)), \
|
|
|
|
fill=fillColour)
|
2018-01-06 20:08:35 -05:00
|
|
|
# }}}
|
|
|
|
# {{{ export(self, outFilePath): XXX
|
|
|
|
def export(self, outFilePath):
|
2018-01-07 10:43:41 -05:00
|
|
|
inSize = (len(self.inCanvasMap[0]), len(self.inCanvasMap))
|
|
|
|
outSize = [a*b for a,b in zip(inSize, self.outImgFontSize)]
|
2018-01-06 20:08:35 -05:00
|
|
|
outCurPos = [0, 0]
|
|
|
|
outImg = Image.new("RGBA", outSize, (*self._ColourMapNormal[1], 255))
|
|
|
|
outImgDraw = ImageDraw.Draw(outImg)
|
2019-08-18 07:02:33 -04:00
|
|
|
outImgDraw.fontmode = "1"
|
2018-01-07 10:43:41 -05:00
|
|
|
for inCurRow in range(len(self.inCanvasMap)):
|
|
|
|
for inCurCol in range(len(self.inCanvasMap[inCurRow])):
|
|
|
|
inCurCell = self.inCanvasMap[inCurRow][inCurCol]
|
2018-01-06 20:08:35 -05:00
|
|
|
outColours = [0, 0]
|
2019-09-03 12:58:50 -04:00
|
|
|
if inCurCell[2] & CanvasImportStore._CellState.CS_BOLD:
|
2018-01-30 14:34:02 -05:00
|
|
|
if inCurCell[3] != " ":
|
|
|
|
if inCurCell[3] == "█":
|
|
|
|
outColours[1] = self._ColourMapNormal[inCurCell[0]]
|
2018-01-11 17:22:02 -05:00
|
|
|
else:
|
2018-01-30 14:34:02 -05:00
|
|
|
outColours[0] = self._ColourMapBold[inCurCell[0]]
|
|
|
|
outColours[1] = self._ColourMapNormal[inCurCell[1]]
|
2018-01-11 17:22:02 -05:00
|
|
|
else:
|
2018-01-30 14:34:02 -05:00
|
|
|
outColours[1] = self._ColourMapNormal[inCurCell[1]]
|
2018-01-06 20:08:35 -05:00
|
|
|
else:
|
2018-01-30 14:34:02 -05:00
|
|
|
if inCurCell[3] != " ":
|
|
|
|
if inCurCell[3] == "█":
|
|
|
|
outColours[1] = self._ColourMapNormal[inCurCell[0]]
|
2018-01-11 17:22:02 -05:00
|
|
|
else:
|
2018-01-30 14:34:02 -05:00
|
|
|
outColours[0] = self._ColourMapNormal[inCurCell[0]]
|
|
|
|
outColours[1] = self._ColourMapNormal[inCurCell[1]]
|
2018-01-11 17:22:02 -05:00
|
|
|
else:
|
2018-01-30 14:34:02 -05:00
|
|
|
outColours[1] = self._ColourMapNormal[inCurCell[1]]
|
2018-01-06 20:08:35 -05:00
|
|
|
outImgDraw.rectangle((*outCurPos, \
|
|
|
|
outCurPos[0] + self.outImgFontSize[0], \
|
|
|
|
outCurPos[1] + self.outImgFontSize[1]), \
|
|
|
|
fill=(*outColours[1], 255))
|
2018-01-30 14:34:02 -05:00
|
|
|
if not inCurCell[3] in " █" \
|
2018-01-06 20:08:35 -05:00
|
|
|
and outColours[0] != outColours[1]:
|
|
|
|
# XXX implement italic
|
|
|
|
outImgDraw.text(outCurPos, \
|
2018-01-30 14:34:02 -05:00
|
|
|
inCurCell[3], (*outColours[0], 255), self.outImgFont)
|
2019-09-03 12:58:50 -04:00
|
|
|
if inCurCell[2] & CanvasImportStore._CellState.CS_UNDERLINE:
|
2018-01-30 14:34:02 -05:00
|
|
|
outColours[0] = self._ColourMapNormal[inCurCell[0]]
|
2018-01-20 03:17:52 -05:00
|
|
|
self._drawUnderLine(outCurPos, \
|
|
|
|
self.outImgFontSize, \
|
|
|
|
outImgDraw, (*outColours[0], 255))
|
2018-01-06 20:08:35 -05:00
|
|
|
outCurPos[0] += self.outImgFontSize[0];
|
|
|
|
outCurPos[0] = 0
|
|
|
|
outCurPos[1] += self.outImgFontSize[1]
|
|
|
|
outImg.save(outFilePath);
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
#
|
2019-09-03 12:58:50 -04:00
|
|
|
# __init__(self, inCanvasMap, fontFilePath, fontSize): initialisation method
|
|
|
|
def __init__(self, inCanvasMap, fontFilePath, fontSize):
|
2018-01-07 10:43:41 -05:00
|
|
|
self.inCanvasMap = inCanvasMap
|
2019-09-03 12:58:50 -04:00
|
|
|
self.outFontFilePath, self.outFontSize = fontFilePath, fontSize
|
|
|
|
self.outImgFont = ImageFont.truetype(self.outFontFilePath, self.outFontSize)
|
2018-01-06 20:08:35 -05:00
|
|
|
self.outImgFontSize = [*self.outImgFont.getsize(" ")]
|
|
|
|
self.outImgFontSize[1] += 3
|
|
|
|
|
|
|
|
#
|
|
|
|
# Entry point
|
|
|
|
def main(*argv):
|
2019-09-03 12:58:50 -04:00
|
|
|
argv0 = argv[0]; optlist, argv = getopt(argv[1:], "f:hs:"); optdict = dict(optlist);
|
|
|
|
if len(argv) < 1:
|
|
|
|
print("""usage: {} [-f fname] [-h] [-s size] fname...
|
|
|
|
-h.........: show this screen
|
|
|
|
-f fname...: font file pathname (defaults to: ../fonts/DejaVuSansMono.ttf)
|
|
|
|
-s size....: font size (defaults to: 11)""".format(argv0), file=sys.stderr)
|
2018-01-06 20:08:35 -05:00
|
|
|
else:
|
2019-09-03 12:58:50 -04:00
|
|
|
if not "-f" in optdict:
|
|
|
|
optdict["-f"] = os.path.join("..", "fonts", "DejaVuSansMono.ttf")
|
|
|
|
optdict["-s"] = 11 if not "-s" in optdict else int(optdict["-s"])
|
|
|
|
for inFile in argv:
|
|
|
|
canvasStore, outFile = CanvasImportStore(inFile=inFile), os.path.splitext(inFile)[0] + ".png"
|
|
|
|
MiRCARTToPngFile(canvasStore.outMap, fontFilePath=optdict["-f"], fontSize=optdict["-s"]).export(outFile)
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(*sys.argv)
|
2018-01-06 20:08:35 -05:00
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|