Upside down text

夙愿已清 提交于 2019-12-02 05:49:22

Try this, a bit of a brute-force approach but works quite well for uppercase, lowercase and number characters - all other characters are presented just as they come:

(define upside-map '#hash(
  (#\a . #\ɐ) (#\b . #\q) (#\c . #\ɔ) (#\d . #\p) (#\e . #\ǝ) (#\f . #\ɟ)
  (#\g . #\ƃ) (#\h . #\ɥ) (#\i . #\ı) (#\j . #\ɾ) (#\k . #\ʞ) (#\l . #\ן)
  (#\m . #\ɯ) (#\n . #\u) (#\o . #\o) (#\p . #\d) (#\q . #\b) (#\r . #\ɹ)
  (#\s . #\s) (#\t . #\ʇ) (#\u . #\n) (#\v . #\ʌ) (#\w . #\ʍ) (#\x . #\x)
  (#\y . #\ʎ) (#\z . #\z) (#\A . #\∀) (#\B . #\𐐒) (#\C . #\Ɔ) (#\D . #\◖)
  (#\E . #\Ǝ) (#\F . #\Ⅎ) (#\G . #\⅁) (#\H . #\H) (#\I . #\I) (#\J . #\s)
  (#\K . #\⋊) (#\L . #\˥) (#\M . #\W) (#\N . #\N) (#\O . #\O) (#\P . #\Ԁ)
  (#\Q . #\Ό) (#\R . #\ᴚ) (#\S . #\S) (#\T . #\⊥) (#\U . #\∩) (#\V . #\Λ)
  (#\W . #\M) (#\X . #\X) (#\Y . #\⅄) (#\Z . #\Z) (#\0 . #\0) (#\1 . #\Ɩ)
  (#\2 . #\ᄅ) (#\3 . #\Ɛ) (#\4 . #\ㄣ) (#\5 . #\ϛ) (#\6 . #\9) (#\7 . #\ㄥ)
  (#\8 . #\8) (#\9 . #\6)))

(define (flip-string str)
  (list->string
   (map (lambda (c)
          (hash-ref upside-map c (const c)))
        (reverse (string->list str)))))

For example:

(flip-string "Hello World")
=> "pןɹoM oןןǝH"

For reference, I used this conversion table taken from Wikipedia. The above solution has a little wrinkle: I couldn't manage to make it work for the 𐐒 character (flipped B), with unicode value of #\u10412 - because it won't fit in a 16-bit unicode character, so it can't be represented. I wasn't aware that Racket doesn't support characters with an encoding requiring more than 16 bits.

Leo

first of all your website must support Unicode, Unicode consists thousands of characters, the first 127 of Unicode are ASCII. It is possible to create text that appears to be upside down by converting character by character to a Unicode sign that looks like the upside down version of the character, for example to convert "6" you can use "9" but the flipped version of "f" is "ɟ", which is a Latin character with Unicode number 607 (hex code 025F)

technically, you need two text area boxes, one for the original text and the other one for the flipped text, also you need a Javascript, use the onkeyup Javascript hook in the first text box to call an upsideDownText() function each time a key is released like this:

<textarea rows="5" cols="70" id="src" onkeyup="upsideDownText()"></textarea>

then do the text processing in the upsideDownText() Javascript function like this:

<script type="text/javascript">
function upsideDownText() {
  var srcText = document.getElementById( 'src' ).value.toLowerCase();
  var out = '';
  for( var i = srcText.length - 1; i >= 0; --i ) {
    var ch = srcText.charAt( i );
    if( ch == 'a' ) {
      out += '\u0250' }
    } else if( ch == 'b' ) {
      out += 'q' }
    } else if( ch == 'c' ) {
      out += '\u0254'
    // etc....
    } else {
      out += ch
    }
  }
  document.getElementById( 'dest' ).value = out;
}
</script>

get the content of the text box identified by id="src" and convert the string to lowercase using the toLowerCase() method. Then loop through the string, character by character, starting from the end of the string. A big if-then-else-if block handles the character conversion. finally push the converted string into the text box identified by id="dest", which is the lower text box.

you can find the full list of how doing this step by step from Source twiki.org

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!