MeasureString函数提供大小,但大于文本大小

时间:2018-08-01 05:15:04

标签: graphics text-size measurestring

我有一个函数可以绘制带有上标和下标的文本。我使用MeasureString函数来获取文本大小。 当我使用“ StringFormat.GenericDefault”时,文本之间存在一些间隙,当我使用“ StringFormat.GenericTypographic”时,存在小于间隙的问题,但是在文本末尾则存在很大的间隙。此差距取决于子文字或超级文字的长度。 您可以在下面的图片中看到它。 There are some gaps between texts

    Private Function GetTextPathRecursive(caption As String, egraphics As Graphics, ByVal font As Font, ByVal location As PointF) As SizeF
    caption = LTrim(RTrim(caption))
    If caption = "" Then Return New Size(0, 0) : Exit Function
    Dim cap() As String = SplitString(caption)
    Dim _factor As Single = 1 / 2
    Dim _smallfont As Font = New Font(font.FontFamily, font.Size * _factor, font.Style)
    On Error Resume Next
    Dim _normsize As SizeF ' normal text size
    Dim _normsizeI As SizeF ' normal text size
    Dim _subsize As SizeF = New SizeF(0, 0) ' subscript size
    Dim _supsize As SizeF = New SizeF(0, 0) ' superscript size
    Dim _subloc As PointF ' start location of subscript text
    Dim _suploc As PointF ' start location of superscript text
    _normsize = New SizeF(egraphics.MeasureString(cap(0), font, New Size(Integer.MaxValue, Integer.MaxValue), StringFormat.GenericTypographic)) ' sf))
    _suploc = New PointF(location.X + _normsize.Width, location.Y)
    _subloc = New PointF(location.X + _normsize.Width - _normsizeI.Width, location.Y + _normsize.Height * (1 - _factor))
    _textpath.AddString(cap(0), font.FontFamily, font.Style, font.Size, location, StringFormat.GenericTypographic)
    _supsize = GetTextPathRecursive(cap(1), egraphics, _smallfont, _suploc)
    _subsize = GetTextPathRecursive(cap(2), egraphics, _smallfont, _subloc)
    Dim _txtsize As SizeF = New SizeF(_normsize.Width +
                                      Math.Max(_supsize.Width,
                                               _subsize.Width), _normsize.Height)
    _smallfont.Dispose()
    Return _txtsize
End Function

0 个答案:

没有答案