Note that there are some explanatory texts on larger screens.

plurals
  1. POLabel is not using custom font
    primarykey
    data
    text
    <p>I'm loading an embedded font using this code:</p> <pre><code>PrivateFontCollection pfc = new PrivateFontCollection(); //declared outside this function using (Stream fontStream = GetType().Assembly.GetManifestResourceStream("NucWar.BSOD_Font.ttf")) { if (null == fontStream) { return; } int fontStreamLength = (int)fontStream.Length; IntPtr data = Marshal.AllocCoTaskMem(fontStreamLength); byte[] fontData = new byte[fontStreamLength]; fontStream.Read(fontData, 0, fontStreamLength); Marshal.Copy(fontData, 0, data, fontStreamLength); pfc.AddMemoryFont(data, fontStreamLength); Marshal.FreeCoTaskMem(data); } </code></pre> <p>And that works fine. If I throw a breakpoint down at the end I can see the font is successfully loaded. If I wire-up the <code>Paint</code> event, and use this:</p> <pre><code>bool bold = false; bool italic = false; e.Graphics.PageUnit = GraphicsUnit.Point; using (SolidBrush b = new SolidBrush(Color.Black)) { int y = 5; foreach (FontFamily fontFamily in pfc.Families) { if (fontFamily.IsStyleAvailable(FontStyle.Regular)) { using (Font font = new Font(fontFamily, 32, FontStyle.Regular)) { e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); } y += 40; } if (fontFamily.IsStyleAvailable(FontStyle.Bold)) { bold = true; using (Font font = new Font(fontFamily, 32, FontStyle.Bold)) { e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); } y += 40; } if (fontFamily.IsStyleAvailable(FontStyle.Italic)) { italic = true; using (Font font = new Font(fontFamily, 32, FontStyle.Italic)) { e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); } y += 40; } if (bold &amp;&amp; italic) { using (Font font = new Font(fontFamily, 32, FontStyle.Bold | FontStyle.Italic)) { e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); } y += 40; } using (Font font = new Font(fontFamily, 32, FontStyle.Underline)) { e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); y += 40; } using (Font font = new Font(fontFamily, 32, FontStyle.Strikeout)) { e.Graphics.DrawString(font.Name, font, b, 5, y, StringFormat.GenericTypographic); } } } </code></pre> <p>that too works fine, the font is painted on the form 5 times as it should.</p> <p>So I find it very confusing when I use this function:</p> <pre><code>//Yes, pts is a valid size. eg 16, 20, 22, 30 private void AdjustFontSize(float pts) { int count = 0; //_yPos is a dict&lt;string, int&gt; that contains the Name of each Label control, and its original Y position // Therefore, Keys contains the Name prop of each label foreach (var lbl in _yPos.Keys) { var con = Controls[lbl] as Label; //Current label FontFamily f = pfc.Families[0]; //Get the font out of pfc. Debugging confirms that f is not null and has a font in it. //Why this no work? It works fine if I use a built in font, like "Times" con.Font = new Font(f, pts, FontStyle.Regular); //Messing with the location prop, still working on it. Probably not important con.Location = new Point(con.Location.X, (int) (_yPos[lbl] + Math.Pow(1.15, (int) pts)*count++)); } } </code></pre> <p>And the label font changes to Arial, not me desired font. What is really bothering me is that If I change <code>con.Font = ...</code> to use <code>"Times"</code> instead of <code>f</code> It correctly changes to the Times font. And on top of that, I know the font is loaded properly because the form paints correctly. By that logic, I am changing the font properly, and I'm loading the font properly, so it should work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload