Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved display of boxing characters using UTF8 box drawing characters #8

Open
m-bob opened this issue Sep 9, 2021 · 8 comments
Open

Comments

@m-bob
Copy link

m-bob commented Sep 9, 2021

Hi,

I apologize for the incomplete nature of this post, but I thought it might be helpful as-is and I don't know when I'll be able to improve it further.

I tried a number of ways to get QNial to use the UTF8 box drawing characters none of which worked. The "right" way to do it appears to involve a lot of effort to rewrite routines in the file picture.c .

But there's a very easy work-around that accomplishes the same thing with little effort. The key idea is simply for QNial to use the IBM box-drawing characters until "the last moment" and then replace them with the equivalent UTF8 characters just before output.

The changes needed are as follows. First, one needs to modify main_stu.c by adding the import

include <locale.h>

and then placing the line
setlocale (LC_ALL, "");
somewhere in main().
One should also tell QNial to use the IBM characters by changing the argument of the initboxchars () call to true:
initboxchars (true);

Next, one needs to modify the file unixif.c .
First, add the imports

include <wchar.h>

include "picture.h"

Second, before any executable code, insert
unsigned char ibm_boxing_chars [] = {ibm_luc, ibm_ruc, ibm_llc, ibm_rlc, ibm_ut, ibm_lt, ibm_rt, ibm_gt, ibm_cro, ibm_hor, ibm_ver};
short int u8_boxing_chars [] = {u'┌', u'┐', u'└', u'┘', u'┬', u'┴', u'┤', u'├', u'┼', u'─', u'│'};

The code looks for the characters in the array ibm_boxing_chars [] and replaces them with the corresponding characters in u8_boxing_chars [] .

Third, I used this simple function to find the characters to change:

int find_index (int c, unsigned char array [], int n)
{ for (int index = 0; index < n; ++index)
{ if (c == (int) array [index])
return index;
}
return -1;
}
Place that code wherever you like.

Fourth, and finally, change the routine writechars (). Here's my routine:

nialint
writechars(FILE * file, char buf, nialint n, int nlflag)
{
static int calls = 0; /
internal counter used for breakchecking */
if (keeplog && (file == stdout))
writelog(buf, n, nlflag);

/* every 25 calls check for a signal (approx one screen full) */
if (++calls > 25) {
calls = 0;
checksignal(NC_CS_OUTPUT);
}
{
int i;
// for (i = 0; i < n; i++)
// putc (buf[i], file);
for (i = 0; i < n; i++)
{ int k = (int) (unsigned char) buf [i];
int index = find_index (k, ibm_boxing_chars, 11);
if (index == -1) putc (buf [i], file);
else putwc (u8_boxing_chars [index], file);
}
if (nlflag)
putc('\n', file);
fflush(file);
}
return (n);
}

The only change is that the single call to putc () in the body of the output for loop is replaced with four lines of code and the surrounding braces. Everything else remains the same.

I haven't extensively tested this code (and certainly not with file output set to anything other than STDOUT), but it works in the cases I tried.

I hope this helps someone!

A nice thing about this approach is that it doesn't constrain the choice of terminal font: any fixed-width font which contains the UTF8 drawing characters can be used. I'm using this code on a Mac and do not need an IBM-specific font.

I haven't tried to determine the level of QNial support for unicode. Certainly full unicode support would be nice, and would likely make this work-around irrelevant.

@gibbonsja
Copy link
Collaborator

Thanks for taking the time and contributing this nice work, I will add it to the distribution and do some testing.

I will initially add a command line flag (-bc) to give users control of the feature.

John

@m-bob
Copy link
Author

m-bob commented Sep 12, 2021 via email

@m-bob
Copy link
Author

m-bob commented Sep 12, 2021 via email

@gibbonsja
Copy link
Collaborator

Hi,

Samples are really 16 bit integers, 2 bytes per sample. The sound buffer defines the number of channels, the sample rate for playing and the number of samples. The samples are packed into the sound buffer with the samples for each channel together.

Ignore the 0 argument. I made it a function, which requires arguments, but it probably should have been and expressino
Hope this helps.

John

@m-bob
Copy link
Author

m-bob commented Sep 13, 2021 via email

@gibbonsja
Copy link
Collaborator

gibbonsja commented Sep 13, 2021 via email

@gibbonsja
Copy link
Collaborator

I have put the sound library created by Stu Smith up on Nial Libraries.

@m-bob
Copy link
Author

m-bob commented Nov 28, 2021

Thanks so much! It'll be awhile before I can check it out, but I expect it'll be useful. And fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants