Tiny Titler — Create Titles for Videos, etc.

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Tiny Titler — Create Titles for Videos, etc.

Post by chris319 »

This is an extremely simple program for creating titles for videos, etc.

It is row-and-page oriented. Pages are drawn in an off-screen image and are copied to a windowed screen. The off-screen image may be saved as a .bmp file by pressing the F12 key. Pressing the F1 key brings up a debug window describing the keyboard functions. Pressing the Esc key exits the program.

Content is entered manually within the source code. The user specifies the text string for each row, along with the x and y origin and font for each row.

The Page Up and Page Down keys increment and decrement the page number. The F2 key centers all rows horizontally. The F4 key vertically centers text on the page. Shift-arrow-down and shift-arrow-up move the entire page of text vertically. The F8 key toggles an alignment grid. The vertical centering function does not work perfectly so it may be necessary to use the shift-arrow keys to center text vertically using the alignment grid.

The program does not have an on-screen GUI so that the program's output can be used for live titling.

The program can be embellished and enhanced in many ways. It could be made interactive wherein text is entered directly on the screen, or page data could be read from a file.

The code may not be sold for financial gain or used in a product which is to be sold for financial gain.

Code: Select all

;Tiny Titler, a simple program for creating titles
;(C) Copyright 2014 Chris Clementson
;THE CODE CONTAINED HEREIN IS FREE. IT MAY NOT BE SOLD OR USED IN A PRODUCT WHICH IS
;TO BE SOLD FOR FINANCIAL GAIN, EITHER As SOURCE CODE OR IN ANY COMPILED FORM.
;THIS COPYRIGHT NOTICE MUST BE DISTRIBUTED IN ITS ENTIRETY ALONG WITH THE CODE CONTAINED HEREIN.

; 8/2/14

;ESC = QUIT PROGRAM
;F1 = HELP WINDOW
;F2 = CENTER ALL HORIZONTALLY
;F4 = VERTICAL CENTER
;F8 = GRID
;F12 = SAVE IMAGE
;PAGE UP = INCREMENT PAGE
;PAGE DOWN = DECREMENT PAGE

InitSprite(): InitKeyboard()
#COLS = 80: #ROWS = 25: #PAGES = 100

