Microsoft Small Basic

Program Listing: KTK906-3
' Measure Text Width in Pixels 0.5
' Copyright © 2014-2015 Nonki Takahashi. The MIT License.
'
' History:
' 2015-02-13 Added [All] button. (KTK906-3)
' 2015-01-13 Considered FontItalic. (KTK906-2)
' 2014-03-14 Considered FontBold. (KTK906-1)
' 2014-02-17 Adopted for Silverlight. (KTK906-0)
' 2014-02-13 Created. (KTK906)
'
GraphicsWindow.Title = "Measure Text Width in Pixels 0.5"
Not = "False=True;True=False"
WQ = Text.GetCharacter(34)
Init()
Main()
Sub Init
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "LightGray"
yc = 170
GraphicsWindow.FillRectangle(0, 0, gw, yc)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(10, 10, "Text")
txt = Controls.AddTextBox(50, 10)
Controls.SetTextBoxText(txt, "test")
GraphicsWindow.DrawText(10, 40, "Size")
size = Controls.AddTextBox(50, 40)
Controls.SetTextBoxText(size, GraphicsWindow.FontSize)
GraphicsWindow.DrawText(10, 70, "Font")
font = Controls.AddTextBox(50, 70)
Controls.SetTextBoxText(font, GraphicsWindow.FontName)
GraphicsWindow.DrawText(260, 10, "Bold")
bold = Controls.AddTextBox(300, 10)
Controls.SetTextBoxText(bold, GraphicsWindow.FontBold)
GraphicsWindow.DrawText(260, 40, "Italic")
italic = Controls.AddTextBox(300, 40)
Controls.SetTextBoxText(italic, GraphicsWindow.FontItalic)
GraphicsWindow.DrawText(10, 100, "Ratio")
all = Controls.AddTextBox(50, 100)
Controls.SetSize(all, 530, 22)
btn = Controls.AddButton("Measure", 10, 130)
btnAll = Controls.AddButton("Ratio", 80, 130)
GraphicsWindow.DrawText(260, 70, "Width")
width = Controls.AddTextBox(300, 70)
Controls.SetTextBoxText(width, 0)
clicked = "False"
Controls.ButtonClicked = OnButtonClicked
EndSub
Sub OnButtonClicked
clicked = "True"
EndSub
Sub Main
While "True"
If clicked Then
If Controls.LastClickedButton = btn Then
MeasureText()
Else
MeasureFont()
EndIf
clicked = "False"
Else
Program.Delay(100)
EndIf
EndWhile
EndSub
Sub Measure
' return px - width in pixel (px)
y = Math.Floor((y0 + y1) / 2)
For x = x0 To x1
color = GraphicsWindow.GetPixel(x, y)
If Not[Text.EndsWith(color, "FFFFFF")] Then
left = x
x = x1 ' break
EndIf
EndFor
For x = x1 To x0 Step -1
color = GraphicsWindow.GetPixel(x, y)
If Not[Text.EndsWith(color, "FFFFFF")] Then
right = x
x = x0 ' break
EndIf
EndFor
For x = right To x0 Step -1
color = GraphicsWindow.GetPixel(x, y)
If Text.EndsWith(color, "FFFFFF") Then
right = x + 1
x = x0 ' break
EndIf
EndFor
px = right - left
EndSub
Sub MeasureFont
alpha = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
n = Text.GetLength(alpha)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, yc, gw, gh - yc)
fs = 100
GraphicsWindow.FontSize = fs
GraphicsWindow.FontName = Controls.GetTextBoxText(font)
GraphicsWindow.FontBold = Controls.GetTextBoxText(bold)
GraphicsWindow.FontItalic = Controls.GetTextBoxText(italic)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(0, yc, "||")
y0 = yc
y1 = yc + fs
x0 = 0
x1 = fs * 2
If gw < x1 Then
x1 = gw - 1
EndIf
Measure()
px0 = px
For i = 1 To n
c = Text.GetSubText(alpha, i, 1)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, yc, gw, gh - yc)
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(0, yc + fs, gw, 1)
str = "|" + c + "|"
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(0, yc, str)
x1 = fs * Text.GetLength(str)
If gw < x1 Then
x1 = gw - 1
EndIf
Measure()
ratio[Text.GetCharacterCode(c)] = px - px0
EndFor
Controls.SetTextBoxText(all, "ratio = " + WQ + ratio + WQ)
EndSub
Sub MeasureText
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, yc, gw, gh - yc)
fs = Controls.GetTextBoxText(size)
GraphicsWindow.FontSize = fs
GraphicsWindow.FontName = Controls.GetTextBoxText(font)
GraphicsWindow.FontBold = Controls.GetTextBoxText(bold)
GraphicsWindow.FontItalic = Controls.GetTextBoxText(italic)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(0, yc, "||")
y0 = yc
y1 = yc + fs
x0 = 0
x1 = fs * 2
If gw < x1 Then
x1 = gw - 1
EndIf
Measure()
px0 = px
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, yc, gw, gh - yc)
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(0, yc + fs, gw, 1)
str = "|" + Controls.GetTextBoxText(txt) + "|"
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(0, yc, str)
x1 = fs * Text.GetLength(str)
If gw < x1 Then
x1 = gw - 1
EndIf
Measure()
Controls.SetTextBoxText(width, (px - px0))
EndSub