Now that I'm finally home and able to work on things once again, here's an updated version of QPrint, and a couple of screenshots showcasing it:
SUB uprint_extra
(BYVAL x&
, BYVAL y&
, BYVAL chars%&
, BYVAL length%&
, BYVAL kern&
, BYVAL do_render&
, txt_width&
, BYVAL charpos%&
, charcount&
, BYVAL colour~&
, BYVAL max_width&
) QPrintTextType = "ASCII"
QPrintTextType = "UTF8"
QPrint temp$
QPrintString x
* 30, y
* uheight
, CHR$(count
) count = count + 1
BreakPoint = ",./- ;:!" 'I consider all these to be valid breakpoints. If you want something else, change them.
IF QPrintTextType
= "ASCII" OR QPrintTextType
= "" THEN text$
= _TRIM$(AnsiTextToUtf8Text$
(temp$
)) ELSE text$
= temp$
count = -1
'first find the natural length of the line
p = uprintwidth(text$, i, 0)
'IF i < LEN(text$) THEN lineend = i - 1 ELSE
lineend = i
t$
= RTRIM$(LEFT$(text$
, lineend
)) 'at most, our line can't be any longer than what fits the screen. x = 1
clean_exit:
QPrintWidth
= uprintwidth
(out$
, LEN(out$
), 0)
QFontHeight = uheight
SUB QPrintString
(x
, y
, text$
) IF QPrintTextType
= "ASCII" OR QPrintTextType
= "" THEN temp$
= _TRIM$(AnsiTextToUtf8Text$
(text$
)) ELSE temp$
= text$
DIM chi&
, ascii%
, unicode&
, aci%
'--- get ANSI char code, reset Unicode ---
IF unicode&
= 0 THEN unicode&
= 65533 'replacement character AnsiTextToUtf8Text$ = AnsiTextToUtf8Text$ + UnicodeToUtf8Char$(unicode&)
'--- option _explicit requirements ---
DIM uc&
, first%
, remain%
, conti%
'--- UTF-8 encoding ---
'--- standard ASCII (0-127) goes as is ---
UnicodeToUtf8Char$
= CHR$(unicode&
) '--- encode the Unicode into UTF-8 notation ---
UnicodeToUtf8Char$ = "": uc& = unicode& 'avoid argument side effect
first% = &B10000000: remain% = 63
first%
= &B10000000
OR (first% \
2): remain%
= (remain% \
2) conti%
= &B10000000
OR (uc&
AND &B00111111
): uc&
= uc& \
64 UnicodeToUtf8Char$
= CHR$(conti%
) + UnicodeToUtf8Char$
first%
= (first%
OR uc&
): uc&
= 0 UnicodeToUtf8Char$
= CHR$(first%
) + UnicodeToUtf8Char$
Now, the first image above, is using QPrint to print Unicode (UTF-8) formatted text. The image on the left is test.txt opened in Microsoft Word, and the image on the right is what we generate using QPrint. As you can see, the font sizes and windows might be a little different resolution, but all the characters are displaying and matching properly for us. We're printing valid unicode to the screen.
The second image, you guys might recognize as our ASCII chart. It's the whole ASCII range of characters mapped over and converted from QB64's codepages over to UTF8 format, and then printed to the screen for us.
By default, QPrint is set to print ASCII-code pages, but it can be converted to use UTF-8 code pages with a simple variable change:
QPrintTextType = "ASCII" <-- This sets us to our default printing using the ASCII code page. (Or just leave it blank as "" does the same.)
QPrintTextType = <ANYTHING ELSE> and we try to print it as UTF-8 formatted text.
Change the global variable, change how you're printing...
It's now THAT simple to display UTF-8 code on to the screen.
NOTE: To use QPrint, you have to load a custom font with it. QB64's in-built fonts currently aren't working as you'd think they should with it, so be certain to load your own font.
Now all I need to do is tie these into my Extended Keyboard library, and add them to our functionality there, and we'll not only have a way to print the full UTF8 character set, but also a means to input from keyboards which use such extended keys. (That is, after someone takes the time to map the proper keys to their specific keyboard layout. At the moment, the library only supports US, German, Italian, and UK keyboard layouts, if I remember correctly.)
File test.txt is included below for those who'd like to test it for themselves. ;)