BUG-1716: Question found another problem, somehow the font was disposed but not re-created. Changed some of the logic, which should optimize the font creation and I currently can't reproduce the error with this change.

This commit is contained in:
RKrom 2014-12-08 12:19:15 +01:00
commit 3f16a5d4f2

View file

@ -38,8 +38,6 @@ namespace Greenshot.Drawing {
/// </summary> /// </summary>
[Serializable] [Serializable]
public class TextContainer : RectangleContainer, ITextContainer { public class TextContainer : RectangleContainer, ITextContainer {
[NonSerialized]
private bool fontInvalidated = true;
// If makeUndoable is true the next text-change will make the change undoable. // If makeUndoable is true the next text-change will make the change undoable.
// This is set to true AFTER the first change is made, as there is already a "add element" on the undo stack // This is set to true AFTER the first change is made, as there is already a "add element" on the undo stack
private bool makeUndoable; private bool makeUndoable;
@ -104,7 +102,6 @@ namespace Greenshot.Drawing {
[OnDeserialized] [OnDeserialized]
private void OnDeserialized(StreamingContext context) { private void OnDeserialized(StreamingContext context) {
Init(); Init();
UpdateFormat();
} }
protected override void Dispose(bool disposing) { protected override void Dispose(bool disposing) {
@ -128,11 +125,12 @@ namespace Greenshot.Drawing {
private void Init() { private void Init() {
_stringFormat = new StringFormat(); _stringFormat = new StringFormat();
_stringFormat.Trimming = StringTrimming.EllipsisWord; _stringFormat.Trimming = StringTrimming.EllipsisWord;
fontInvalidated = true;
CreateTextBox(); CreateTextBox();
AlignTextbox();
UpdateFormat(); UpdateFormat();
UpdateTextBoxFormat(); UpdateTextBoxFormat();
PropertyChanged += TextContainer_PropertyChanged; PropertyChanged += TextContainer_PropertyChanged;
FieldChanged += TextContainer_FieldChanged; FieldChanged += TextContainer_FieldChanged;
} }
@ -146,7 +144,6 @@ namespace Greenshot.Drawing {
} }
public void FitToText() { public void FitToText() {
UpdateFormat();
Size textSize = TextRenderer.MeasureText(text, _font); Size textSize = TextRenderer.MeasureText(text, _font);
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS); int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
Width = textSize.Width + lineThickness; Width = textSize.Width + lineThickness;
@ -154,6 +151,12 @@ namespace Greenshot.Drawing {
} }
void TextContainer_PropertyChanged(object sender, PropertyChangedEventArgs e) { void TextContainer_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (_textBox.Visible) {
_textBox.Invalidate();
}
UpdateTextBoxPosition();
UpdateTextBoxFormat();
if (e.PropertyName.Equals("Selected")) { if (e.PropertyName.Equals("Selected")) {
if (!Selected && _textBox.Visible) { if (!Selected && _textBox.Visible) {
HideTextBox(); HideTextBox();
@ -165,8 +168,6 @@ namespace Greenshot.Drawing {
} }
} }
if (_textBox.Visible) { if (_textBox.Visible) {
UpdateTextBoxPosition();
UpdateTextBoxFormat();
_textBox.Invalidate(); _textBox.Invalidate();
} }
} }
@ -175,10 +176,14 @@ namespace Greenshot.Drawing {
if (_textBox.Visible) { if (_textBox.Visible) {
_textBox.Invalidate(); _textBox.Invalidate();
} }
// Only dispose the font, and re-create it, when a font field has changed.
if (e.Field.FieldType.Name.StartsWith("FONT")) {
_font.Dispose(); _font.Dispose();
_font = null; _font = null;
fontInvalidated = true; UpdateFormat();
}
UpdateTextBoxFormat(); UpdateTextBoxFormat();
if (_textBox.Visible) { if (_textBox.Visible) {
_textBox.Invalidate(); _textBox.Invalidate();
} }
@ -202,24 +207,6 @@ namespace Greenshot.Drawing {
_textBox.Visible = false; _textBox.Visible = false;
} }
private void AlignTextbox() {
if (_textBox == null) {
return;
}
StringAlignment alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT);
switch (alignment) {
case StringAlignment.Near:
_textBox.TextAlign = HorizontalAlignment.Left;
break;
case StringAlignment.Far:
_textBox.TextAlign = HorizontalAlignment.Right;
break;
case StringAlignment.Center:
_textBox.TextAlign = HorizontalAlignment.Center;
break;
}
}
private void ShowTextBox() { private void ShowTextBox() {
_parent.KeysLocked = true; _parent.KeysLocked = true;
_parent.Controls.Add(_textBox); _parent.Controls.Add(_textBox);
@ -265,8 +252,7 @@ namespace Greenshot.Drawing {
float fontSize = GetFieldValueAsFloat(FieldType.FONT_SIZE); float fontSize = GetFieldValueAsFloat(FieldType.FONT_SIZE);
fontSize *= factor; fontSize *= factor;
SetFieldValue(FieldType.FONT_SIZE, fontSize); SetFieldValue(FieldType.FONT_SIZE, fontSize);
UpdateFormat();
fontInvalidated = true;
} }
/// <summary> /// <summary>
@ -278,7 +264,6 @@ namespace Greenshot.Drawing {
bool fontItalic = GetFieldValueAsBool(FieldType.FONT_ITALIC); bool fontItalic = GetFieldValueAsBool(FieldType.FONT_ITALIC);
float fontSize = GetFieldValueAsFloat(FieldType.FONT_SIZE); float fontSize = GetFieldValueAsFloat(FieldType.FONT_SIZE);
try { try {
if (fontInvalidated && fontFamily != null && fontSize != 0) {
FontStyle fs = FontStyle.Regular; FontStyle fs = FontStyle.Regular;
bool hasStyle = false; bool hasStyle = false;
@ -308,8 +293,7 @@ namespace Greenshot.Drawing {
} }
} }
_font = new Font(fam, fontSize, fs, GraphicsUnit.Pixel); _font = new Font(fam, fontSize, fs, GraphicsUnit.Pixel);
} _textBox.Font = _font;
fontInvalidated = false;
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.Data.Add("fontFamily", fontFamily); ex.Data.Add("fontFamily", fontFamily);
@ -352,11 +336,24 @@ namespace Greenshot.Drawing {
} }
private void UpdateTextBoxFormat() { private void UpdateTextBoxFormat() {
UpdateFormat(); if (_textBox == null) {
return;
}
StringAlignment alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT);
switch (alignment) {
case StringAlignment.Near:
_textBox.TextAlign = HorizontalAlignment.Left;
break;
case StringAlignment.Far:
_textBox.TextAlign = HorizontalAlignment.Right;
break;
case StringAlignment.Center:
_textBox.TextAlign = HorizontalAlignment.Center;
break;
}
Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR); Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR);
_textBox.ForeColor = lineColor; _textBox.ForeColor = lineColor;
_textBox.Font = _font;
AlignTextbox();
} }
void textBox_KeyDown(object sender, KeyEventArgs e) { void textBox_KeyDown(object sender, KeyEventArgs e) {
@ -375,7 +372,7 @@ namespace Greenshot.Drawing {
public override void Draw(Graphics graphics, RenderMode rm) { public override void Draw(Graphics graphics, RenderMode rm) {
base.Draw(graphics, rm); base.Draw(graphics, rm);
UpdateFormat();
graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality;