MeasureText seems to not work for monospaced fonts

A forum dedicated to WPF version of LightningChart Ultimate.

Moderator: Arction_LasseP

ShawnCG
Posts: 57
Joined: Tue Oct 15, 2013 8:31 pm

MeasureText seems to not work for monospaced fonts

Post by ShawnCG » Thu Mar 26, 2015 11:36 pm

When I call the MeasureText function, I get misleading results for the case of monospaced fonts.
For example, when I use Lucida Console font, the following returns different values for the width:

PointInt textMeasure1 = _lightningChart.MeasureText("mmmm", annotation.TextStyle.Font);
PointInt textMeasure2 = _lightningChart.MeasureText("iiii", annotation.TextStyle.Font);

Is there any trick to making this work? It makes it really difficult to position annotations if I can't get an accurate size measurement.

User avatar
ArctionLasse
Posts: 26
Joined: Fri Mar 27, 2015 11:23 am

Re: MeasureText seems to not work for monospaced fonts

Post by ArctionLasse » Fri Mar 27, 2015 11:59 am

I'm afraid we can't reproduce the problem you describe.

When strings of same amount of letters are measured, monospaced fonts produce the same values no matter the characters used, as it should. Also the annotation automatic size detemination seems to be working correctly.
Tested the system with Lucica Console and Courier New fonts of several different sizes and with different short strings.

Short clip from the immediate window:

Code: Select all

MeasureText("iiii", new WPFFont("Lucida Console", 12));
{}
    X: 28
    Y: 12
MeasureText("mmmm", new WPFFont("Lucida Console", 12));
{}
    X: 28
    Y: 12

Could you please send us a simple test code where the measurement method fails. Also, what version of the LightningChart are you using and on what operating system?
LightningChart Support Team, LV

ShawnCG
Posts: 57
Joined: Tue Oct 15, 2013 8:31 pm

Re: MeasureText seems to not work for monospaced fonts

Post by ShawnCG » Fri Mar 27, 2015 4:07 pm

Interesting. Creating the WPFFont object using the constructor that you chose, it works! I have been using an alternate constructor:
WPFFont font = new WPFFont(fontSpec.TheFontFamily, emSize, fontSpec.TheFontStyle);

and using this method, the font measurements have always been a little flaky. I'll switch to your method and see if that helps.

User avatar
ArctionLasse
Posts: 26
Joined: Fri Mar 27, 2015 11:23 am

Re: MeasureText seems to not work for monospaced fonts

Post by ArctionLasse » Wed Apr 01, 2015 8:59 am

There indeed seems to be a bug in text measurement with WPFFonts created using constructors taking System.Drawing.FontFamily as a parameter.

The problem will be fixed in the next assembly pack. Untill then, the fonts created with the other constructors can be used to get proper measurement results.
LightningChart Support Team, LV