Skip to content

Commit

Permalink
Fix issue with NDX Extract and add Hearts font
Browse files Browse the repository at this point in the history
  • Loading branch information
fortiersteven committed Jan 7, 2025
1 parent 30d258f commit ad30c13
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 36 deletions.
264 changes: 263 additions & 1 deletion TranslationApp/TextPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TranslationApp
{

public partial class TextPreview : PictureBox
{
public Bitmap fontAtlasImage { get; set; }
Expand Down Expand Up @@ -252,6 +254,107 @@ public font_glyph(byte x, byte y)
/* 。 */ new font_glyph(01, 12),
};

private readonly font_glyph[] toh_glyphs = new font_glyph[97]
{
/* */ new font_glyph(00, 18),
/* 0 */ new font_glyph(00, 13),
/* 1 */ new font_glyph(00, 19),
/* 2 */ new font_glyph(00, 13),
/* 3 */ new font_glyph(00, 13),
/* 4 */ new font_glyph(00, 13),
/* 5 */ new font_glyph(00, 13),
/* 6 */ new font_glyph(00, 13),
/* 7 */ new font_glyph(00, 13),
/* 8 */ new font_glyph(00, 13),
/* 9 */ new font_glyph(00, 13),
/* A */ new font_glyph(00, 13),
/* B */ new font_glyph(00, 13),
/* C */ new font_glyph(00, 13),
/* D */ new font_glyph(00, 13),
/* E */ new font_glyph(00, 13),
/* F */ new font_glyph(00, 13),
/* G */ new font_glyph(00, 13),
/* H */ new font_glyph(00, 13),
/* I */ new font_glyph(00, 13),
/* J */ new font_glyph(00, 21),
/* K */ new font_glyph(00, 13),
/* L */ new font_glyph(00, 13),
/* M */ new font_glyph(00, 13),
/* N */ new font_glyph(00, 13),
/* O */ new font_glyph(00, 13),
/* P */ new font_glyph(00, 13),
/* Q */ new font_glyph(00, 13),
/* R */ new font_glyph(00, 13),
/* S */ new font_glyph(00, 13),
/* T */ new font_glyph(00, 13),
/* U */ new font_glyph(00, 13),
/* V */ new font_glyph(00, 13),
/* W */ new font_glyph(00, 13),
/* X */ new font_glyph(00, 13),
/* Y */ new font_glyph(00, 13),
/* Z */ new font_glyph(00, 13),
/* a */ new font_glyph(00, 13),
/* b */ new font_glyph(00, 13),
/* c */ new font_glyph(00, 13),
/* d */ new font_glyph(00, 13),
/* e */ new font_glyph(00, 13),
/* f */ new font_glyph(00, 13),
/* g */ new font_glyph(00, 13),
/* h */ new font_glyph(00, 13),
/* i */ new font_glyph(00, 21),
/* j */ new font_glyph(00, 15),
/* k */ new font_glyph(00, 15),
/* l */ new font_glyph(00, 19),
/* m */ new font_glyph(00, 13),
/* n */ new font_glyph(00, 13),
/* o */ new font_glyph(00, 13),
/* p */ new font_glyph(00, 13),
/* q */ new font_glyph(00, 13),
/* r */ new font_glyph(00, 13),
/* s */ new font_glyph(00, 14),
/* t */ new font_glyph(00, 14),
/* u */ new font_glyph(00, 15),
/* v */ new font_glyph(00, 13),
/* w */ new font_glyph(00, 13),
/* x */ new font_glyph(00, 13),
/* y */ new font_glyph(00, 13),
/* z */ new font_glyph(00, 13),
/* , */ new font_glyph(00, 20),
/* . */ new font_glyph(00, 20),
/* ・ */ new font_glyph(00, 20),
/* : */ new font_glyph(00, 20),
/* ; */ new font_glyph(00, 20),
/* ? */ new font_glyph(00, 14),
/* ! */ new font_glyph(00, 22),
/* / */ new font_glyph(00, 14),
/* ( */ new font_glyph(00, 18),
/* ) */ new font_glyph(00, 18),
/* [ */ new font_glyph(00, 18),
/* ] */ new font_glyph(00, 18),
/* { */ new font_glyph(00, 16),
/* } */ new font_glyph(00, 16),
/* + */ new font_glyph(00, 14),
/* - */ new font_glyph(00, 20),
/* = */ new font_glyph(00, 14),
/* < */ new font_glyph(00, 18),
/* > */ new font_glyph(00, 18),
/* % */ new font_glyph(00, 14),
/* # */ new font_glyph(00, 14),
/* & */ new font_glyph(00, 14),
/* * */ new font_glyph(00, 14),
/* @ */ new font_glyph(00, 14),
/* | */ new font_glyph(00, 22),
/* ” */ new font_glyph(00, 18),
/* ’ */ new font_glyph(00, 22),
/* ^ */ new font_glyph(00, 16),
/* 「 */ new font_glyph(00, 18),
/* 」 */ new font_glyph(00, 18),
/* 〜 */ new font_glyph(00, 14),
/* _ */ new font_glyph(00, 14),
/* 、 */ new font_glyph(00, 20),
/* 。 */ new font_glyph(00, 16),
};

private readonly font_glyph[] rm2_glyphs = new font_glyph[97]
{
/* */ new font_glyph(16, 00),
Expand Down Expand Up @@ -381,6 +484,12 @@ public void ChangeImage(string id)
BackColor = Color.FromArgb(0xA0, 0x0, 0x0, 0x0);
glyphs = rm2_glyphs;
break;

case "TOH":
res = "TranslationApp.res.toh_font_atlas.png";
BackColor = Color.FromArgb(0xA0, 0x0, 0x0, 0x0);
glyphs = toh_glyphs;
break;
default:
fontAtlasImage = null;
glyphs = null;
Expand Down Expand Up @@ -422,6 +531,7 @@ public void ReDraw(string text)
Invalidate();
}


private void Raster()
{
// No image, no fun
Expand Down Expand Up @@ -603,6 +713,158 @@ private void Raster()
}
}




