Switch-Toolbox/Switch_Toolbox_Library/Imaging/BitmapFont.cs

157 lines
5.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
//Thanks EFE!
//https://github.com/Gericom/EveryFileExplorer/blob/f9f00d193c9608d71c9a23d9f3ab7e752f4ada2a/LibEveryFileExplorer/GFX/BitmapFont.cs
namespace LibEveryFileExplorer.GFX
{
public class BitmapFont : IDisposable
{
public BitmapFont()
{
Characters = new Dictionary<char, Character>();
}
public Dictionary<char, Character> Characters { get; private set; }
public int LineHeight { get; set; }
public int GetLineWidth(String Text, FontRenderSettings Settings, out int LineEnd)
{
LineEnd = -1;
int result = 0;
int i = 0;
foreach (char c in Text)
{
if (c == '\n')
{
if (Text.Length > i + 1) LineEnd = i + 1;
break;
}
else if (c == '\r') { i++; continue; }
if (!Characters.ContainsKey(c)) result += Settings.CharSpacing * 4;
else
{
Character info = Characters[c];
result += (int)(info.CharWidth * Settings.XScale) + Settings.CharSpacing;
}
i++;
}
if (result > 0) result -= Settings.CharSpacing;
return result;
}
public Bitmap PrintToBitmap(String Text, FontRenderSettings Settings)
{
int Width = 0;
int Height = 0;
int linestart = 0;
do
{
int lns;
int linewidth = GetLineWidth(Text.Substring(linestart), Settings, out lns);
if (lns != -1) linestart += lns;
else linestart = -1;
if (linewidth > Width) Width = linewidth;
Height += (int)(LineHeight * Settings.YScale) + Settings.LineSpacing;
}
while (linestart != -1);
if (Width == 0 || Height == 0) return null;
Height -= Settings.LineSpacing;
Width += 2;
Bitmap b = new Bitmap(Width, Height);
using (Graphics g = Graphics.FromImage(b))
{
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
int X = 0;
int Y = 0;
foreach (char c in Text)
{
if (c == '\n')
{
X = 0;
Y += (int)(LineHeight * Settings.YScale) + Settings.LineSpacing;
continue;
}
else if (c == '\r') continue;
if (!Characters.ContainsKey(c)) X += Settings.CharSpacing * 4;
else
{
Character info = Characters[c];
g.DrawImage(info.CharBitmap,
new RectangleF(X + info.LeftOffset, Y, info.GlyphWidth * Settings.XScale, LineHeight * Settings.YScale),
new Rectangle(0, 0, info.GlyphWidth, LineHeight),
GraphicsUnit.Pixel);
X += (int)(info.CharWidth * Settings.XScale) + Settings.CharSpacing;
}
}
}
return b;
}
public void Dispose()
{
if (Characters == null) return;
foreach (var character in Characters.Values)
character.CharBitmap?.Dispose();
Characters.Clear();
GC.SuppressFinalize(this);
}
public class Character
{
public Character(Bitmap CharBitmap, int LeftOffset, int GlyphWidth, int CharWidth)
{
this.CharBitmap = CharBitmap;
this.LeftOffset = LeftOffset;
this.GlyphWidth = GlyphWidth;
this.CharWidth = CharWidth;
}
public Bitmap CharBitmap { get; set; }
public int LeftOffset { get; set; }
public int GlyphWidth { get; set; }
public int CharWidth { get; set; }
}
public class FontRenderSettings
{
public FontRenderSettings()
{
CharSpacing = 3;
XScale = YScale = 1;
HAlignment = XAlignment.Left;
VAlignment = YAlignment.Top;
}
Tons more layout editor improvements. - The UI has been completely redone. It's far much more clean and intuitive to edit with. - All major pane types can be created now. Part panes are not supported but will be added in a future update due to being more complex to mess with. - Window panes can be fully customized now, with custom frame adjusting, adding, and editing materials per frame and content regions. - Picture panes have improved UV editing, and vertex color editing (which can set by corner or all at once). - Text boxes will have a dialog for selecting the font file. These also can be switchted in the text editor. - Improved pane deleting signifcantly. Material references are removed, and undo/redo works perfectly fine. - Fixed many flags for properties which didn't get set correctly if edited. - Fixed layout saving for text boxes with using the wrong encoding. Also some padding fixes. - Text panes now auto calculate the text length and allow restricted lengths to be edited. - Properties can now be scrolled down, and kept at that state when refocused. - Add a selection box for selecting multiple panes at once - Textures can be added, removed and edited in editor. Make sure these are in the same archive!!! Wii U auto does it in the same archive opened, switch must have a bntx in it. Automatic creaton of these will come - Picture panes can be generated via textures. Drag and drop one from a list. Also keeps the original image sizes. - Fixed window pane rendering with 1 frame and flipping textures. - Materials can add textures, and have new custom blend and alpha modes. when i finish the new layout export dialog. - Added an edit option for image editor to gamma fix smash ultimate bntx.
2019-10-14 01:02:39 +00:00
public Color BottomColor;
public Color TopColor;
public int CharSpacing { get; set; }
public int LineSpacing { get; set; }
public float XScale { get; set; }
public float YScale { get; set; }
public XAlignment HAlignment { get; set; }
public YAlignment VAlignment { get; set; }
public enum XAlignment
{
Left, Center, Right
}
public enum YAlignment
{
Top, Center, Bottom
}
}
}
}