Skip to content

Instantly share code, notes, and snippets.

@ward
Created September 29, 2011 19:48
Show Gist options
  • Save ward/1251738 to your computer and use it in GitHub Desktop.
Save ward/1251738 to your computer and use it in GitHub Desktop.
[Not mine] Takes ASCII from stdin, shows it in fancy mathematical symbols
#!/usr/bin/env python
# vim: set fileencoding=UTF-8 :
#
# Copyright (c) 2010, Jonas Häggqvist <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the program nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import sys
import codecs
import locale
ALLRANGES = {
'bold': {
(65, 90) : 0x1D400,
(97, 122) : 0x1D41A,
(48, 57) : 0x1D7CE, # Digits
(0x391, 0x3A1) : 0x1D6A8, # ALPHA-RHO
(0x3A3, 0x3A9) : 0x1D6BA, # SIGMA-OMEGA
(0x3B1, 0x3C9) : 0x1D6C2, # alpha-omega
},
'italic': {
(65, 90) : 0x1D434,
(97, 103) : 0x1D44E, # a-g
(104, 104): 0x210E, # h
(105, 122): 0x1D456, # i-z
(0x391, 0x3A1) : 0x1D6E2, # ALPHA-RHO
(0x3A3, 0x3A9) : 0x1D6F4, # SIGMA-OMEGA
(0x3B1, 0x3C9) : 0x1D6FC, # alpha-omega
},
'bolditalic': {
(65, 90) : 0x1D468,
(97, 122) : 0x1D482,
(0x391, 0x3A1) : 0x1D71C, # ALPHA-RHO
(0x3A3, 0x3A9) : 0x1D72E, # SIGMA-OMEGA
(0x3B1, 0x3C9) : 0x1D736, # alpha-omega
},
'script': {
(65, 65) : 0x1D49C, # A
(66, 66) : 0x212C, # B
(67, 68) : 0x1D49E, # C-D
(69, 70) : 0x2130, # E-F
(71, 71) : 0x1D4A2, # G
(72, 72) : 0x210B, # H
(73, 73) : 0x2110, # I
(74, 75) : 0x1D4A5, # J-K
(76, 76) : 0x2112, # L
(77, 77) : 0x2133, # M
(78, 81) : 0x1D4A9, # N-Q
(82, 82) : 0x211B, # R
(83, 90) : 0x1D4AE, # S-Z
(97, 100) : 0x1D4B6, # a-d
(101, 101): 0x212F, # e
(102, 102): 0x1D4BB, # f
(103, 103): 0x210A, # g
(104, 110): 0x1D4BD, # h-n
(111, 111): 0x2134, # o
(112, 122): 0x1D4C5, # p-z
},
'boldscript': {
(65, 90) : 0x1D4D0,
(97, 122) : 0x1D4EA,
},
'fraktur': {
(65, 66) : 0x1D504, # A-B
(67, 67) : 0x212D, # C
(68, 71) : 0x1D507, # D-G
(72, 72) : 0x210C, # H
(73, 73) : 0x2111, # I
(74, 81) : 0x1D50D, # J-Q
(82, 82) : 0x211C, # R
(83, 89) : 0x1D516, # S-Y
(90, 90) : 0x2128, # Z
(97, 122) : 0x1D51E,
},
'boldfraktur': {
(65, 90) : 0x1D56C,
(97, 122) : 0x1D586,
},
'doublestruck': {
(65, 66) : 0x1D538, # A-B
(67, 67) : 0x2102, # C
(68, 71) : 0x1D53B, # D-G
(72, 72) : 0x210D, # H
(73, 77) : 0x1D540, # I-M
(78, 78) : 0x2115, # N
(79, 79) : 0x1D546, # O
(80, 81) : 0x2119, # P-Q
(82, 82) : 0x211D, # R
(83, 89) : 0x1D54A, # S-Y
(90, 90) : 0x2124, # Z
(97, 122) : 0x1D552,
(48, 57) : 0x1D7D8,
},
'sans-serif': {
(65, 90) : 0x1D5A0,
(97, 122) : 0x1D5BA,
(48, 57) : 0x1D7E2, # Digits
},
'boldsans-serif': {
(65, 90) : 0x1D5D4,
(97, 122) : 0x1D5EE,
(48, 57) : 0x1D7EC, # Digits
(0x391, 0x3A1) : 0x1D756, # ALPHA-RHO
(0x3A3, 0x3A9) : 0x1D768, # SIGMA-OMEGA
(0x3B1, 0x3C9) : 0x1D770, # alpha-omega
},
'italicsans-serif': {
(65, 90) : 0x1D608,
(97, 122) : 0x1D622,
},
'bolditalicsans-serif': {
(65, 90) : 0x1D63C,
(97, 122) : 0x1D656,
(0x391, 0x3A1) : 0x1D790, # ALPHA-RHO
(0x3A3, 0x3A9) : 0x1D7A2, # SIGMA-OMEGA
(0x3B1, 0x3C9) : 0x1D7AA, # alpha-omega
},
'monospace': {
(65, 90) : 0x1D670,
(97, 122) : 0x1D68A,
(48, 57) : 0x1D7F6, # Digits
},
}
def cursifychar(c, ranges):
n = ord(c)
for (fromrange, torange) in ranges:
dest = ranges[(fromrange, torange)]
if n >= fromrange and n <= torange:
return unichr(n + dest - fromrange)
return c
def cursify(s, ranges):
res = u""
for c in s:
res += cursifychar(c, ranges)
return res
if __name__ == "__main__":
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout);
sys.stdin = codecs.getreader(locale.getpreferredencoding())(sys.stdin);
line = "x"
if len(sys.argv) > 1 and sys.argv[1] in ALLRANGES:
ranges = ALLRANGES[sys.argv[1]]
else:
ranges = ALLRANGES['script']
while line != u"":
line = sys.stdin.readline()
line = cursify(line, ranges)
sys.stdout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment