using System.Text.Json;
using Cosmos.Kernel.System.Graphics.Fonts;
using Cosmos.Kernel.System.Graphics;
using System.Drawing;
using XFESystem.Kernel;
System.Runtime.Loader.AssemblyLoadContext.Default.Resolving+=(_,name)=>{string path=Path.Combine(AppContext.BaseDirectory,name.Name+".dll");return File.Exists(path)?System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(path):null;};
Probe.Run(args);
static class Probe
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
public static void Run(string[] args)
{
string root=Path.GetFullPath(args.Length>0?args[0]:".");
var rows=new List<object>();
foreach(string relative in new[]{"assets/fonts/cascadia-mono-2407.24/CascadiaMono-Regular.ttf","assets/fonts/lato-2.015/Lato-Regular.ttf","assets/fonts/lato-2.015/Lato-Bold.ttf","assets/fonts/noto-sans-sc-2.004/XfeWebCjk-Regular.ttf","assets/fonts/noto-sans-sc-2.004/XfeWebCjk-Bold.ttf"})
{
byte[] bytes=File.ReadAllBytes(Path.Combine(root,relative));var metrics=new NativeWebFontMetrics(bytes);var face=new TrueTypeFont(bytes);
double Measure(string text){int height=(int)Math.Round(16*64*metrics.LineToEm);double width=0;char previous='\0';foreach(char ch in text){if(previous!='\0')width+=face.GetKerning(previous,ch,height)/64d;width+=face.GetAdvance(ch,height)/64d;previous=ch;}return width;}
bool cjk=relative.Contains("Cjk");double wide=Measure(cjk?"中文文":"WWW"),narrow=Measure(cjk?",,,":"iii");bool mono=relative.Contains("Mono");
if(!cjk&&(mono?wide!=narrow:wide<=narrow*1.8))throw new Exception("Actual font widths are incorrect: "+relative);
if(metrics.LineToEm<.5||metrics.LineToEm>2)throw new Exception("Invalid CSS em conversion.");
int ink=0,levels=0;
if(cjk)
{
if(!"中文文档预览,这是测试。".All(face.HasGlyph))throw new Exception("CJK outline coverage missing.");
var canvas=new Canvas(256,80);canvas.Clear(0);canvas.DrawString("中文文档预览",face,(int)Math.Round(32*metrics.LineToEm),Color.White,0,0);
var alpha=canvas.GetBuffer()!.Select(value=>(byte)((uint)value>>24)).ToArray();ink=alpha.Count(value=>value>0);levels=alpha.Where(value=>value>0&&value<255).Distinct().Count();
if(ink<500||levels<16)throw new Exception("Real CJK TrueType outline rasterization failed.");
}
rows.Add(new{Font=relative,Sha256=Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes)),CssEm=16,metrics.LineToEm,metrics.AscentToEm,metrics.NaturalLineToEm,Wide=wide,Narrow=narrow,OutlineRasterInk=ink,AntialiasLevels=levels,Passed=true});
}
string iconPath=Path.Combine(root,"assets/fonts/bootstrap-icons-1.11.3/BootstrapIcons.ttf");byte[] iconBytes=File.ReadAllBytes(iconPath);var iconFace=new TrueTypeFont(iconBytes);var iconMetrics=new NativeWebFontMetrics(iconBytes);
string originalCss=File.ReadAllText(Path.Combine(root,"Source/XFESystem.Documents/Assets/Website/Exact/BootstrapIcons.min.css"));var icons=new List<object>();var masks=new HashSet<string>();
foreach(string name in new[]{"bi-list-ol","bi-download","bi-arrows-fullscreen","bi-copy"})
{
var match=System.Text.RegularExpressions.Regex.Match(originalCss,@"\."+name+@"::before\s*\{content:\s*""\\([0-9a-f]+)""");if(!match.Success)throw new Exception("Original icon CSS missing: "+name);
char character=(char)Convert.ToInt32(match.Groups[1].Value,16);if(!iconFace.HasGlyph(character))throw new Exception("Actual TTF cmap missing icon: "+name);
var tile=new Canvas(64,64);tile.Clear(0);int rasterSize=(int)Math.Round(32*iconMetrics.LineToEm);tile.DrawString(character.ToString(),iconFace,rasterSize,Color.White,8,0);
var alpha=tile.GetBuffer()!.Select(value=>(byte)((uint)value>>24)).ToArray();int ink=alpha.Count(value=>value>0),levels=alpha.Where(value=>value>0&&value<255).Distinct().Count();string hash=Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(alpha));
if(ink<50||levels<4||!masks.Add(hash))throw new Exception("Actual icon outline raster failed or duplicates another shape: "+name);
icons.Add(new{Class=name,Codepoint="U+"+((int)character).ToString("X4"),CssEm=32,Advance=iconFace.GetAdvance(character,(int)Math.Round(32*64*iconMetrics.LineToEm))/64d,OutlineRasterInk=ink,AntialiasLevels=levels,AlphaSha256=hash,Passed=true});
}
Console.WriteLine(JsonSerializer.Serialize(new{Schema="XfeNativeWebFontProbe1",Passed=true,Note="Executes the pinned Cosmos TrueTypeFont API on the host; native frame evidence is separate.",Fonts=rows,BootstrapIcons=new{FontSha256=Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(iconBytes)),CSSSha256=Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(originalCss))),OriginalCssUnchanged=true,Glyphs=icons}},new JsonSerializerOptions{WriteIndented=true}));
}
}
using System.Text.Json;
using Cosmos.Kernel.System.Graphics.Fonts;
using Cosmos.Kernel.System.Graphics;
using System.Drawing;
using XFESystem.Kernel;
System.Runtime.Loader.AssemblyLoadContext.Default.Resolving+=(_,name)=>{string path=Path.Combine(AppContext.BaseDirectory,name.Name+".dll");return File.Exists(path)?System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(path):null;};
Probe.Run(args);
static class Probe
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
public static void Run(string[] args)
{
string root=Path.GetFullPath(args.Length>0?args[0]:".");
var rows=new List<object>();
foreach(string relative in new[]{"assets/fonts/cascadia-mono-2407.24/CascadiaMono-Regular.ttf","assets/fonts/lato-2.015/Lato-Regular.ttf","assets/fonts/lato-2.015/Lato-Bold.ttf","assets/fonts/noto-sans-sc-2.004/XfeWebCjk-Regular.ttf","assets/fonts/noto-sans-sc-2.004/XfeWebCjk-Bold.ttf"})
{
byte[] bytes=File.ReadAllBytes(Path.Combine(root,relative));var metrics=new NativeWebFontMetrics(bytes);var face=new TrueTypeFont(bytes);
double Measure(string text){int height=(int)Math.Round(16*64*metrics.LineToEm);double width=0;char previous='\0';foreach(char ch in text){if(previous!='\0')width+=face.GetKerning(previous,ch,height)/64d;width+=face.GetAdvance(ch,height)/64d;previous=ch;}return width;}
bool cjk=relative.Contains("Cjk");double wide=Measure(cjk?"中文文":"WWW"),narrow=Measure(cjk?",,,":"iii");bool mono=relative.Contains("Mono");
if(!cjk&&(mono?wide!=narrow:wide<=narrow*1.8))throw new Exception("Actual font widths are incorrect: "+relative);
if(metrics.LineToEm<.5||metrics.LineToEm>2)throw new Exception("Invalid CSS em conversion.");
int ink=0,levels=0;
if(cjk)
{
if(!"中文文档预览,这是测试。".All(face.HasGlyph))throw new Exception("CJK outline coverage missing.");
var canvas=new Canvas(256,80);canvas.Clear(0);canvas.DrawString("中文文档预览",face,(int)Math.Round(32*metrics.LineToEm),Color.White,0,0);
var alpha=canvas.GetBuffer()!.Select(value=>(byte)((uint)value>>24)).ToArray();ink=alpha.Count(value=>value>0);levels=alpha.Where(value=>value>0&&value<255).Distinct().Count();
if(ink<500||levels<16)throw new Exception("Real CJK TrueType outline rasterization failed.");
}
rows.Add(new{Font=relative,Sha256=Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes)),CssEm=16,metrics.LineToEm,metrics.AscentToEm,metrics.NaturalLineToEm,Wide=wide,Narrow=narrow,OutlineRasterInk=ink,AntialiasLevels=levels,Passed=true});
}
string iconPath=Path.Combine(root,"assets/fonts/bootstrap-icons-1.11.3/BootstrapIcons.ttf");byte[] iconBytes=File.ReadAllBytes(iconPath);var iconFace=new TrueTypeFont(iconBytes);var iconMetrics=new NativeWebFontMetrics(iconBytes);
string originalCss=File.ReadAllText(Path.Combine(root,"Source/XFESystem.Documents/Assets/Website/Exact/BootstrapIcons.min.css"));var icons=new List<object>();var masks=new HashSet<string>();
foreach(string name in new[]{"bi-list-ol","bi-download","bi-arrows-fullscreen","bi-copy"})
{
var match=System.Text.RegularExpressions.Regex.Match(originalCss,@"\."+name+@"::before\s*\{content:\s*""\\([0-9a-f]+)""");if(!match.Success)throw new Exception("Original icon CSS missing: "+name);
char character=(char)Convert.ToInt32(match.Groups[1].Value,16);if(!iconFace.HasGlyph(character))throw new Exception("Actual TTF cmap missing icon: "+name);
var tile=new Canvas(64,64);tile.Clear(0);int rasterSize=(int)Math.Round(32*iconMetrics.LineToEm);tile.DrawString(character.ToString(),iconFace,rasterSize,Color.White,8,0);
var alpha=tile.GetBuffer()!.Select(value=>(byte)((uint)value>>24)).ToArray();int ink=alpha.Count(value=>value>0),levels=alpha.Where(value=>value>0&&value<255).Distinct().Count();string hash=Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(alpha));
if(ink<50||levels<4||!masks.Add(hash))throw new Exception("Actual icon outline raster failed or duplicates another shape: "+name);
icons.Add(new{Class=name,Codepoint="U+"+((int)character).ToString("X4"),CssEm=32,Advance=iconFace.GetAdvance(character,(int)Math.Round(32*64*iconMetrics.LineToEm))/64d,OutlineRasterInk=ink,AntialiasLevels=levels,AlphaSha256=hash,Passed=true});
}
Console.WriteLine(JsonSerializer.Serialize(new{Schema="XfeNativeWebFontProbe1",Passed=true,Note="Executes the pinned Cosmos TrueTypeFont API on the host; native frame evidence is separate.",Fonts=rows,BootstrapIcons=new{FontSha256=Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(iconBytes)),CSSSha256=Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(originalCss))),OriginalCssUnchanged=true,Glyphs=icons}},new JsonSerializerOptions{WriteIndented=true}));
}
}