Skip to content

Commit 37dc85d

Browse files
committed
Merge branch 'release/1.2.0'
2 parents 3705233 + 5980df8 commit 37dc85d

12 files changed

+318
-38
lines changed

CHANGELOG.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,63 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
# [1.2.0]
9+
10+
### Added
11+
12+
- Guess rows and cols based on the texture
13+
- Showing error for Custom Character Properties when the given character is empty
14+
- Monospaced font to characters (under testing)
15+
- Character count
16+
17+
### Changed
18+
19+
- Ignoring \n characters to make it easier to add them to the characters field
20+
21+
---
22+
23+
## [1.1.1]
24+
25+
### Fixed
26+
27+
- Resources folder creation at startup
28+
29+
---
30+
31+
## [1.1.0]
32+
33+
### Added
34+
35+
- All error checks for font creation
36+
- Warning when the material or font settings already exists
37+
- Local options popup
38+
- Warn before replacing current settings
39+
- Warn on replace profile preference
40+
- Rollback button
41+
- Setting readonly editor - to avoid changing settings directly in the file and creating inconsistencies
42+
43+
### Changed
44+
45+
- Loading selected profile on open
46+
47+
### Removed
48+
49+
- Unecessary example folder
50+
51+
### Fixed
52+
53+
- Prefereces creation
54+
- Specific data for examples
55+
- Canceling settings override doesn't swap profile anymore
56+
- When changing to no profile, no setting changes, thus no dialog should be displayed
57+
- Error when checking settings for the first time
58+
59+
---
60+
861
## [1.0.0]
962

1063
Initial version
1164

12-
[1.0.0]: https://github.com/kleber-swf/unity-bitmap-font-creator/releases/tag/1.0.0
65+
[1.1.1]: https://github.com/kleber-swf/unity-bitmap-font-creator/releases/tag/1.1.1
66+
[1.1.0]: https://github.com/kleber-swf/unity-bitmap-font-creator/releases/tag/1.1.0
67+
[1.0.0]: https://github.com/kleber-swf/unity-bitmap-font-creator/releases/tag/1.0.0

Documentation/screenshot-01.png

4.07 KB
Loading

Editor/BitmapFontCreator.cs

+86-16
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ namespace dev.klebersilva.tools.bitmapfontcreator
77
{
88
internal static class BitmapFontCreator
99
{
10-
public static void TryCreateFont(ExecutionData data, bool warnBeforeOverwrite)
10+
private const char IgnoreCharacter = ' ';
11+
12+
public static bool TryCreateFont(ExecutionData data, bool warnBeforeOverwrite, out string error)
1113
{
12-
var error = CheckForErrors(data);
13-
if (!string.IsNullOrEmpty(error))
14-
{
15-
Debug.LogError(error);
16-
return;
17-
}
14+
error = CheckForErrors(data);
15+
if (!string.IsNullOrEmpty(error)) return false;
1816

1917
var path = AssetDatabase.GetAssetPath(data.Texture);
2018
var baseName = Path.GetFileNameWithoutExtension(path);
@@ -25,16 +23,18 @@ public static void TryCreateFont(ExecutionData data, bool warnBeforeOverwrite)
2523
if (warnBeforeOverwrite && !(AssetDatabase.GUIDFromAssetPath(materialPath) == null && AssetDatabase.GUIDFromAssetPath(fontPath) == null))
2624
{
2725
if (!EditorUtility.DisplayDialog("Warning", "Asset already exists. Overwrite? (It will keep the references)", "Yes", "No"))
28-
return;
26+
return false;
2927
}
3028

3129
var material = CreateMaterial(baseName, data.Texture);
32-
var font = CreateFontAsset(baseName, material, data);
30+
var font = CreateFontAsset(baseName, material, data, out error);
31+
if (!string.IsNullOrEmpty(error)) return false;
3332

3433
AssetDatabase.CreateAsset(material, materialPath);
3534
CreateOrReplaceAsset(font, fontPath);
3635

3736
AssetDatabase.Refresh();
37+
return true;
3838
}
3939

4040
private static string CheckForErrors(ExecutionData data)
@@ -45,8 +45,8 @@ private static string CheckForErrors(ExecutionData data)
4545
if (data.Texture == null) return "Texture cannot be null";
4646
if (!data.Texture.isReadable) return "Texture must be readable. Set Read/Write Enabled to true inside Texture Properties";
4747

48-
if (data.Characters.Length != data.Cols * data.Rows)
49-
return $"Characters length ({data.Characters.Length}) must be equal to Cols ({data.Cols}) * Rows ({data.Rows})";
48+
if (data.ValidCharactersCount != data.Cols * data.Rows)
49+
return $"Characters length ({data.ValidCharactersCount}) must be equal to Cols ({data.Cols}) * Rows ({data.Rows})";
5050

5151
return null;
5252
}
@@ -60,11 +60,20 @@ private static Material CreateMaterial(string baseName, Texture2D texture)
6060
};
6161
}
6262

