Jump to content
Sign in to follow this  
seesoe

text wrap

Recommended Posts

seesoe

hello, im trying to wrap text so that i can print it on receipt paper from my thermal printer.

i am trying to achieve what was said in post 132

Share this post


Link to post
Share on other sites
PsaltyDS

hello, im trying to wrap text so that i can print it on receipt paper from my thermal printer.

i am trying to achieve what was said in post 132

If you want to hire someone to write it for you, see the Rent-A-Coder link in my sig.

If you want to write it yourself, post what you already have and a specific question about that code.

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
BrettF

Well if your using a monospace font, then split the string by character lengths and then reconstruct it... Otherwise you might need to get a total width for each line etc.

Share this post


Link to post
Share on other sites
seesoe

i have the following trimmed code, i am trying to wrap

Func print_client_order($raw_order_data)
    Local $mmssgg,$marginx,$marginy
    $hp = _PrintDllStart($mmssgg);this must always be called first
    if $hp = 0 then
         consolewrite("Error from dllstart = " & $mmssgg & @CRLF)
         Exit
     endif

    $PrinterName = _PrintGetPrinter($hp)
    _PrintSelectPrinter($hp,$PrinterName)

    _PrintPageOrientation($hp,1)
    _PrintStartPrint($hp)
    $pght = _PrintGetpageheight($hp) - _PrintGetYOffset($hp)
    $pgwd = _PrintGetpageWidth($hp) - _PrintGetXOffset($hp)
    $axisx = Round($pgwd * 0.8)
    $axisy = Round($pght * 0.8)

    $print_order_1 = StringTrimLeft($raw_order_data, 1)
    $print_order_2 = StringTrimRight($print_order_1, 1)
    $print_order = StringSplit($print_order_2,"~")

    $from_1 = StringSplit($print_order[3],"=")
    $from = StringSplit($from_1[2],",")
    $from_comments_header = "-----------------------COMMENTS-----------------------"
    $from_comments = $from[4]
    $from_comments_footer = "-----------------------------------------------------------"
        _PrintSetFont($hp,"Times New Roman",11,0,'bold')
            _PrintText($hp,$from_comments_header,Int($pgwd/2 - _PrintGetTextWidth($hp,$from_comments_header)/2),800)
        _PrintSetFont($hp,"Times New Roman",11,0,'')
            _PrintText($hp,$from_comments,Int($pgwd/2 - _PrintGetTextWidth($hp,$from_comments)/2),850,0)
            _PrintText($hp,$from_comments_footer,Int($pgwd/2 - _PrintGetTextWidth($hp,$from_comments_footer)/2),950)

for testing matters we can replace $from[4] with a long string that needs to be wraped

Share this post


Link to post
Share on other sites
Melba23

seesoe,

If you know the width of the page you want to print relative to the screen pixels, then this UDF which wraps the text might work for you. It was designed to wrap a long string to fit into messageboxes and labels with a set width, but I think you could adapt it to do what you want. The wrapped string itself is the [0] element of the returned array.

#include-once

#include <WindowsConstants.au3>
#include <SendMessage.au3>
#include <WinAPI.au3>

