Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account
This post has been closed. You can still view previous posts, but you can't post any new replies.

Monospaced font solution for Arial Font using Python

In my previous post, I requested a monospaced font to play Hangman with my players, but it didn't get enough traction.

So, I wrote a Python script that outputs dashes and letters correctly spaced so that Arial font behaves like a monospaced font.

Currently, the script works only for capital letters and numbers.

To use it, copy and paste the output to the Roll20 battle map.

I used Hair width, Narrow width, and normal Space to generate white space.

If you encounter any issues, you can replace the spaces in the variables 

wids, Hair, Narrow, and Space from here

I am a self-taught coder, so those of you with more programming experience might be able to create a cleaner solution.

PS. All the value of wids were generated by generating checking the change in width of text box in inspect element at 100% zoom.  If you want to new characters, follow the following stesp

  1. Set zoom to 100%
  2. Add a textbox with Arial font 40 with text 'asdf'

  3. Start inspect element and locate the chat box

  4. Locate the element of the previously added text box

  5. Ensure that the texteditor_outline has a width of 140 px (see image below)

    1. If you get the width as 140 px, then all other values should be in wids should be same

    2. If not, then add 1 character at a time and see what is the new width (Note, do not delete the asdf)

  6. Note the new width value, character width is the new value - 140

  7. Update wids with this new information

  8. Update wrd variable with text you want to be monospaced

  9. Run the code

  10. Copy paste the output

  11. You can change font size to whatever you like

Notes

  1. I had trouble with the special spaces with python, so I chose use print function seems to work fine for me (see second image below) 
  2. With grid snapping ON, there is small offset between the dashes and letters, just hold the alt key and use arrow keys to adjust position
  3. Seems to do a decent enough job to serve my purpose
  4. I know it is boxing and not woxing
  5. Sorry if I have missed any steps


The code

import numpy as np
wids = {'A':26, 'B':26, 'C':28, 'D':28, 'E':26, 
        'F':24, 'G':31, 'H':28, 'I':11, 'J':20, 
        'K':26, 'L':22, 'M':33, 'N':28, 'O':31, 
        'P':26, 'Q':31, 'R':28, 'S':26, 'T':24, 
        'U':28, 'V':26, 'W':37, 'X':26, 'Y':26, 
        'Z':24, '0':22, '1':22, '2':22, '3':22, 
        '4':22, '5':22, '6':22, '7':22, '8':22, 
        '9':22, 'Hair':3, ' ':3,  'Space':11, 
        ' ':11, '_':22, '?':22, 'Narrow':8, ' ':8}
Narrow = ' '
Hair = ' '
Space = ' '
def totWids(s):
    tot = 0
    for c in s:
        tot = tot+wids[c]
    return tot
def dashes(s):
    das = ''
    for c in s:
        if c == ' ':
            das = das + '      '
        else:
            das = das + '  _  '
    return das
def whites(targ):
    dist = targ
    for hair in range(30):
        for narrow in range(30):
            for space in range(30):
                white = hair*wids['Hair']+narrow*wids['Narrow']+space*wids[' ']
                if abs(targ - white) < dist:
                    dist = abs(targ - white)
                    HAIR, NARROW, SPACE = hair, narrow, space
                if targ - white == 0:
                    return hair, narrow, space
#     print(abs(targ - white), dist)
    return HAIR, NARROW, SPACE
target = totWids('  _  ')
def mono(s):
    temp = ''
    spaced = np.array((0, 0, 0))
    balance = target/2
    widsTillNow = 0
    itr = 0
    for c in s:
        itr = itr + 1
        white = whites((balance - wids[c]))# + target/2 )
#         print(white)
        spaced = spaced + np.array(white)
        widsTillNow = widsTillNow + wids[c]
#         print (white, spaced)
        temp = temp + Hair*white[0]+Narrow*white[1]+Space*white[2]+c
        balance = (itr+0.5)*target - (sum(spaced*np.array([wids['Hair'], wids['Narrow'], wids['Space']])) + widsTillNow)
#         print(balance)
    return temp
wrd= 'THE FIVE BOXING WIZARDS JUMPED QUICKLY'#    
print(mono(wrd))
dashes(wrd)


August 23 (6 months ago)
[Deleted]
Pro
Marketplace Creator

Thanks for the suggestion!

After 30 days, Suggestions and Ideas with fewer than 10 votes are closed and the votes are refunded to promote freshness.

Your suggestion didn't build the right momentum this time, but feel free to submit it again! We find that the best suggestions describe the problem you are having, and the solution you want. You can learn more about the process of making suggestions on the Roll20 Wiki! More details can be found here.