#MONITOR_V = 768 ;VERTICAL RESOLUTION OF MONITOR
Global screenWd = 1024, vPixels
Global row, page: Global imgWd = 1024, imgHt = 768, vCenter = imgHt / 2, hCenter = imgWd / 2
Global Dim bgColor(#PAGES)
Global scrnLeft = 192, scrnRight = 1727, gridFlag
Global Dim baseline(#NUMFONTS): Global Dim fontHeight(#NUMFONTS);#NUMFONTS=UP TO 22 FONTS
Global Dim rowXorg(#PAGES,#ROWS): Global Dim rowYorg(#PAGES,#ROWS)
Global Dim row$(#PAGES,#ROWS): Global Dim rowFont(#PAGES,#ROWS)
Global Dim rowHt(#PAGES,#ROWS): Global Dim rowColor(#PAGES,#ROWS)
Global Dim baseLn(#PAGES,#ROWS)

fontName$ = "Times New Roman"

fontHeight(1) = 72: result = LoadFont(1, fontName$, fontHeight(1))
If result = 0: MessageRequester("Error", "Unable to load "+fontName$) :EndIf
pointsize = fontHeight(1): fnt = LoadFont(#PB_Any, fontName$, -fontHeight(1))
Define TM.TEXTMETRIC ; http://msdn.microsoft.com/en-us/library/dd183499%28v=vs.85%29.aspx
font_obj = FontID(fnt)
HWnd = 0: hDC = GetDC_(HWnd) ; handle of your win here, 0 = desktop
     old_obj = SelectObject_(hDC, font_obj): GetTextMetrics_(hDC, @TM)
     SelectObject_(hDC, old_obj): baseline(1) = TM\tmAscent + TM\tmInternalLeading
     fontHeight(1) = TM\tmHeight + TM\tmExternalLeading
     
fontHeight(2) = 48: result = LoadFont(2, fontname$, fontHeight(2))
If result = 0: MessageRequester("Error", "Unable to load "+fontname$) :EndIf

pointsize = fontHeight(2): fnt = LoadFont(2, fontName$, pointsize)
Define TM.TEXTMETRIC ; http://msdn.microsoft.com/en-us/library/dd183499%28v=vs.85%29.aspx
     pointsize = fontHeight(2): fnt = LoadFont(#PB_Any, fontName$, -fontHeight(2))
     font_obj = FontID(fnt): HWnd = 0: hDC = GetDC_(HWnd) ; handle of your win here, 0 = desktop
     old_obj = SelectObject_(hDC, font_obj): GetTextMetrics_(hDC, @TM)
     SelectObject_(hDC, old_obj): baseline(2) = TM\tmAscent + TM\tmInternalLeading
     fontHeight(2) = TM\tmHeight + TM\tmExternalLeading
     
OpenWindow(0,0,0, screenWd, #MONITOR_V, "",#PB_Window_BorderLess)
OpenWindowedScreen(WindowID(0),0,0,imgWd,imgHt, #True,0,0,#PB_Screen_NoSynchronization)
ShowCursor_(#False)
CreateImage(0, imgWd, imgHt)

AddKeyboardShortcut(0, #PB_Shortcut_Escape, #PB_Shortcut_Escape) ;"ESCAPE" KEY TO QUIT PROGRAM
AddKeyboardShortcut(0, #PB_Shortcut_PageUp, #PB_Shortcut_PageUp) ;PAGE UP ADVANCES PAGE NUMBER
AddKeyboardShortcut(0, #PB_Shortcut_PageDown, #PB_Shortcut_PageDown) ;
AddKeyboardShortcut(0, #PB_Shortcut_F1, #PB_Shortcut_F1) ;F1 FOR HELP WINDOW
AddKeyboardShortcut(0, #PB_Shortcut_F2, #PB_Shortcut_F2) ;F2 H CENTER ROW
AddKeyboardShortcut(0, #PB_Shortcut_F4, #PB_Shortcut_F4) ;F4 VERTICAL CENTER
AddKeyboardShortcut(0, #PB_Shortcut_F8, #PB_Shortcut_F8) ;F8 GRID ON OFF
AddKeyboardShortcut(0, #PB_Shortcut_F12, #PB_Shortcut_F12) ;F12 SAVE IMAGE
AddKeyboardShortcut(0, #PB_Shortcut_Shift|#PB_Shortcut_Up,#PB_Shortcut_Up);SHIFT UP ARROW
AddKeyboardShortcut(0, #PB_Shortcut_Shift|#PB_Shortcut_Down,#PB_Shortcut_Down);SHIFT DOWN ARROW

Procedure DrawRow(page,row)
DrawingFont(rowFont(page,row))
DrawText(rowXorg(page,row),rowYorg(page,row), row$(page,row),rowColor(page,row))
EndProcedure

Procedure DrawPage(page)
StartDrawing(ImageOutput(0))
Box(0,0,imgWd,imgHt,bgColor(page))

If gridFlag = #True
For ct = 1 To 10
  Box(0,ct * (imgHt / 10),imgWd,2,$ffffff)
Next
For ct = 1 To 10
  Box(ct * (imgWd / 10),0,2,imgHt,$ffffff)
Next
Box(0,vCenter,imgWd,2,$00ff00);SCREEN V CENTER
Box(hCenter,0,2,imgHt,$00ff00) ;SCREEN H CENTER
EndIf

DrawingMode(#PB_2DDrawing_Transparent)
 
 For ct = 1 To #ROWS
   If row$(page,ct) <> #NULL$
     If rowFont(page,ct) = FontID(1):rowHt(page,ct) = fontHeight(1):baseln(page,ct)=baseline(1)
 ElseIf rowFont(page,ct) = FontID(2):rowHt(page,ct) = fontHeight(2):baseln(page,ct)=baseline(2)
   EndIf

   DrawRow(page,ct)
   EndIf
 Next

StopDrawing()
StartDrawing(ScreenOutput())
DrawImage(ImageID(0),0,0):StopDrawing():FlipBuffers()
EndProcedure

Procedure SaveImg()
  drawpage(page)
  filename$ = "page " + Str(page) + ".bmp"
  SaveImage(0, filename$)
EndProcedure

For page = 1 To #PAGES
For row = 1 To #ROWS
rowColor(page,row) = #White
Next
Next

;ENTER CONTENT BELOW
page = 1:row = 2
rowFont(page,row) = FontID(2)
rowYorg(page,row) = 200

row = 1: bgColor(page) = #Blue
rowYorg(page,row) = rowYorg(page,2)
rowFont(page,row) = FontID(1)

pg = 1: row$(pg,1) = "Pure Basic": row$(pg,2) = "My Title": bgColor(pg) = $ff0000
rowFont(pg,1) = FontID(1): rowFont(pg,2) = FontID(2): rowYorg(pg,2) = 200: rowYorg(pg,1) = 50

pg = 2
row$(pg,1) = "Another Page": row$(pg,2) = "Another Title": bgColor(pg) = $ff0000
rowFont(pg,1) = FontID(2): rowFont(pg,2) = FontID(1): rowYorg(pg,2) = 200: rowYorg(pg,1) = 50

pg = 3
row$(pg,1) = "Page Three": row$(pg,2) = "More Titles": bgColor(pg) = $ff0000
rowFont(pg,1) = FontID(2): rowFont(pg,2) = FontID(1): rowYorg(pg,2) = 200: rowYorg(pg,1) = 50

;WAIT FOR WINDOW EVENTS
Repeat
event = WaitWindowEvent()
  
  If event = #PB_Event_Repaint
DrawPage(page)

  ElseIf event = #PB_Event_Menu ;KEYBOARD INPUT
      menuItem = EventMenu()

      Select menuItem
          
        Case #PB_Shortcut_PageUp
          page + 1: DrawPage(page)
        
        Case #PB_Shortcut_PageDown
          If page > 1: page - 1: EndIf: DrawPage(page)
          
      Case #PB_Shortcut_F1; LOWER THIRD
        Debug "ESC Exit program"
        Debug "F2 Center all rows horizontally"
        Debug "F4 Vertical center page"
        Debug "F8 Grid"
        Debug "F12 Save image"
        
      Case #PB_Shortcut_F2 ;CENTER ALL ROWS HORIZONTALLY
        For ct = 1 To #ROWS
          If row$(page,ct)<> #NULL$
          StartDrawing(ImageOutput(0)):DrawingFont(rowFont(page,ct))
          rowXorg(page,ct) = (imgWd/2) - TextWidth(row$(page,ct)) / 2: StopDrawing()
          EndIf  
        Next
        
      Case #PB_Shortcut_F4; VERTICAL CENTER
        vPixels = 0: rowCount = 0
        For ct = 1 To #ROWS
If row$(page,ct) <> #NULL$: rowCount + 1
  vPixels + rowHt(page,ct)
EndIf
Next

rowYorg(page, rowCount) = (vcenter + (vPixels /2)) - rowHt(page,rowCount)

For ct = rowCount - 1 To 1 Step - 1:rowYorg(page,ct) = rowYorg(page,ct + 1)- rowHt(page,ct):Next
        
Case #PB_Shortcut_F8 ;GRID ON/OFF
        If gridFlag = #False: gridFlag = #True: Else: gridFlag = #False: EndIf: DrawPage(page)
        
Case #PB_Shortcut_F12;SAVE IMAGE
        SaveImg()
        
Case #PB_Shortcut_Up ;SHIFT PAGE UP
         For ct = 1 To #ROWS
          If row$(page,ct) <> #NULL$: rowYorg(page,ct) - 1:EndIf
          Next
        
Case #PB_Shortcut_Down ;SHIFT PAGE DOWN
         For ct = 1 To #ROWS
          If row$(page,ct) <> #NULL$: rowYorg(page,ct) + 1:EndIf
          Next
          
Case #PB_Shortcut_Escape
          FreeFont(1): FreeFont(2)
          If ImageID(0) <> 0: FreeImage(0): CloseWindow(0): End: EndIf ;ESCAPE KEY PRESSED
          End
                    
    EndSelect 
    DrawPage(page)
    
EndIf
ForEver