stdecden
14-07-2007, 21:29
Salve a tutti!
In un programma voglio sapere su quale carattere di una stringa mi trovo e quindi devo misurare ogni singolo carattere con la seguente funzione:
- i: numero del carattere nella stringa
- s: Stringa da verificare
- f: Carattere
- tx,ty: Distanza dal margine del controllo
- e System Graphics
Questa funzione dovrebbe anche essere in grado di risolvere il kerning del font.
L'unico problema riscontrato é che qli spazi me li calcola troppo piccoli, e la lettera sucessiva troppo grande.
So che é una domanda difficile, ma spero che qualcuno mi possa aiutare
Public Function getCharBounds(ByVal i As Integer, ByVal s As String, ByVal f As Font, ByVal tx As Integer, ByVal ty As Integer, ByVal e As Graphics) As Rectangle
Dim L As Integer = Len(s)
If L <> 1 Then
If (i = 1) Then
Dim size As SizeF = e.MeasureString(Mid(s, 1, 1), f)
Dim size2 As SizeF = e.MeasureString(Mid(s, 1, 2), f)
Dim size3 As SizeF = e.MeasureString(Mid(s, 2, 1), f)
Dim Bounds As New Rectangle(tx, ty, size.Width - (size2.Width - size3.Width - size.Width) * -1 / 2, size.Height)
Return Bounds
ElseIf (i = L) Then
Dim size As SizeF = e.MeasureString(Mid(s, L, 1), f)
Dim size2 As SizeF = e.MeasureString(Mid(s, 1, L - 1), f)
Dim size3 As SizeF = e.MeasureString(Mid(s, 1, L), f)
Dim Bounds As New Rectangle(tx + size2.Width - (size3.Width - size2.Width - size.Width) / -2, ty, size.Width - (size3.Width - size2.Width - size.Width) / -2, size.Height)
Return Bounds
Else
Dim a As SizeF = e.MeasureString(Mid(s, i, 1), f)
Dim b As SizeF = e.MeasureString(Mid(s, 1, i - 1), f)
Dim c As SizeF = e.MeasureString(Mid(s, i + 1, L - i), f)
Dim ab As SizeF = e.MeasureString(Mid(s, 1, i + 1), f)
Dim ac As SizeF = e.MeasureString(Mid(s, i, L - i + 1), f)
Dim abc As SizeF = e.MeasureString(Mid(s, 1, L), f)
Dim Bounds As New Rectangle(tx + b.Width - (abc.Width - b.Width - ac.Width) / -2, ty, _
((abc.Width - c.Width - ab.Width) / 2 + ab.Width) - ((ab.Width - b.Width - a.Width) / 2 + b.Width), a.Height)
Return Bounds
End If
Else
Dim a As SizeF = e.MeasureString(s, f)
Dim bounds As New Rectangle(tx, ty, a.Width, a.Height)
End If
End Function
In un programma voglio sapere su quale carattere di una stringa mi trovo e quindi devo misurare ogni singolo carattere con la seguente funzione:
- i: numero del carattere nella stringa
- s: Stringa da verificare
- f: Carattere
- tx,ty: Distanza dal margine del controllo
- e System Graphics
Questa funzione dovrebbe anche essere in grado di risolvere il kerning del font.
L'unico problema riscontrato é che qli spazi me li calcola troppo piccoli, e la lettera sucessiva troppo grande.
So che é una domanda difficile, ma spero che qualcuno mi possa aiutare
Public Function getCharBounds(ByVal i As Integer, ByVal s As String, ByVal f As Font, ByVal tx As Integer, ByVal ty As Integer, ByVal e As Graphics) As Rectangle
Dim L As Integer = Len(s)
If L <> 1 Then
If (i = 1) Then
Dim size As SizeF = e.MeasureString(Mid(s, 1, 1), f)
Dim size2 As SizeF = e.MeasureString(Mid(s, 1, 2), f)
Dim size3 As SizeF = e.MeasureString(Mid(s, 2, 1), f)
Dim Bounds As New Rectangle(tx, ty, size.Width - (size2.Width - size3.Width - size.Width) * -1 / 2, size.Height)
Return Bounds
ElseIf (i = L) Then
Dim size As SizeF = e.MeasureString(Mid(s, L, 1), f)
Dim size2 As SizeF = e.MeasureString(Mid(s, 1, L - 1), f)
Dim size3 As SizeF = e.MeasureString(Mid(s, 1, L), f)
Dim Bounds As New Rectangle(tx + size2.Width - (size3.Width - size2.Width - size.Width) / -2, ty, size.Width - (size3.Width - size2.Width - size.Width) / -2, size.Height)
Return Bounds
Else
Dim a As SizeF = e.MeasureString(Mid(s, i, 1), f)
Dim b As SizeF = e.MeasureString(Mid(s, 1, i - 1), f)
Dim c As SizeF = e.MeasureString(Mid(s, i + 1, L - i), f)
Dim ab As SizeF = e.MeasureString(Mid(s, 1, i + 1), f)
Dim ac As SizeF = e.MeasureString(Mid(s, i, L - i + 1), f)
Dim abc As SizeF = e.MeasureString(Mid(s, 1, L), f)
Dim Bounds As New Rectangle(tx + b.Width - (abc.Width - b.Width - ac.Width) / -2, ty, _
((abc.Width - c.Width - ab.Width) / 2 + ab.Width) - ((ab.Width - b.Width - a.Width) / 2 + b.Width), a.Height)
Return Bounds
End If
Else
Dim a As SizeF = e.MeasureString(s, f)
Dim bounds As New Rectangle(tx, ty, a.Width, a.Height)
End If
End Function