static string BuildCharRangesParamValue(FontCompileParams @params) { var ranges = @params.CharRanges; var buffer = new StringBuilder(); for (int i = 0; i < ranges.Length; ++i) { if (i > 0) { buffer.Append(','); } var range = ranges[i]; buffer.Append(range.Start); buffer.Append('-'); buffer.Append(range.End); } return(buffer.ToString()); }
public static FontCompileResult Compile(FontCompileParams @params) { var process_info = new ProcessStartInfo { UseShellExecute = false, FileName = COMPILER_PATH }; var font_file_copy_path = Path.Combine(Path.GetTempPath(), Path.GetFileName(@params.FontFilePath)); var output_font_image_path = Path.Combine(Path.GetTempPath(), @params.FontName + FONT_IMAGE_SUFFIX + FONT_IMAGE_EXT); var output_font_descr_path = Path.Combine(Path.GetTempPath(), @params.FontName + FONT_DESCR_EXT); File.Delete(font_file_copy_path); File.Delete(output_font_image_path); File.Delete(output_font_descr_path); File.Copy(@params.FontFilePath, font_file_copy_path); @params.FontFilePath = font_file_copy_path; if (@params.CharRangeLevel == 0) { @params.CharRanges = new CharRange[1]; @params.CharRanges[0] = CharRange.Latin; } else if (@params.CharRangeLevel == 1) { @params.CharRanges = new CharRange[2]; @params.CharRanges[0] = CharRange.Latin; @params.CharRanges[1] = CharRange.LatinSupplement; } int tex_size = GetOptimalTextureSize(@params.FontSize, @params.CharRanges.Length); @params.TexWidth = tex_size; @params.TexHeight = tex_size; process_info.Arguments = BuildArgs(@params); process_info.WorkingDirectory = Path.GetTempPath(); var proc = Process.Start(process_info); proc?.WaitForExit(); var output = proc?.ExitCode ?? -1; string result = string.Empty; if (output != 0 && output != -1) { using var reader = proc?.StandardError; result = reader?.ReadToEnd(); } if (result.Length > 0) { throw new Exception($"Error while building font: {result}"); } try { using var font_image_stream = File.OpenRead(output_font_image_path); var font_image = ImageResult.FromStream(font_image_stream, ColorComponents.RedGreenBlueAlpha); byte[] font_desc_bytes = File.ReadAllBytes(output_font_descr_path); return(new FontCompileResult(font_image.Data, @params.TexWidth, @params.TexHeight, font_desc_bytes)); } catch (Exception e) { throw new Exception($"Error while processing font compile output: {e.Message}"); } }
private static string BuildArgs(FontCompileParams @params) {
public static FontData Build(FontBuildParams @params) { var compile_params = new FontCompileParams() { CharRangeLevel = @params.CharRangeLevel, FontFilePath = ResourceLoader.GetFullResourcePath(@params.Path), FontName = @params.Id, FontSize = @params.Size, SpacingH = 1, SpacingV = 1, PaddingUp = @params.PaddingUp, PaddingDown = @params.PaddingDown, PaddingLeft = @params.PaddingLeft, PaddingRight = @params.PaddingRight }; Console.WriteLine($"Compiling Font: {@params.Id}, Size: {@params.Size}"); if (@params.DropShadow) { if (Calc.Abs(@params.ShadowOffsetX) > @params.PaddingLeft + @params.PaddingRight) { @params.PaddingLeft = Calc.Abs(@params.ShadowOffsetX); @params.PaddingRight = Calc.Abs(@params.ShadowOffsetX); } if (Calc.Abs(@params.ShadowOffsetY) > @params.PaddingUp + @params.PaddingDown) { @params.PaddingUp = Calc.Abs(@params.ShadowOffsetY); @params.PaddingDown = Calc.Abs(@params.ShadowOffsetY); } compile_params.PaddingUp = @params.PaddingUp; compile_params.PaddingDown = @params.PaddingDown; compile_params.PaddingLeft = @params.PaddingLeft; compile_params.PaddingRight = @params.PaddingRight; } var compile_result = FontCompiler.Compile(compile_params); if (@params.DropShadow) { Blitter.Begin(compile_result.FontImageData, compile_result.FontImageWidth, compile_result.FontImageHeight); Blitter.DropShadow(@params.ShadowOffsetX, @params.ShadowOffsetY, new Color(@params.ShadowColor)); Blitter.End(); } var font_data = new FontData() { Id = @params.Id, FontSheet = new ImageData() { Id = @params.Id + "_texture", Data = compile_result.FontImageData, Width = compile_result.FontImageWidth, Height = compile_result.FontImageHeight }, }; FontDescrParser.ParseAndFillData(font_data, compile_result.FontDescrData); return(font_data); }