Skip to content

[API Proposal]: Trim friendly Font ToLogFont/FromLogFont #8846

Closed

Description

@kant2002

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

Activity

  1. added
    api-suggestion
    on Jun 7, 2022
  2. ghost added
    area-System.Drawing
    on Jun 7, 2022
  3. ghost added
    untriaged
    on Jun 7, 2022
  4. changed the title [-][API Proposal]: [/-] [+][API Proposal]: Trim friendly Font ToLogFont/FromLogFont[/+] on Jun 7, 2022
  5. ViktorHofer commented on Jun 7, 2022

    @ViktorHofer
    Member

    @JeremyKuhne @RussKie, given that there's already a PR up for the implementation, should this get reviewed in one of the api design review meetings? Would anyone of you want to be the champion for it?

  6. JeremyKuhne commented on Jun 7, 2022

    @JeremyKuhne
    Member

    @ViktorHofer sure, mark it and I'll represent.

  7. RussKie commented on Jun 7, 2022

    @RussKie
    Contributor

    I'll watch the recording

  8. added
    api-ready-for-review
    and removed
    untriaged
    on Jun 8, 2022
  9. ViktorHofer commented on Jun 8, 2022

    @ViktorHofer
    Member

    Great, thank you. Sent out a mail.

  10. terrajobst commented on Jun 8, 2022

    @terrajobst
    Contributor

    How urgent is this?

  11. removed
    api-suggestion
    on Jun 11, 2022
  12. added this to the Future milestone on Jun 29, 2022
  13. ViktorHofer commented on Jun 29, 2022

    @ViktorHofer
    Member

    @kant2002 can you answer @terrajobst's question regarding the priority of getting this into main?

  14. 4 remaining items

  15. bartonjs commented on Aug 9, 2022

    @bartonjs
    Member

    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);
        }
    }
  16. added
    api-approved
    and removed
    api-ready-for-review
    on Aug 9, 2022
  17. elachlan commented on Jan 18, 2023

    @elachlan
    Contributor

    Now the API change is approved, can we reopen dotnet/runtime#70224?

  18. ViktorHofer commented on Jan 23, 2023

    @ViktorHofer
    Member

    @kant2002 feel free to re-open your PR and we can give it another glance.

  19. transferred this issue fromdotnet/runtimeon Mar 14, 2023
  20. elachlan commented on Mar 14, 2023

    @elachlan
    Contributor

    @kant2002 did you want to reroll your patch now System.Drawing has moved to winforms?

  21. added
    🚧 work in progress
    on Apr 14, 2023
  22. JeremyKuhne commented on Apr 14, 2023

    @JeremyKuhne
    Member

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

  23. added a commit that references this issue on Apr 14, 2023
    904a5b6
  24. added a commit that references this issue on Apr 14, 2023
    decf3fa
  25. ghost removed
    🚧 work in progress
    on Apr 14, 2023
  26. ghost locked as resolved and limited conversation to collaborators on May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Drawing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions