8x8 ROM-PixelFont-Generator

  • Nachdem ich so ein Tool für eines meiner Projekte brauche, und ich in AutoIt lange nichts mehr geschrieben habe, dachte ich mir, ich staub mal SciTE ab :P

    Das Ganze ist auf die ersten 128 ASCII-Zeichen beschränkt, auf Wunsch kann ich es ja noch erweitern. Die Sonderzeichen (Die ersten 32 Zeichen + DEL können nicht bearbeitet werden und werden als Punkt-Zeichen abgespeichert.)
    Das Tool exportiert die Zeichen in C-Header-File, in ein 2-dimensionales Array (128 Zeichen zu je 8 Values). Jede der 8 Zeilen wird als einzelne hexadezimale Zahl (unsigned char, 8 Bit) abgespeichert, die einzelnen Bits repräsentieren eben dann das Zeichen.

    Code
    [autoit]

    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>
    #include <WinAPI.au3>
    #include <Array.au3>
    #include <String.au3>

    [/autoit] [autoit][/autoit] [autoit]

    OnAutoItExitRegister('_DoDishes')

    [/autoit] [autoit][/autoit] [autoit]

    Global $hGUI, $idMenu, $idMenu_ExportC
    Global $hGraphics, $hBrush
    Global $aFields[95][64]
    Global $aidLabels_Values[8]

    [/autoit] [autoit][/autoit] [autoit]

    $hGUI = GUICreate('8x8 ROM-Pixel-Font Generator', 300, 320, Default, Default, BitOR($GUI_SS_DEFAULT_GUI, $WS_VISIBLE))
    $idMenu = GUICtrlCreateMenu('File')
    $idMenu_ExportC = GUICtrlCreateMenuItem('Export To C-Header', $idMenu)
    $idMenu_Close = GUICtrlCreateMenuItem('Close', $idMenu)
    $idCombo_Characters = GUICtrlCreateCombo('Space', 20, 20, 260)
    GUICtrlSetData($idCombo_Characters, _GenCharList())
    $idDummyLabel = GUICtrlCreateLabel('', 20, 75, 200, 200)

    [/autoit] [autoit][/autoit] [autoit]

    _StartGDI()
    _DrawCharField()
    For $i = 0 To 7
    $aidLabels_Values[$i] = GUICtrlCreateLabel('0x00', 250, 82+($i*25))
    Next
    _InitFields()
    _UpdateLabels()

    [/autoit] [autoit][/autoit] [autoit]

    Global $aAccelKeys[2][2] = [['^s', $idMenu_ExportC], ['{ESC}', $idMenu_Close]]
    GUISetAccelerators($aAccelKeys)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    Switch GUIGetMsg()
    Case -3
    Exit
    Case $idMenu_ExportC
    _Export_To_C_Header()
    Case $idDummyLabel
    _FieldClickHandler()
    Case $idCombo_Characters
    _RedrawField()
    Case $idMenu_Close
    Exit
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _Export_To_C_Header()
    Local $p = '{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00 },', $r
    Local $t[33] = ['NUL', 'SOH', 'STX', 'ETX', 'EOT', 'ENQ', 'ACK', 'BEL', 'BS ', 'TAB', 'LF ', 'VT ', _
    'FF ', 'CR ', 'SO ', 'SI ', 'DLE', 'DC1', 'DC2', 'DC3', 'DC4', 'NAK', 'SYN', 'ETB', _
    'CAN', 'EM ', 'SUB', 'ESC', 'FS ', 'GS ', 'RS ', 'US ', 'DEL']
    Local $s = '/* Exported from 8x8 ROM-Pixel-Font Generator */' & @CRLF & @CRLF
    $s &= 'const unsigned char ROM_PixelFont[128][8] = {' & @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 0 To 31
    $s &= StringFormat(' %s /* %s */%s', $p, $t[$i], @CRLF)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 32 To 126
    $r = '{ '
    For $j = 1 To 7
    $r &= _ToHex(_GetRow(Chr($i), $j)) & ', '
    Next
    $r &= _ToHex(_GetRow(Chr($i), 8)) & ' },'
    $s &= StringFormat(' %s /* %s */%s', $r, Chr($i), @CRLF)
    Next
    $s &= StringFormat(' %s /* %s */%s};%s', $p, $t[32], @CRLF, @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    FileWrite("exported.h", $s)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _FieldClickHandler()
    Local $pos = _GetClientMousePos(), $lin, $index = _GetCurrentField()
    $pos[0] -= 20
    $pos[1] -= 75

    [/autoit] [autoit][/autoit] [autoit]

    $lin = _GetLinearField($pos)

    [/autoit] [autoit][/autoit] [autoit]

    If ($aFields[$index][$lin[2]]) Then
    _GDIPlus_GraphicsFillRect($hGraphics, $lin[0]*25+21, $lin[1]*25+76, 24, 24, $hBrush)
    $aFields[$index][$lin[2]] = 0
    Else
    _GDIPlus_GraphicsFillRect($hGraphics, $lin[0]*25+20, $lin[1]*25+75, 25, 25, 0)
    $aFields[$index][$lin[2]] = 1
    EndIf
    _UpdateLabels()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _RedrawField()
    _DrawCharField()
    _UpdateLabels()
    Local $i = _GetCurrentField(), $x = 21, $y = 76, $tmp[8]

    [/autoit] [autoit][/autoit] [autoit]

    For $k = 0 To 63 Step 8
    For $j = 0 To 7
    If ($aFields[$i][$k+$j]) Then _GDIPlus_GraphicsFillRect($hGraphics, $x, $y, 24, 24, 0)
    $x += 25
    Next
    $x = 21
    $y += 25
    Next
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _UpdateLabels()
    Local $i = _GetCurrentField(), $a[8]
    For $j = 0 To 63 Step 8
    For $k = 0 To 7
    $a[$k] = $aFields[$i][$j+$k]
    Next
    GUICtrlSetData($aidLabels_Values[$j/8], _ToHex($a))
    Next
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetRow($c, $n)
    Local $ret[8], $i = 0
    $n = ($n-1) * 8
    $c = Asc($c)-32
    For $j = $n To $n+7
    $ret[$i] = $aFields[$c][$j]
    $i += 1
    Next
    _ArrayReverse($ret)
    Return $ret
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetLinearField($pos)
    Local $ret = [-1, -1, 0]
    If (($pos[0] > 25) Or ($pos[1] > 25)) Then
    While ($pos[0] > 0)
    $pos[0] -= 25
    $ret[0] += 1
    WEnd
    While ($pos[1] > 0)
    $pos[1] -= 25
    $ret[1] += 1
    WEnd
    $ret[2] = ($ret[0] + ($ret[1] * 8))
    Else
    $ret[0] = 0
    $ret[1] = 0
    $ret[2] = 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Return $ret
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _InitFields()
    For $i = 0 To 94
    For $j = 0 To 63
    $aFields[$i][$j] = 0
    Next
    Next
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _DrawCharField()
    _GDIPlus_GraphicsFillRect($hGraphics, 20, 75, 200, 200, $hBrush)
    _GDIPlus_GraphicsDrawRect($hGraphics, 20, 75, 200, 200, 0)

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 45 To 220 Step 25
    _GDIPlus_GraphicsDrawLine($hGraphics, $i, 75, $i, 275, 0)
    _GDIPlus_GraphicsDrawLine($hGraphics, 20, $i+30, 220, $i+30, 0)
    Next
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _StartGDI()
    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _DoDishes()
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Shutdown()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetCurrentField()
    If (GUICtrlRead($idCombo_Characters) = 'Space') Then
    Return 0
    Else
    Return (Asc(GUICtrlRead($idCombo_Characters)) - 32)
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _GenCharList()
    Local $s = ''
    For $i = 33 To 126
    $s &= Chr($i) & '|'
    Next
    Return $s
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetClientMousePos()
    Local $pos = MouseGetPos()
    Local $point = DllStructCreate('int X;int Y')
    DllStructSetData($point, 'X', $pos[0])
    DllStructSetData($point, 'Y', $pos[1])
    _WinAPI_ScreenToClient($hGUI, $point)
    $pos[0] = DllStructGetData($point, 'X')
    $pos[1] = DllStructGetData($point, 'Y')
    Return $pos
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _ToHex($a)
    Local $ret = 0
    _ArrayReverse($a)
    For $i = 0 To 7
    $ret = BitOR($ret, BitShift($a[$i], -$i))
    Next
    $ret = StringFormat('0x%x', $ret)
    If (StringLen($ret) < 4) Then
    $ret = _StringInsert($ret, '0', 2)
    EndIf
    Return $ret
    EndFunc

    [/autoit]


    Ist kein Meisterwerk, da ich es heute Nachmittag nur mal schnell geschrieben habe.
    Vielleicht kann es wer brauchen, vielleicht auch nicht.

    (Das ganze hab ich mit 3.3.10.1 getestet, hatte die Version grade installiert.)

    Auf Wunsch kann ich auch noch ein paar andere Sachen einbauen, wie z.B. Laden von C-Header-Files. (Derzeit gibt es ja keinen vordefinierten Zeichensatz.)

    There's a joke that C has the speed and efficieny of assembly language combined with readability of....assembly language. In other words, it's just a glorified assembly language. - Teh Interwebz

    C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, you blow off your whole leg. - Bjarne Stroustrup
    Genie zu sein, bedeutet für mich, alles zu tun, was ich will. - Klaus Kinski

    2 Mal editiert, zuletzt von PainTain (15. August 2014 um 17:06)