Skip to content

Commit

Permalink
Merge branch 'release/1.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Apr 8, 2024
2 parents 9a1998b + cf1145b commit e744800
Show file tree
Hide file tree
Showing 48 changed files with 742 additions and 470 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.5.0]

### Added

- Default button

### Changed

- Monospaced fonts now respects custom character properties
- Keeping changes at runtime

---

## [1.4.0]

### Added
Expand Down Expand Up @@ -119,6 +132,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Initial version

[1.5.0]: https://github.com/kleber-swf/unity-bitmap-font-creator/releases/tag/1.5.0
[1.4.0]: https://github.com/kleber-swf/unity-bitmap-font-creator/releases/tag/1.4.0
[1.3.0]: https://github.com/kleber-swf/unity-bitmap-font-creator/releases/tag/1.3.0
[1.2.1]: https://github.com/kleber-swf/unity-bitmap-font-creator/releases/tag/1.2.1
Expand Down
Binary file modified Documentation/screenshot-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions Editor/Resources/RuntimeData.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c4647d479c338325f9328893d838e266, type: 3}
m_Name: RuntimeData
m_EditorClassIdentifier:
8 changes: 8 additions & 0 deletions Editor/Resources/RuntimeData.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 32 additions & 22 deletions Editor/Scripts/BitmapFontCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,14 @@ private static Material CreateMaterial(string baseName, Texture2D texture)

private static Font CreateFontAsset(string baseName, Material material, ExecutionData data, out string error)
{
error = null;
var map = new Dictionary<char, CharacterProps>();
for (var i = 0; i < data.CustomCharacterProps.Count; i++)
{
var e = data.CustomCharacterProps[i];
if (string.IsNullOrEmpty(e.Character))
{
error = $"Character for Custom Character Properties at position {i + 1} is empty";
return null;
}
map.Add(e.Character[0], e);
}
var custom = GetCustomPropsAsMap(data.CustomCharacterProps, out error);
if (!string.IsNullOrEmpty(error)) return null;

var metrics = new FontMetrics();
var font = new Font(baseName)
{
material = material,
characterInfo = CreateCharacters(data, map, data.Descent, ref metrics),
characterInfo = CreateCharacters(data, custom, data.Descent, ref metrics),
};

var so = new SerializedObject(font);
Expand All @@ -115,8 +105,27 @@ private static Font CreateFontAsset(string baseName, Material material, Executio
return font;
}

private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<char, CharacterProps> map,
float descent, ref FontMetrics metrics)
private static Dictionary<char, CharacterProps> GetCustomPropsAsMap(List<CharacterProps> props, out string error)
{
error = null;
var map = new Dictionary<char, CharacterProps>();

for (var i = 0; i < props.Count; i++)
{
var e = props[i];
if (string.IsNullOrEmpty(e.Character))
{
error = $"Character for Custom Character Properties at position {i + 1} is empty";
return null;
}
map.Add(e.Character[0], e);
}

return map;
}

private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<char, CharacterProps> custom,
float descent, ref FontMetrics metrics)
{
var texSize = new Vector2Int(data.Texture.width, data.Texture.height);
var cellSize = new Vector2Int(Mathf.FloorToInt(texSize.x / data.Cols), Mathf.FloorToInt(texSize.y / data.Rows));
Expand Down Expand Up @@ -164,14 +173,14 @@ ref gi
gi.uvBottom = cellUVSize.x * (col + 1) - ((cellSize.x - gi.xMax) * ratio.x);
gi.uvRight = cellUVSize.y * (data.Rows - row - 1) + ((cellSize.y - gi.yMax) * ratio.y);

var info = CreateCharacterInfo(ch, gi, map);
var info = CreateCharacterInfo(ch, gi, custom);
characters.Add(info);

if (!data.CaseInsentive) continue;
var ch2 = char.ToUpper(ch);
if (ch2 != ch) characters.Add(CreateCharacterInfo(ch2, gi, map));
if (ch2 != ch) characters.Add(CreateCharacterInfo(ch2, gi, custom));
ch2 = char.ToLower(ch2);
if (ch2 != ch) characters.Add(CreateCharacterInfo(ch2, gi, map));
if (ch2 != ch) characters.Add(CreateCharacterInfo(ch2, gi, custom));

#if BITMAP_FONT_CREATOR_DEV
static string _(float x) => $"<color=yellow>{x}</color>";
Expand All @@ -183,12 +192,12 @@ ref gi
characters.Add(CreateSpaceCharacter(advanceMin));

if (data.Monospaced)
SetFontAsMonospaced(characters, advanceMax);
SetFontAsMonospaced(characters, advanceMax, custom);

return characters.ToArray();
}

