简体   繁体   中英

Print braille characters in ncurses

I attempted to print braille characters in ncurses.

This is my code:

#include <ncurses.h>

char *str = 
    " ⠁⠂⠃⠄⠅⠆⠇⠈⠉⠊⠋⠌⠍⠎⠏\n"
    "⠐⠑⠒⠓⠔⠕⠖⠗⠘⠙⠚⠛⠜⠝⠞⠟\n"
    "⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯\n"
    "⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿\n";


int main(int argc, const char *argv[]) {
    initscr();
    printw("%s", str);
    getch();
    printf("%s", curses_version());
    endwin();
    printf("%s", str);
    return 0;
}

The output is:

 ?~A?~B?~C?~D?~E?~F?~G?~H?~I?~J?~K?~L?~M?~N?~O
?~P?~Q?~R?~S?~T?~U?~V?~W?~X?~Y?~Z?~[?~\?~]?~^?~_
⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯
⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿

How do I output all characters correctly?

Update: I also tried printf which seems to work, addstr produces the same output as printw .

If I change the locale with setlocale(LC_ALL, ""); I get the output:

  A B C D E F G H I J K L M N O
 P Q R S T U V W X Y Z [ \ ] ^ _
⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯
⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿

With some experimentation, I found what is probably wrong:

  1. Your program uses the C locale by default. This assumes ASCII encoding. It's not a problem when you output multi-byte characters with stdio , because these functions just deliver the bytes as is to the console. But ncurses actually uses the locale, so it can know things like how many bytes make a character (important for exact positioning) etc. Change your program as following:

     #include <curses.h> #include <locale.h> int main(int argc, char *argv[]) { // initialize locale to system's default: setlocale(LC_ALL, ""); // now init and use curses ... 
  2. If you still get garbled output, your system's libncurses doesn't handle unicode. In that case, link ncursesw instead of ncurses and you should be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM