Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Graphic Text, _Printstring & other stuffs
#1
Hi
here a little demo to solve a task: print a text into a graphic area without going out of borders...when you cannot use _fontwidth because the font is NOT monospace.
thanks for suggestions to Luke, RhoSigma and Steve.
Here has been used 4 ways to get the result...

1  using  _PRINTWIDTH in procedural way
2. using  _PRINTWIDTH & math
3. using  _UCHARPOS
4. using a graphical solution by _PUTIMAGE as a camera
5. using _UPRINTSTRING and setting max width of text displayed

Code: (Select All)
Rem Beginner's All-purpose Symbolic Instruction Code
Rem demo of text in graphic mode using _printstring and Font
Dim As Long Font1, Screen1, TextArea, PrintArea, CharPos, Index
ReDim CharsPos(0 To 200) As Long


Screen1 = _NewImage(1200, 600, 32) ' main window
TextArea = _NewImage(600, 400, 32) ' area that shows the text in graphic mode
PrintArea = _NewImage(1220, 600, 32) ' area on which the text has printed in graphic mode

Screen Screen1
' init font
rootpath$ = Environ$("SYSTEMROOT") 'normally "C:\WINDOWS"
fontfile$ = rootpath$ + "\Fonts\cour.ttf" 'TTF file in Windows
style$ = "bold" 'font style is not case sensitive
size = 40

again:
Font1 = _LoadFont(fontfile$, size, style$)
If Font1 Then _Font Font1 Else Print Font1; " Failure loading font"
' -- font has been loaded

' initialization window of application
_Dest 0
Cls , _RGB32(0, 0, 0, 255)

'initalization area of printing text by _printstring
_Dest PrintArea
Cls , _RGB32(127, 127, 227)

'initialization area showing text on the window
_Dest TextArea
Cls , _RGB32(55, 194, 78)
_PutImage (500, 100), TextArea, Screen1


' graphic stuff
_Dest 0
sentence$ = "Hello QB64pe in graphic text 32bit color mode with boxtext"
_PrintString (1, 20), sentence$ ' it writes in graphic mode on screen

' procedural way step by step to fit green area using _PRINTWIDTH()
tmp$ = sentence$
While _PrintWidth(tmp$) > _Width(TextArea)
    tmp$ = Left$(tmp$, Len(tmp$) - 1)
Wend
Color , _RGB32(0, 0, 0, 0)
_PrintString (501, 120), tmp$

' Procedural way step by step to fit green area using _UCHARPOS
ReDim CharsPos(0 To Len(sentence$)) As Long

CharPos = _UCharPos(sentence$, CharsPos(), , Font1)
For Index = UBound(CharsPos) To 0 Step -1
    If CharsPos(Index) < _Width(TextArea) Then Exit For
Next
_PrintString (501, 160), Mid$(sentence$, 1, Index)
'debug data print , these data showed will be used by math way
Locate 14, 1
Print _PrintWidth(sentence$) > _Width(TextArea), _PrintWidth(sentence$), _Width(TextArea), _Width(TextArea) / (_PrintWidth(sentence$) / Len(sentence$))
' math way to fit green area
_PrintString (501, 200), Mid$(sentence$, 1, Fix(_Width(TextArea) / (_PrintWidth(sentence$) / Len(sentence$))))

' graphic print way by _putimage
_Dest PrintArea
_Font Font1
Color , _RGB32(0, 0, 0, 0)
_PrintString (1, 1), sentence$
_PutImage (501, 240), PrintArea, Screen1, (1, 1)-(600, _FontHeight)
_Dest 0

' print graphic mode with _Uprintstring
_UPrintString (501, 280), sentence$, _Width(TextArea), , Font1

'loop for different sizes of font's character
If size > 16 Then
    size = size - 8
    Sleep 2
    GoTo again
End If
End
Reply




Users browsing this thread: 1 Guest(s)