Search code examples
pythonpython-3.xfontsioascii

Making ASCII art print on the same line


I'm making a program which takes a file with an ASCII art alphabet and uses it to print words. It takes 3 lines of input The width, The Height and The desired word. My problem is that I can make the characters print on the same line, they print one after the other like so:

Height: 8
Width: 9
Text: TOP
_________
\__   __/
   ) (   
   | |   
   | |   
   | |   
   | |   
   )_(   
 _______ 
(  ___  )
| (   ) |
| |   | |
| |   | |
| |   | |
| (___) |
(_______)
 _______ 
(  ____ )
| (    )|
| (____)|
|  _____)
| (      
| )      
|/       

This is my code:

temp = []
hi = input('Height: ')
wi = input('Width: ')
tx = input('Text: ')
fi = open("font.txt")
for i in tx:
  temp = cd[i]
  var1 = int(temp[0])
  ra1 = (var1 * int(hi))
  ra1n = (ra1 + int(hi))
  temp = []
  fi = open("font.txt")
  lines = fi.readlines()
  print(''.join(lines[ra1:ra1n]),end='')

font.txt looks like this

 _______ 
(  ___  )
| (   ) |
| (___) |
|  ___  |
| (   ) |
| )   ( |
|/     \|
 ______  
(  ___ \ 
| (   ) )
| (__/ / 
|  __ (  
| (  \ \ 
| )___) )
|/ \___/ 
 _______ 
(  ____ \
| (    \/
| |      
| |      
| |      
| (____/\
(_______/
 ______  
(  __  \ 
| (  \  )
| |   ) |
| |   | |
| |   ) |
| (__/  )
(______/ 
 _______ 
(  ____ \
| (    \/
| (__    
|  __)   
| (      
| (____/\
(_______/
 _______ 
(  ____ \
| (    \/
| (__    
|  __)   
| (      
| )      
|/       
 _______ 
(  ____ \
| (    \/
| |      
| | ____ 
| | \_  )
| (___) |
(_______)

|\     /|
| )   ( |
| (___) |
|  ___  |
| (   ) |
| )   ( |
|/     \|
_________
\__   __/
   ) (   
   | |   
   | |   
   | |   
___) (___
\_______/
_________
\__    _/
   )  (  
   |  |  
   |  |  
   |  |  
|\_)  )  
(____/   
 _       
| \    /\
|  \  / /
|  (_/ / 
|   _ (  
|  ( \ \ 
|  /  \ \
|_/    \/
 _       
( \      
| (      
| |      
| |      
| |      
| (____/\
(_______/
 _______ 
(       )
| () () |
| || || |
| |(_)| |
| |   | |
| )   ( |
|/     \|
 _       
( (    /|
|  \  ( |
|   \ | |
| (\ \) |
| | \   |
| )  \  |
|/    )_)
 _______ 
(  ___  )
| (   ) |
| |   | |
| |   | |
| |   | |
| (___) |
(_______)
 _______ 
(  ____ )
| (    )|
| (____)|
|  _____)
| (      
| )      
|/       
 _______ 
(  ___  )
| (   ) |
| |   | |
| |   | |
| | /\| |
| (_\ \ |
(____\/_)
 _______ 
(  ____ )
| (    )|
| (____)|
|     __)
| (\ (   
| ) \ \__
|/   \__/
 _______ 
(  ____ \
| (    \/
| (_____ 
(_____  )
      ) |
/\____) |
\_______)
_________
\__   __/
   ) (   
   | |   
   | |   
   | |   
   | |   
   )_(   

|\     /|
| )   ( |
| |   | |
| |   | |
| |   | |
| (___) |
(_______)

|\     /|
| )   ( |
| |   | |
( (   ) )
 \ \_/ / 
  \   /  
   \_/   

|\     /|
| )   ( |
| | _ | |
| |( )| |
| || || |
| () () |
(_______)

|\     /|
( \   / )
 \ (_) / 
  ) _ (  
 / ( ) \ 
( /   \ )
|/     \|

|\     /|
( \   / )
 \ (_) / 
  \   /  
   ) (   
   | |   
   \_/   
 _______ 
/ ___   )
\/   )  |
    /   )
   /   / 
  /   /  
 /   (_/\
(_______/

Solution

  • If your font has been properly padded (all lines are the same length) then you can use a list of lists that represent a matrix of lines and characters; you only assemble this into output to print after processing all letters of your text:

    hi = int(hi)
    output = [[] for _ in range(hi)]
    with open("font.txt") as fi:
        lines = fi.readlines()
    
    for character in tx:
        offset = int(cd[character])
        start = offset * hi
        end = start + hi
        letter = lines[start:end]
        for outputline, letterline in zip(output, letter):
            outputline.append(letterline.rstrip('\n'))
    
    for line in output:
        print(''.join(line))
    

    Note that you need to remove the newlines included in the font file; the str.rstrip() call takes care of that.

    Alternatively, you can remove all the newlines when reading the file:

    with open("font.txt") as fi:
        lines = fi.read().splitlines()
    

    If your font is not properly padded, you need to get the maximum width of any given character first and pad out the rest of the lines:

    width = max(len(l) for l in lines[start:end])
    letter = lines[start:end]
    for outputline, letterline in zip(output, letter):
        outputline.append(letterline.rstrip('\n').ljust(width))