Skip to content

Commit

Permalink
FontGenerator updates (#2)
Browse files Browse the repository at this point in the history
* FontGenerator updates

- FontGenerator Save functionality added
- Black/White & White/Black preview added to make easiter to validate font quality.
- Font list initializatin cleaned

* Character bitmap offset X,Y implemented
  • Loading branch information
jyberg authored and hagronnestad committed Jan 7, 2019
1 parent 559f547 commit 0b73026
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 176 deletions.
426 changes: 273 additions & 153 deletions NextionFontEditor/NextionFontEditor/FormFontGenerator.Designer.cs

Large diffs are not rendered by default.

39 changes: 30 additions & 9 deletions NextionFontEditor/NextionFontEditor/FormFontGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class FormFontGenerator : Form {
public FormFontGenerator() {
InitializeComponent();
}
private ZiFont ziFont = new ZiFont();

private void FormFontGenerator_Load(object sender, EventArgs e) {
InitializeNextionFontSizesList();
Expand Down Expand Up @@ -46,7 +47,7 @@ private void CreatePreview() {
var codePage = CodePages.GetCodePage(CodePageIdentifier.ISO_8859_1);

var fontName = lstFonts.SelectedItem?.ToString() ?? "";
var fontSize = (int) numFontSize.Value;
var fontSize = (int)numFontSize.Value;

var size = int.Parse(cmbNextionFontSize.Text);
var width = size / 2;
Expand All @@ -66,18 +67,24 @@ private void CreatePreview() {

var bChar = new Bitmap(sChar.Width, sChar.Height);

using (var gChar = CreateGraphics(bChar)) {
var sb = bChar.Width > bPreview.Width ? new SolidBrush(Color.LightCoral) : new SolidBrush(Color.LightGreen);
using (var gChar = CreateGraphics(bChar))
{
var sb = PreviewTest.Checked ?
bChar.Width > bPreview.Width ? new SolidBrush(Color.LightCoral) : new SolidBrush(Color.LightGreen) :
PreviewBW.Checked ? new SolidBrush(Color.White) : new SolidBrush(Color.Black);

gChar.FillRectangle(sb, 0, 0, sChar.Width, sChar.Height);

gChar.DrawString(c.ToString(), font,
new SolidBrush(Color.Black),
0, 0, StringFormat.GenericTypographic
PreviewWB.Checked ? new SolidBrush(Color.White) : new SolidBrush(Color.Black),
(float)numCharOffsetX.Value, (float)numCharOffsetY.Value, StringFormat.GenericTypographic
);
}

gPreview.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
gPreview.FillRectangle(PreviewTest.Checked ?
bChar.Width > bPreview.Width ? new SolidBrush(Color.LightCoral) : new SolidBrush(Color.LightGreen) :
PreviewBW.Checked ? new SolidBrush(Color.White) : new SolidBrush(Color.Black),
0, 0, width, height);

if (bChar.Width > bPreview.Width) {
gPreview.DrawImage(bChar, 0, 0, width, height);
Expand All @@ -89,7 +96,7 @@ private void CreatePreview() {

pPreviews.Add(bPreview);
}

panelPreview.BackColor = PreviewTest.Checked ? Color.Transparent : PreviewBW.Checked ? Color.White : Color.Black;
panelPreview.SuspendLayout();
panelPreview.Controls.Clear();
panelPreview.Controls.AddRange(
Expand All @@ -101,8 +108,7 @@ private void CreatePreview() {
}).ToArray());
panelPreview.ResumeLayout();

var newZiFont = ZiFont.FromCharacterBitmaps("test", (byte) width, (byte) height, codePage, pPreviews);
newZiFont.Save("test.zi", codePage);
ziFont = ZiFont.FromCharacterBitmaps(fontName+" "+ cmbNextionFontSize.Text, (byte) width, (byte) height, codePage, pPreviews, PreviewWB.Checked);
}

private int GetMaxFontSizeForRect(string text, String fontName, int fontSize, SizeF rect) {
Expand Down Expand Up @@ -160,5 +166,20 @@ private void rbUseSingleCharacterMaxSize_CheckedChanged(object sender, EventArgs
private void numDbs_ValueChanged(object sender, EventArgs e) {
CreatePreview();
}

private void btnSave_Click(object sender, EventArgs e)
{
sfd.FileName = ziFont.Name;
var res = sfd.ShowDialog();
if (res == DialogResult.OK)
{
ziFont.Save(sfd.FileName);
}
}

private void PreviewTest_CheckedChanged(object sender, EventArgs e)
{
CreatePreview();
}
}
}
18 changes: 18 additions & 0 deletions NextionFontEditor/NextionFontEditor/FormFontGenerator.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,25 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="sfd.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 48</value>
</metadata>
<metadata name="toolStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>130, 48</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="btnSave.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAClSURBVDhPY6AK6C5d+B8ZF9dN/59W2Y+BS2pn/O+vXFID
1YYAIE13rn4C42O7noEVP3z9CQODxKfWr/7WX724DqoVAggZUNE9B27A1bOv/0+rX/ulr3JpG1Q7aQaA
1Fw58wrsVah2VANOH3j5v657CVgxOm6euByuDqcBxGLaGUAI0MeAQy+wYxAgygB8gCgDsNkOwiBAlAH4
AE4DyMFQ7ZQABgYA8IyChn1Fh6IAAAAASUVORK5CYII=
</value>
</data>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>76</value>
</metadata>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAANcNAADXDQAAAAAAAAAA
Expand Down
2 changes: 2 additions & 0 deletions NextionFontEditor/NextionFontEditor/NextionFontEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
<PropertyGroup>
<ApplicationIcon>icons8-text-box.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup />
<PropertyGroup />
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
Binary file added NextionFontEditor/NextionFontEditor/ZiLib.dll
Binary file not shown.
10 changes: 7 additions & 3 deletions NextionFontEditor/ZiLib/BinaryTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ public static bool[] BytesToBits(byte[] bytes) {
return bits;
}

public static byte[] BitmapTo1BppData(Bitmap b) {
public static byte[] BitmapTo1BppData(Bitmap b, bool invertColour=false) {
var pixels = new bool[b.Width * b.Height];

var A = invertColour ? Color.White.A : Color.Black.A;
var B = invertColour ? Color.White.B : Color.Black.B;
var G = invertColour ? Color.White.G : Color.Black.G;
var R = invertColour ? Color.White.R : Color.Black.R;
for (int y = 0; y < b.Height; y++) {

for (int x = 0; x < b.Width; x++) {
var pixel = b.GetPixel(x, y);
pixels[(y * b.Width) + x] = (pixel.A == 255 && pixel.R == 0 && pixel.G == 0 && pixel.B == 0);
pixels[(y * b.Width) + x] = (pixel.A == A && pixel.R == R && pixel.G == G && pixel.B == B);
}

}

var data = new byte[b.Width * b.Height / 8];
Expand Down
28 changes: 17 additions & 11 deletions NextionFontEditor/ZiLib/ZiFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,26 @@ public class ZiFont {
// CharacterHeight = height;
// CodePage = codePage;
//}

public void Save(string fileName, CodePage codePage) {
public void Save(string fileName, CodePage codePage)
{
CodePage = codePage;
_charData = CreateCharData(CharBitmaps);
Save(fileName);
}

public void Save(string fileName) {
// _charData = CreateCharData(CharBitmaps);

var file = new List<byte>();

file.AddRange(MagicNumbers);
file.AddRange(BitConverter.GetBytes((ushort) codePage.CodePageIdentifier));
file.AddRange(BitConverter.GetBytes((ushort) CodePage.CodePageIdentifier));
file.Add(CharacterWidth);
file.Add(CharacterHeight);
file.Add((byte) (codePage.IsMultibyte ? codePage.FirstByteStart : 0));
file.Add((byte) (codePage.IsMultibyte ? codePage.FirstByteEnd : 0));
file.Add((byte) (codePage.IsMultibyte ? codePage.SecondByteStart : codePage.FirstByteStart));
file.Add((byte) (codePage.IsMultibyte ? codePage.SecondByteEnd : codePage.FirstByteEnd));
file.Add((byte) (CodePage.IsMultibyte ? CodePage.FirstByteStart : 0));
file.Add((byte) (CodePage.IsMultibyte ? CodePage.FirstByteEnd : 0));
file.Add((byte) (CodePage.IsMultibyte ? CodePage.SecondByteStart : CodePage.FirstByteStart));
file.Add((byte) (CodePage.IsMultibyte ? CodePage.SecondByteEnd : CodePage.FirstByteEnd));
file.AddRange(BitConverter.GetBytes(CodePage.CharacterCount));
file.Add(FileFormatVersion);
file.Add(NameLength);
Expand Down Expand Up @@ -130,17 +136,17 @@ private void CreateBitmaps() {
}
}

private byte[] CreateCharData(List<Bitmap> characters) {
private byte[] CreateCharData(List<Bitmap> characters, bool invertColour=false) {
var charData = new List<byte>();

foreach (var cb in characters) {
charData.AddRange(BinaryTools.BitmapTo1BppData(cb));
charData.AddRange(BinaryTools.BitmapTo1BppData(cb, invertColour));
}

return charData.ToArray();
}

public static ZiFont FromCharacterBitmaps(string fontName, byte width, byte height, CodePage codePage, List<Bitmap> characters) {
public static ZiFont FromCharacterBitmaps(string fontName, byte width, byte height, CodePage codePage, List<Bitmap> characters, bool invertColour = false) {
var bytesPerChar = width * height / 8;
var charDataLength = (uint) (bytesPerChar * characters.Count());

Expand All @@ -155,7 +161,7 @@ public static ZiFont FromCharacterBitmaps(string fontName, byte width, byte heig
BytesPerChar = bytesPerChar,
};

ziFont._charData = ziFont.CreateCharData(characters);
ziFont._charData = ziFont.CreateCharData(characters, invertColour);

//var charData = new List<byte>();
//foreach (var cb in characters) {
Expand Down

0 comments on commit 0b73026

Please sign in to comment.