question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[API Proposal]: Trim friendly Font ToLogFont/FromLogFont

See original GitHub issue

Background and motivation

Right now WinForm use System.Drawing.Common.Font.ToLogFont/FromLogFont with their own interop structures. That API produce trimming warning. This API would serve same purpose as existing API, but provide trim friendly alternative.

API Proposal

namespace System.Drawing;

public sealed partial class Font
{
    public void ToLogFont<T>(ref T logFont, Graphics graphics) where T: struct, unmanaged  { throw null; }
    public static Font FromLogFont<T>(in T logFont) where T: struct, unmanaged { throw null; }
    public static Font FromLogFont<T>(in T logFont, IntPtr hdc) where T: struct, unmanaged { throw null; }
    public void ToLogFont<T>(ref T logFont) where T: struct, unmanaged { throw null; }
}

API Usage

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public unsafe struct MyLOGFONT
{
    public int lfHeight;
    public int lfWidth;
    public int lfEscapement;
    public int lfOrientation;
    public int lfWeight;
    public byte lfItalic;
    public byte lfUnderline;
    public byte lfStrikeOut;
    public byte lfCharSet;
    public byte lfOutPrecision;
    public byte lfClipPrecision;
    public byte lfQuality;
    public byte lfPitchAndFamily;
    private fixed char _lfFaceName[LF_FACESIZE];
}

// And similar 
var f = new Font();
MyLOGFONT interopStruct = default;
f.ToLogFont(ref interopStruct);

Alternative Designs

Ask users of this API, reimplement it itself within their application and make that code trim-friendly.

Risks

No response

Accompanying implementation dotnet/runtime#70224

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremyKuhnecommented, Apr 14, 2023

I’ve implemented this and will be putting up a PR tomorrow.

1reaction
bartonjscommented, Aug 9, 2022

Video

  • We made Interop.LOGFONT public and removed the genericness of the functions
namespace System.Drawing
{
    public sealed partial class Font
    {
        public void ToLogFont(out Interop.LOGFONT logFont, Graphics graphics)  { throw null; }
        public static Font FromLogFont(in Interop.LOGFONT logFont) { throw null; }
        public static Font FromLogFont(in Interop.LOGFONT logFont, IntPtr hdc) { throw null; }
        public void ToLogFont(out Interop.LOGFONT logFont) { throw null; }
    }
}

namespace System.Drawing.Interop
{
    public unsafe struct LOGFONT
    {
        public int lfHeight;
        public int lfWidth;
        public int lfEscapement;
        public int lfOrientation;
        public int lfWeight;
        public byte lfItalic;
        public byte lfUnderline;
        public byte lfStrikeOut;
        public byte lfCharSet;
        public byte lfOutPrecision;
        public byte lfClipPrecision;
        public byte lfQuality;
        public byte lfPitchAndFamily;
        private fixed char _lfFaceName[LF_FACESIZE];

        [UnscopedRef]
        public Span<char> lfFaceName => MemoryMarshal.CreateSpan(ref _lfFaceName[0], LF_FACESIZE);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

[API Proposal]: Trim friendly Font ToLogFont/FromLogFont
NET UI framework for building Windows desktop applications. - [API Proposal]: Trim friendly Font ToLogFont/FromLogFont · dotnet/winforms@6013922.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found