spatialillusions / milsymbol

Military Symbols in JavaScript
www.spatialillusions.com/milsymbol
MIT License
556 stars 136 forks source link

C++ port of Milsymbol #255

Closed JackGit3 closed 3 years ago

JackGit3 commented 3 years ago

There's a comment at the bottom of this issue: https://github.com/spatialillusions/milsymbol/issues/36 that mentions Milsymbol has been ported to C++ before. I can't find any information about it from googling though and I'm wondering if anybody knows what that's about and where I could find it if it is opensource.

stewienj commented 3 years ago

I don't know if this will help you, but I've been using this JavaScript library in C++ by calling it via a .NET javascript compiler from Microsoft called ClearScript https://github.com/microsoft/ClearScript which works on Windows, Linux, and MacOS.

I use ClearScript in C# like this to run milsymbol.js:

V8ScriptEngine engine = new V8ScriptEngine(nameof(MilStd2525), V8ScriptEngineFlags.EnableDebugging);
string jsContents = string.Empty;
using (Stream stream = File.Open("milsymbol.js"))
{
  using (var reader = new StreamReader(stream))
  {
    jsContents = reader.ReadToEnd();
  }
}
engine.Execute(jsContents);

and I have a function that gives me the symbol like this:

public string CreateSymbolAsSvgString(int size, string sidc, out bool isValid)
{
  sidc = $"\"{sidc}\"";

  // Insert javascript as a string for the milsymbol creation
  var script = $@"var size = {size};
            var sidc = {sidc};
            var symbol = new ms.Symbol(sidc, {{ size: {size} }});
            var symbolValid = symbol.isValid(false);
            var symbolSvg = symbol.asSVG();";
  engine.Execute(script);

  isValid = (bool)engine.Script.symbolValid;

  // Return svg as string
  return (string)engine.Script.symbolSvg;
}
JackGit3 commented 3 years ago

Thanks for the example code, I ended up doing something similar since I was using UE4, I just used the unreal.js plugin to handle the milsymbol side of things and then send the svg string over to cpp.