; #FUNCTION# =======================================================================================
;
; Name...........: _StringSize
; Description ...: Returns size of rectangle required to display string - width can be chosen
; Syntax ........: _StringSize($sText[, $iSize[, $iWeight[, $iAttrib[, $sName[, $iWidth]]]]])
; Parameters ....: $sText   - String to display
;                  $iSize   - Font size in points - default AutoIt GUI default
;                  $iWeight - Font weight (400 = normal) - default AutoIt GUI default
;                  $iAttrib - Font attribute (0-Normal, 2-Italic, 4-Underline, 8 Strike - default AutoIt
;                  $sName   - Font name - default AutoIt GUI default
;                  $iWidth  - [optional] Width of rectangle - default is unwrapped width of string
; Requirement(s) : v3.2.12.1 or higher
; Return values .: Success - Returns array with details of rectangle required for text:
;                  |$array[0] = String formatted with @CRLF at required wrap points
;                  |$array[1] = Height of single line in selected font
;                  |$array[2] = Width of rectangle required to hold formatted string
;                  |$array[3] = Height of rectangle required to hold formatted string
;                  Failure - Returns 0 and sets @error:
;                  |1 - Incorrect parameter type (@extended = parameter index)
;                  |2 - Failure to create GUI to test label size
;                  |3 - Failure of _WinAPI_SelectObject
;                  |4 - Font too large for chosen width - longest word will not fit
; Author ........: Melba23
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
;===================================================================================================
Func _StringSize($sText, $iSize = Default, $iWeight = Default, $iAttrib = Default, $sName = Default, $iWidth = 0)
    
    Local $hWnd, $hFont, $hDC, $oFont, $tSize, $hGUI, $hText_Label, $sTest_Line
    Local $iLine_Count, $iLine_Width, $iWrap_Count, $iLast_Word
    Local $asLines[1], $avSize_Info[4], $aiPos[4]
    
    ; Check parameters are correct type
    If Not IsString($sText) Then Return SetError(1, 1, 0)
    If Not IsNumber($iSize) And $iSize <> Default   Then Return SetError(1, 2, 0)
    If Not IsInt($iWeight)  And $iWeight <> Default Then Return SetError(1, 3, 0)
    If Not IsInt($iAttrib)  And $iAttrib <> Default Then Return SetError(1, 4, 0)
    If Not IsString($sName) And $sName <> Default   Then Return SetError(1, 5, 0)
    If Not IsNumber($iWidth) Then Return SetError(1, 6, 0)

    ; Create GUI to contain test labels, set to passed font parameters 
    $hGUI = GUICreate("", 1200, 500, 10, 10)
        If $hGUI = 0 Then Return SetError(2, 0, 0)
        GUISetFont($iSize, $iWeight, $iAttrib, $sName)
    
    ; Store unwrapped text
    $avSize_Info[0] = $sText
    
    ; Ensure EoL is @CRLF and break text into lines
    If StringInStr($sText, @CRLF) = 0 Then StringRegExpReplace($sText, "[x0a|x0d]", @CRLF)
    $asLines = StringSplit($sText, @CRLF, 1)
        
    ; Draw label with unwrapped lines to check on max width
    $hText_Label = GUICtrlCreateLabel($sText, 10, 10)
    $aiPos = ControlGetPos($hGUI, "", $hText_Label)
    GUICtrlDelete($hText_Label)
    
    ; Store line height for this font size after removing label padding (always 8)
    $avSize_Info[1] = ($aiPos[3] - 8)/ $asLines[0]
    ; Store width and height of this label
    $avSize_Info[2] = $aiPos[2]
    $avSize_Info[3] = $aiPos[3] - 4 ; Reduce margin
    
    ; Check if wrapping is required
    If $aiPos[2] > $iWidth And $iWidth > 0 Then
        
        ; Set returned text element to null
        $avSize_Info[0] = ""
        
        ; Set width element to max allowed
        $avSize_Info[2] = $iWidth
        
        ; Set line count to zero
        $iLine_Count = 0
        
        ; Take each line in turn
        For $j = 1 To $asLines[0]
            
            ; Size this line unwrapped
            $hText_Label = GUICtrlCreateLabel($asLines[$j], 10, 10)
            $aiPos = ControlGetPos($hGUI, "", $hText_Label)
            GUICtrlDelete($hText_Label)
            
            ; Check wrap status
            If $aiPos[2] < $iWidth Then
                ; No wrap needed so count line and store
                $iLine_Count += 1
                $avSize_Info[0] &= $asLines[$j] & @CRLF
            Else
                ; Wrap needed so need to count wrapped lines
                
                ; Create label to hold line as it grows
                $hText_Label = GUICtrlCreateLabel("", 0, 0)
                ; Initialise Point32 method
                $hWnd = ControlGetHandle($hGui, "", $hText_Label)
                $hFont = _SendMessage($hWnd, $WM_GETFONT)
                $hDC = _WinAPI_GetDC($hWnd)
                $oFont = _WinAPI_SelectObject($hDC, $hFont)
                If $oFont = 0 Then Return SetError(3, 0, 0)
                
                ; Zero counter
                $iWrap_Count = 0

                While 1
                    
                    ; Set line width to 0
                    $iLine_Width = 0
                    ; Initialise pointer for end of word
                    $iLast_Word = 0
                    
                    For $i = 1 To StringLen($asLines[$j])

                        ; Is this just past a word ending?
                        If StringMid($asLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1
                        ; Increase line by one character
                        $sTest_Line = StringMid($asLines[$j], 1, $i)
                        ; Place line in label
                        GUICtrlSetData($hText_Label, $sTest_Line)
                        ; Get line length
                        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sTest_Line)
                        $iLine_Width = DllStructGetData($tSize, "X")
                        
                        ; If too long exit the loop
                        If $iLine_Width >= $iWidth - Int($iSize / 2) Then ExitLoop
                    Next
                        
                    ; End of the line of text?
                    If $i > StringLen($asLines[$j]) Then
                        ; Yes, so add final line to count
                        $iWrap_Count += 1
                        ; Store line
                        $avSize_Info[0] &= $sTest_Line & @CRLF
                        ExitLoop
                    Else
                        ; No, but add line just completed to count
                        $iWrap_Count += 1
                        ; Check at least 1 word completed or return error
                        If $iLast_Word = 0 Then
                            GUIDelete($hGUI)
                            Return SetError(4, 0, 0)
                        EndIf
                        ; Store line up to end of last word
                        $avSize_Info[0] &= StringLeft($sTest_Line, $iLast_Word) & @CRLF
                        ; Strip string to point reached
                        $asLines[$j] = StringTrimLeft($asLines[$j], $iLast_Word)
                        ; Trim leading whitespace
                        $asLines[$j] = StringStripWS($asLines[$j], 1)
                        ; Repeat with remaining characters in line
                    EndIf
                    
                WEnd
                
                ; Add the number of wrapped lines to the count
                $iLine_Count += $iWrap_Count
                
                ; Clean up
                _WinAPI_ReleaseDC($hWnd, $hDC)
                GUICtrlDelete($hText_Label)
                
            EndIf
            
        Next
        
        ; Convert lines to pixels and add reduced margin
        $avSize_Info[3] = ($iLine_Count * $avSize_Info[1]) + 4
        
    EndIf

    ; Clean up
    GUIDelete($hGUI)
    
    ; Return array
    Return $avSize_Info

EndFunc ; => _StringSize

#cs

; StringSize Test example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

; This Test code includes the fully commented StringSize code
; Hence the need for these include files
#include <WindowsConstants.au3>
#include <SendMessage.au3>
#include <WinAPI.au3>
; These include lines are contained in the code of the UDF itself

Global $aFont[4] = ["Arial", "Tahoma", "Courier New", "Comic Sans MS"]
Global $aSize[4] = [9, 12, 10, 15]
Global $aWeight[4] = [200, 400, 600, 800]
Global $aAttrib[4] = [0, 2, 4, 0]
Global $aMaxWidth[4] = [250, 300, 400, 500]
Global $aColour[4] = [0xFFFF88, 0xBBFF88, 0xBB88FF, 0xFF88FF]
Global $aButMsg[4] = ["Press for next example", "Click here", "Please push", "And the next one please..."]

$sLabelMsg  = "This is a message with very long lines which are unlikely to fit within the maximum width specified.  " & _
"This UDF will return the size of rectangle needed to display it in a selected font and size." & @CRLF & @CRLF & _
"The GUI can then be sized to fit and accurately placed on screen.  Other controls, such as the automatically " & _
"sized button under this label, can also be accurately positioned relative to the text."

GUICreate("String Sizing Test", 500, 500)
GUISetState()

While 1
    
    $sFont = $aFont[Random(0, 3, 1)]
    $iSize = $aSize[Random(0, 3, 1)]
    $iWeight = $aWeight[Random(0, 3, 1)]
    $iAttrib = $aAttrib[Random(0, 3, 1)]
    $iMaxWidth = Random(200, 850, 1)
    $iColour = $aColour[Random(0, 3, 1)]
    $sButMsg = $aButMsg[Random(0, 3, 1)]
    
    $aButReturn = _StringSize($sButMsg)
    $aMsgReturn = _StringSize($sLabelMsg, $iSize, $iWeight, $iAttrib, $sFont, $iMaxWidth)
    $iError = @error
    
    If IsArray($aMsgReturn) = 1 Then
        
        WinMove("String Sizing Test", "", (@DesktopWidth - ($aMsgReturn[2] + 25)) / 2, (@DesktopHeight - ($aMsgReturn[3] + 85)) / 2, $aMsgReturn[2] + 25, $aMsgReturn[3] + 85 )
        
        $hLabel = GUICtrlCreateLabel($aMsgReturn[0], 10, 10, $aMsgReturn[2], $aMsgReturn[3], 1)
            GUICtrlSetFont(-1, $iSize, $iWeight, $iAttrib, $sFont)
            GUICtrlSetBkColor(-1, $iColour)
        $hNext = GUICtrlCreateButton($sButMsg, ($aMsgReturn[2] - $aButReturn[2]) / 2, $aMsgReturn[3] + 20, $aButReturn[2] + 20, 30)
        
        While 1
    
        $iMsg = GUIGetMsg()
        
        If $iMsg = -3 Then Exit
        
        If $iMsg = $hNext Then ExitLoop
    
        WEnd

        GUICtrlDelete($hNext)
        GUICtrlDelete($hLabel)
    Else
        MsgBox(0, "Error", "Code = " & $iError)
    EndIf

WEnd

Exit

; End of example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#ce

What units does your "$pgwd = _PrintGetpageWidth($hp) - _PrintGetXOffset($hp)" use?

Happy to help further.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
seesoe

interesting

i really dont understand how the page width and all that stuff works with the print udf, but i found this in the udf

;_PrintGetPageWidth
;parameters - $hDll, the handle for printmg.dll
;returns page width in tenths of a millimetre and @error = 0
;
;on failure returns -1 and @error = 1
Func _PrintGetPageWidth($hDll)
    $vDllAns = DllCall($hDll,'int','GetPageWidth')
    If @error = 0 Then Return $vDllAns[0]
    SetError(1)
    Return -1
    
EndFunc
Edited by seesoe

Share this post


Link to post
Share on other sites
seesoe

so we have page width from the print udf in millimeters, do i use the word wrapping udf for info on how to split the text or the actually split of the text? im not sure i get how to proceed in trying to write the code

Share this post


Link to post
Share on other sites
martin

so we have page width from the print udf in millimeters, do i use the word wrapping udf for info on how to split the text or the actually split of the text? im not sure i get how to proceed in trying to write the code

The print udf has functions to tell you the width and height of text. So if you have a string can select the next word, see if it will fit in the line and if it does then add it to the current line, if it doesn't fit then print the last line and start a new one.

I think Prg@ndy has written a printing function to deal with word wrapping using API calls.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
seesoe

@martin, is there any chance that you will ever be adding text wrap to your udf?

Edited by seesoe

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×