How can we post ASCII art on Facebook wall?

2019-02-25 09:06发布

问题:

In my app, I have a requirement to post the ASCII art from my iPhone app onto the facebook wall post. But the problem I face is that facebook font (Lucida Console) Changes the formatting of my ASCII art. I have made my ASCII art in Courier New.

What can be done?

Is there a way I can post my ASCII art on facebook without having to re-format the whole thing?

Please Help and Suggest.

Thanks

回答1:

Courier is a monospaced font. That means, that every letter has the same space. Thats why it is easy to use for ASCII art and popular for coding — as words in with same lentgh will always be at same positions.

from facebook css:

font-family: 'lucida grande', tahoma, verdana, arial, sans-serif;

Lucida Grande is a propotional font. i.e. an i uses much less space than a m. Words in different lines won't match very well.

edit
Have a look at this facebook group. The users compensate the absence of a monospaced font by

  1. using just symbols with roughly the same width
  2. filling room with short symbols like .

monospaced
♥♥'''''''''''''''♥♥
♥♥'''''''''''''''♥♥

propotional
♥♥'''''''''''''''♥♥



回答2:

I came up with a Python script, only tested it with simple examples so far.

#!/usr/bin/python
'''
fbformat -- format ASCII for Facebook
'''
import sys, os
PRINTABLE = [' '] + map(chr, range(ord('!'), ord('~') + 1))
FB_ABLE = [u'\u3000'] + map(unichr, range(0xff01, 0xff5f))
TO_FB = dict(zip(PRINTABLE, FB_ABLE))
FROM_FB = dict(zip(FB_ABLE, PRINTABLE))
COMMAND = os.path.splitext(os.path.basename(sys.argv[0]))[0]
TEXT = sys.stdin.read().decode('utf8')
TO = ''.join([TO_FB.get(C, C) for C in TEXT])
FROM = ''.join([FROM_FB.get(C, C) for C in TEXT])
sys.stdout.write([TO, FROM][COMMAND == 'fbunformat'].encode('utf8'))

symlink it as ~/home/bin/fbformat and ~/home/bin/fbunformat, and make sure ~/home/bin is in your PATH.

enter the following as test.txt:

YES!
\o/
 |
/ \

then:

jcomeau@aspire:~/rentacoder/gdavis$ fbformat < /tmp/test.txt
YES!
\o/
 |
/ \
jcomeau@aspire:~/rentacoder/gdavis$ fbformat < /tmp/test.txt | fbunformat
YES!
\o/
 |
/ \

resources: http://www.cs.tut.fi/~jkorpela/chars/spaces.html and https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms