private SizeF GetStringSize(Graphics graphics, LayoutSpec line, float fontEmSize) { using (var font = new Font(line.FontFamily, fontEmSize, line.FontStyle)) { return(graphics.MeasureString(line.Text, font)); } }
/// <summary> /// Draws the background of the title card to this object's graphics context. /// </summary> public void DrawBackground() { Graphics.Clear(BackgroundPrimaryColor); string fontFamilyName = FirstAvailableFont(BackgroundFont); var family = new FontFamily(fontFamilyName); var style = FontStyle.Regular; var brush = new SolidBrush(BackgroundSecondaryColor); var spec = new LayoutSpec() { Text = DecorativeInitial, FontFamily = family, FontStyle = style, Color = brush.Color }; var boundingBox = new SizeF(Width, Height); float fontSize = 0; SizeF stringSize = new SizeF(); GetMaxFontSize(Graphics, spec, boundingBox, out fontSize, out stringSize); using (var font = new Font(family, fontSize, style)) { var x = -0.6F * stringSize.Width; var y = 0.3F * stringSize.Height; Graphics.DrawString(DecorativeInitial, font, brush, x, y); } }
private void GetMaxFontSize(Graphics graphics, LayoutSpec line, SizeF boundingBox, out float fontSize, out SizeF stringSize) { float testedFontSize = 5; SizeF testedStringSize = GetStringSize(graphics, line, testedFontSize); do { fontSize = testedFontSize; stringSize = testedStringSize; testedFontSize += 1; testedStringSize = GetStringSize(graphics, line, testedFontSize); }while (testedStringSize.Width < boundingBox.Width && testedStringSize.Height < boundingBox.Height); }