|
| 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 | +} |
0 commit comments