Skip to content

Commit

Permalink
Better Font Remap
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussacana committed Feb 28, 2020
1 parent e946d9f commit 37c0e97
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 62 deletions.
58 changes: 0 additions & 58 deletions StringReloads/Engine/Main.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;

using StringReloads.AutoInstall;
Expand Down Expand Up @@ -119,63 +118,6 @@ private void TriggerFlags(LSTParser.LSTFlag[] Flags) {
}
}


public FontRemap? GetFontRemap(string Facename, int Width, int Height, uint Charset) {
var Remap = (from x in Config.Default.FontRemaps where x["from"] == Facename select x).FirstOrDefault();

if (Remap == null)
Remap = (from x in Config.Default.FontRemaps where x["from"] == "*" select x).FirstOrDefault();

Log.Trace($"Font \"{Facename}\", Width: {Width}, Height: {Height}, Charset: 0x{Charset:X2}");

if (Remap == null)
return null;

FontRemap Rst = new FontRemap();
Rst.From = Remap["from"];

if (Remap.ContainsKey("to"))
Rst.To = Remap["to"];
else Rst.To = Facename;

if (Remap.ContainsKey("charset"))
Rst.Charset = Remap["charset"].ToUInt32();
else Rst.Charset = Charset;

if (Remap.ContainsKey("width"))
{
var nWidth = Remap["width"];

if (nWidth.StartsWith("."))
Rst.Width = nWidth.Substring(1).ToInt32();
else if (nWidth.StartsWith("+") || nWidth.StartsWith("-"))
Rst.Width = Width + nWidth.ToInt32();
else
Rst.Width = nWidth.ToInt32();
}
else
Rst.Width = Width;

if (Remap.ContainsKey("height"))
{
var nHeight = Remap["height"];

if (nHeight.StartsWith("."))
Rst.Height = nHeight.Substring(1).ToInt32();
else if (nHeight.StartsWith("+") || nHeight.StartsWith("-"))
Rst.Height = Width + nHeight.ToInt32();
else
Rst.Height = nHeight.ToInt32();
}
else
Rst.Height = Height;


Log.Debug($"Font Remaped to {Rst.To}, Width: {Rst.Width}, Height: {Rst.Height}, Charset: 0x{Rst.Charset:X2}");

return Rst;
}

public void EnableHook(Hook.Base.Hook Hook) {
Array.Resize(ref _Hooks, _Hooks.Length + 1);
_Hooks[_Hooks.Length - 1] = Hook;
Expand Down
111 changes: 111 additions & 0 deletions StringReloads/Engine/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,117 @@ public char ResolveRemap(char Char) {
}

return null;
}

public FontRemap? ResolveRemap(string Facename, int Width, int Height, uint Charset) {
var Remap = (from x in Config.Default.FontRemaps where
CheckFontValue(x, "From", Facename, false) &&
CheckFontValue(x, "FromWidth", Width.ToString(), true ) &&
CheckFontValue(x, "FromHeight", Height.ToString(), true ) &&
CheckFontValue(x, "FromCharset", Charset.ToString(), false)
select x).FirstOrDefault();

if (Remap == null)
Remap = (from x in Config.Default.FontRemaps where
CheckFontValue(x, "From", "*", false) &&
CheckFontValue(x, "FromWidth", Width.ToString(), true ) &&
CheckFontValue(x, "FromHeight", Height.ToString(), true ) &&
CheckFontValue(x, "FromCharset", Charset.ToString(), false)
select x).FirstOrDefault();

Log.Trace($"CreateFont -> Width: {Width:+00;-00}, Height: {Height:+00;-00}, Charset: 0x{Charset:X2}, Name: \"{Facename}\"");

if (Remap == null)
return null;

FontRemap Rst = new FontRemap();
Rst.From = Remap["from"];

if (Remap.ContainsKey("to"))
Rst.To = Remap["to"];
else Rst.To = Facename;

if (Remap.ContainsKey("charset"))
Rst.Charset = Remap["charset"].ToUInt32();
else Rst.Charset = Charset;

if (Remap.ContainsKey("width"))
{
var nWidth = Remap["width"];

if (nWidth.StartsWith("."))
Rst.Width = nWidth.Substring(1).ToInt32();
else if (nWidth.StartsWith("+") || nWidth.StartsWith("-"))
Rst.Width = Width + nWidth.ToInt32();
else
Rst.Width = nWidth.ToInt32();
}
else
Rst.Width = Width;

if (Remap.ContainsKey("height"))
{
var nHeight = Remap["height"];

if (nHeight.StartsWith("."))
Rst.Height = nHeight.Substring(1).ToInt32();
else if (nHeight.StartsWith("+") || nHeight.StartsWith("-"))
Rst.Height = Height + nHeight.ToInt32();
else
Rst.Height = nHeight.ToInt32();
}
else
Rst.Height = Height;


Log.Debug($"Remaped -> Width: {Rst.Width:+00;-00}, Height: {Rst.Height:+00;-00}, Charset: 0x{Rst.Charset:X2}, Name: \"{Rst.To}\"");

return Rst;
}

