Font changes when not in edit mode

Posted by: superjason on 8 September 2017, 12:29 pm EST

  • Posted 8 September 2017, 12:29 pm EST

    I'm trying to get my grid to look like Excel 2007, so I want to use the new Calibri font.  It's installed on my machine, but I'm having trouble displaying it correctly.

    Here is my code:
    grid.Font = new Font("Calibri", 11, FontStyle.Regular, GraphicsUnit.Point);
    _sheet.ColumnHeader.Rows[0].Font = new Font("Calibri", 11, FontStyle.Regular, GraphicsUnit.Point);
    When I'm editing a cell, the font appears to be correct, but when I leave the cell, it gets ugly.  The anti-aliasing is off, and it doesn't even look like the same font.  If I use a bogus font name, it actually looks better, probably because it uses a default font.  I'm attaching a screenshot.

    Any ideas?

    2007/04/Grid-Font.gif
  • Replied 8 September 2017, 12:29 pm EST

    Jason -

    I downloaded the new vista fonts for use in a test for your issue on my XP machine.  I see very little difference, if any, in the font appearance between the editor and the renderer.  Do you have any other settings applied for your sheet or spread?
  • Replied 8 September 2017, 12:30 pm EST

    Jason -

    I am testing with the same version and I can load any font I choose.  Can you zip up your project and attach it to see if I can reproduce it.
  • Replied 8 September 2017, 12:30 pm EST

    I set up a brand new project, dropped in the .NET spread control (version 2.5.2009.2005 on Farpoint.Win.Spared.dll).  I then added this method, and did nothing else:
    		private void Form1_Load(object sender, EventArgs e)
    {
    fpSpread1.Font = new Font("Calibri", 11, FontStyle.Regular, GraphicsUnit.Point);
    }
    I ran that project, and had the same problem.  I believe it happens with any font other than the default.
  • Replied 8 September 2017, 12:30 pm EST

    Jason -

    Attached is a screenshot from my machine with ClearType off.

    2007/04/jason.JPG
  • Replied 8 September 2017, 12:30 pm EST

    Ok, figured it out:

    Cleartype off: Looks ugly all the time
    Standard font smoothing: Looks good all the time
    Cleartype: Looks ugly half the time.

    Other applications handle all of the scenarios just fine (aka Excel).  You got a bug.
  • Replied 8 September 2017, 12:30 pm EST

    I attached a zip file containing all of the project files.  The project references the spread DLLs in the bin folder, so you'll have to copy them in there or update the references.  It's a 2005 project.

    I tried it on a second computer and it has the same problem as mine.

    Thanks,
    Jason
    2007/04/WindowsApplication1.zip
  • Replied 8 September 2017, 12:30 pm EST

    I will test this on another machine in the morning.  I am referencing the TextRenderer dll in my project, but I was not in Jason's and the results were the same.
  • Replied 8 September 2017, 12:30 pm EST

    Hello,


    I have not tested this. But are you referencing the TextRenderer.DLL in your project? Try removing this reference to see if it paints better. I believe we have a current bug in the TextRenderer that causes an issue like you are describing.

  • Replied 8 September 2017, 12:30 pm EST

    hmm, that's really bizarre.

    I tried it on 3 machines.  2 developer machines and 1 documentation machine.  Can you try your EXE on another computer just to make sure it isn't something unique to yours?

    Any other ideas for what to try on my end?
  • Replied 8 September 2017, 12:30 pm EST

    Jason -

    I tested this on another machine this morning and the result is the same as on my machine.
  • Replied 8 September 2017, 12:30 pm EST

    I am not referencing that DLL.
  • Replied 8 September 2017, 12:30 pm EST

    Any ideas?

    If there an easy way to intercept the PaintCell method without having to inherit every cell type?  I don't mind using that work around, but I use a couple of different cell types.

    Thanks for all of the help so far.
  • Replied 8 September 2017, 12:30 pm EST

    Did both machines have CRT monitors by chance?
  • Replied 8 September 2017, 12:30 pm EST

    One was a CRT the other an LCD.
  • Replied 8 September 2017, 12:30 pm EST

    I tried this on two more computers (including a Vista machine) and I still cannot reproduce your issue.  Each cell type would have to have its own PaintCell method.
  • Replied 8 September 2017, 12:30 pm EST

    Here is some code that almost completely fixes the problem.  Hopefully it will help you figure out what is going on: 
    class GoodCellType : FarPoint.Win.Spread.CellType.TextCellType
    {
    public override void PaintCell(System.Drawing.Graphics g, System.Drawing.Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)
    {
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    g.TextContrast = 12;

    base.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor);
    }
    }
     
  • Replied 8 September 2017, 12:30 pm EST

    Our DPI settings are 96 (the default for XP).  Switching to 120 made it look fine.  You should switch to 96 to see if you can reproduce it.
  • Replied 8 September 2017, 12:30 pm EST

    Jason -

    I ran the exe you sent and I still do not see the issue.  What are the DPI settings on the computers at your place?  Mine are set to 120.
  • Replied 8 September 2017, 12:30 pm EST

    Can I email you the binary version to see if it's a problem in how it's built?  It's about 1mb total, and I can't attach it.
  • Replied 8 September 2017, 12:30 pm EST

    That probably also explains why it worked on your machines.  I didn't have the spread control installed on any of the machines I tried it on.  You probably had it in your GAC.
  • Replied 8 September 2017, 12:30 pm EST

    Using the TextRenderer fixed it.  I'll try it for a while and see if I run into any issues.

    Thanks for all of your help.

    Jason
  • Replied 8 September 2017, 12:30 pm EST

    Text is drawn in Spread using Graphics.DrawString unless FarPoint.Win.TextRenderer.dll is found somewhere in the application bin folder or Global Assembly Cache.  If the TextRenderer assembly is found when the application loads, then it will be used to do the drawing to the screen by making calls to the System.WindowsForms.TextRenderer class.

    You can add a reference to the FarPoint.TextRenderer.dll in your project, or copy it to the bin folder, to make your application use it instead of GDI+.  Please note that this assembly is not referenced by the Spread assembly or the FarPoint.Win assembly.  It is an optional assembly for use in cases where you want to make an application use GDI instead of GDI+ for all text rendering.  The control will attempt to load this assembly and will use it if it is found, and it will catch and supress the exception if it is not found.  The Managed Debugging Assistant for BindingFailure will raise notifications when this happens during your debugging session if it is enabled; this is not a problem with your application or with the control, and can be eliminated by unchecking the box for that MDA in the dialog for Debug -> Exceptions or simply ignored.

    The framework documentation states that there are certain limitations to using System.WindowsForms.TextRenderer.  It cannot be used to draw ClearType font smoothing effects into a bitmap and then copy that to a graphics with DrawImage.  It cannot be used with a Graphics that has RotateTransforms on it (the RotateTransforms are ignored).  It also cannot be used to draw to a printer device context.  As a result of the first two limitations, text that is rotated in a cell with TextOrientation will not look good when it is drawn with TextRenderer (the font smoothing effects do not draw correctly, resulting in a very dark type face, as if the font effect is simply putting black pixels where it should be putting alpha blended colors from adjacent pixels to do the font effect highlighting).  This sounds very much like what you are seeing when it looks bad.  The limitation with printing simply means that we must always use GDI+ when printing the sheet.
  • Replied 8 September 2017, 12:30 pm EST



    We determined that if you render the font with GDI+, it
    doesn't look right, but GDI renders it correctly.





    If you use a .NET label, it displays the font
    correctly.  Also, if you use
    TextRenderer.DrawText, it displays the font correctly.  However, if you use Graphics.DrawString, it
    shows it the wrong way, as we have been seeing.





    The .NET label has 2 modes, standard mode, and a
    compatibility mode.  Compatibility mode
    looks like crap, and standard mode looks great.





    Do you use Graphics.DrawString for the cells in the grid?





    We're also wondering if we have something GDI related on
    our computers that is causing it to work.





    I'm no GDI expert, so I'm hoping you can add your
    knowledge.  We're still working on it on
    our end as well.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels