开发者

How can we post ASCII art on Facebook wall?

开发者 https://www.devze.com 2023-01-28 11:47 出处:网络
In my app, I have a requirement to pos开发者_Go百科t 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

In my app, I have a requirement to pos开发者_Go百科t 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.

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


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

From Facebook CSS:

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

Lucida Grande is a proportional font. i.e. an i uses much less space than an 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:

 `♥♥'''''''''''''''♥♥`  
 `♥♥'''''''''''''''♥♥`

Proportional:

♥♥'''''''''''''''♥♥


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

0

精彩评论

暂无评论...
验证码 换一张
取 消