I have a drawingcontrol based on a canvas.
The user is able to draw a rectangle by pressing the mouse button on the topleft position and dragging the mouse to the bottom right position. If the user lets go of the mouse button, the rectangle is drawn on the exact position that the user performed that action.
Now I need to draw text on this drawing control too, and it is the same procedure, the idea is that when the user lets go of the mouse the rectangle is filled in with the tekst "label", and that it exactly fills the surrounding rectangle. If I change the tekst to say "Lgl" the formatting should be reapplied because most likely the 'g' has a negative bottom bearing. How can I make this happen ? (Not the reformatting event, that is happening ok, but the fitting in the surrounding rectangle itself)
If I say the fontsize is 50, the resulting size is most likely bigger.
I used following code to do some investigation already based on what I found on the net :
public class HeightFontSizeConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = (string)values[0];
double desiredHeight = (double)values[1];
FontFamily family = (FontFamily)values[2];
FontStyle style = (FontStyle)values[3];
FontWeight weight = (FontWeight)values[4];
FontStretch stretch = FontStretches.Normal;
if (desiredHeight == 0) return (double)0;
Typeface face = new Typeface(family,style,weight,stretch);
GlyphTypeface gtf;
if (face.TryGetGlyphTypeface(out gtf))
{
for (int i = 0; i < text.Length; ++i)
{
ushort index = gtf.CharacterToGlyphMap[text[i]];
double h = gtf.AdvanceHeights[index];
double b = gtf.BottomSideBearings[index];
double t = gtf.TopSideBearings[index];
double baseline = gtf.Baseline;
double height = gtf.Height;
Debug.WriteLine("in HeightFontSizeConverter {0} baseline={1} height={2} advh={3} bsb={4} tsb={5}",text[i], baseline, height, h, b,t);
}
return desiredHeight;
}
else return (double)0;
}
The problem basically is that I don't seem to find the meaning of the parameters in GlyphTypeface. I am not interested in left side bearing and right side bearing, I have the feeling it has to do with top side bearing and bottom side bearing but where do they start exactly.
I don't want any space above the tekst and I do not want any space below the tekst, it should fit the rectangle exactly.