Skip to content

Commit d9b5ca1

Browse files
committed
Adjustments for buggy linux drawing
1 parent ede2864 commit d9b5ca1

File tree

3 files changed

+126
-20
lines changed

3 files changed

+126
-20
lines changed

TS3AudioBot/Helper/ImageUtil.cs

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// TS3AudioBot - An advanced Musicbot for Teamspeak 3
2+
// Copyright (C) 2016 TS3AudioBot contributors
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace TS3AudioBot.Helper
18+
{
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Drawing;
22+
using System.Drawing.Drawing2D;
23+
using System.Drawing.Text;
24+
25+
static class ImageUtil
26+
{
27+
private static StringFormat avatarTextFormat = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near };
28+
private static Pen avatarTextOutline = new Pen(Color.Black, 4) { LineJoin = LineJoin.Round };
29+
30+
public static void BuildStringImage(string str, Image img, RectangleF rect)
31+
{
32+
using (var graphics = Graphics.FromImage(img))
33+
{
34+
if (Util.IsLinux)
35+
{
36+
BuildStringImageLinux(str, graphics, rect);
37+
}
38+
else
39+
{
40+
using (var gp = new GraphicsPath())
41+
{
42+
gp.AddString(str, FontFamily.GenericSansSerif, 0, 15, rect, avatarTextFormat);
43+
44+
graphics.InterpolationMode = InterpolationMode.High;
45+
graphics.SmoothingMode = SmoothingMode.HighQuality;
46+
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
47+
graphics.CompositingQuality = CompositingQuality.HighQuality;
48+
49+
graphics.DrawPath(avatarTextOutline, gp);
50+
graphics.FillPath(Brushes.White, gp);
51+
}
52+
}
53+
}
54+
}
55+
56+
private static void BuildStringImageLinux(string str, Graphics target, RectangleF rect)
57+
{
58+
const int maxMonoBugWidth = 150;
59+
60+
using (var gp = new GraphicsPath())
61+
using (var builder = new Bitmap(maxMonoBugWidth, 100))
62+
using (var bg = Graphics.FromImage(builder))
63+
{
64+
gp.AddString("X", FontFamily.GenericMonospace, 0, 15, rect, avatarTextFormat);
65+
var bounds = gp.GetBounds();
66+
var charW = bounds.Width;
67+
var charH = bounds.Height * 2;
68+
if (charW < 0.1e-6)
69+
return;
70+
71+
var buildRect = new RectangleF(0, 0, maxMonoBugWidth, charH);
72+
73+
var sep = new List<string>();
74+
75+
bg.InterpolationMode = InterpolationMode.High;
76+
bg.SmoothingMode = SmoothingMode.HighQuality;
77+
bg.TextRenderingHint = TextRenderingHint.AntiAlias;
78+
bg.CompositingQuality = CompositingQuality.HighQuality;
79+
target.CompositingQuality = CompositingQuality.HighQuality;
80+
81+
int lastBreak = 0;
82+
int lastBreakOption = 0;
83+
for (int i = 0; i < str.Length; i++)
84+
{
85+
if (!char.IsLetterOrDigit(str[i]))
86+
{
87+
lastBreakOption = i;
88+
}
89+
90+
if ((i - lastBreak) * charW >= rect.Width && lastBreak != lastBreakOption)
91+
{
92+
sep.Add(str.Substring(lastBreak, lastBreakOption - lastBreak));
93+
lastBreak = lastBreakOption;
94+
}
95+
}
96+
sep.Add(str.Substring(lastBreak));
97+
98+
var step = (int)(maxMonoBugWidth / charW) - 1;
99+
for (int i = 0; i < sep.Count; i++)
100+
{
101+
var line = sep[i];
102+
float flLeft = 0;
103+
for (int j = 0; j * step < line.Length; j++)
104+
{
105+
var part = line.Substring(j * step, Math.Min(step, line.Length - j * step));
106+
gp.Reset();
107+
gp.AddString(part, FontFamily.GenericMonospace, 0, 15, buildRect, avatarTextFormat);
108+
109+
bg.Clear(Color.Transparent);
110+
bg.DrawPath(avatarTextOutline, gp);
111+
bg.FillPath(Brushes.White, gp);
112+
113+
target.DrawImageUnscaled(builder, (int)(rect.X + j * (maxMonoBugWidth - 5)), (int)(rect.Y + i * charH));
114+
flLeft += gp.GetBounds().Width;
115+
}
116+
}
117+
}
118+
}
119+
}
120+
}

TS3AudioBot/MainBot.cs

+5-20
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ internal static void Main(string[] args)
8080
public RightsManager RightsManager { get; private set; }
8181

8282
public bool QuizMode { get; set; }
83-
private StringFormat avatarTextFormat = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near };
84-
private Pen avatarTextOutline = new Pen(Color.Black, 4) { LineJoin = System.Drawing.Drawing2D.LineJoin.Round };
8583

8684
public MainBot()
8785
{
@@ -1606,21 +1604,10 @@ private void GenerateStatusImage(object sender, EventArgs e)
16061604

16071605
using (var bmp = thumresult.Value)
16081606
{
1609-
using (var graphics = Graphics.FromImage(bmp))
1610-
using (var gp = new System.Drawing.Drawing2D.GraphicsPath())
1611-
{
1612-
gp.AddString("Now playing: " + startEvent.ResourceData.ResourceTitle,
1613-
FontFamily.GenericSansSerif, 0, 15,
1614-
new RectangleF(0, 0, bmp.Width, bmp.Height), avatarTextFormat);
1615-
1616-
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
1617-
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
1618-
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
1619-
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
1620-
1621-
graphics.DrawPath(avatarTextOutline, gp);
1622-
graphics.FillPath(Brushes.White, gp);
1623-
}
1607+
ImageUtil.BuildStringImage(
1608+
"Now playing: " + startEvent.ResourceData.ResourceTitle,
1609+
bmp,
1610+
new RectangleF(0, 0, bmp.Width, bmp.Height));
16241611
using (var mem = new MemoryStream())
16251612
{
16261613
bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Png);
@@ -1657,9 +1644,7 @@ public void Dispose()
16571644
if (!isDisposed) isDisposed = true;
16581645
else return;
16591646
Log.Write(Log.Level.Info, "Exiting...");
1660-
1661-
avatarTextOutline.Dispose();
1662-
1647+
16631648
WebManager?.Dispose(); // before: logStream,
16641649
WebManager = null;
16651650

TS3AudioBot/TS3AudioBot.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="CommandSystem\CommandResults\JsonCommandResult.cs" />
9999
<Compile Include="CommandSystem\CommandResults\StringCommandResult.cs" />
100100
<Compile Include="Helper\Cache.cs" />
101+
<Compile Include="Helper\ImageUtil.cs" />
101102
<Compile Include="ResourceFactories\AudioType.cs" />
102103
<Compile Include="ResourceFactories\IFactory.cs" />
103104
<Compile Include="ResourceFactories\IThumbnailFactory.cs" />

0 commit comments

Comments
 (0)