private static CharacterInfo CreateCharacterInfo(char ch, GlyphInfo g, Dictionary<char, CharacterProps> map)
private static CharacterInfo CreateCharacterInfo(char ch, GlyphInfo g, Dictionary<char, CharacterProps> custom)
{
var info = new CharacterInfo
{
Expand All @@ -203,7 +212,7 @@ private static CharacterInfo CreateCharacterInfo(char ch, GlyphInfo g, Dictionar
advance = g.advance,
};

if (map.TryGetValue(ch, out var props))
if (custom.TryGetValue(ch, out var props))
{
info.minX += props.Padding.x;
info.maxX += props.Padding.x;
Expand Down Expand Up @@ -256,10 +265,11 @@ private static CharacterInfo CreateSpaceCharacter(int advance)
};
}

private static void SetFontAsMonospaced(List<CharacterInfo> characters, int advMax)
private static void SetFontAsMonospaced(List<CharacterInfo> characters, int advMax, Dictionary<char, CharacterProps> custom)
{
for (var i = 0; i < characters.Count; i++)
{
if (custom.ContainsKey((char)characters[i].index)) continue;
var c = characters[i];
var x = c.minX + Mathf.RoundToInt((advMax - c.advance) * 0.5f);
c.minX += x;
Expand Down
51 changes: 13 additions & 38 deletions Editor/Scripts/Model/BitmapFontCreatorModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,8 @@

namespace dev.klebersilva.tools.bitmapfontcreator
{
internal enum Orientation
{
Horizontal,
Vertical,
}

[Serializable]
internal class CharacterProps
{
public string Character = "";
public Vector2Int Padding = Vector2Int.zero;
public int Spacing = 0;

public CharacterProps Clone()
{
return new CharacterProps
{
Character = Character,
Padding = Padding,
Spacing = Spacing,
};
}
}

[Serializable]
internal class BitmapFontCreatorData : ISerializationCallbackReceiver
internal class BitmapFontCreatorModel : ISerializationCallbackReceiver
{
private const string IgnoreCharacter = "\n";

Expand Down Expand Up @@ -63,7 +39,7 @@ public string Characters
public string ValidCharacters { get; protected set; } = string.Empty;
public int ValidCharactersCount { get; protected set; } = 0;

public virtual void CopyTo(BitmapFontCreatorData dest)
public virtual void CopyTo(BitmapFontCreatorModel dest)
{
if (dest == null) return;

Expand All @@ -74,10 +50,10 @@ public virtual void CopyTo(BitmapFontCreatorData dest)
dest.AlphaThreshold = AlphaThreshold;
dest.LineSpacing = LineSpacing;
dest.AutoLineSpacing = AutoLineSpacing;
dest.Monospaced = Monospaced;
dest.CaseInsentive = CaseInsentive;
dest.FontSize = FontSize;
dest.AutoFontSize = AutoFontSize;
dest.Monospaced = Monospaced;
dest.CaseInsentive = CaseInsentive;
dest.Ascent = Ascent;
dest.Descent = Descent;
dest.DefaultCharacterSpacing = DefaultCharacterSpacing;
Expand All @@ -91,7 +67,6 @@ public virtual void CopyTo(BitmapFontCreatorData dest)
UpdateChacters();
}

// TODO this should be called automatically when the field Characters changes
private void UpdateChacters()
{
ValidCharacters = _characters.Replace(IgnoreCharacter, string.Empty);
Expand All @@ -100,23 +75,23 @@ private void UpdateChacters()

public void OnBeforeSerialize() { }
public void OnAfterDeserialize() { UpdateChacters(); }
}

internal class ExecutionData : BitmapFontCreatorData
{
[NonSerialized] public Texture2D Texture;

public static ExecutionData Default => new()
public static BitmapFontCreatorModel Default => new()
{
Texture = null,
Characters = string.Empty,
Orientation = Orientation.Horizontal,
Cols = 1,
Rows = 1,
AlphaThreshold = 0f,
DefaultCharacterSpacing = 10,
Monospaced = false,
LineSpacing = 0f,
AutoLineSpacing = true,
FontSize = 0f,
AutoFontSize = true,
Monospaced = false,
CaseInsentive = false,
Ascent = 0f,
Descent = 0f,
DefaultCharacterSpacing = 0,
CustomCharacterProps = new List<CharacterProps>(),
ValidCharacters = string.Empty,
ValidCharactersCount = 0
Expand Down
23 changes: 23 additions & 0 deletions Editor/Scripts/Model/CharacterProps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using UnityEngine;

namespace dev.klebersilva.tools.bitmapfontcreator
{
[Serializable]
internal class CharacterProps
{
public string Character = "";
public Vector2Int Padding = Vector2Int.zero;
public int Spacing = 0;

public CharacterProps Clone()
{
return new CharacterProps
{
Character = Character,
Padding = Padding,
Spacing = Spacing,
};
}
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/Model/CharacterProps.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Editor/Scripts/Model/ExecutionData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using UnityEngine;

namespace dev.klebersilva.tools.bitmapfontcreator
{
[Serializable]
internal class ExecutionData : BitmapFontCreatorModel
{
public Texture2D Texture;

public static new ExecutionData Default
{
get
{
var data = new ExecutionData { Texture = null };
BitmapFontCreatorModel.Default.CopyTo(data);
return data;
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/Model/ExecutionData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/Scripts/Model/Orientation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace dev.klebersilva.tools.bitmapfontcreator
{
internal enum Orientation
{
Horizontal,
Vertical,
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/Model/Orientation.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/Scripts/Model/PrefsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ public static void Save(PrefsModel model)
EditorPrefs.SetString(EditorPrefsKey, content);
}
}
}
}
18 changes: 18 additions & 0 deletions Editor/Scripts/Model/RuntimeData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UnityEngine;

namespace dev.klebersilva.tools.bitmapfontcreator
{
// [CreateAssetMenu(fileName = "RuntimeData", menuName = "RuntimeData", order = 0)]
internal class RuntimeData : ScriptableObject
{
private const string DataPath = "RuntimeData";
public ExecutionData Data;

public static RuntimeData Load()
{
var data = Resources.Load<RuntimeData>(DataPath);
data.Data ??= ExecutionData.Default;
return data;
}
}
}
11 changes: 11 additions & 0 deletions Editor/Scripts/Model/RuntimeData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e744800

Please sign in to comment.