-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.vb
More file actions
551 lines (496 loc) · 21.4 KB
/
Functions.vb
File metadata and controls
551 lines (496 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
Imports digitalSlate.World.Functions
Imports digitalSlate.World.mainClass
Imports digitalSlate.World.Vars.vDefaults
Imports Newtonsoft.Json
Imports System.Threading.Tasks
Imports System.IO
Imports System.Windows.Forms
Imports System.Media
Namespace World
Public Class Functions
Public Shared PendingExternalSlateFilePath As String = String.Empty
Public Shared Function IsSessionMetadataEnabled() As Boolean
Return World.vMain.sessionMetadataEnabled = 1
End Function
Public Shared Function GenerateNewSessionId() As String
Return Guid.NewGuid().ToString("N")
End Function
Public Shared Sub ApplySessionMetadataPolicy()
If IsSessionMetadataEnabled() Then
If String.IsNullOrWhiteSpace(World.vMain.sessionId) Then
World.vMain.sessionId = GenerateNewSessionId()
End If
Else
World.vMain.sessionId = String.Empty
World.vMain.unitName = String.Empty
World.vMain.operatorName = String.Empty
End If
End Sub
Public Shared Sub loadFromSettings()
Try
World.vMain.scene = My.Settings.cfgScene
World.vMain.scenePre = My.Settings.cfgScenePre
World.vMain.sceneNum = My.Settings.cfgSceneNum
World.vMain.shot = My.Settings.cfgShot
World.vMain.take = My.Settings.cfgTake
World.vMain.roll = My.Settings.cfgRoll
World.vMain.cameraNum = My.Settings.cfgCameraNum
World.vMain.camCardNum = My.Settings.cfgCamCardNum
World.vMain.production = My.Settings.cfgProduction
World.vMain.director = My.Settings.cfgDirector
World.vMain.dop = My.Settings.cfgDOP
World.vMain.fps = My.Settings.cfgFPS
World.vMain.custDate = My.Settings.cfgCustDate
World.vMain.currentDate = My.Settings.cfgCurrentDate
World.vMain.int = My.Settings.cfgInt
World.vMain.day = My.Settings.cfgDay
World.vMain.sync = My.Settings.cfgSync
World.vMain.beepCount = My.Settings.cfgBeepCount
World.vMain.skipSound = My.Settings.cfgSkipSound
World.vMain.countdownCount = My.Settings.cfgCountdownCount
World.vMain.displayCaps = My.Settings.cfgDisplayCaps
World.vMain.slateScaleMultiplier = If(My.Settings.cfgSlateScaleMultiplier > 0, My.Settings.cfgSlateScaleMultiplier, World.vDefaults.slateScaleMultiplier)
World.vMain.ltcEnabled = My.Settings.cfgLtcEnabled
World.vMain.ltcFpsMode = My.Settings.cfgLtcFpsMode
World.vMain.ltcOutputDeviceId = My.Settings.cfgLtcOutputDeviceId
World.vMain.ltcUnmute = My.Settings.cfgLtcUnmute
World.vMain.showCountdownNumbers = My.Settings.cfgShowCountdownNumbers
World.vMain.alwaysFullPreroll = My.Settings.cfgAlwaysFullPreroll
World.vMain.metadataFlashFpsEnabled = My.Settings.cfgMetadataFlashFpsEnabled
World.vMain.metadataFlashDateEnabled = My.Settings.cfgMetadataFlashDateEnabled
World.vMain.logOutToFile = My.Settings.cfgLogOutToFile
World.vMain.logOutputFolder = My.Settings.cfgLogOutputFolder
World.vMain.markerAppendDaily = My.Settings.cfgMarkerAppendDaily
World.vMain.sessionId = My.Settings.cfgSessionId
World.vMain.unitName = My.Settings.cfgUnitName
World.vMain.operatorName = My.Settings.cfgOperatorName
World.vMain.sessionMetadataEnabled = My.Settings.cfgSessionMetadataEnabled
ApplySessionMetadataPolicy()
Catch ex As Exception
MessageBox.Show("Error loading settings: " & ex.Message)
End Try
End Sub
Public Shared Sub SaveToSettings()
Try
My.Settings.cfgScene = World.vMain.scene
My.Settings.cfgScenePre = World.vMain.scenePre
My.Settings.cfgSceneNum = World.vMain.sceneNum
My.Settings.cfgShot = World.vMain.shot
My.Settings.cfgTake = World.vMain.take
My.Settings.cfgRoll = World.vMain.roll
My.Settings.cfgCameraNum = World.vMain.cameraNum
My.Settings.cfgCamCardNum = World.vMain.camCardNum
My.Settings.cfgProduction = World.vMain.production
My.Settings.cfgDirector = World.vMain.director
My.Settings.cfgDOP = World.vMain.dop
My.Settings.cfgFPS = World.vMain.fps
My.Settings.cfgCustDate = World.vMain.custDate
My.Settings.cfgCurrentDate = World.vMain.currentDate
My.Settings.cfgInt = World.vMain.int
My.Settings.cfgDay = World.vMain.day
My.Settings.cfgSync = World.vMain.sync
My.Settings.cfgBeepCount = World.vMain.beepCount
My.Settings.cfgSkipSound = World.vMain.skipSound
My.Settings.cfgCountdownCount = World.vMain.countdownCount
My.Settings.cfgDisplayCaps = World.vMain.displayCaps
My.Settings.cfgSlateScaleMultiplier = World.vMain.slateScaleMultiplier
My.Settings.cfgLtcEnabled = World.vMain.ltcEnabled
My.Settings.cfgLtcFpsMode = World.vMain.ltcFpsMode
My.Settings.cfgLtcOutputDeviceId = World.vMain.ltcOutputDeviceId
My.Settings.cfgLtcUnmute = World.vMain.ltcUnmute
My.Settings.cfgShowCountdownNumbers = World.vMain.showCountdownNumbers
My.Settings.cfgAlwaysFullPreroll = World.vMain.alwaysFullPreroll
My.Settings.cfgMetadataFlashFpsEnabled = World.vMain.metadataFlashFpsEnabled
My.Settings.cfgMetadataFlashDateEnabled = World.vMain.metadataFlashDateEnabled
My.Settings.cfgLogOutToFile = World.vMain.logOutToFile
My.Settings.cfgLogOutputFolder = World.vMain.logOutputFolder
My.Settings.cfgMarkerAppendDaily = World.vMain.markerAppendDaily
My.Settings.cfgSessionMetadataEnabled = World.vMain.sessionMetadataEnabled
If IsSessionMetadataEnabled() Then
My.Settings.cfgSessionId = World.vMain.sessionId
My.Settings.cfgUnitName = World.vMain.unitName
My.Settings.cfgOperatorName = World.vMain.operatorName
Else
My.Settings.cfgSessionId = String.Empty
My.Settings.cfgUnitName = String.Empty
My.Settings.cfgOperatorName = String.Empty
End If
My.Settings.Save()
Catch ex As Exception
MessageBox.Show("Error saving settings: " & ex.Message)
End Try
End Sub
Public Shared Sub loadToForm(source As Form)
Dim myParse As New Functions()
If source.Name.ToString() = "frmDigitalSlate" Then
refreshSlate()
ElseIf source.name.ToString() = "frmEdit" Then
With frmEdit
'Send to Editor outputs
.txtScene.Text = World.vMain.scene
.txtSceneNum.Text = CStr(World.vMain.sceneNum)
.txtShot.Text = World.vMain.shot
.txtTake.Text = CStr(World.vMain.take)
.txtRoll.Text = World.vMain.roll
.txtCameraNum.Text = World.vMain.cameraNum
.txtCardNum.Text = CStr(World.vMain.camCardNum)
.txtProduction.Text = World.vMain.production
.txtDirector.Text = World.vMain.director
.txtDOP.Text = World.vMain.dop
.txtSessionId.Text = World.vMain.sessionId
.txtUnitName.Text = World.vMain.unitName
.txtOperatorName.Text = World.vMain.operatorName
.txtFPS.Text = CStr(World.vMain.fps)
myParse.parseScene()
If Not String.IsNullOrEmpty(World.vMain.currentDate) And String.IsNullOrEmpty(World.vMain.custDate) Then
.cbTodaysDate.Checked = True
.txtCustDate.Text = ""
ElseIf Not String.IsNullOrEmpty(World.vMain.custDate) Then
.cbTodaysDate.Checked = False
.txtCustDate.Text = World.vMain.custDate
End If
If World.vMain.int = 1 Then .rbInt.Checked = True
If World.vMain.int = 0 Then .rbExt.Checked = True
If World.vMain.day = 1 Then .rbDay.Checked = True
If World.vMain.day = 0 Then .rbNite.Checked = True
If World.vMain.sync = 1 Then .rbSync.Checked = True
If World.vMain.sync = 0 Then .rbMOS.Checked = True
End With
End If
End Sub
Public Shared Sub resetSlate()
' Reset slate state (World.vMain) to defaults.
' Intentionally DO NOT touch app/user preferences (beepCount, countdownCount, displayCaps).
World.vMain.custDate = World.vDefaults.custDate
World.vMain.scenePre = String.Empty
World.vMain.sceneNum = World.vDefaults.sceneNum
World.vMain.shot = World.vDefaults.shot
World.vMain.take = World.vDefaults.take
World.vMain.cameraNum = World.vDefaults.cameraNum
World.vMain.camCardNum = World.vDefaults.camCardNum
World.vMain.production = World.vDefaults.production
World.vMain.director = World.vDefaults.director
World.vMain.dop = World.vDefaults.dop
World.vMain.fps = World.vDefaults.fps
World.vMain.int = World.vDefaults.int
World.vMain.day = World.vDefaults.day
World.vMain.sync = World.vDefaults.sync
World.vMain.tcTimerGo = 0
If IsSessionMetadataEnabled() Then
World.vMain.sessionId = GenerateNewSessionId()
Else
World.vMain.sessionId = String.Empty
World.vMain.unitName = String.Empty
World.vMain.operatorName = String.Empty
End If
updateScene()
updateRoll()
With frmDigitalSlate
refreshSlate()
.lblTimecode.Text = World.vDefaults.zeroTC
.tsiZeroTC.Enabled = True
If .WindowState = FormWindowState.Normal Then
.WindowState = FormWindowState.Maximized
End If
End With
End Sub
Public Shared Sub refreshSlate()
Dim useCaps As Boolean = (World.vMain.displayCaps = 1)
Dim applyCaps As Func(Of String, String) = Function(value As String) If(useCaps, If(value, String.Empty).ToUpperInvariant(), If(value, String.Empty))
With frmDigitalSlate
.lblProduction.Text = applyCaps(World.vMain.production)
.lblDirector.Text = applyCaps(World.vMain.director)
.lblDOP.Text = applyCaps(World.vMain.dop)
.lblFPS.Text = CStr(World.vMain.fps)
.lblTake.Text = World.vMain.take.ToString()
.lblHideInt.Visible = False
.lblHideExt.Visible = False
.lblHideDay.Visible = False
.lblHideNite.Visible = False
.lblHideSync.Visible = False
.lblHideMos.Visible = False
If World.vMain.custDate = "" Then
.lblDate.Text = applyCaps(World.vMain.currentDate)
Else
.lblDate.Text = applyCaps(World.vMain.custDate)
End If
updateScene()
updateRoll()
.lblRoll.Text = applyCaps(World.vMain.roll)
.lblScene.Text = applyCaps(World.vMain.scene)
If World.vMain.int = 0 Then .lblHideInt.Visible = True
If World.vMain.int = 1 Then .lblHideExt.Visible = True
If World.vMain.day = 0 Then .lblHideDay.Visible = True
If World.vMain.day = 1 Then .lblHideNite.Visible = True
If World.vMain.sync = 0 Then .lblHideSync.Visible = True
If World.vMain.sync = 1 Then .lblHideMos.Visible = True
AdjustFontSizeToFitText(.lblScene)
AdjustFontSizeToFitText(.lblTake)
AdjustFontSizeToFitText(.lblRoll)
AdjustFontSizeToFitText(.lblProduction)
AdjustFontSizeToFitText(.lblDirector)
AdjustFontSizeToFitText(.lblDOP)
End With
End Sub
Public Shared Sub updateScene()
Dim paddedScNum As String = World.vMain.sceneNum.ToString("D2")
World.vMain.scene = String.Concat(World.vMain.scenePre, paddedScNum, World.vMain.shot)
End Sub
Public Shared Sub updateRoll()
Dim paddedCard As String = World.vMain.camCardNum.ToString("D3")
World.vMain.roll = String.Concat(World.vMain.cameraNum, paddedCard)
End Sub
' Flash message on lblTimecode for a duration (ms)
Private Shared Async Function FlashTimecodeAsync(message As String, durationMs As Integer) As Task
Try
Dim prevText As String = String.Empty
If frmDigitalSlate IsNot Nothing AndAlso frmDigitalSlate.lblTimecode IsNot Nothing Then
prevText = frmDigitalSlate.lblTimecode.Text
frmDigitalSlate.lblTimecode.Text = message
Await Task.Delay(durationMs)
If frmDigitalSlate IsNot Nothing AndAlso frmDigitalSlate.lblTimecode IsNot Nothing Then
frmDigitalSlate.lblTimecode.Text = prevText
End If
End If
Catch ex As Exception
System.Diagnostics.Debug.WriteLine($"FlashTimecodeAsync error: {ex.Message}")
End Try
End Function
' Save current slate using SaveFileDialog (1 file per slate). Disallowed while Timer1 is running.
Public Shared Async Sub SaveSlateWithDialog()
Try
' Prevent save while running
If frmDigitalSlate.Timer1.Enabled Then
MessageBox.Show("Cannot save while timecode is running.", "Save disabled", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
Using sfd As New SaveFileDialog()
sfd.Filter = "Clapper files (*.clap)|*.clap|All files (*.*)|*.*"
sfd.DefaultExt = "clap"
sfd.AddExtension = True
sfd.Title = "Save slate configuration"
If sfd.ShowDialog() = DialogResult.OK Then
' Build strongly-typed profile (numeric types used directly)
Dim settings As New SettingsProfile With {
.Scene = World.vMain.scene,
.ScenePre = World.vMain.scenePre,
.SceneNum = World.vMain.sceneNum,
.Shot = World.vMain.shot,
.Take = World.vMain.take,
.Roll = World.vMain.roll,
.CameraNum = World.vMain.cameraNum,
.CamCardNum = World.vMain.camCardNum,
.Production = World.vMain.production,
.Director = World.vMain.director,
.DOP = World.vMain.dop,
.FPS = World.vMain.fps,
.CustDate = World.vMain.custDate,
.CurrentDate = World.vMain.currentDate,
.Int = World.vMain.int,
.Day = World.vMain.day,
.Sync = World.vMain.sync,
.LtcEnabled = World.vMain.ltcEnabled,
.LtcFpsMode = World.vMain.ltcFpsMode,
.LtcOutputDeviceId = World.vMain.ltcOutputDeviceId,
.LtcUnmute = World.vMain.ltcUnmute,
.SkipSound = World.vMain.skipSound,
.ShowCountdownNumbers = World.vMain.showCountdownNumbers,
.AlwaysFullPreroll = World.vMain.alwaysFullPreroll,
.MetadataFlashFpsEnabled = World.vMain.metadataFlashFpsEnabled,
.MetadataFlashDateEnabled = World.vMain.metadataFlashDateEnabled,
.LogOutToFile = World.vMain.logOutToFile,
.LogOutputFolder = World.vMain.logOutputFolder,
.MarkerAppendDaily = World.vMain.markerAppendDaily,
.SessionId = If(IsSessionMetadataEnabled(), World.vMain.sessionId, String.Empty),
.UnitName = If(IsSessionMetadataEnabled(), World.vMain.unitName, String.Empty),
.OperatorName = If(IsSessionMetadataEnabled(), World.vMain.operatorName, String.Empty)
}
Try
JsonSettingsManager.SaveSlateToFile(settings, sfd.FileName)
Catch ex As Exception
MessageBox.Show("Error saving file: " & ex.Message, "Save error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
' verify file
Dim verified As SettingsProfile = Nothing
If JsonSettingsManager.TryLoadSlateFromFile(sfd.FileName, verified) Then
' Success: flash message for 3 seconds
Await FlashTimecodeAsync("FILE SAVED!", 3000)
Else
MessageBox.Show("File saved but verification failed. The file may be corrupted.", "Verification failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
End Using
Catch ex As Exception
MessageBox.Show("Unexpected error saving slate: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
' Load a slate file chosen by the user. Disallowed while Timer1 is running.
Public Shared Async Sub LoadSlateWithDialog()
Try
If frmDigitalSlate.Timer1.Enabled Then
MessageBox.Show("Cannot load while timecode is running.", "Load disabled", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
Using ofd As New OpenFileDialog()
ofd.Filter = "Clapper files (*.clap)|*.clap|All files (*.*)|*.*"
ofd.Title = "Open slate configuration"
If ofd.ShowDialog() = DialogResult.OK Then
Await LoadSlateFromFilePath(ofd.FileName)
End If
End Using
Catch ex As Exception
MessageBox.Show("Unexpected error loading slate: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Shared Async Function LoadSlateFromFilePath(filePath As String, Optional flashLoadedMessage As Boolean = True) As Task(Of Boolean)
Try
If String.IsNullOrWhiteSpace(filePath) Then Return False
If frmDigitalSlate.Timer1.Enabled Then
MessageBox.Show("Cannot load while timecode is running.", "Load disabled", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return False
End If
Dim settings As SettingsProfile = Nothing
If Not JsonSettingsManager.TryLoadSlateFromFile(filePath, settings) Then
MessageBox.Show("Selected file is not a valid slate configuration.", "Invalid file", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
Try
World.vMain.scene = settings.Scene
World.vMain.scenePre = settings.ScenePre
World.vMain.sceneNum = settings.SceneNum
World.vMain.shot = settings.Shot
World.vMain.take = settings.Take
World.vMain.roll = settings.Roll
World.vMain.cameraNum = settings.CameraNum
World.vMain.camCardNum = settings.CamCardNum
World.vMain.production = settings.Production
World.vMain.director = settings.Director
World.vMain.dop = settings.DOP
World.vMain.fps = settings.FPS
World.vMain.custDate = settings.CustDate
World.vMain.currentDate = settings.CurrentDate
World.vMain.int = settings.Int
World.vMain.day = settings.Day
World.vMain.sync = settings.Sync
World.vMain.ltcEnabled = settings.LtcEnabled
World.vMain.ltcFpsMode = settings.LtcFpsMode
World.vMain.ltcOutputDeviceId = settings.LtcOutputDeviceId
World.vMain.ltcUnmute = settings.LtcUnmute
World.vMain.skipSound = settings.SkipSound
World.vMain.showCountdownNumbers = settings.ShowCountdownNumbers
World.vMain.alwaysFullPreroll = settings.AlwaysFullPreroll
World.vMain.metadataFlashFpsEnabled = settings.MetadataFlashFpsEnabled
World.vMain.metadataFlashDateEnabled = settings.MetadataFlashDateEnabled
World.vMain.logOutToFile = settings.LogOutToFile
World.vMain.logOutputFolder = settings.LogOutputFolder
World.vMain.markerAppendDaily = settings.MarkerAppendDaily
If IsSessionMetadataEnabled() Then
World.vMain.sessionId = settings.SessionId
World.vMain.unitName = settings.UnitName
World.vMain.operatorName = settings.OperatorName
If String.IsNullOrWhiteSpace(World.vMain.sessionId) Then
World.vMain.sessionId = GenerateNewSessionId()
End If
Else
World.vMain.sessionId = String.Empty
World.vMain.unitName = String.Empty
World.vMain.operatorName = String.Empty
End If
refreshSlate()
If flashLoadedMessage Then
Await FlashTimecodeAsync("FILE LOADED", 3000)
End If
Return True
Catch ex As Exception
MessageBox.Show("Error applying loaded slate: " & ex.Message, "Apply error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
Catch ex As Exception
MessageBox.Show("Unexpected error loading slate: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Sub parseScene()
With frmEdit
' Reset all checkboxes initially
.cbScV.Checked = False
.cbScR.Checked = False
.cbScX.Checked = False
If Not String.IsNullOrEmpty(World.vMain.scenePre) Then
Dim editScPre As String = World.vMain.scenePre
Dim result As Integer = containsChars(editScPre)
Select Case editScPre.Length
Case 3
If result = 6 Then
.cbScV.Checked = True
.cbScR.Checked = True
.cbScX.Checked = True
End If
Case 2
Select Case result
Case 3
.cbScV.Checked = True
.cbScR.Checked = True
Case 4
.cbScV.Checked = True
.cbScX.Checked = True
Case 5
.cbScR.Checked = True
.cbScX.Checked = True
End Select
Case 1
Select Case editScPre
Case "V"
.cbScV.Checked = True
Case "R"
.cbScR.Checked = True
Case "X"
.cbScX.Checked = True
End Select
End Select
End If
End With
End Sub
Public Function containsChars(input As String) As Integer
Dim letterValues As New Dictionary(Of Char, Integer) From {
{"V"c, 1},
{"R"c, 2},
{"X"c, 3}
}
Dim sum As Integer = 0
For Each letter As Char In letterValues.Keys
If input.Contains(letter) Then
sum += letterValues(letter)
End If
Next
Return sum
End Function
Public Shared Sub AdjustFontSizeToFitText(label As Label)
If label Is Nothing Then Return
Dim text As String = label.Text
If String.IsNullOrEmpty(text) Then Return
' Use a small vertical padding to avoid ascender/descender clipping
Dim verticalPadding As Integer = CInt(Math.Max(2, label.Height * 0.06)) ' ~6% of height or min 2px
Dim maxFontSize As Single = Math.Min(65.0F, label.Font.Size) ' don't jump larger than current font
Dim minFontSize As Single = 8.0F
Dim flags As System.Windows.Forms.TextFormatFlags = System.Windows.Forms.TextFormatFlags.SingleLine Or System.Windows.Forms.TextFormatFlags.NoPadding Or System.Windows.Forms.TextFormatFlags.NoPrefix
' Try decreasing font sizes until text fits both width and height (respect padding)
Using g As Graphics = label.CreateGraphics()
For fontSize As Single = maxFontSize To minFontSize Step -0.5F
Using testFont As New Font(label.Font.FontFamily, fontSize, label.Font.Style)
' Measure using TextRenderer to account for WinForms rendering
Dim measured As Size = TextRenderer.MeasureText(text, testFont, New Size(label.Width, Integer.MaxValue), flags)
If measured.Width <= label.ClientSize.Width AndAlso measured.Height <= (label.ClientSize.Height - verticalPadding) Then
' Found a size that fits; apply and exit
label.Font = New Font(label.Font.FontFamily, fontSize, label.Font.Style)
Return
End If
End Using
Next
' If nothing fits, apply the minimum readable size
label.Font = New Font(label.Font.FontFamily, minFontSize, label.Font.Style)
End Using
End Sub
End Class
End Namespace