Draw w/max font size in rectangle using DrawVectorText("12")
Posted: Tue Jan 09, 2018 6:28 pm
				
				What is the best approach here?
Should I Loop VectorFont() size until some bounding box is crossed?
Has anyone tried this with the Vector lib?
I need a small single line text within a horizontal rectangle and a rotated -90° rectangle.
			Should I Loop VectorFont() size until some bounding box is crossed?
Has anyone tried this with the Vector lib?
I need a small single line text within a horizontal rectangle and a rotated -90° rectangle.
Code: Select all
Macro Min(x, y)
  (Bool((x) <= (y)) * (x) + Bool((y) < (x)) * (y))
EndMacro
Define.s r$ = "1234"
Define.d rWd, rHt, rX, rY
Define.d tWd, tHt, tX, tY
If OpenWindow(0, 0, 0, 400, 250, "DrawVectorText() in a box?", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 0, 0, 400, 250)
  LoadFont(0, "Segoe UI", 8, #PB_Font_Bold)
  If StartVectorDrawing(CanvasVectorOutput(0))
    ;/////////////////////////////////////////////////////
    ; Horizontal Rectangle
    ;/////////////////////////////////////////////////////
    rWd = 100
    rHt = 30
    AddPathBox(5, 5, rWd, rHt)     ; Horizontal Rectangle
    VectorSourceColor(RGBA(255, 0, 0, 255))
    StrokePath(1)
    
    VectorFont(FontID(0));, 20) ;<-- Loop VectorFont() size until some bounding box is crossed?
    tHt = VectorTextHeight(r$, #PB_VectorText_Visible)
    tWd = VectorTextWidth(r$, #PB_VectorText_Visible)
    MovePathCursor(5 + 100/2, 5 + 30/2)
    DrawVectorText(r$)
    ;DrawVectorParagraph(r$, 100, 30);, #PB_VectorParagraph_Center)
    
    ;/////////////////////////////////////////////////////
    ; Rectangle Rotated ±90°, Text Wd & Ht swapped.
    ;/////////////////////////////////////////////////////
    rWd = 30
    rHt = 100
    AddPathBox(150, 5, rWd, rHt)     ; Horizontal Rectangle
    VectorSourceColor(RGBA(255, 0, 0, 255))
    StrokePath(1)
    
    VectorFont(FontID(0));, 20)
    tWd = VectorTextHeight(r$, #PB_VectorText_Visible)
    tHt = VectorTextWidth(r$, #PB_VectorText_Visible)
    MovePathCursor(150 + rwd/2, 5 + rht/2)
    RotateCoordinates(PathCursorX(), PathCursorY(), -90)
    ;Debug "Rotated: " + StrD(PathCursorX()) + ", " + StrD(PathCursorY())
    DrawVectorText(r$)
    StrokePath(1, #PB_Path_SquareEnd)
    ResetCoordinates()
    
    StopVectorDrawing()
  EndIf
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf