Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimenting with Linux support. #524

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
12 changes: 11 additions & 1 deletion Domain/Misc/ConnectionIconBlock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
using PdfSharp.Drawing;

namespace Trizbort.Domain.Misc {
Expand Down Expand Up @@ -84,7 +86,15 @@ public void DrawBlock(XGraphics graphics) {
//


graphics.DrawImage(Image, pos.ToPointF() + offsets);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
graphics.DrawImage(Image, pos.ToPointF() + offsets);
}
else {
MemoryStream strm = new MemoryStream();
Image.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
XImage ximg = XImage.FromStream(strm);
graphics.DrawImage(ximg, pos.ToPointF() + offsets);
}
}
}
}
45 changes: 37 additions & 8 deletions Domain/Misc/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ THE SOFTWARE.
using System.Drawing;
using System.Linq;
using PdfSharp.Drawing;
using System.Runtime.InteropServices;

namespace Trizbort.Domain.Misc {
internal class TextBlock {
Expand All @@ -45,9 +46,37 @@ internal class TextBlock {
private Vector m_size;
private XSize m_sizeChecker;
private string m_text = string.Empty;
// This provides a perhaps more useful/educational exception.
// It appears that PDFSharp still has Windows dependencies?
// private XFont initialXFont = new XFont("Arial", 12F);

public static int RebuildCount => s_rebuildCount;

private XSize BrokenMeasureString(XGraphics graphics, String str, Font font) {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
return graphics.MeasureString(str, font);
}
else {
Console.WriteLine(String.Format("Measured string '{0}' as ({1}, {2})",
str, str.Length * 9F, font.Height));
return new XSize(str.Length * 9F, font.Height);
}
}

private void BrokenDrawString(XGraphics graphics, String str, Font font, Brush brush,
double x, double y, XStringFormat fmt) {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
graphics.DrawString(str, font, brush, x, y, fmt);
}
else {
Console.WriteLine("Not drawing string: " + str);
// graphics.DrawString(str, font, brush, x, y, fmt); // Crash!
// XFont xfont = font; // Crash!
// XFont xfont = new XFont("Arial", 9F); // Crash!
// XBrush xbrush = brush; // Does NOT crash.
}
}

public string Text {
get => m_text;
set {
Expand Down Expand Up @@ -76,7 +105,7 @@ public string Text {
public Rect Draw(XGraphics graphics, Font font, Brush brush, Vector pos, Vector size, XStringFormat format) {
// do a quick test to see if text is going to get drawn at the same size as last time;
// if so, assume we don't need to recompute our layout for that reason.
var sizeChecker = graphics.MeasureString("M q", font);
var sizeChecker = BrokenMeasureString(graphics, "M q", font);
if (sizeChecker != m_sizeChecker ||
pos != m_pos ||
m_size != size ||
Expand Down Expand Up @@ -117,7 +146,7 @@ public Rect Draw(XGraphics graphics, Font font, Brush brush, Vector pos, Vector
graphics.SmoothingMode = XSmoothingMode.HighQuality;


graphics.DrawString(line, font, brush, origin.X, origin.Y, m_actualFormat);
BrokenDrawString(graphics, line, font, brush, origin.X, origin.Y, m_actualFormat);
origin += m_delta;
size.Y -= m_lineHeight;
}
Expand Down Expand Up @@ -146,17 +175,17 @@ private void RebuildCachedLayout(XGraphics graphics, Font font, ref Vector pos,
m_size = size;

var text = m_text;
if (text.IndexOf('\n') == -1 && size.X > 0 && size.Y > 0 && graphics.MeasureString(text, font).Width > size.X) {
if (text.IndexOf('\n') == -1 && size.X > 0 && size.Y > 0 && BrokenMeasureString(graphics, text, font).Width > size.X) {
// wrap single-line text to fit in rectangle

// measure a space, countering the APIs unwillingness to measure spaces
var spaceLength = (float) (graphics.MeasureString("M M", font).Width - graphics.MeasureString("M", font).Width * 2);
var hyphenLength = (float) graphics.MeasureString("-", font).Width;
var spaceLength = (float) (BrokenMeasureString(graphics, "M M", font).Width - BrokenMeasureString(graphics, "M", font).Width * 2);
var hyphenLength = (float) BrokenMeasureString(graphics, "-", font).Width;

var wordsStep1 = new List<Word>();
foreach (var word in text.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries)) {
if (wordsStep1.Count != 0) wordsStep1.Add(new Word(" ", spaceLength));
wordsStep1.Add(new Word(word, (float) graphics.MeasureString(word, font).Width));
wordsStep1.Add(new Word(word, (float) BrokenMeasureString(graphics, word, font).Width));
}

bool isSplitDash = Setup.Settings.WrapTextAtDashes;
Expand All @@ -170,14 +199,14 @@ private void RebuildCachedLayout(XGraphics graphics, Font font, ref Vector pos,
else if (tWordList.Count != 0 && isSplitDash)
tWordList.Add(new Word("-", hyphenLength));

tWordList.Add(new Word(tWord, (float) graphics.MeasureString(tWord, font).Width));
tWordList.Add(new Word(tWord, (float) BrokenMeasureString(graphics, tWord, font).Width));
}

words.AddRange(tWordList);
} else {
if (words.Count != 0)
words.Add(new Word(" ", spaceLength));
words.Add(new Word(splits[0], (float) graphics.MeasureString(splits[0], font).Width));
words.Add(new Word(splits[0], (float) BrokenMeasureString(graphics, splits[0], font).Width));
}

var lineLength = 0.0f;
Expand Down
23 changes: 17 additions & 6 deletions Domain/StatusBar/Status.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Trizbort.Domain.StatusBar {
public enum StatusItems {
Expand Down Expand Up @@ -92,12 +93,22 @@ private void updateInfoMessage(IStatusWidget helpItem) {
}

private void setDefaultItems() {
Items = new List<StatusItem> {
new StatusItem {Id = StatusItems.tsb_Info, Show = true},
new StatusItem {Id = StatusItems.tsb_CapsLock, Show = true, Widget = new CapsLockStatusWidget()},
new StatusItem {Id = StatusItems.tsb_NumLock, Show = true, Widget = new NumLockStatusWidget()},
new StatusItem {Id = StatusItems.tsb_Zoom, Show = true, Widget = new ZoomStatusWidget()}
};
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
Items = new List<StatusItem> {
new StatusItem {Id = StatusItems.tsb_Info, Show = true},
new StatusItem {Id = StatusItems.tsb_CapsLock, Show = true, Widget = new CapsLockStatusWidget()},
new StatusItem {Id = StatusItems.tsb_NumLock, Show = true, Widget = new NumLockStatusWidget()},
new StatusItem {Id = StatusItems.tsb_Zoom, Show = true, Widget = new ZoomStatusWidget()}
};
}
// Is KeyLocked() is not supported in Mono on Linux and MacOS yet.
// https://github.com/mono/mono/blob/main/mcs/class/System.Windows.Forms/System.Windows.Forms/Control.cs#L3487
else {
Items = new List<StatusItem> {
new StatusItem {Id = StatusItems.tsb_Info, Show = true},
new StatusItem {Id = StatusItems.tsb_Zoom, Show = true, Widget = new ZoomStatusWidget()}
};
}
}
}
}
9 changes: 2 additions & 7 deletions UI/Controls/Canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,13 +1778,8 @@ private void drawElements(XGraphics graphics, Palette palette, bool finalRender)
context.Hover = !context.Selected && element == HoverElement && !finalRender;
if (context.Hover && dragMode == DragModes.MovePort) context.Hover = false;

try {
var elementBounds = element.UnionBoundsWith(Rect.Empty, true).ToRectangleF();
if (finalRender || clipToScreen.IntersectsWith(elementBounds)) element.Draw(graphics, palette, context);
}
catch (Exception) {
// avoid GDI+ exceptions (vast shapes, etc.) taking down the canvas
}
var elementBounds = element.UnionBoundsWith(Rect.Empty, true).ToRectangleF();
if (finalRender || clipToScreen.IntersectsWith(elementBounds)) element.Draw(graphics, palette, context);
}
}

Expand Down
4 changes: 3 additions & 1 deletion UI/Controls/TrizbortTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public string Watermark {

private void updateCue() {
if (this.IsHandleCreated && mCue != null) {
SendMessage(this.Handle, EM_SETCUEBANNER, (IntPtr) 1, mCue);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
SendMessage(this.Handle, EM_SETCUEBANNER, (IntPtr) 1, mCue);
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions UI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,12 @@ private void FileExportHugoMenuItem_Click(object sender, EventArgs e) {

private void FileExportImageMenuItem_Click(object sender, EventArgs e) {
using (var dialog = new SaveFileDialog()) {
dialog.Filter = "PNG Images|*.png|JPEG Images|*.jpg|BMP Images|*.bmp|Enhanced Metafiles (EMF)|*.emf|All Files|*.*||";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
dialog.Filter = "PNG Images|*.png|JPEG Images|*.jpg|BMP Images|*.bmp|Enhanced Metafiles (EMF)|*.emf|All Files|*.*||";
}
else {
dialog.Filter = "PNG Images|*.png|JPEG Images|*.jpg|BMP Images|*.bmp|All Files|*.*||";
}
dialog.Title = "Export Image";
dialog.DefaultExt = getExtensionForDefaultImageType();
dialog.InitialDirectory = PathHelper.SafeGetDirectoryName(ApplicationSettingsController.AppSettings.LastExportImageFileName);
Expand Down Expand Up @@ -750,7 +755,9 @@ private bool saveImage(string fileName) {
format = ImageFormat.Jpeg;
else if (StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".bmp") == 0)
format = ImageFormat.Bmp;
else if (StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".emf") == 0) format = ImageFormat.Emf;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
&& StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".emf") == 0)
format = ImageFormat.Emf;

var size = Canvas.ComputeCanvasBounds(true).Size * (ApplicationSettingsController.AppSettings.SaveAt100 ? 1.0f : Canvas.ZoomFactor);
size.X = Numeric.Clamp(size.X, 16, 8192);
Expand Down