Advertisement
faubiguy

Smaracters (Faubi version 1.0)

Sep 19th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # Faubi implementation of smaracters. Version 1.0
  3. # Example usage: python smaracters.py "this is an example message" -f lowercase_latin.json
  4. # lowercase_latin.json: http://pastebin.com/1AsrDEkX
  5. import sys, argparse, json
  6.  
  7. argparser = argparse.ArgumentParser(description='Writes text using phpbb emoji (i.e. smaracters)')
  8.  
  9. argparser.add_argument('-l', '--line-length', type=int, default=40, help='Max number of emoji to display per line')
  10. argparser.add_argument('-s', '--space-length', type=int, default=1, help='Number of spaces to put between characters')
  11. argparser.add_argument('-f', '--char-file', action='append', help='A json file to load characters from')
  12. argparser.add_argument('message', help='The text to write with emoji. If not present will load from stdin')
  13.  
  14. args = argparser.parse_args()
  15.  
  16. letter_height = 0
  17.  
  18. characters = {}
  19.  
  20. if not args.char_file:
  21.     sys.exit('No character files specified')
  22.    
  23. # Load character data from supplied json files
  24. for json_filename in args.char_file:
  25.     try:
  26.         with open(json_filename) as json_file:
  27.             json_data = json.load(json_file)
  28.     except FileNotFoundError:
  29.         sys.exit('No such file: {0}'.format(json_filename))
  30.     except ValueError:
  31.         sys.exit('Unable to parse file: {0}'.format(json_filename))
  32.     try:
  33.         letter_height = max(letter_height, json_data['letter_height'])
  34.         for character, value in json_data['characters'].items():
  35.             if character not in characters:
  36.                 characters[character] = value
  37.     except IndexError:
  38.         sys.exit('Invalid data in file: {0}'.format(json_filename))
  39.        
  40. # Make all characters the same height by adding black rows to top of short characters
  41. for character in characters:
  42.     char_height = len(characters[character])
  43.     if char_height < letter_height:
  44.         char_width = len(characters[character][0])
  45.         characters[character] = [' '*char_width]*(letter_height-char_height) + characters[character]
  46.  
  47. emoji = {' ': ':D', '#': ':mrgreen:'}
  48.  
  49. if args.message:
  50.     message = args.message
  51. else:
  52.     message = sys.stdin.readline()[:-1]
  53.  
  54. current_line = ['']*letter_height
  55. current_length = 0
  56. new_length = 0
  57.  
  58. # Loop through once before printing anything to find invalid characters
  59. for character in message:
  60.     if character not in characters:
  61.         sys.exit('Unsupported character: {0}'.format(character))
  62.  
  63. # Loop through characters, adding each to the current line
  64. for index, character in enumerate(message):
  65.     char_lines = characters[character]
  66.    
  67.     char_width = len(char_lines[0])
  68.    
  69.     if char_width > args.line_length:
  70.         sys.exit('Character wider than line length limit: {0}'.format(character))
  71.        
  72.     new_length += char_width
  73.    
  74.     # Line break if new character would make line too long
  75.     if new_length > args.line_length:
  76.         for line_number, line in enumerate(current_line):
  77.             print(' '.join(emoji[character] for character in line))
  78.             current_line[line_number] = ''
  79.         print()
  80.         new_length = char_width
  81.     else:
  82.         # Add space before character if not at beginning of line
  83.         if index != 0:
  84.             for line_number in range(len(current_line)):
  85.                 current_line[line_number] += ' '*args.space_length
  86.             new_length += args.space_length
  87.    
  88.     # Add character to current line
  89.     for line_number, char_line in enumerate(char_lines):
  90.             current_line[line_number] += char_line
  91.    
  92.     current_length = new_length
  93.        
  94. # Print last line
  95. if current_line[0] != '':
  96.     for line_number, line in enumerate(current_line):
  97.         print(' '.join(emoji[character] for character in line))
  98.         current_line[line_number] = ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement