From 29c7f466ec27a2a5cd4d62a7098b5e1cecce6545 Mon Sep 17 00:00:00 2001 From: Robin Date: Fri, 10 Apr 2015 14:04:16 +0200 Subject: [PATCH] BUG-1770: Fix problems when a font doesn't want to draw itself. [skip ci] --- Greenshot/Controls/FontFamilyComboBox.cs | 39 +++++++++++++++++------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/Greenshot/Controls/FontFamilyComboBox.cs b/Greenshot/Controls/FontFamilyComboBox.cs index c4df132c3..410c87402 100644 --- a/Greenshot/Controls/FontFamilyComboBox.cs +++ b/Greenshot/Controls/FontFamilyComboBox.cs @@ -57,30 +57,47 @@ namespace Greenshot.Controls { if (e.Index > -1) { FontFamily fontFamily = Items[e.Index] as FontFamily; - FontStyle fs = FontStyle.Regular; + FontStyle fontStyle = FontStyle.Regular; if (!fontFamily.IsStyleAvailable(FontStyle.Regular)) { if (fontFamily.IsStyleAvailable(FontStyle.Bold)) { - fs = FontStyle.Bold; + fontStyle = FontStyle.Bold; } else if (fontFamily.IsStyleAvailable(FontStyle.Italic)) { - fs = FontStyle.Italic; + fontStyle = FontStyle.Italic; } else if (fontFamily.IsStyleAvailable(FontStyle.Strikeout)) { - fs = FontStyle.Strikeout; + fontStyle = FontStyle.Strikeout; } else if (fontFamily.IsStyleAvailable(FontStyle.Underline)) { - fs = FontStyle.Underline; + fontStyle = FontStyle.Underline; } } - using (Font font = new Font(fontFamily, this.Font.Size + 5, fs, GraphicsUnit.Pixel)) { - // Make sure the text is visible by centering it in the line - using(StringFormat stringFormat = new StringFormat()) { - stringFormat.LineAlignment = StringAlignment.Center; - e.Graphics.DrawString(fontFamily.Name, font, Brushes.Black, e.Bounds, stringFormat); - } + try { + DrawText(e.Graphics, fontFamily, fontStyle, e.Bounds, fontFamily.Name); + } catch { + // If the drawing failed, BUG-1770 seems to have a weird case that causes: Font 'Lucida Sans Typewriter' does not support style 'Regular' + DrawText(e.Graphics, FontFamily.GenericSansSerif, FontStyle.Regular, e.Bounds, fontFamily.Name); } } // Uncomment this if you actually like the way the focus rectangle looks //e.DrawFocusRectangle (); } + /// + /// Helper method to draw the string + /// + /// + /// + /// + /// + /// + private void DrawText(Graphics graphics, FontFamily fontFamily, FontStyle fontStyle, Rectangle bounds, string text) { + using (Font font = new Font(fontFamily, this.Font.Size + 5, fontStyle, GraphicsUnit.Pixel)) { + // Make sure the text is visible by centering it in the line + using (StringFormat stringFormat = new StringFormat()) { + stringFormat.LineAlignment = StringAlignment.Center; + graphics.DrawString(text, font, Brushes.Black, bounds, stringFormat); + } + } + } + void BindableToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Text"));