Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wordwrap (with unicode support!)
#1
Code: (Select All)
SCREEN _NEWIMAGE(800, 600, 32)
f = _LOADFONT("consola.ttf", 16, "monospace")
_FONT f

'test wordwrap with some ascii code
'the first character would normally be part of a 2-byte utf-8 code, which is why it's here for testing
WordWrap "Àabc123 1234567890.1234567890 123457890 1234567890 1234567890 abc123 1234567890.1234567890 123457890 1234567890 1234567890 abc123 1234567890.1234567890 123457890 1234567890 1234567890 ", 0

'and now test it with some utf-8 code
OPEN "textfile1.txt" FOR INPUT AS #1
FOR i = 1 TO 375
    LINE INPUT #1, utf8_test$
    IF i > 360 THEN WordWrap utf8_test$, 8
NEXT

WordWrap utf8_test$, 8

SUB WordWrap (text$, format AS LONG)
    SELECT CASE format
        CASE 0, 8, 16, 32
        CASE ELSE
            _MESSAGEBOX "Invalid Format", "Invalid format sent to WordWrap.", "error"
            EXIT SUB
    END SELECT
    temp$ = text$
    w = _WIDTH: h = _HEIGHT
    y = (CSRLIN - 1)
    IF _FONTWIDTH THEN 'monospace font
        x = (POS(0) - 1) * _FONTWIDTH
    ELSE 'variable width font
        x = POS(0)
    END IF
    wMAx = w - x 'the most width that we have left on this line to print to
    DO
        l = LEN(temp$)
        pw = _UPRINTWIDTH(temp$, format)
        IF pw < wMAx THEN 'if the printwidth is smaller than the available width on the line, print it
            _UPRINTSTRING (x, y * _UFONTHEIGHT), temp$, , format
            x = 0: y = y + 1
            LOCATE y + 1, 1
            EXIT DO
        ELSE 'we determine how much we can print, and print what we can in the available space
            cp = 1: bp = 0
            t$ = ""
            DO
                a = ASC(temp$, cp)
                SELECT CASE format
                    CASE 0 'ASCII
                        length = 1
                    CASE 8 'UTF-8
                        SELECT CASE a
                            CASE 0 TO 127: length = 1
                            CASE 128 TO 191: length = 2
                            CASE 192 TO 223: length = 2
                            CASE 224 TO 239: length = 3
                            CASE 240 TO 248: length = 4
                            CASE 252, 253: length = 5
                            CASE ELSE: length = 1 'we should never see these. Use alt text here
                        END SELECT
                    CASE 16 'UTF-16
                        length = 2 'not fully true, but we'll come back to this later
                    CASE 32 'UTF-32
                        length = 4
                END SELECT
                t$ = t$ + MID$(temp$, cp, length)
                IF _UPRINTWIDTH(t$, format) > wMAx THEN
                    t$ = LEFT$(temp$, bp) 'back up to the last breakpoint
                    temp$ = MID$(temp$, bp + 1) 'remove what we print from the string
                    _UPRINTSTRING (x, y * _UFONTHEIGHT), t$, , format 'print what we remove
                    x = 0: y = y + 1 'update the print position
                    LOCATE y + 1, 1
                    EXIT DO
                ELSE
                    SELECT CASE a 'valid breakpoints
                        CASE 10 'chr$(10) line ending
                        CASE 32: bp = cp 'space
                        CASE 46: bp = cp 'period .
                        CASE 44: bp = cp 'comma ,
                        CASE 45: bp = cp 'dash -
                        CASE 33: bp = cp 'exclaimation point !
                        CASE 63: bp = cp 'question mark ?
                        CASE 59: bp = cp 'semi-colon ;
                    END SELECT
                    cp = cp + length
                END IF
            LOOP

        END IF
    LOOP
END SUB

Use with the unicode test file below.  Smile

Note that this doesn't do page scrolling or such yet, but it *does* do word wrap and proper character displaying for utf-8 endcoded text!  

For me, personally, this is a huge step forward so that I can eventually add full unicode support to my own programs.  My _Keyhit library has allowed us to get unicode values from keypresses for ages now.  I just haven't had a good means to display those characters and wordwrap them and such.  This is the next step to doing just that.  

Eventually, I'll have a set of libraries which will give me full unicode support.  No need for CodePages and a 256 character limit like ASCII has.  I'll be able to do everything with all the characters that unicode (and my chosen font) can support!  Smile


Attached Files
.txt   TextFile1.txt (Size: 44.71 KB / Downloads: 26)
Reply




Users browsing this thread: 1 Guest(s)