BUG-2011: Fix for unavailable (whatever reason) font

This commit is contained in:
Robin 2016-08-16 16:58:22 +02:00
parent 798ca503a5
commit c2603579e7
2 changed files with 220 additions and 117 deletions

View file

@ -32,12 +32,14 @@ using System.Drawing.Text;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Windows.Forms; using System.Windows.Forms;
namespace Greenshot.Drawing { namespace Greenshot.Drawing
{
/// <summary> /// <summary>
/// Represents a textbox (extends RectangleContainer for border/background support /// Represents a textbox (extends RectangleContainer for border/background support
/// </summary> /// </summary>
[Serializable] [Serializable]
public class TextContainer : RectangleContainer, ITextContainer { public class TextContainer : RectangleContainer, ITextContainer
{
// 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
// Although the name is wrong, we can't change it due to file serialization // Although the name is wrong, we can't change it due to file serialization
@ -61,16 +63,21 @@ namespace Greenshot.Drawing {
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
private string text; private string text;
// there is a binding on the following property! // there is a binding on the following property!
public string Text { public string Text
{
get { return text; } get { return text; }
set { set
{
ChangeText(value, true); ChangeText(value, true);
} }
} }
internal void ChangeText(string newText, bool allowUndoable) { internal void ChangeText(string newText, bool allowUndoable)
if ((text == null && newText != null) || !string.Equals(text, newText)) { {
if (makeUndoable && allowUndoable) { if ((text == null && newText != null) || !string.Equals(text, newText))
{
if (makeUndoable && allowUndoable)
{
makeUndoable = false; makeUndoable = false;
_parent.MakeUndoable(new TextChangeMemento(this), false); _parent.MakeUndoable(new TextChangeMemento(this), false);
} }
@ -79,11 +86,13 @@ namespace Greenshot.Drawing {
} }
} }
public TextContainer(Surface parent) : base(parent) { public TextContainer(Surface parent) : base(parent)
{
Init(); Init();
} }
protected override void InitializeFields() { protected override void InitializeFields()
{
AddField(GetType(), FieldType.LINE_THICKNESS, 2); AddField(GetType(), FieldType.LINE_THICKNESS, 2);
AddField(GetType(), FieldType.LINE_COLOR, Color.Red); AddField(GetType(), FieldType.LINE_COLOR, Color.Red);
AddField(GetType(), FieldType.SHADOW, true); AddField(GetType(), FieldType.SHADOW, true);
@ -100,22 +109,28 @@ namespace Greenshot.Drawing {
/// Do some logic to make sure all field are initiated correctly /// Do some logic to make sure all field are initiated correctly
/// </summary> /// </summary>
/// <param name="streamingContext">StreamingContext</param> /// <param name="streamingContext">StreamingContext</param>
protected override void OnDeserialized(StreamingContext streamingContext) { protected override void OnDeserialized(StreamingContext streamingContext)
{
base.OnDeserialized(streamingContext); base.OnDeserialized(streamingContext);
Init(); Init();
} }
protected override void Dispose(bool disposing) { protected override void Dispose(bool disposing)
if (disposing) { {
if (_font != null) { if (disposing)
{
if (_font != null)
{
_font.Dispose(); _font.Dispose();
_font = null; _font = null;
} }
if (_stringFormat != null) { if (_stringFormat != null)
{
_stringFormat.Dispose(); _stringFormat.Dispose();
_stringFormat = null; _stringFormat = null;
} }
if (_textBox != null) { if (_textBox != null)
{
_textBox.Dispose(); _textBox.Dispose();
_textBox = null; _textBox = null;
} }
@ -123,7 +138,8 @@ namespace Greenshot.Drawing {
base.Dispose(disposing); base.Dispose(disposing);
} }
private void Init() { private void Init()
{
_stringFormat = new StringFormat _stringFormat = new StringFormat
{ {
Trimming = StringTrimming.EllipsisWord Trimming = StringTrimming.EllipsisWord
@ -139,69 +155,89 @@ namespace Greenshot.Drawing {
} }
public override void Invalidate() { public override void Invalidate()
{
base.Invalidate(); base.Invalidate();
if (_textBox != null && _textBox.Visible) { if (_textBox != null && _textBox.Visible)
{
_textBox.Invalidate(); _textBox.Invalidate();
} }
} }
public void FitToText() { public void FitToText()
{
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;
Height = textSize.Height + lineThickness; Height = textSize.Height + lineThickness;
} }
void TextContainer_PropertyChanged(object sender, PropertyChangedEventArgs e) { void TextContainer_PropertyChanged(object sender, PropertyChangedEventArgs e)
if (_textBox.Visible) { {
if (_textBox.Visible)
{
_textBox.Invalidate(); _textBox.Invalidate();
} }
UpdateTextBoxPosition(); UpdateTextBoxPosition();
UpdateTextBoxFormat(); UpdateTextBoxFormat();
if (e.PropertyName.Equals("Selected")) { if (e.PropertyName.Equals("Selected"))
if (!Selected && _textBox.Visible) { {
if (!Selected && _textBox.Visible)
{
HideTextBox(); HideTextBox();
} else if (Selected && Status == EditStatus.DRAWING) { }
else if (Selected && Status == EditStatus.DRAWING)
{
ShowTextBox(); ShowTextBox();
} else if (_parent != null && Selected && Status == EditStatus.IDLE && _textBox.Visible) { }
else if (_parent != null && Selected && Status == EditStatus.IDLE && _textBox.Visible)
{
// Fix (workaround) for BUG-1698 // Fix (workaround) for BUG-1698
_parent.KeysLocked = true; _parent.KeysLocked = true;
} }
} }
if (_textBox.Visible) { if (_textBox.Visible)
{
_textBox.Invalidate(); _textBox.Invalidate();
} }
} }
void TextContainer_FieldChanged(object sender, FieldChangedEventArgs e) { void TextContainer_FieldChanged(object sender, FieldChangedEventArgs e)
if (_textBox.Visible) { {
if (_textBox.Visible)
{
_textBox.Invalidate(); _textBox.Invalidate();
} }
// Only dispose the font, and re-create it, when a font field has changed. // Only dispose the font, and re-create it, when a font field has changed.
if (e.Field.FieldType.Name.StartsWith("FONT")) { if (e.Field.FieldType.Name.StartsWith("FONT"))
{
if (_font != null) if (_font != null)
{ {
_font.Dispose(); _font.Dispose();
_font = null; _font = null;
} }
UpdateFormat(); UpdateFormat();
} else { }
else
{
UpdateAlignment(); UpdateAlignment();
} }
UpdateTextBoxFormat(); UpdateTextBoxFormat();
if (_textBox != null && _textBox.Visible) { if (_textBox != null && _textBox.Visible)
{
_textBox.Invalidate(); _textBox.Invalidate();
} }
} }
public override void OnDoubleClick() { public override void OnDoubleClick()
{
ShowTextBox(); ShowTextBox();
} }
private void CreateTextBox() { private void CreateTextBox()
{
_textBox = new TextBox _textBox = new TextBox
{ {
ImeMode = ImeMode.On, ImeMode = ImeMode.On,
@ -217,7 +253,8 @@ namespace Greenshot.Drawing {
_textBox.KeyDown += textBox_KeyDown; _textBox.KeyDown += textBox_KeyDown;
} }
private void ShowTextBox() { private void ShowTextBox()
{
if (_parent != null) if (_parent != null)
{ {
_parent.KeysLocked = true; _parent.KeysLocked = true;
@ -231,16 +268,21 @@ namespace Greenshot.Drawing {
/// <summary> /// <summary>
/// Makes textbox background dark if text color is very bright /// Makes textbox background dark if text color is very bright
/// </summary> /// </summary>
private void EnsureTextBoxContrast() { private void EnsureTextBoxContrast()
{
Color lc = GetFieldValueAsColor(FieldType.LINE_COLOR); Color lc = GetFieldValueAsColor(FieldType.LINE_COLOR);
if (lc.R > 203 && lc.G > 203 && lc.B > 203) { if (lc.R > 203 && lc.G > 203 && lc.B > 203)
{
_textBox.BackColor = Color.FromArgb(51, 51, 51); _textBox.BackColor = Color.FromArgb(51, 51, 51);
} else { }
else
{
_textBox.BackColor = Color.White; _textBox.BackColor = Color.White;
} }
} }
private void HideTextBox() { private void HideTextBox()
{
_parent.Focus(); _parent.Focus();
_textBox.Hide(); _textBox.Hide();
_parent.KeysLocked = false; _parent.KeysLocked = false;
@ -251,7 +293,8 @@ namespace Greenshot.Drawing {
/// Make sure the size of the font is scaled /// Make sure the size of the font is scaled
/// </summary> /// </summary>
/// <param name="matrix"></param> /// <param name="matrix"></param>
public override void Transform(Matrix matrix) { public override void Transform(Matrix matrix)
{
Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
int pixelsBefore = rect.Width * rect.Height; int pixelsBefore = rect.Width * rect.Height;
@ -268,59 +311,94 @@ namespace Greenshot.Drawing {
UpdateFormat(); UpdateFormat();
} }
/// <summary> private Font CreateFont(string fontFamily, bool fontBold, bool fontItalic, float fontSize)
/// Generate the Font-Formal so we can draw correctly {
/// </summary>
protected void UpdateFormat() {
string fontFamily = GetFieldValueAsString(FieldType.FONT_FAMILY);
bool fontBold = GetFieldValueAsBool(FieldType.FONT_BOLD);
bool fontItalic = GetFieldValueAsBool(FieldType.FONT_ITALIC);
float fontSize = GetFieldValueAsFloat(FieldType.FONT_SIZE);
try {
FontStyle fs = FontStyle.Regular; FontStyle fs = FontStyle.Regular;
bool hasStyle = false; bool hasStyle = false;
using(FontFamily fam = new FontFamily(fontFamily)) { using (FontFamily fam = new FontFamily(fontFamily))
{
bool boldAvailable = fam.IsStyleAvailable(FontStyle.Bold); bool boldAvailable = fam.IsStyleAvailable(FontStyle.Bold);
if (fontBold && boldAvailable) { if (fontBold && boldAvailable)
{
fs |= FontStyle.Bold; fs |= FontStyle.Bold;
hasStyle = true; hasStyle = true;
} }
bool italicAvailable = fam.IsStyleAvailable(FontStyle.Italic); bool italicAvailable = fam.IsStyleAvailable(FontStyle.Italic);
if (fontItalic && italicAvailable) { if (fontItalic && italicAvailable)
{
fs |= FontStyle.Italic; fs |= FontStyle.Italic;
hasStyle = true; hasStyle = true;
} }
if (!hasStyle) { if (!hasStyle)
{
bool regularAvailable = fam.IsStyleAvailable(FontStyle.Regular); bool regularAvailable = fam.IsStyleAvailable(FontStyle.Regular);
if (regularAvailable) { if (regularAvailable)
{
fs = FontStyle.Regular; fs = FontStyle.Regular;
} else { }
if (boldAvailable) { else
{
if (boldAvailable)
{
fs = FontStyle.Bold; fs = FontStyle.Bold;
} else if(italicAvailable) { }
else if (italicAvailable)
{
fs = FontStyle.Italic; fs = FontStyle.Italic;
} }
} }
} }
return new Font(fam, fontSize, fs, GraphicsUnit.Pixel);
}
}
/// <summary>
/// Generate the Font-Formal so we can draw correctly
/// </summary>
protected void UpdateFormat()
{
string fontFamily = GetFieldValueAsString(FieldType.FONT_FAMILY);
bool fontBold = GetFieldValueAsBool(FieldType.FONT_BOLD);
bool fontItalic = GetFieldValueAsBool(FieldType.FONT_ITALIC);
float fontSize = GetFieldValueAsFloat(FieldType.FONT_SIZE);
try
{
var newFont = CreateFont(fontFamily, fontBold, fontItalic, fontSize);
_font?.Dispose(); _font?.Dispose();
_font = new Font(fam, fontSize, fs, GraphicsUnit.Pixel); _font = newFont;
_textBox.Font = _font; _textBox.Font = _font;
} }
} catch (Exception ex) { catch (Exception ex)
{
// Problem, try again with the default
try
{
fontFamily = FontFamily.GenericSansSerif.Name;
SetFieldValue(FieldType.FONT_FAMILY, fontFamily);
var newFont = CreateFont(fontFamily, fontBold, fontItalic, fontSize);
_font?.Dispose();
_font = newFont;
_textBox.Font = _font;
}
catch (Exception ex2)
{
// When this happens... the PC is broken
ex.Data.Add("fontFamily", fontFamily); ex.Data.Add("fontFamily", fontFamily);
ex.Data.Add("fontBold", fontBold); ex.Data.Add("fontBold", fontBold);
ex.Data.Add("fontItalic", fontItalic); ex.Data.Add("fontItalic", fontItalic);
ex.Data.Add("fontSize", fontSize); ex.Data.Add("fontSize", fontSize);
throw; throw ex;
}
} }
UpdateAlignment(); UpdateAlignment();
} }
private void UpdateAlignment() { private void UpdateAlignment()
{
_stringFormat.Alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT); _stringFormat.Alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT);
_stringFormat.LineAlignment = (StringAlignment)GetFieldValue(FieldType.TEXT_VERTICAL_ALIGNMENT); _stringFormat.LineAlignment = (StringAlignment)GetFieldValue(FieldType.TEXT_VERTICAL_ALIGNMENT);
} }
@ -329,36 +407,43 @@ namespace Greenshot.Drawing {
/// This will create the textbox exactly to the inner size of the element /// This will create the textbox exactly to the inner size of the element
/// is a bit of a hack, but for now it seems to work... /// is a bit of a hack, but for now it seems to work...
/// </summary> /// </summary>
private void UpdateTextBoxPosition() { private void UpdateTextBoxPosition()
{
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS); int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
int lineWidth = (int)Math.Floor(lineThickness / 2d); int lineWidth = (int)Math.Floor(lineThickness / 2d);
int correction = (lineThickness + 1) % 2; int correction = (lineThickness + 1) % 2;
if (lineThickness <= 1) { if (lineThickness <= 1)
{
lineWidth = 1; lineWidth = 1;
correction = -1; correction = -1;
} }
Rectangle absRectangle = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); Rectangle absRectangle = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
_textBox.Left = absRectangle.Left + lineWidth; _textBox.Left = absRectangle.Left + lineWidth;
_textBox.Top = absRectangle.Top + lineWidth; _textBox.Top = absRectangle.Top + lineWidth;
if (lineThickness <= 1) { if (lineThickness <= 1)
{
lineWidth = 0; lineWidth = 0;
} }
_textBox.Width = absRectangle.Width - 2 * lineWidth + correction; _textBox.Width = absRectangle.Width - 2 * lineWidth + correction;
_textBox.Height = absRectangle.Height - 2 * lineWidth + correction; _textBox.Height = absRectangle.Height - 2 * lineWidth + correction;
} }
public override void ApplyBounds(RectangleF newBounds) { public override void ApplyBounds(RectangleF newBounds)
{
base.ApplyBounds(newBounds); base.ApplyBounds(newBounds);
UpdateTextBoxPosition(); UpdateTextBoxPosition();
} }
private void UpdateTextBoxFormat() { private void UpdateTextBoxFormat()
if (_textBox == null) { {
if (_textBox == null)
{
return; return;
} }
StringAlignment alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT); StringAlignment alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT);
switch (alignment) { switch (alignment)
{
case StringAlignment.Near: case StringAlignment.Near:
_textBox.TextAlign = HorizontalAlignment.Left; _textBox.TextAlign = HorizontalAlignment.Left;
break; break;
@ -374,21 +459,25 @@ namespace Greenshot.Drawing {
_textBox.ForeColor = lineColor; _textBox.ForeColor = lineColor;
} }
void textBox_KeyDown(object sender, KeyEventArgs e) { void textBox_KeyDown(object sender, KeyEventArgs e)
{
// ESC and Enter/Return (w/o Shift) hide text editor // ESC and Enter/Return (w/o Shift) hide text editor
if(e.KeyCode == Keys.Escape || ((e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter) && e.Modifiers == Keys.None)) { if (e.KeyCode == Keys.Escape || ((e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter) && e.Modifiers == Keys.None))
{
HideTextBox(); HideTextBox();
e.SuppressKeyPress = true; e.SuppressKeyPress = true;
} }
} }
void textBox_LostFocus(object sender, EventArgs e) { void textBox_LostFocus(object sender, EventArgs e)
{
// next change will be made undoable // next change will be made undoable
makeUndoable = true; makeUndoable = true;
HideTextBox(); HideTextBox();
} }
public override void Draw(Graphics graphics, RenderMode rm) { public override void Draw(Graphics graphics, RenderMode rm)
{
base.Draw(graphics, rm); base.Draw(graphics, rm);
graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality;
@ -398,11 +487,13 @@ namespace Greenshot.Drawing {
graphics.TextRenderingHint = TextRenderingHint.SystemDefault; graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
if (Selected && rm == RenderMode.EDIT) { if (Selected && rm == RenderMode.EDIT)
{
DrawSelectionBorder(graphics, rect); DrawSelectionBorder(graphics, rect);
} }
if (string.IsNullOrEmpty(text) ) { if (string.IsNullOrEmpty(text))
{
return; return;
} }
@ -427,21 +518,26 @@ namespace Greenshot.Drawing {
/// <param name="stringFormat"></param> /// <param name="stringFormat"></param>
/// <param name="text"></param> /// <param name="text"></param>
/// <param name="font"></param> /// <param name="font"></param>
public static void DrawText(Graphics graphics, Rectangle drawingRectange, int lineThickness, Color fontColor, bool drawShadow, StringFormat stringFormat, string text, Font font) { public static void DrawText(Graphics graphics, Rectangle drawingRectange, int lineThickness, Color fontColor, bool drawShadow, StringFormat stringFormat, string text, Font font)
{
int textOffset = lineThickness > 0 ? (int)Math.Ceiling(lineThickness / 2d) : 0; int textOffset = lineThickness > 0 ? (int)Math.Ceiling(lineThickness / 2d) : 0;
// draw shadow before anything else // draw shadow before anything else
if (drawShadow) { if (drawShadow)
{
int basealpha = 100; int basealpha = 100;
int alpha = basealpha; int alpha = basealpha;
int steps = 5; int steps = 5;
int currentStep = 1; int currentStep = 1;
while (currentStep <= steps) { while (currentStep <= steps)
{
int offset = currentStep; int offset = currentStep;
Rectangle shadowRect = GuiRectangle.GetGuiRectangle(drawingRectange.Left + offset, drawingRectange.Top + offset, drawingRectange.Width, drawingRectange.Height); Rectangle shadowRect = GuiRectangle.GetGuiRectangle(drawingRectange.Left + offset, drawingRectange.Top + offset, drawingRectange.Width, drawingRectange.Height);
if (lineThickness > 0) { if (lineThickness > 0)
{
shadowRect.Inflate(-textOffset, -textOffset); shadowRect.Inflate(-textOffset, -textOffset);
} }
using (Brush fontBrush = new SolidBrush(Color.FromArgb(alpha, 100, 100, 100))) { using (Brush fontBrush = new SolidBrush(Color.FromArgb(alpha, 100, 100, 100)))
{
graphics.DrawString(text, font, fontBrush, shadowRect, stringFormat); graphics.DrawString(text, font, fontBrush, shadowRect, stringFormat);
currentStep++; currentStep++;
alpha = alpha - basealpha / steps; alpha = alpha - basealpha / steps;
@ -449,19 +545,25 @@ namespace Greenshot.Drawing {
} }
} }
if (lineThickness > 0) { if (lineThickness > 0)
{
drawingRectange.Inflate(-textOffset, -textOffset); drawingRectange.Inflate(-textOffset, -textOffset);
} }
using (Brush fontBrush = new SolidBrush(fontColor)) { using (Brush fontBrush = new SolidBrush(fontColor))
if (stringFormat != null) { {
if (stringFormat != null)
{
graphics.DrawString(text, font, fontBrush, drawingRectange, stringFormat); graphics.DrawString(text, font, fontBrush, drawingRectange, stringFormat);
} else { }
else
{
graphics.DrawString(text, font, fontBrush, drawingRectange); graphics.DrawString(text, font, fontBrush, drawingRectange);
} }
} }
} }
public override bool ClickableAt(int x, int y) { public override bool ClickableAt(int x, int y)
{
Rectangle r = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); Rectangle r = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
r.Inflate(5, 5); r.Inflate(5, 5);
return r.Contains(x, y); return r.Contains(x, y);

View file

@ -26,6 +26,7 @@ Fixed:
* BUG-1949: Can't delete Imgur upload * BUG-1949: Can't delete Imgur upload
* BUG-1965: Activation border around window is visible in the capture * BUG-1965: Activation border around window is visible in the capture
* BUG-1991: Greenshot portable (PAF) uses wrong log configuration * BUG-1991: Greenshot portable (PAF) uses wrong log configuration
* BUG-2011: Editor issues when the font is gone or broken
* FEATURE-916: Added ico file format * FEATURE-916: Added ico file format
* FEATURE-919: Allow adding of space around screenshot (use Ctrl + / Ctrl -) * FEATURE-919: Allow adding of space around screenshot (use Ctrl + / Ctrl -)
* FEATURE-945: Added environment variables resolving to the external command * FEATURE-945: Added environment variables resolving to the external command