mirror of
https://github.com/lalbornoz/roar.git
synced 2024-11-22 15:26:37 +00:00
MiRC2png.py: renamed from MiRCART.py.
{IrcClient,IrcMiRCARTBot,MiRC2png}.py: update Vim modeline w/ `sw=4 ts=4'. {IrcMiRCARTBot,MiRC2png}.py: update header legend. IrcMiRCARTBot.py:IrcMiRCARTBot._dispatchPrivmsg(): lower rate limit to (once per) 30 seconds. IrcMiRCARTBot.py:IrcMiRCARTBot._dispatchPrivmsg(): eliminate useless instance variable. IrcMiRCARTBot.py:IrcMiRCARTBot._urlretrieveReportHook(): compare against correct limit of 1 MB (2**20.) MiRCART.py: initial commit. README.md: updated.
This commit is contained in:
parent
49705ad4bc
commit
1a2dd5f692
@ -123,4 +123,4 @@ class IrcClient:
|
|||||||
self.serverHname = serverHname; self.serverPort = serverPort;
|
self.serverHname = serverHname; self.serverPort = serverPort;
|
||||||
self.clientNick = clientNick; self.clientIdent = clientIdent; self.clientGecos = clientGecos;
|
self.clientNick = clientNick; self.clientIdent = clientIdent; self.clientGecos = clientGecos;
|
||||||
|
|
||||||
# vim:expandtab foldmethod=marker sw=8 ts=8 tw=120
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#
|
#
|
||||||
# mirc2png -- convert ASCII w/ mIRC control codes to monospaced PNG (for EFnet #MiRCART)
|
# IrcMiRCARTBot.py -- XXX
|
||||||
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
@ -25,7 +25,7 @@
|
|||||||
import base64
|
import base64
|
||||||
import os, sys, time
|
import os, sys, time
|
||||||
import json
|
import json
|
||||||
import IrcClient, MiRCART
|
import IrcClient, MiRC2png
|
||||||
import requests, urllib.request
|
import requests, urllib.request
|
||||||
|
|
||||||
class IrcMiRCARTBot(IrcClient.IrcClient):
|
class IrcMiRCARTBot(IrcClient.IrcClient):
|
||||||
@ -106,7 +106,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
|
|||||||
def _dispatchPrivmsg(self, message):
|
def _dispatchPrivmsg(self, message):
|
||||||
if message[2].lower() == self.clientChannel.lower() \
|
if message[2].lower() == self.clientChannel.lower() \
|
||||||
and message[3].startswith("!pngbot "):
|
and message[3].startswith("!pngbot "):
|
||||||
if (int(time.time()) - self.clientLastMessage) < 45:
|
if (int(time.time()) - self.clientLastMessage) < 30:
|
||||||
self._log("Ignoring request on {} from {} due to rate limit: {}".format(message[2].lower(), message[0], message[3]))
|
self._log("Ignoring request on {} from {} due to rate limit: {}".format(message[2].lower(), message[0], message[3]))
|
||||||
return
|
return
|
||||||
elif message[0].split("!")[0].lower() not in self.clientChannelOps:
|
elif message[0].split("!")[0].lower() not in self.clientChannelOps:
|
||||||
@ -138,7 +138,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
|
|||||||
self._log("Unknown URL type specified!")
|
self._log("Unknown URL type specified!")
|
||||||
self.queue("PRIVMSG", message[2], "4/!\\ Unknown URL type specified!")
|
self.queue("PRIVMSG", message[2], "4/!\\ Unknown URL type specified!")
|
||||||
return
|
return
|
||||||
_MiRCART = MiRCART.MiRCART(asciiTmpFilePath, imgTmpFilePath, "DejaVuSansMono.ttf", 11)
|
MiRC2png.MiRC2png(asciiTmpFilePath, imgTmpFilePath, "DejaVuSansMono.ttf", 11)
|
||||||
imgurResponse = self._uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd")
|
imgurResponse = self._uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd")
|
||||||
if imgurResponse[0] == 200:
|
if imgurResponse[0] == 200:
|
||||||
self._log("Uploaded as: {}".format(imgurResponse[1]))
|
self._log("Uploaded as: {}".format(imgurResponse[1]))
|
||||||
@ -183,7 +183,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
|
|||||||
# }}}
|
# }}}
|
||||||
# {{{ _urlretrieveReportHook(): Limit downloads to 1 MB
|
# {{{ _urlretrieveReportHook(): Limit downloads to 1 MB
|
||||||
def _urlretrieveReportHook(count, blockSize, totalSize):
|
def _urlretrieveReportHook(count, blockSize, totalSize):
|
||||||
if (totalSize > pow(2,10)):
|
if (totalSize > pow(2,20)):
|
||||||
raise IrcMiRCARTBot.ContentTooLargeException
|
raise IrcMiRCARTBot.ContentTooLargeException
|
||||||
# }}}
|
# }}}
|
||||||
# {{{ connect(): Connect to server and (re)initialise w/ optional timeout
|
# {{{ connect(): Connect to server and (re)initialise w/ optional timeout
|
||||||
@ -256,4 +256,4 @@ if __name__ == "__main__":
|
|||||||
else:
|
else:
|
||||||
main(*sys.argv)
|
main(*sys.argv)
|
||||||
|
|
||||||
# vim:expandtab foldmethod=marker sw=8 ts=8 tw=120
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|
||||||
|
211
MiRC2png.py
Executable file
211
MiRC2png.py
Executable file
@ -0,0 +1,211 @@
|
|||||||
|
#!/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;
|
||||||
|
inCurCol = None;
|
||||||
|
|
||||||
|
# {{{ _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
|
||||||
|
]
|
||||||
|
# }}}
|
||||||
|
# {{{ _State: Parsing loop state
|
||||||
|
class _State(Enum):
|
||||||
|
STATE_CHAR = 1
|
||||||
|
STATE_COLOUR_SPEC = 2
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# {{{ _getMaxCols(): Calculate widest row in lines, ignoring non-printable & mIRC control code sequences
|
||||||
|
def _getMaxCols(self, lines):
|
||||||
|
maxCols = 0;
|
||||||
|
for curRow in range(0, len(lines)):
|
||||||
|
curRowCols = 0; curState = self._State.STATE_CHAR;
|
||||||
|
curCol = 0; curColLen = len(lines[curRow]);
|
||||||
|
while curCol < curColLen:
|
||||||
|
curChar = lines[curRow][curCol]
|
||||||
|
if curState == self._State.STATE_CHAR:
|
||||||
|
if curChar == "":
|
||||||
|
curState = self._State.STATE_COLOUR_SPEC; curCol += 1;
|
||||||
|
elif curChar in string.printable:
|
||||||
|
curRowCols += 1; curCol += 1;
|
||||||
|
else:
|
||||||
|
curCol += 1;
|
||||||
|
elif curState == self._State.STATE_COLOUR_SPEC:
|
||||||
|
if curChar in set(",0123456789"):
|
||||||
|
curCol += 1;
|
||||||
|
else:
|
||||||
|
curState = self._State.STATE_CHAR;
|
||||||
|
maxCols = max(maxCols, curRowCols)
|
||||||
|
return maxCols
|
||||||
|
# }}}
|
||||||
|
# {{{ _parseAsChar(): Parse single character as regular character and mutate state
|
||||||
|
def _parseAsChar(self, char):
|
||||||
|
if char == "":
|
||||||
|
self.inCurCol += 1; self.inCurBold = 0 if self.inCurBold else 1;
|
||||||
|
elif char == "":
|
||||||
|
self._State = self._State.STATE_COLOUR_SPEC; self.inCurCol += 1;
|
||||||
|
elif char == "":
|
||||||
|
self.inCurCol += 1; self.inCurItalic = 0 if self.inCurItalic else 1;
|
||||||
|
elif char == "":
|
||||||
|
self.inCurCol += 1;
|
||||||
|
self.inCurBold = 0; self.inCurItalic = 0; self.inCurUnderline = 0;
|
||||||
|
self.inCurColourSpec = "";
|
||||||
|
elif char == "":
|
||||||
|
self.inCurCol += 1
|
||||||
|
self.outCurColourBg, self.outCurColourFg = self.outCurColourFg, self.outCurColourBg;
|
||||||
|
elif char == "":
|
||||||
|
self.inCurCol += 1; self.inCurUnderline = 0 if self.inCurUnderline else 1;
|
||||||
|
elif char == " ":
|
||||||
|
if self.inCurBold:
|
||||||
|
colourBg = self._ColourMapBold[self.outCurColourBg]
|
||||||
|
else:
|
||||||
|
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)
|
||||||
|
self.outCurX += self.outImgFontSize[0]; self.inCurCol += 1;
|
||||||
|
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)
|
||||||
|
self.outCurX += self.outImgFontSize[0]; self.inCurCol += 1;
|
||||||
|
# }}}
|
||||||
|
# {{{ _parseAsColourSpec(): Parse single character as mIRC colour control code sequence and mutate state
|
||||||
|
def _parseAsColourSpec(self, char):
|
||||||
|
if char in set(",0123456789"):
|
||||||
|
self.inCurColourSpec += char; self.inCurCol += 1;
|
||||||
|
else:
|
||||||
|
self.inCurColourSpec = self.inCurColourSpec.split(",")
|
||||||
|
if len(self.inCurColourSpec) == 2:
|
||||||
|
self.outCurColourFg = int(self.inCurColourSpec[0])
|
||||||
|
self.outCurColourBg = int(self.inCurColourSpec[1] or self.outCurColourBg)
|
||||||
|
elif len(self.inCurColourSpec) == 1:
|
||||||
|
self.outCurColourFg = int(self.inCurColourSpec[0])
|
||||||
|
else:
|
||||||
|
self.outCurColourBg = 1; self.outCurColourFg = 15;
|
||||||
|
self.inCurColourSpec = ""; self._State = self._State.STATE_CHAR;
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
#
|
||||||
|
# Initialisation method
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
while self.inCurCol < len(self.inLines[inCurRow]):
|
||||||
|
if self._State == self._State.STATE_CHAR:
|
||||||
|
self._parseAsChar(self.inLines[inCurRow][self.inCurCol])
|
||||||
|
elif self._State == self._State.STATE_COLOUR_SPEC:
|
||||||
|
self._parseAsColourSpec(self.inLines[inCurRow][self.inCurCol])
|
||||||
|
self.outCurX = 0; self.outCurY += self.outImgFontSize[1];
|
||||||
|
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
|
283
MiRCART.py
283
MiRCART.py
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#
|
#
|
||||||
# mirc2png -- convert ASCII w/ mIRC control codes to monospaced PNG (for EFnet #MiRCART)
|
# MiRCART.py -- XXX
|
||||||
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
@ -22,47 +22,18 @@
|
|||||||
# SOFTWARE.
|
# SOFTWARE.
|
||||||
#
|
#
|
||||||
|
|
||||||
from enum import Enum
|
import wx
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
import sys
|
||||||
import string, sys
|
|
||||||
|
|
||||||
class MiRCART:
|
class MiRCARTCanvas(wx.Panel):
|
||||||
"""Abstraction over ASCIIs containing mIRC control codes"""
|
"""XXX"""
|
||||||
inFilePath = inFile = None;
|
canvasPos = canvasSize = None
|
||||||
inLines = inColsMax = inRows = None;
|
canvasMap = None
|
||||||
|
cellPos = cellSize = None
|
||||||
outFontFilePath = outFontSize = None;
|
brushBg = brushFg = penBg = penFg = None
|
||||||
outImg = outImgDraw = outImgFont = None;
|
mircBg = mircFg = None
|
||||||
outCurColourBg = outCurColourFg = None;
|
# {{{ mircColours: mIRC colour number to RGBA map given none of ^[BFV_] (bold, italic, reverse, underline)
|
||||||
outCurX = outCurY = None;
|
mircColours = [
|
||||||
|
|
||||||
inCurBold = inCurItalic = inCurUnderline = None;
|
|
||||||
inCurColourSpec = None;
|
|
||||||
state = None;
|
|
||||||
inCurCol = None;
|
|
||||||
|
|
||||||
# {{{ _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
|
(255, 255, 255, 255), # White
|
||||||
(0, 0, 0, 255), # Black
|
(0, 0, 0, 255), # Black
|
||||||
(0, 0, 187, 255), # Blue
|
(0, 0, 187, 255), # Blue
|
||||||
@ -81,131 +52,133 @@ class MiRCART:
|
|||||||
(187, 187, 187, 255), # Light Grey
|
(187, 187, 187, 255), # Light Grey
|
||||||
]
|
]
|
||||||
# }}}
|
# }}}
|
||||||
# {{{ _State: Parsing loop state
|
|
||||||
class _State(Enum):
|
|
||||||
STATE_CHAR = 1
|
|
||||||
STATE_COLOUR_SPEC = 2
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
# {{{ _getMaxCols(): Calculate widest row in lines, ignoring non-printable & mIRC control code sequences
|
# {{{ _onMouseEvent(): XXX
|
||||||
def _getMaxCols(self, lines):
|
def _onMouseEvent(self, event):
|
||||||
maxCols = 0;
|
eventObject = event.GetEventObject()
|
||||||
for curRow in range(0, len(lines)):
|
if event.Dragging():
|
||||||
curRowCols = 0; curState = self._State.STATE_CHAR;
|
eventDc = wx.ClientDC(self)
|
||||||
curCol = 0; curColLen = len(lines[curRow]);
|
eventPoint = event.GetLogicalPosition(eventDc)
|
||||||
while curCol < curColLen:
|
rectX = eventPoint.x - (eventPoint.x % self.cellSize[0])
|
||||||
curChar = lines[curRow][curCol]
|
rectY = eventPoint.y - (eventPoint.y % self.cellSize[1])
|
||||||
if curState == self._State.STATE_CHAR:
|
mapX = int(rectX / 7 if rectX else 0)
|
||||||
if curChar == "":
|
mapY = int(rectY / 14 if rectY else 0)
|
||||||
curState = self._State.STATE_COLOUR_SPEC; curCol += 1;
|
eventDc.SetBackground(self.brushBg);
|
||||||
elif curChar in string.printable:
|
if event.LeftIsDown():
|
||||||
curRowCols += 1; curCol += 1;
|
eventDc.SetBrush(self.brushFg);
|
||||||
else:
|
eventDc.SetPen(self.penFg)
|
||||||
curCol += 1;
|
self.canvasMap[mapX][mapY] = [self.mircFg, self.mircFg, " "]
|
||||||
elif curState == self._State.STATE_COLOUR_SPEC:
|
elif event.RightIsDown():
|
||||||
if curChar in set(",0123456789"):
|
eventDc.SetBrush(self.brushBg);
|
||||||
curCol += 1;
|
eventDc.SetPen(self.penBg)
|
||||||
else:
|
self.canvasMap[mapX][mapY] = [self.mircBg, self.mircBg, " "]
|
||||||
curState = self._State.STATE_CHAR;
|
eventDc.DrawRectangle(rectX, rectY, \
|
||||||
maxCols = max(maxCols, curRowCols)
|
self.cellSize[0], self.cellSize[1])
|
||||||
return maxCols
|
|
||||||
# }}}
|
# }}}
|
||||||
# {{{ _parseAsChar(): Parse single character as regular character and mutate state
|
# {{{ onCharHook(): XXX
|
||||||
def _parseAsChar(self, char):
|
def onCharHook(self, event):
|
||||||
if char == "":
|
keyCode = event.GetKeyCode()
|
||||||
self.inCurCol += 1; self.inCurBold = 0 if self.inCurBold else 1;
|
if keyCode == wx.WXK_UP:
|
||||||
elif char == "":
|
self.mircFg = self.mircFg + 1 if self.mircFg < 15 else 15
|
||||||
self._State = self._State.STATE_COLOUR_SPEC; self.inCurCol += 1;
|
elif keyCode == wx.WXK_DOWN:
|
||||||
elif char == "":
|
self.mircFg = self.mircFg - 1 if self.mircFg > 0 else 0
|
||||||
self.inCurCol += 1; self.inCurItalic = 0 if self.inCurItalic else 1;
|
self.brushBg = wx.Brush(wx.Colour(self.mircColours[self.mircBg]), wx.BRUSHSTYLE_SOLID)
|
||||||
elif char == "":
|
self.brushFg = wx.Brush(wx.Colour(self.mircColours[self.mircFg]), wx.BRUSHSTYLE_SOLID)
|
||||||
self.inCurCol += 1;
|
self.penBg = wx.Pen(wx.Colour(self.mircColours[self.mircBg]), 1)
|
||||||
self.inCurBold = 0; self.inCurItalic = 0; self.inCurUnderline = 0;
|
self.penFg = wx.Pen(wx.Colour(self.mircColours[self.mircFg]), 1)
|
||||||
self.inCurColourSpec = "";
|
|
||||||
elif char == "":
|
|
||||||
self.inCurCol += 1
|
|
||||||
self.outCurColourBg, self.outCurColourFg = self.outCurColourFg, self.outCurColourBg;
|
|
||||||
elif char == "":
|
|
||||||
self.inCurCol += 1; self.inCurUnderline = 0 if self.inCurUnderline else 1;
|
|
||||||
elif char == " ":
|
|
||||||
if self.inCurBold:
|
|
||||||
colourBg = self._ColourMapBold[self.outCurColourBg]
|
|
||||||
else:
|
|
||||||
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)
|
|
||||||
self.outCurX += self.outImgFontSize[0]; self.inCurCol += 1;
|
|
||||||
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)
|
|
||||||
self.outCurX += self.outImgFontSize[0]; self.inCurCol += 1;
|
|
||||||
# }}}
|
# }}}
|
||||||
# {{{ _parseAsColourSpec(): Parse single character as mIRC colour control code sequence and mutate state
|
# {{{ onLeftDown(): XXX
|
||||||
def _parseAsColourSpec(self, char):
|
def onLeftDown(self, event):
|
||||||
if char in set(",0123456789"):
|
self._onMouseEvent(event)
|
||||||
self.inCurColourSpec += char; self.inCurCol += 1;
|
# }}}
|
||||||
else:
|
# {{{ onMotion(): XXX
|
||||||
self.inCurColourSpec = self.inCurColourSpec.split(",")
|
def onMotion(self, event):
|
||||||
if len(self.inCurColourSpec) == 2:
|
self._onMouseEvent(event)
|
||||||
self.outCurColourFg = int(self.inCurColourSpec[0])
|
# }}}
|
||||||
self.outCurColourBg = int(self.inCurColourSpec[1] or self.outCurColourBg)
|
# {{{ onPaint(): XXX
|
||||||
elif len(self.inCurColourSpec) == 1:
|
def onPaint(self, event):
|
||||||
self.outCurColourFg = int(self.inCurColourSpec[0])
|
eventDc = wx.BufferedPaintDC(self)
|
||||||
else:
|
eventDc.SetBackground(wx.Brush(wx.BLACK))
|
||||||
self.outCurColourBg = 1; self.outCurColourFg = 15;
|
eventDc.Clear()
|
||||||
self.inCurColourSpec = ""; self._State = self._State.STATE_CHAR;
|
for cellX in range(0, self.canvasSize[0]):
|
||||||
|
for cellY in range(0, self.canvasSize[1]):
|
||||||
|
eventDc.SetBackground(wx.Brush(wx.Colour(self.mircColours[self.canvasMap[cellX][cellY][0]]), wx.BRUSHSTYLE_SOLID))
|
||||||
|
eventDc.SetBrush(wx.Brush(wx.Colour(self.mircColours[self.canvasMap[cellX][cellY][1]]), wx.BRUSHSTYLE_SOLID))
|
||||||
|
eventDc.SetPen(wx.Pen(wx.Colour(self.mircColours[self.canvasMap[cellX][cellY][1]]), 1))
|
||||||
|
rectX = cellX * self.cellSize[0]; rectY = cellY * self.cellSize[1];
|
||||||
|
eventDc.DrawRectangle(rectX, rectY, \
|
||||||
|
self.cellSize[0], self.cellSize[1])
|
||||||
|
# }}}
|
||||||
|
# {{{ onRightDown(): XXX
|
||||||
|
def onRightDown(self, event):
|
||||||
|
self._onMouseEvent(event)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
#
|
#
|
||||||
# Initialisation method
|
# Initialisation method
|
||||||
def __init__(self, inFilePath, imgFilePath, fontFilePath="DejaVuSansMono.ttf", fontSize=11):
|
def __init__(self, parent, canvasPos, cellSize, canvasSize):
|
||||||
self.inFilePath = inFilePath; self.inFile = open(inFilePath, "r");
|
super().__init__(parent, pos=canvasPos, size=( \
|
||||||
self.inLines = self.inFile.readlines()
|
cellSize[0] * canvasSize[0],
|
||||||
self.inColsMax = self._getMaxCols(self.inLines)
|
cellSize[1] * canvasSize[1]))
|
||||||
self.inRows = len(self.inLines)
|
|
||||||
self.outFontFilePath = fontFilePath; self.outFontSize = int(fontSize);
|
self.canvasPos = canvasPos; self.canvasSize = canvasSize;
|
||||||
self.outImgFont = ImageFont.truetype(self.outFontFilePath, self.outFontSize)
|
self.canvasMap = [[[1, 1, " "] for y in range(canvasSize[1])] for x in range(canvasSize[0])]
|
||||||
self.outImgFontSize = list(self.outImgFont.getsize(" ")); self.outImgFontSize[1] += 3;
|
self.cellPos = (0, 0); self.cellSize = cellSize;
|
||||||
self.outImg = Image.new("RGBA", (self.inColsMax * self.outImgFontSize[0], self.inRows * self.outImgFontSize[1]), self._ColourMapNormal[1])
|
self.brushBg = wx.Brush(wx.Colour(self.mircColours[1]), wx.BRUSHSTYLE_SOLID)
|
||||||
self.outImgDraw = ImageDraw.Draw(self.outImg)
|
self.brushFg = wx.Brush(wx.Colour(self.mircColours[4]), wx.BRUSHSTYLE_SOLID)
|
||||||
self.outCurColourBg = 1; self.outCurColourFg = 15;
|
self.penBg = wx.Pen(wx.Colour(self.mircColours[1]), 1)
|
||||||
self.outCurX = 0; self.outCurY = 0;
|
self.penFg = wx.Pen(wx.Colour(self.mircColours[4]), 1)
|
||||||
for inCurRow in range(0, len(self.inLines)):
|
self.mircBg = 1; self.mircFg = 4;
|
||||||
self.inCurBold = 0; self.inCurItalic = 0; self.inCurUnderline = 0;
|
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
|
||||||
self.inCurColourSpec = ""; self._State = self._State.STATE_CHAR;
|
|
||||||
self.inCurCol = 0;
|
self.Bind(wx.EVT_CHAR_HOOK, self.onCharHook)
|
||||||
while self.inCurCol < len(self.inLines[inCurRow]):
|
self.Bind(wx.EVT_LEFT_DOWN, self.onLeftDown)
|
||||||
if self._State == self._State.STATE_CHAR:
|
self.Bind(wx.EVT_MOTION, self.onMotion)
|
||||||
self._parseAsChar(self.inLines[inCurRow][self.inCurCol])
|
self.Bind(wx.EVT_PAINT, self.onPaint)
|
||||||
elif self._State == self._State.STATE_COLOUR_SPEC:
|
self.Bind(wx.EVT_RIGHT_DOWN, self.onRightDown)
|
||||||
self._parseAsColourSpec(self.inLines[inCurRow][self.inCurCol])
|
|
||||||
self.outCurX = 0; self.outCurY += self.outImgFontSize[1];
|
class MiRCARTFrame(wx.Frame):
|
||||||
self.inFile.close();
|
"""XXX"""
|
||||||
self.outImg.save(imgFilePath);
|
menuFile = menuFileSaveAs = menuFileExit = menuBar = None
|
||||||
|
panelSkin = panelCanvas = None
|
||||||
|
|
||||||
|
# {{{ onFileSaveAs(): XXX
|
||||||
|
def onFileSaveAs(self, event):
|
||||||
|
pass
|
||||||
|
# }}}
|
||||||
|
# {{{ onFileExit(): XXX
|
||||||
|
def onFileExit(self, event):
|
||||||
|
self.Close(True)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
#
|
||||||
|
# Initialisation method
|
||||||
|
def __init__(self, parent, appSize=(1024, 768), canvasPos=(25, 25), cellSize=(7, 14), canvasSize=(80, 25)):
|
||||||
|
super().__init__(parent, wx.ID_ANY, "MiRCART", size=appSize)
|
||||||
|
|
||||||
|
self.menuFile = wx.Menu()
|
||||||
|
self.menuFileExit = self.menuFile.Append(wx.ID_EXIT, "E&xit", "Exit")
|
||||||
|
self.menuFileSaveAs = self.menuFile.Append(wx.ID_SAVE, "Save &As...", "Save As...")
|
||||||
|
self.menuBar = wx.MenuBar()
|
||||||
|
self.menuBar.Append(self.menuFile, "&File")
|
||||||
|
|
||||||
|
self.panelSkin = wx.Panel(self, wx.ID_ANY)
|
||||||
|
self.panelCanvas = MiRCARTCanvas(self.panelSkin, \
|
||||||
|
canvasPos=canvasPos, cellSize=cellSize, canvasSize=canvasSize)
|
||||||
|
|
||||||
|
self.Bind(wx.EVT_MENU, self.onFileExit, self.menuFileExit)
|
||||||
|
self.Bind(wx.EVT_MENU, self.onFileSaveAs, self.menuFileSaveAs)
|
||||||
|
self.CreateStatusBar()
|
||||||
|
self.SetMenuBar(self.menuBar)
|
||||||
|
self.Show(True)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Entry point
|
# Entry point
|
||||||
def main(*argv):
|
def main(*argv):
|
||||||
_MiRCART = MiRCART(*argv[1:])
|
wxApp = wx.App(False)
|
||||||
|
MiRCARTFrame(None)
|
||||||
|
wxApp.MainLoop()
|
||||||
if __name__ == "__main__":
|
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)
|
main(*sys.argv)
|
||||||
|
|
||||||
# vim:expandtab foldmethod=marker sw=8 ts=8 tw=120
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|
||||||
|
12
README.md
12
README.md
@ -1,4 +1,10 @@
|
|||||||
# mirc2png -- convert ASCII w/ mIRC control codes to monospaced PNG (for EFnet #MiRCART)
|
# MiRCART -- XXX
|
||||||
* Prerequisites: python3 && python3-pil (&& python3-{json,requests,urllib3} for IrcMiRCARTBot.py) on Debian-family Linux distributions
|
* Prerequisites: python3 && python-wx{gtk2.8,tools} on Debian-family Linux distributions
|
||||||
|
|
||||||
|
# IrcMiRCARTBot.py -- XXX
|
||||||
|
* Prerequisites: python3 && python3-{json,requests,urllib3} on Debian-family Linux distributions
|
||||||
* IrcMiRCARTBot.py usage: IrcMiRCARTBot.py `<IRC server hostname>` [`<IRC server port; defaults to 6667>`] [`<IRC bot nick name; defaults to pngbot>`] [`<IRC bot user name; defaults to pngbot>`] [`<IRC bot real name; defaults to pngbot>`] [`<IRC bot channel name; defaults to #MiRCART>`]
|
* IrcMiRCARTBot.py usage: IrcMiRCARTBot.py `<IRC server hostname>` [`<IRC server port; defaults to 6667>`] [`<IRC bot nick name; defaults to pngbot>`] [`<IRC bot user name; defaults to pngbot>`] [`<IRC bot real name; defaults to pngbot>`] [`<IRC bot channel name; defaults to #MiRCART>`]
|
||||||
* MiRCART.py usage: MiRCART.py `<MiRCART input file pathname>` `<PNG image output file pathname>` [`<Font file pathname; defaults to DejaVuSansMono.ttf>`] [`<Font size; defaults to 11>`]
|
|
||||||
|
# MiRC2png.py -- convert ASCII w/ mIRC control codes to monospaced PNG (for EFnet #MiRCART)
|
||||||
|
* Prerequisites: python3 && python3-pil on Debian-family Linux distributions
|
||||||
|
* MiRC2png.py usage: MiRC2png.py `<MiRCART input file pathname>` `<PNG image output file pathname>` [`<Font file pathname; defaults to DejaVuSansMono.ttf>`] [`<Font size; defaults to 11>`]
|
||||||
|
Loading…
Reference in New Issue
Block a user