2018-01-04 15:24:06 +00:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
#
|
|
|
|
|
# MiRC2png.py -- convert ASCII w/ mIRC control codes to monospaced PNG (for EFnet #MiRCART)
|
|
|
|
|
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
from enum import Enum
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
import string, sys
|
|
|
|
|
|
|
|
|
|
class MiRC2png:
|
|
|
|
|
"""Abstraction over ASCIIs containing mIRC control codes"""
|
|
|
|
|
inFilePath = inFile = None;
|
|
|
|
|
inLines = inColsMax = inRows = None;
|
|
|
|
|
|
|
|
|
|
outFontFilePath = outFontSize = None;
|
|
|
|
|
outImg = outImgDraw = outImgFont = None;
|
|
|
|
|
outCurColourBg = outCurColourFg = None;
|
|
|
|
|
outCurX = outCurY = None;
|
|
|
|
|
|
|
|
|
|
inCurBold = inCurItalic = inCurUnderline = None;
|
|
|
|
|
inCurColourSpec = None;
|
|
|
|
|
state = None;
|
2018-01-04 23:52:02 +00:00
|
|
|
|
inCurCol = inCurDigits = None;
|
2018-01-04 15:24:06 +00:00
|
|
|
|
|
|
|
|
|
# {{{ _ColourMapBold: mIRC colour number to RGBA map given ^B (bold)
|
|
|
|
|
_ColourMapBold = [
|
|
|
|
|
(255, 255, 255, 255), # White
|
|
|
|
|
(85, 85, 85, 255), # Grey
|
|
|
|
|
(85, 85, 255, 255), # Light Blue
|
|
|
|
|
(85, 255, 85, 255), # Light Green
|
|
|
|
|
(255, 85, 85, 255), # Light Red
|
|
|
|
|
(255, 85, 85, 255), # Light Red
|
|
|
|
|
(255, 85, 255, 255), # Pink
|
|
|
|
|
(255, 255, 85, 255), # Light Yellow
|
|
|
|
|
(255, 255, 85, 255), # Light Yellow
|
|
|
|
|
(85, 255, 85, 255), # Light Green
|
|
|
|
|
(85, 255, 255, 255), # Light Cyan
|
|
|
|
|
(85, 255, 255, 255), # Light Cyan
|
|
|
|
|
(85, 85, 255, 255), # Light Blue
|
|
|
|
|
(255, 85, 255, 255), # Pink
|
|
|
|
|
(85, 85, 85, 255), # Grey
|
|
|
|
|
(255, 255, 255, 255), # White
|
|
|
|
|
]
|
|
|
|
|
# }}}
|
|
|
|
|
# {{{ _ColourMapNormal: mIRC colour number to RGBA map given none of ^[BFV_] (bold, italic, reverse, underline)
|
|
|
|
|
_ColourMapNormal = [
|
|
|
|
|
(255, 255, 255, 255), # White
|
|
|
|
|
(0, 0, 0, 255), # Black
|
|
|
|
|
(0, 0, 187, 255), # Blue
|
|
|
|
|
(0, 187, 0, 255), # Green
|
|
|
|
|
(255, 85, 85, 255), # Light Red
|
|
|
|
|
(187, 0, 0, 255), # Red
|
|
|
|
|
(187, 0, 187, 255), # Purple
|
|
|
|
|
(187, 187, 0, 255), # Yellow
|
|
|
|
|
(255, 255, 85, 255), # Light Yellow
|
|
|
|
|
(85, 255, 85, 255), # Light Green
|
|
|
|
|
(0, 187, 187, 255), # Cyan
|
|
|
|
|
(85, 255, 255, 255), # Light Cyan
|
|
|
|
|
(85, 85, 255, 255), # Light Blue
|
|
|
|
|
(255, 85, 255, 255), # Pink
|
|
|
|
|
(85, 85, 85, 255), # Grey
|
|
|
|
|
(187, 187, 187, 255), # Light Grey
|
|
|
|
|
]
|
|
|
|
|
# }}}
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _State(Enum): Parsing loop state
|
2018-01-04 15:24:06 +00:00
|
|
|
|
class _State(Enum):
|
|
|
|
|
STATE_CHAR = 1
|
|
|
|
|
STATE_COLOUR_SPEC = 2
|
2018-01-04 23:52:02 +00:00
|
|
|
|
STATE_CSPEC_DIGIT0 = 2
|
|
|
|
|
STATE_CSPEC_DIGIT1 = 3
|
2018-01-04 15:24:06 +00:00
|
|
|
|
# }}}
|
|
|
|
|
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _countChar(self, char): XXX
|
2018-01-04 23:52:02 +00:00
|
|
|
|
def _countChar(self, char):
|
|
|
|
|
return True
|
|
|
|
|
# }}}
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _countColourSpecState(self, colourSpec): XXX
|
2018-01-04 23:52:02 +00:00
|
|
|
|
def _countColourSpecState(self, colourSpec):
|
|
|
|
|
return 0
|
|
|
|
|
# }}}
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _render(self): XXX
|
2018-01-04 23:52:02 +00:00
|
|
|
|
def _render(self):
|
|
|
|
|
self.outCurX = 0; self.outCurY = 0;
|
|
|
|
|
for inCurRow in range(0, len(self.inLines)):
|
|
|
|
|
self.inCurBold = 0; self.inCurItalic = 0; self.inCurUnderline = 0;
|
|
|
|
|
self.inCurColourSpec = ""; self._State = self._State.STATE_CHAR;
|
|
|
|
|
self.inCurCol = 0; self.inCurDigits = 0;
|
|
|
|
|
while self.inCurCol < len(self.inLines[inCurRow]):
|
|
|
|
|
if self._State == self._State.STATE_CHAR:
|
|
|
|
|
self._parseAsChar( \
|
|
|
|
|
self.inLines[inCurRow][self.inCurCol], \
|
|
|
|
|
self._syncChar)
|
|
|
|
|
elif self._State == self._State.STATE_CSPEC_DIGIT0 \
|
|
|
|
|
or self._State == self._State.STATE_CSPEC_DIGIT1: \
|
|
|
|
|
self._parseAsColourSpec( \
|
|
|
|
|
self.inLines[inCurRow][self.inCurCol], \
|
|
|
|
|
self._syncColourSpecState)
|
|
|
|
|
self.outCurX = 0; self.outCurY += self.outImgFontSize[1];
|
|
|
|
|
# }}}
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _getMaxCols(self, lines): Calculate widest row in lines, ignoring non-printable & mIRC control code sequences
|
2018-01-04 15:24:06 +00:00
|
|
|
|
def _getMaxCols(self, lines):
|
|
|
|
|
maxCols = 0;
|
2018-01-04 23:52:02 +00:00
|
|
|
|
for inCurRow in range(0, len(lines)):
|
|
|
|
|
self.inCurBold = 0; self.inCurItalic = 0; self.inCurUnderline = 0;
|
|
|
|
|
self.inCurColourSpec = ""; self._State = self._State.STATE_CHAR;
|
|
|
|
|
self.inCurCol = 0; self.inCurDigits = 0; curRowCols = 0;
|
|
|
|
|
while self.inCurCol < len(self.inLines[inCurRow]):
|
|
|
|
|
if self._State == self._State.STATE_CHAR:
|
|
|
|
|
if self._parseAsChar( \
|
|
|
|
|
self.inLines[inCurRow][self.inCurCol], self._countChar):
|
|
|
|
|
curRowCols += 1
|
|
|
|
|
elif self._State == self._State.STATE_CSPEC_DIGIT0 \
|
|
|
|
|
or self._State == self._State.STATE_CSPEC_DIGIT1:
|
|
|
|
|
self._parseAsColourSpec( \
|
|
|
|
|
self.inLines[inCurRow][self.inCurCol], \
|
|
|
|
|
self._countColourSpecState)
|
2018-01-04 15:24:06 +00:00
|
|
|
|
maxCols = max(maxCols, curRowCols)
|
|
|
|
|
return maxCols
|
|
|
|
|
# }}}
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _parseAsChar(self, char, fn): Parse single character as regular character and mutate state
|
2018-01-04 23:52:02 +00:00
|
|
|
|
def _parseAsChar(self, char, fn):
|
|
|
|
|
if char == "":
|
|
|
|
|
self._State = self._State.STATE_CSPEC_DIGIT0; self.inCurCol += 1;
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
self.inCurCol += 1; return fn(char);
|
|
|
|
|
# }}}
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _parseAsColourSpec(self, char, fn): Parse single character as mIRC colour control code sequence and mutate state
|
2018-01-04 23:52:02 +00:00
|
|
|
|
def _parseAsColourSpec(self, char, fn):
|
|
|
|
|
if self._State == self._State.STATE_CSPEC_DIGIT0 \
|
|
|
|
|
and char == ",":
|
|
|
|
|
self.inCurColourSpec += char; self.inCurCol += 1;
|
|
|
|
|
self._State = self._State.STATE_CSPEC_DIGIT1;
|
|
|
|
|
self.inCurDigits = 0
|
|
|
|
|
return [False]
|
|
|
|
|
elif self._State == self._State.STATE_CSPEC_DIGIT0 \
|
|
|
|
|
and char in set("0123456789") \
|
|
|
|
|
and self.inCurDigits <= 1:
|
|
|
|
|
self.inCurColourSpec += char; self.inCurCol += 1;
|
|
|
|
|
self.inCurDigits += 1
|
|
|
|
|
return [False]
|
|
|
|
|
elif self._State == self._State.STATE_CSPEC_DIGIT1 \
|
|
|
|
|
and char in set("0123456789") \
|
|
|
|
|
and self.inCurDigits <= 1:
|
|
|
|
|
self.inCurColourSpec += char; self.inCurCol += 1;
|
|
|
|
|
self.inCurDigits += 1
|
|
|
|
|
return [False]
|
|
|
|
|
else:
|
|
|
|
|
result = fn(self.inCurColourSpec)
|
|
|
|
|
self.inCurColourSpec = ""; self._State = self._State.STATE_CHAR;
|
|
|
|
|
self.inCurDigits = 0
|
|
|
|
|
return [True, result]
|
|
|
|
|
# }}}
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _syncChar(self, char): XXX
|
2018-01-04 23:52:02 +00:00
|
|
|
|
def _syncChar(self, char):
|
2018-01-04 22:45:30 +00:00
|
|
|
|
if char == "":
|
2018-01-04 23:52:02 +00:00
|
|
|
|
self.inCurBold = 0 if self.inCurBold else 1;
|
2018-01-04 22:45:30 +00:00
|
|
|
|
elif char == "":
|
2018-01-04 23:52:02 +00:00
|
|
|
|
self.inCurItalic = 0 if self.inCurItalic else 1;
|
2018-01-04 22:45:30 +00:00
|
|
|
|
elif char == "":
|
|
|
|
|
self.inCurBold = 0; self.inCurItalic = 0; self.inCurUnderline = 0;
|
|
|
|
|
self.inCurColourSpec = "";
|
|
|
|
|
elif char == "":
|
|
|
|
|
self.outCurColourBg, self.outCurColourFg = self.outCurColourFg, self.outCurColourBg;
|
|
|
|
|
elif char == "":
|
2018-01-04 23:52:02 +00:00
|
|
|
|
self.inCurUnderline = 0 if self.inCurUnderline else 1;
|
2018-01-04 22:45:30 +00:00
|
|
|
|
elif char == " ":
|
|
|
|
|
if self.inCurBold:
|
|
|
|
|
colourBg = self._ColourMapBold[self.outCurColourBg]
|
2018-01-04 15:24:06 +00:00
|
|
|
|
else:
|
2018-01-04 22:45:30 +00:00
|
|
|
|
colourBg = self._ColourMapNormal[self.outCurColourBg]
|
|
|
|
|
self.outImgDraw.rectangle(((self.outCurX, self.outCurY), (self.outCurX + self.outImgFontSize[0], self.outCurY + self.outImgFontSize[1])), fill=colourBg)
|
|
|
|
|
if self.inCurUnderline:
|
|
|
|
|
self.outImgDraw.line((self.outCurX, self.outCurY + (self.outImgFontSize[1] - 2), self.outCurX + self.outImgFontSize[0], self.outCurY + (self.outImgFontSize[1] - 2)), fill=colourFg)
|
2018-01-04 23:52:02 +00:00
|
|
|
|
self.outCurX += self.outImgFontSize[0];
|
2018-01-04 22:45:30 +00:00
|
|
|
|
else:
|
|
|
|
|
if self.inCurBold:
|
|
|
|
|
colourBg = self._ColourMapBold[self.outCurColourBg]
|
|
|
|
|
colourFg = self._ColourMapBold[self.outCurColourFg]
|
|
|
|
|
else:
|
|
|
|
|
colourBg = self._ColourMapNormal[self.outCurColourBg]
|
|
|
|
|
colourFg = self._ColourMapNormal[self.outCurColourFg]
|
|
|
|
|
self.outImgDraw.rectangle(((self.outCurX, self.outCurY), (self.outCurX + self.outImgFontSize[0], self.outCurY + self.outImgFontSize[1])), fill=colourBg)
|
|
|
|
|
# XXX implement italic
|
|
|
|
|
self.outImgDraw.text((self.outCurX, self.outCurY), char, colourFg, self.outImgFont)
|
|
|
|
|
if self.inCurUnderline:
|
|
|
|
|
self.outImgDraw.line((self.outCurX, self.outCurY + (self.outImgFontSize[1] - 2), self.outCurX + self.outImgFontSize[0], self.outCurY + (self.outImgFontSize[1] - 2)), fill=colourFg)
|
2018-01-04 23:52:02 +00:00
|
|
|
|
self.outCurX += self.outImgFontSize[0];
|
|
|
|
|
return True
|
2018-01-04 15:24:06 +00:00
|
|
|
|
# }}}
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# {{{ _syncColourSpecState(self, colourSpec): XXX
|
2018-01-04 23:52:02 +00:00
|
|
|
|
def _syncColourSpecState(self, colourSpec):
|
2018-01-05 15:48:50 +00:00
|
|
|
|
if len(colourSpec) > 0:
|
|
|
|
|
colourSpec = colourSpec.split(",")
|
|
|
|
|
if len(colourSpec) == 2:
|
|
|
|
|
self.outCurColourFg = int(colourSpec[0])
|
|
|
|
|
self.outCurColourBg = int(colourSpec[1] or self.outCurColourBg)
|
|
|
|
|
elif len(colourSpec) == 1:
|
|
|
|
|
self.outCurColourFg = int(colourSpec[0])
|
|
|
|
|
else:
|
|
|
|
|
self.outCurColourBg = 1; self.outCurColourFg = 15;
|
2018-01-04 23:52:02 +00:00
|
|
|
|
return True
|
2018-01-04 15:24:06 +00:00
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
#
|
2018-01-06 00:44:45 +00:00
|
|
|
|
# __init__(self, inFilePath, imgFilePath, fontFilePath="DejaVuSansMono.ttf", fontSize=11): initialisation method
|
2018-01-04 15:24:06 +00:00
|
|
|
|
def __init__(self, inFilePath, imgFilePath, fontFilePath="DejaVuSansMono.ttf", fontSize=11):
|
|
|
|
|
self.inFilePath = inFilePath; self.inFile = open(inFilePath, "r");
|
|
|
|
|
self.inLines = self.inFile.readlines()
|
|
|
|
|
self.inColsMax = self._getMaxCols(self.inLines)
|
|
|
|
|
self.inRows = len(self.inLines)
|
|
|
|
|
self.outFontFilePath = fontFilePath; self.outFontSize = int(fontSize);
|
|
|
|
|
self.outImgFont = ImageFont.truetype(self.outFontFilePath, self.outFontSize)
|
|
|
|
|
self.outImgFontSize = list(self.outImgFont.getsize(" ")); self.outImgFontSize[1] += 3;
|
|
|
|
|
self.outImg = Image.new("RGBA", (self.inColsMax * self.outImgFontSize[0], self.inRows * self.outImgFontSize[1]), self._ColourMapNormal[1])
|
|
|
|
|
self.outImgDraw = ImageDraw.Draw(self.outImg)
|
|
|
|
|
self.outCurColourBg = 1; self.outCurColourFg = 15;
|
2018-01-04 23:52:02 +00:00
|
|
|
|
self._render()
|
2018-01-04 15:24:06 +00:00
|
|
|
|
self.inFile.close();
|
|
|
|
|
self.outImg.save(imgFilePath);
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Entry point
|
|
|
|
|
def main(*argv):
|
|
|
|
|
MiRC2png(*argv[1:])
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
if ((len(sys.argv) - 1) < 2)\
|
|
|
|
|
or ((len(sys.argv) - 1) > 4):
|
|
|
|
|
print("usage: {} " \
|
|
|
|
|
"<MiRCART input file pathname> " \
|
|
|
|
|
"<PNG image output file pathname> " \
|
|
|
|
|
"[<Font file pathname; defaults to DejaVuSansMono.ttf>] " \
|
|
|
|
|
"[<Font size; defaults to 11>]".format(sys.argv[0]), file=sys.stderr)
|
|
|
|
|
else:
|
|
|
|
|
main(*sys.argv)
|
|
|
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|