DAY 017: _MAPUNICODE
#1
As there's been several people asking about how to display extended characters (such as greek characters) onto the screen, I thought _MAPUNICODE would be a nice word for today.  Wink

First:  Download the attached unicode font file and extract it into your qb64pe root directory (the same one with the qb64pe executable).

Then copy and paste the code below into qb64pe, and run it:

Code: (Select All)
Screen _NewImage(1280, 720, 32)

f = _LoadFont("DejaVuSansMono.ttf", 24, "monospace")
_Font f

fw = _FontWidth: fh = _FontHeight 'font width, font height
cpl = _Width \ fw: cpp = _Height \ fh 'characters per line, characters per page


For y = 0 To cpp - 1
    For x = 0 To cpl - 1
        i = i + 1
        _PrintString (x * fw, y * fh), Chr$(i)
        If i = 255 Then GoTo skip 'show the first 255 that people are used to seeing usually.
    Next 'then exit...  This is just lazy formatting
Next
skip:
Sleep


'Just to show all the tons of different unicode symbols inside the unicode file...
i = 0
For y = 4 To cpp - 1
    For x = 0 To cpl - 1
        i = i + 1
        _MapUnicode i To 0
        _PrintString (x * fw, y * fh), Chr$(0)
        If i = 255 Then Sleep 'show the first 255 that people are used to seeing usually.
    Next 'then fill the page with extra junk!  (I dunno what any of these are supposed to be. :P )
Next
Sleep

'Greek codes are 913 to 969
'note that there is no greek symbol at 930, so that unicode value has been recycled to be something else.
'pay no attention to it, if you don't need it.  (And I don't think most people would.)


Cls
i = 912
Print "The Greek character set:"
For y = 1 To cpp - 1
    For x = 0 To cpl - 1
        i = i + 1
        _MapUnicode i To 0
        _PrintString (x * fw, y * fh), Chr$(0)
        If i = 969 Then GoTo done
    Next
Next
done:

'Latin is 256 to 328
i = 255
Locate 5, 1: Print "The Latin character set:"
For y = 5 To cpp - 1
    For x = 0 To cpl - 1
        i = i + 1
        _MapUnicode i To 0
        _PrintString (x * fw, y * fh), Chr$(0)
        If i = 328 Then GoTo done2
    Next
Next
done2:
Sleep
System


Running the above, we start out with a burst of characters across the screen that everyone should be familiar with -- the 255 ASCII characters that we see and use all the time in QB64!

Hit any key, and then you can see what the unicode characters from 1 to 255 look like.  A large portion of the first half maps directly over from our current ASCII set to the UNICODE set of characters (everything from 32 to 128), but, as you can see, there's no symbols defined in our font for unicode characters 1 to 31.  Those are/were control codes back in the day, and the vast majority of them were non-printing, so many unicode fonts don't have those characters in them.  Our ASCII code actually maps unicode symbols from other values, and puts it in those spots, to make the character set that we're used to seeing.  Wink

Now from 128 to 255, there's not a single character which matches up between the unicode values and our ASCII values.  We tend to, by default, map the character codes from codepage 437 into those spots, so that our codes will match the same ones QB45 and all other basics used to use by default.  Now, one problem with this is that we aren't offering a lot of the specialized characters (such as those with various accents and such), as we're offering the old character sets that were used to build boxes and such in old terminal screens. 

So, how would one fix that, if they wanted to display other symbols besides the ones we currently offer??

Hit any key in the demo, and you can see that there's a whole bunch of other characters inside the font we loaded!  Even though QB64 only makes use of 256 of those characters, that doesn't mean our font doesn't have a whole lot more than 256 that we can choose from!  (After all, if we couldn't use other characters, I'd never be able to create the demo that you're now viewing, now would I??)

To pick and choose our display set, we simply make use of the _MAPUNICODE command.  To easily get the most out of this command, I recommend the following steps:

1) Go to ✔️ ❤️ ★ Unicode Character Table (unicode-table.com) -- There's a ton of unicode symbols available out there.  Find which code corresponds to the character you want.
2) Find a current ASCII character that you're not using.  For my demo, I chose CHR$(0) for all my needs -- you can choose any of the 0 to 255 characters to replace that you want.
3) _MAPUNICDE unicode_number TO ascii_number

It's that simple!!

Now, whenever you need to display that character upon the screen, you can call upon it as needed.

PRINT "Hello World " + CHR$(150) '150 for whatever symbol/character you decided to map to 150.  Wink



Note that not every unicode font has every unicode character in it.  When you run my demo, you'll see several square boxes -- that's default for "Nope!  No symbol here!"  If the font you have doesn't support the language/symbols you need, then you'll just have to swap to a different font.  After all, you can't print characters that don't exist in your font set!  Wink


Attached Files
.zip   fonts.zip (Size: 312.35 KB / Downloads: 37)
Reply


Messages In This Thread
DAY 017: _MAPUNICODE - by SMcNeill - 11-22-2022, 03:23 PM
RE: DAY 017: _MAPUNICODE - by Pete - 11-22-2022, 05:32 PM
RE: DAY 017: _MAPUNICODE - by PhilOfPerth - 11-23-2022, 04:55 AM
RE: DAY 017: _MAPUNICODE - by PhilOfPerth - 11-23-2022, 05:06 AM
RE: DAY 017: _MAPUNICODE - by SMcNeill - 11-23-2022, 05:19 AM
RE: DAY 017: _MAPUNICODE - by gaslouk - 11-23-2022, 09:58 AM
RE: DAY 017: _MAPUNICODE - by bplus - 11-23-2022, 04:18 PM



Users browsing this thread: 4 Guest(s)