private bool CheckFontValue(Dictionary<string, string> Dic, string Entry, string Expected, bool Evalaute = false) {
if (!Dic.ContainsKey(Entry.ToLowerInvariant()))
return true;

string Value = Dic[Entry.ToLowerInvariant()];

if (Evalaute) {
var Mode = "=";

if (Value.StartsWith(">=")) {
Mode = ">=";
Value = Value.Substring(2);
} else if (Value.StartsWith("<=")) {
Mode = "<=";
Value = Value.Substring(2);
} else if (Value.StartsWith(">")) {
Mode = ">";
Value = Value.Substring(1);
} else if (Value.StartsWith("<")) {
Mode = "<";
Value = Value.Substring(1);
}

var Val = long.Parse(Value);
var Exp = long.Parse(Expected);

switch (Mode) {
case "=":
return Exp == Val;
case ">":
return Exp > Val;
case "<":
return Exp < Val;
case ">=":
return Exp >= Val;
case "<=":
return Exp <= Val;
}

} else if (Value == Expected)
return true;

return false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion StringReloads/Hook/CreateFontA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override void Initialize()
}

void* CreateFontHook(int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, int fdwItalic, int fdwUnderline, int fdwStrikeOut, uint fdwCharSet, int fdwOutputPrecision, int fdwClipPrecision, int fdwQuality, int fdwPitchAndFamily, string lpszFace) {
var Remap = EntryPoint.SRL.GetFontRemap(lpszFace, nWidth, nHeight, fdwCharSet);
var Remap = EntryPoint.SRL.Match.ResolveRemap(lpszFace, nWidth, nHeight, fdwCharSet);

if (Remap != null) {
nHeight = Remap.Value.Height;
Expand Down
2 changes: 1 addition & 1 deletion StringReloads/Hook/CreateFontIndirectA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override void Initialize()
}

public void* CreateFontIndirect(ref LOGFONTA Info) {
var Remap = EntryPoint.SRL.GetFontRemap(Info.lfFaceName, Info.lfWidth, Info.lfHeight, Info.lfCharSet);
var Remap = EntryPoint.SRL.Match.ResolveRemap(Info.lfFaceName, Info.lfWidth, Info.lfHeight, Info.lfCharSet);

if (Remap != null) {
Info.lfHeight = Remap.Value.Height;
Expand Down
2 changes: 1 addition & 1 deletion StringReloads/Hook/CreateFontIndirectW.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override void Initialize()
}

public void* CreateFontIndirect(ref LOGFONTW Info) {
var Remap = EntryPoint.SRL.GetFontRemap(Info.lfFaceName, Info.lfWidth, Info.lfHeight, Info.lfCharSet);
var Remap = EntryPoint.SRL.Match.ResolveRemap(Info.lfFaceName, Info.lfWidth, Info.lfHeight, Info.lfCharSet);

if (Remap != null) {
Info.lfHeight = Remap.Value.Height;
Expand Down
2 changes: 1 addition & 1 deletion StringReloads/Hook/CreateFontW.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override void Initialize()
}

void* CreateFontHook(int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, int fdwItalic, int fdwUnderline, int fdwStrikeOut, uint fdwCharSet, int fdwOutputPrecision, int fdwClipPrecision, int fdwQuality, int fdwPitchAndFamily, string lpszFace) {
var Remap = EntryPoint.SRL.GetFontRemap(lpszFace, nWidth, nHeight, fdwCharSet);
var Remap = EntryPoint.SRL.Match.ResolveRemap(lpszFace, nWidth, nHeight, fdwCharSet);

if (Remap != null) {
nHeight = Remap.Value.Height;
Expand Down
3 changes: 3 additions & 0 deletions StringReloads/SRL.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ PatchRedir=false

[Font.0]
From=*
;FromWidth=0
;FromHeight=0
;FromCharset=0
To=Gothic Modded
Charset=0
Width=+0
Expand Down

0 comments on commit 37c0e97

Please sign in to comment.