Вопрос

I am trying to get all chars of Segoe UI Symbol Font.

I got them, converted to char, converted to Hex value and added to listview as items.

So, somebody else can use their hex values for XAML projects as icon.

But the problem is this in the code: i am always getting OverFlowException at the function Convert.ToChar.

Code is running correct, but when the index variable is bigger than 65535 which is max char value, i got overflowexception.

But if you run the code, as you will see, in the Segoe UI Symbol fontfamily there are more chars which is bigger than 65535.

Maybe my method is wrong, you can advice me another method.

MainWindow.xaml file:

<Grid Loaded="Grid_Loaded">
    <ListView x:Name="listview">
        <ListView.View>
            <GridView> 
                <GridViewColumn Header="HexValue"  />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

MainWindow.xaml.vb file

Class MainWindow

    Public glyph As GlyphTypeface
    Dim characterMap As IDictionary(Of Integer, UShort)

    Private Sub Grid_Loaded(sender As Object, e As RoutedEventArgs)
        SymbolleriGetir()
    End Sub

    Public Sub SymbolleriGetir()
        Dim segoeUiSymbol As FontFamily
        For Each font As FontFamily In Fonts.SystemFontFamilies
            Dim fontName As String
            fontName = font.Source
            If fontName = "Segoe UI Symbol" Then
                segoeUiSymbol = font
            End If
        Next

        For Each typeFace As Typeface In segoeUiSymbol.GetTypefaces
            typeFace.TryGetGlyphTypeface(glyph)
            If glyph IsNot Nothing Then
                characterMap = glyph.CharacterToGlyphMap
            Else
                Continue For
            End If
        Next

        For i As Integer = 0 To characterMap.Keys.Count
            Dim index As Integer = characterMap.Keys.ElementAt(i)
            Dim c As Char = Nothing
            c = Convert.ToChar(index)
            Dim charText As String = c.ToString()
            listview.Items.Add(String.Format("&#x{0:x2};", System.Convert.ToUInt32(c)))
        Next

    End Sub
End Class
Это было полезно?

Решение

CharacterToGlyphMap is a lookup map (IDictionary(Of Integer, UShort)) with the UShort being the unicode char so it is not necessary to convert.

I am no VB developer, but I just just coded this up and tested which enumerates the chars, and creates an image glyph next to each hex value:

enter image description here

Wingdings:

enter image description here

Your loaded event handler: (I exited after 100 due to load time)

    Private Sub Grid_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim glyph As GlyphTypeface
        Dim glyphIndex As UShort
        Dim typeface As System.Windows.Media.Typeface = New System.Windows.Media.Typeface("Segoe UI Symbol")
        If (typeface.TryGetGlyphTypeface(glyph)) Then
            Dim glyphLookupMap As IDictionary(Of Integer, UShort) = glyph.CharacterToGlyphMap
            Dim x As Integer = 0
            For Each kvp As KeyValuePair(Of Integer, UShort) In glyphLookupMap
                Dim c As Char = Convert.ToChar(kvp.Value)
                Dim glyphImage As ImageSource = Nothing
                If (glyphLookupMap.TryGetValue(kvp.Key, glyphIndex)) Then
                    glyphImage = Me.CreateGlyph(glyph, glyphIndex, kvp.Value, Brushes.Blue)
                End If
                Me._listview.Items.Add(Me.CreateGlyphListboxEntry(kvp.Key, glyphImage))
                Dim num As Integer = x + 1
                x = num
                If (num > 100) Then
                    Exit For
                End If
            Next
        End If
    End Sub

And here would be the Glyph image creator

    Private Function CreateGlyph(ByVal glyphTypeface As System.Windows.Media.GlyphTypeface, ByVal glyphIndex As UShort, ByVal charUShortVal As UShort, ByVal foreground As Brush) As System.Windows.Media.ImageSource
        Dim imageSource As System.Windows.Media.ImageSource
        Dim flag As Boolean = False
        Dim drawingImage As System.Windows.Media.DrawingImage = Nothing
        Try
            Dim glyphIndexes As IList(Of UShort) = New List(Of UShort)() From
            {
                charUShortVal 
            }
            Dim advanceWidths As IList(Of Double) = New List(Of Double)() From
            {
                glyphTypeface.AdvanceWidths(glyphIndex)
            }
            Dim glyphRun As System.Windows.Media.GlyphRun = New System.Windows.Media.GlyphRun(glyphTypeface, 0, False, 1, glyphIndexes, New Point(0, 0), advanceWidths, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
            drawingImage = New System.Windows.Media.DrawingImage(New System.Windows.Media.GlyphRunDrawing(foreground, glyphRun))
        Catch exception As System.Exception
            imageSource = Nothing
            flag = True
        End Try
        If (Not flag) Then
            imageSource = drawingImage
        End If
        flag = False
        Return imageSource
    End Function

And finally the Listbox Entry creator:

Private Function CreateGlyphListboxEntry(ByVal charIntValue As Integer, ByVal glyphImage As ImageSource) As FrameworkElement
    Dim result As StackPanel = New StackPanel() With
    {
        .Orientation = Orientation.Horizontal
    }
    Dim text As TextBlock = New TextBlock() With
    {
        .Text = String.Format("{0:X}", charIntValue),
        .Foreground = Brushes.Black,
        .FontSize = 17,
        .Margin = New Thickness(10, 0, 10, 0)
    }
    result.Children.Add(text)
    If (glyphImage IsNot Nothing) Then
        Dim image As System.Windows.Controls.Image = New System.Windows.Controls.Image()
        Dim num As Double = 32
        Dim num1 As Double = num
        image.Height = num
        image.Width = num1
        image.Stretch = Stretch.Uniform
        image.Source = glyphImage
        result.Children.Add(image)
    End If
    Return result
End Function

Hope this helps!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top