63-
private static Font CreateFontAsset(string baseName, Material material, ExecutionData data)
63+
private static Font CreateFontAsset(string baseName, Material material, ExecutionData data, out string error)
6464
{
65+
error = null;
6566
var map = new Dictionary<char, CharacterProps>();
66-
foreach (var e in data.CustomCharacterProps)
67+
for (var i = 0; i < data.CustomCharacterProps.Count; i++)
68+
{
69+
var e = data.CustomCharacterProps[i];
70+
if (string.IsNullOrEmpty(e.Character))
71+
{
72+
error = $"Character for Custom Character Properties at position {i + 1} is empty";
73+
return null;
74+
}
6775
map.Add(e.Character[0], e);
76+
}
6877

6978
return new Font(baseName)
7079
{
@@ -83,16 +92,15 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
8392
int xMin, xMax, advance;
8493
int largestAdvance = 0;
8594

86-
// horizontal
8795
for (var row = 0; row < data.Rows; row++)
8896
{
8997
for (var col = 0; col < data.Cols; col++)
9098
{
9199
var i = data.Orientation == Orientation.Horizontal
92100
? (row * data.Cols) + col
93101
: (col * data.Rows) + row;
94-
var ch = data.Characters[i];
95-
if (ch == ' ' || ch == '\r' || ch == '\n') continue;
102+
var ch = data.ValidCharacters[i];
103+
if (ch == IgnoreCharacter) continue;
96104

97105
GetCharacterBounds(
98106
tex: data.Texture,
@@ -174,5 +182,67 @@ private static void CreateOrReplaceAsset<T>(T asset, string path) where T : Obje
174182
AssetDatabase.SaveAssets();
175183
}
176184
}
185+
186+
public static Vector2Int GuessRowsAndCols(Texture2D tex)
187+
{
188+
var rows = 0;
189+
var cols = 0;
190+
191+
uint state = 0; // 0 = looking for not transparent, 1 = looking for transparent
192+
bool foundNonTransparentPixel;
193+
194+
for (var x = 0; x < tex.width; x++)
195+
{
196+
foundNonTransparentPixel = false;
197+
for (var y = 0; y < tex.height; y++)
198+
{
199+
if (tex.GetPixel(x, y).a == 0) continue;
200+
foundNonTransparentPixel = true;
201+
break;
202+
}
203+
204+
if (state == 0)
205+
{
206+
if (foundNonTransparentPixel)
207+
{
208+
state = 1;
209+
cols++;
210+
}
211+
}
212+
else
213+
{
214+
if (!foundNonTransparentPixel)
215+
state = 0;
216+
}
217+
}
218+
219+
state = 0;
220+
for (var y = 0; y < tex.height; y++)
221+
{
222+
foundNonTransparentPixel = false;
223+
for (var x = 0; x < tex.width; x++)
224+
{
225+
if (tex.GetPixel(x, y).a == 0) continue;
226+
foundNonTransparentPixel = true;
227+
break;
228+
}
229+
230+
if (state == 0)
231+
{
232+
if (foundNonTransparentPixel)
233+
{
234+
state = 1;
235+
rows++;
236+
}
237+
}
238+
else
239+
{
240+
if (!foundNonTransparentPixel)
241+
state = 0;
242+
}
243+
}
244+
245+
return new(rows, cols);
246+
}
177247
}
178248
}

Editor/Model/BitmapFontCreatorModel.cs

+35-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ internal class CharacterProps
1717
public int Spacing = 0;
1818
}
1919

20-
internal class BitmapFontCreatorData
20+
[Serializable]
21+
internal class BitmapFontCreatorData : ISerializationCallbackReceiver
2122
{
22-
public string Characters;
23+
private const string IgnoreCharacter = "\n";
24+
25+
[SerializeField] private string _characters = string.Empty;
2326
public Orientation Orientation;
2427
public int Cols;
2528
public int Rows;
@@ -28,11 +31,25 @@ internal class BitmapFontCreatorData
2831
public int DefaultCharacterSpacing;
2932
public List<CharacterProps> CustomCharacterProps;
3033

34+
public string Characters
35+
{
36+
get { return _characters; }
37+
set
38+
{
39+
if (_characters == value) return;
40+
_characters = value;
41+
UpdateChacters();
42+
}
43+
}
44+
45+
public string ValidCharacters { get; protected set; } = string.Empty;
46+
public int ValidCharactersCount { get; protected set; } = 0;
47+
3148
public virtual void CopyTo(BitmapFontCreatorData dest)
3249
{
3350
if (dest == null) return;
3451

35-
dest.Characters = Characters;
52+
dest.Characters = _characters;
3653
dest.Orientation = Orientation;
3754
dest.Cols = Cols;
3855
dest.Rows = Rows;
@@ -45,7 +62,19 @@ public virtual void CopyTo(BitmapFontCreatorData dest)
4562

4663
foreach (var e in CustomCharacterProps)
4764
dest.CustomCharacterProps.Add(new CharacterProps() { Character = e.Character, Spacing = e.Spacing });
65+
66+
UpdateChacters();
4867
}
68+
69+
// TODO this should be called automatically when the field Characters changes
70+
private void UpdateChacters()
71+
{
72+
ValidCharacters = _characters.Replace(IgnoreCharacter, string.Empty);
73+
ValidCharactersCount = ValidCharacters.Length;
74+
}
75+
76+
public void OnBeforeSerialize() { }
77+
public void OnAfterDeserialize() { UpdateChacters(); }
4978
}
5079

5180
internal class ExecutionData : BitmapFontCreatorData
@@ -55,14 +84,16 @@ internal class ExecutionData : BitmapFontCreatorData
5584
public static ExecutionData Default => new()
5685
{
5786
Texture = null,
58-
Characters = "",
87+
Characters = string.Empty,
5988
Orientation = Orientation.Horizontal,
6089
Cols = 1,
6190
Rows = 1,
6291
AlphaThreshold = 0f,
6392
DefaultCharacterSpacing = 10,
6493
Monospaced = false,
6594
CustomCharacterProps = new List<CharacterProps>(),
95+
ValidCharacters = string.Empty,
96+
ValidCharactersCount = 0
6697
};
6798
}
6899
}

Editor/Resources.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Resources/Fonts.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Resources/Fonts/monospaced.ttf

118 KB
Binary file not shown.

Editor/Resources/Fonts/monospaced.ttf.meta

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)