public string DoLineBreak(string text, int maxWidth)
{
List<(string Word, int Size)> words = GetListWords(text);
int sum = 0;
int i = 0;
string final = "";

if (maxWidth < words.Select(x => x.Size).Max())
{
maxWidth = words.Select(x => x.Size).Max();

}
while (i < words.Count)
{
sum = 0;
List<int> output = words.Select(x => sum += x.Size).ToList();
List<int> maxCount = Enumerable.Range(0, output.Count).Where(ind => output[ind] <= maxWidth).ToList();

string line = "";
foreach (int c in maxCount)
line += words[c].Word;

words.RemoveRange(0, maxCount.Count);
i += maxCount.Count;
final += (line + Environment.NewLine);


}

return final;


}

public string WordWrap(string text, int maxWidth)
{
Rectangle space = GetCharacterRectangleFromAtlas(' ', out int s, out bool add);
List<(string,int)> words = GetListWords(text);

int curLineLength = 0;
StringBuilder strBuilder = new StringBuilder();
foreach ((string word, int width) in words)
{
string finalWord = word;
// If adding the new word to the current line would be too long,
// then put it on a new line (and split it up if it's too long).
if (curLineLength + width > maxWidth)
{
// Only move down to a new line if we have text on the current line.
// Avoids situation where
// wrapped whitespace causes emptylines in text.
if (curLineLength > 0)
{
strBuilder.Append(Environment.NewLine);
curLineLength = 0;
}

// Remove leading whitespace from the word,
// so the new line starts flush to the left.
finalWord = word.TrimStart();
}
strBuilder.Append(word + " ");
curLineLength += (width + space.Width);
}

return strBuilder.ToString();
}

public List<(string, int)> GetListWords(string text)
{
//Split tags and text
string[] result = Regex.Split(text.Replace("\r", ""), @"(<[\w/]+:?\w+>[,|.|\[||\]]*)", RegexOptions.IgnoreCase).Where(x => x != "").ToArray();
Rectangle space = GetCharacterRectangleFromAtlas(' ', out int s, out bool add);

string textToRender = "";
bool shear = false;
List<(string Word, int Size)> wordsSize = new List<(string, int)>();

foreach (string element in result)
{

int d = 0;
string tag = "";

int pos = element.IndexOf(">");
if (pos > -1)
{
tag = element.Substring(0, pos+1);

if (names.Contains(tag))
{
textToRender = tag.Substring(1, pos - 1);
}
else if (element.StartsWith("<unk") || element.StartsWith("<var") || element.StartsWith("<icon"))
{
textToRender = "***";
}
else if (element == "<Italic>")
{
shear = true;
continue;
}
else if (element == "</Italic>")
{
shear = false;
continue;
}
else if (element.StartsWith("<nmb:"))
{
string el = element.Substring(5, element.Length - 6);
textToRender = Convert.ToInt32(el, 16).ToString();
}

if (pos < element.Length)
textToRender += element.Substring(pos+1, element.Length - pos - 1);

}
else
{
textToRender = element;
}


//Remove all linebreak
textToRender = textToRender.Replace("\n", " ");

foreach (string word in textToRender.Split(' ').Where(x => x != ""))
{

wordsSize.Add((word, GetWordWidth(word)));
wordsSize.Add((" ", space.Width));

}
}

wordsSize.RemoveAt(wordsSize.Count - 1);
return wordsSize;
}

private int GetWordWidth(string word)
{
int width = 0;
foreach (char c in word) {
Rectangle rect = GetCharacterRectangleFromAtlas(c, out int x, out bool b);
width += rect.Width;
}

return width;
}
private void TextPreview_Paint(object sender, PaintEventArgs e)
{

Expand Down Expand Up @@ -719,7 +981,7 @@ private Rectangle GetCharacterRectangleFromAtlas(int character, out int s, out b
int y = index * charHeight;
int x = glyphs[index].lskip;

charWidth -= glyphs[index].lskip;
charWidth -= (glyphs[index].lskip);
s = glyphs[index].rskip;
return new Rectangle(x, y, charWidth, charHeight);
}
Expand Down
3 changes: 3 additions & 0 deletions TranslationApp/TranslationApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@
<ItemGroup>
<EmbeddedResource Include="res\rm2_font_atlas.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="res\toh_font_atlas.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
Loading

0 comments on commit ad30c13

Please sign in to comment.