A
A
Alexey2011-11-23 11:55:52
Mono
Alexey, 2011-11-23 11:55:52

Porting a C# program from .NET to Mono

Good afternoon.

In principle, if you do not use Win32 and other system-dependent calls, then there are no problems with porting from .NET to Mono. But our team encountered a problem in the form of the lack of an analogue of GetFontData in Mono.

Let me remind you that a Windows application using GetFontData can access the contents of a font file (or collections of fonts). Why might this be needed? For example, to create a PDF document, font data is needed to calculate character widths, as well as to embed the fonts used in the document. And if the introduction of fonts is a useful, but optional feature, then without calculating the widths, the generated PDF document takes on a “blank” look.

Of course, before posting this question, I carefully googled on this topic and found a similar question asked by a certain foreign gentleman on a good dozen forums. He did not receive an answer (on these forums). However, God bless him, here is a piece of code that does not work in Mono.

my_font is an object of type System.Drawing.Font. You need to get the contents of the font file in font_data.

            FontType CollectionMode;
            Bitmap tempBitmap = new Bitmap(1, 1);
            font_data = IntPtr.Zero;

            fonts_collection = new ArrayList();

            using (Graphics g = Graphics.FromImage(tempBitmap))
            {
                IntPtr hdc = g.GetHdc();
                IntPtr f = my_font.ToHfont();
                SelectObject(hdc, f);

                CollectionMode = FontType.TrueTypeCollection;
                uint fontDataSize = GetFontData(hdc, (uint)CollectionMode, 0, IntPtr.Zero, 0);
                if (fontDataSize == uint.MaxValue)
                {
                        CollectionMode = FontType.TrueTypeFont;
                        fontDataSize = GetFontData(hdc, (uint)CollectionMode, 0, IntPtr.Zero, 0);
                }
                font_data = Marshal.AllocHGlobal((int)fontDataSize);
                GetFontData(hdc, (uint) CollectionMode, 0, font_data, fontDataSize);
            }


As a matter of fact, a beautiful and universal solution is being sought for implementing this code under Mono.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rasa, 2011-11-25
@rasa

GTK.FontInfo won't help?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question