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: 36)
Reply
#2
Ewwww I hates _MAP-UNEE-CODE! Cant's even gets a menu symbol to the title bar with the carnsarn idgit.

 - Sam from Sam-Clip.

Well settle down there, cowboy. the title bar may have certain restrictions placed in it by Windows. Since the title needs to be used in things like Task Manager, maybe the developers didn't want odd characters present. Now you can display a blank box, as Steve described. Try...

Code: (Select All)
title$ = CHR$(4) + " FOO"
_TITLE title$

The Windows system simply displays the empty box symbol, instead of the solid diamond character, which is CHR$(4). Now in Task manager, what do we see? We see just FOO.exe. The empty box symbol is ignored. Other windows symbols given to characters that won't show in the title bar are the = sign and the ? mark.

Honestly I don't know for certain that we cannot start a title for the title bar with non-numeric and non-alphabetical Unicode characters, but so far no luck in finding a method to make it so.

Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#3
@SMcNeill
Wow, it worked! I have my Copyright symbol as chr$(0)!
Very 'citing!  Big Grin
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply
#4
Next question is: Can I still continue to use the rest of my "normal" symbols as well, or would I have to map those back?
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply
#5
(11-23-2022, 05:06 AM)PhilOfPerth Wrote: Next question is: Can I still continue to use the rest of my "normal" symbols as well, or would I have to map those back?

You have the normal character set that you're used to using and seeing all the time.  _MAPCUNICODE only overwrites the characters that you tell it to -- nothing more.

So, in your case where you mapped the copyright symbol to chr$(0), *ONLY* chr$(0) changed.  All the rest of your ASCII codes are exactly the same as they've always been.  

If you just need a quick "swap out" for a brief moment to display one particular character, let me suggest this style trick:

old_code = _MAPUNICODE(ASCII_position) 'get the unicode value of your old ascii position
_MAPUNICODE new_unicode_value TO ASCII_position  'swap it out with the new unicode value you need

'do stuff with it

_MAPUNICODE(ASCII_position) = old_code 'swap it back to the old value


For example:

old_code = _MAPUNICODE(1) 'store whatever the ASCII character is for that cute little smiley face
_MAPUNICODE 169 TO  1 'your copyright symbol is now chr$(1), and our smiley face is gone...

PRINT CHR$(1) 'print that copyright symbol

_MAPUNICODE old_code TO 1 'map that old code value back, so CHR$(1) is our heroic smiley face once more!   Wink
Reply
#6
(11-23-2022, 05:19 AM)SMcNeill Wrote: Smile You have the normal character set that you're used to using and seeing all the time.  _MAPCUNICODE only overwrites the characters that you tell it to -- nothing more.

So, in your case where you mapped the copyright symbol to chr$(0), *ONLY* chr$(0) changed.  All the rest of your ASCII codes are exactly the same as they've always been.  

If you just need a quick "swap out" for a brief moment to display one particular character, let me suggest this style trick:

old_code = _MAPUNICODE(ASCII_position) 'get the unicode value of your old ascii position
_MAPUNICODE new_unicode_value TO ASCII_position  'swap it out with the new unicode value you need

'do stuff with it

_MAPUNICODE(ASCII_position) = old_code 'swap it back to the old value


For example:

old_code = _MAPUNICODE(1) 'store whatever the ASCII character is for that cute little smiley face
_MAPUNICODE 169 TO  1 'your copyright symbol is now chr$(1), and our smiley face is gone...

PRINT CHR$(1) 'print that copyright symbol

_MAPUNICODE old_code TO 1 'map that old code value back, so CHR$(1) is our heroic smiley face once more!

Smilethank you very much for this small but necessary help all of you. Smile 

Gaslouk.
Reply
#7
Thanks for this, I got stuck right on this word in Wiki looking into Phil's question after I printed a 'c' and drew a circle around it. Too much work, so little payoff. I was thinking of pulling out my character editor code and app. Nah! We don't need that just for a CR, Spriggsy has THE SOLUTION for quick CR symbol.

Good luck to @Gaslouk I admire those who take on coding in English.
b = b + ...
Reply




Users browsing this thread: 2 Guest(s)