使用 Aspose.Font 进行摩尔斯电码翻译 | .NET

使用 Aspose.Font for .NET 进行摩尔斯电码翻译

什么是摩尔斯电码翻译?

该功能可让您:

目标工作原理
编码 纯文本到字体(或位图图像)的莫尔斯电码字形IFontMorseEncoder 将字符转换为点 (.) 和划 (-) 符号序列,并将其映射到所提供 IFont 的字形标识符。
将摩尔斯电码字符串解码为可读文本IMorseDecoder 返回纯文本;IFontMorseDecoder 使用指定字体返回字形标识符或 PNG 流。
将文本作为摩尔斯电码渲染为 PNG渲染重载接受字体、字号、行距和最大宽度,并返回 PNG 流。

该功能在 Aspose.Fontfor.NET23.10 中引入(FONTNET-602 – 支持摩尔斯电码编码)。

为什么要使用它?

开始使用

1 安装NuGet包

1dotnet add package Aspose.Font

2 添加命名空间 (C#)

1using System;
2using System.IO;
3using System.Linq;
4using Aspose.Font;
5using Aspose.Font.Factories;
6using Aspose.Font.Glyphs;
7using Aspose.Font.Renderers;
8using Aspose.Font.Sources;
9using Aspose.Font.TextUtils;

API概览

1 个工厂 – 入口点

使用 new TextUtilsFactory() 创建 TextUtilsFactory 实例,然后调用其实例方法:

方法返回值描述
GetFontMorseEncoder()IFontMorseEncoder使用指定字体将文本编码为字形标识符或 PNG 流
GetMorseEncoder()IMorseEncoder无需字体,将文本编码为摩尔斯电码字符串
GetFontMorseDecoder()IFontMorseDecoder使用指定字体将摩尔斯电码解码为字形标识符或 PNG 流
GetMorseDecoder()IMorseDecoder无需字体,将摩尔斯电码字符串解码为纯文本

该工厂位于 Aspose.Font.Factories.TextUtilsFactory (在 23.10 中添加)。

2 核心接口

IFontMorseEncoder

字形重载返回 GlyphId[]。渲染重载返回 PNG 格式的 Stream,调用方应在复制或处理完成后释放该流。

结果参数
GlyphId[]文本和 IFont,使用自动字母表检测或显式指定的 MorseAlphabets 值
PNG Stream文本、IFont、fontSize、RenderingUtils.LineSpacingType、lineSpacingValue 和 maxWidth;另有重载可显式指定字母表

LineSpacingType 支持 Pixels 和 PercentOfFontHeight。渲染器会计算图像高度,没有固定高度参数。

可选字符参数 inputSeparator 和 outputSeparator 用于分隔单词。编码时,它们的默认值分别为空格和 /。它们不会替换摩尔斯电码中的点和划。

IFontMorseDecoder

IFontMorseDecoder 接受摩尔斯电码字符串、字体以及显式指定的 MorseAlphabets 值。它返回解码后文本的 GlyphId[],或者在提供渲染参数时返回 PNG 格式的 Stream,而不是纯文本。

使用 IMorseDecoder 可在不加载字体的情况下获得解码后的 string。解码时,inputSeparator 默认为 /,outputSeparator 默认为空格。

IMorseEncoder / IMorseDecoder

这些仅适用于原始字符串(不涉及字体)。当您只需要“点划”序列时使用它们。

3 支持枚举 – MorseAlphabets

ValueScript
Latin英语和大多数西方语言
Greek希腊字母
Cyrillic俄语、保加利亚语等
Hebrew希伯来字母
Arabic阿拉伯文字
Portuguese葡萄牙语特有符号
Kurdish库尔德语字母
Persian波斯语/法尔西语字母

如果省略枚举,编码器会尝试自动检测。

实际例子

使用字体的示例采用 TTF/LatoWeb-Regular.ttf。将 DataDir 设置为包含 TTF 文件夹的目录,将 OutputDir 设置为已存在的输出目录。解码为纯文本不需要字体。

  1.  编码为字形 ID
 1// Encode text as Morse-code glyph identifiers
 2
 3// Load a font containing the Morse-code symbols.
 4string fontPath = Path.Combine(DataDir, "TTF", "LatoWeb-Regular.ttf");
 5Font font = Font.Open(new FontDefinition(
 6    FontType.TTF, "ttf", new FileSystemStreamSource(fontPath)));
 7
 8// Create the factory and get the font encoder.
 9var factory = new TextUtilsFactory();
10IFontMorseEncoder encoder = factory.GetFontMorseEncoder();
11
12// Encode HELLO WORLD using automatic alphabet detection.
13GlyphId[] glyphIds = encoder.Encode("HELLO WORLD", font);
14Console.WriteLine(string.Join(", ", glyphIds.Select(id => id.ToString())));

结果:表示摩尔斯电码符号的 GlyphId 对象数组,可用于自定义绘制流程。

  1.  编码并渲染 PNG

此片段复用前一个示例中的 font 和 encoder,并将 morse.png 保存到 OutputDir。

 1// Encode text as Morse code and save a PNG
 2
 3double fontSize = 48;
 4var spacing = RenderingUtils.LineSpacingType.Pixels;
 5int lineSpacing = 10;
 6int maxWidth = 800;
 7
 8// The renderer returns a stream and determines the image height.
 9using (Stream pngStream = encoder.Encode(
10    "SOS HELP", font, fontSize, spacing, lineSpacing, maxWidth))
11{
12    if (pngStream.CanSeek)
13        pngStream.Position = 0;
14
15    using (FileStream output = File.Create(Path.Combine(OutputDir, "morse.png")))
16    {
17        pngStream.CopyTo(output);
18    }
19}

PNG 使用字体中的点(.)和划(-)字形显示摩尔斯电码。字符参数控制的是单词分隔符,而不是替代的点或划符号。

  1.  解码回纯文本
1// Decode a Morse-code string to plain text
2
3// Plain-text decoding does not require a font.
4IMorseDecoder decoder = new TextUtilsFactory().GetMorseDecoder();
5string morse = "... --- ...";
6string result = decoder.Decode(morse, MorseAlphabets.Latin);
7Console.WriteLine(result); // SOS

当编码期间使用自定义字母表时,将相同的“MorseAlphabets”值传递给解码器。

  1. 组合示例(C#)
 1// Render Morse code, obtain glyph identifiers and decode a message
 2
 3// Load a font.
 4string fontPath = Path.Combine(DataDir, "TTF", "LatoWeb-Regular.ttf");
 5Font font = Font.Open(new FontDefinition(
 6    FontType.TTF, "ttf", new FileSystemStreamSource(fontPath)));
 7
 8var factory = new TextUtilsFactory();
 9IFontMorseEncoder encoder = factory.GetFontMorseEncoder();
10IMorseDecoder decoder = factory.GetMorseDecoder();
11const string message = "CALL ME AT 5PM";
12
13// Encode to PNG. Height is calculated by the renderer.
14using (Stream pngStream = encoder.Encode(
15    message, font,
16    fontSize: 36,
17    lineSpacingType: RenderingUtils.LineSpacingType.Pixels,
18    lineSpacingValue: 10,
19    maxWidth: 900))
20{
21    if (pngStream.CanSeek)
22        pngStream.Position = 0;
23
24    using (FileStream output = File.Create(Path.Combine(OutputDir, "callme.png")))
25    {
26        pngStream.CopyTo(output);
27    }
28}
29Console.WriteLine("PNG created.");
30
31// Encode to glyph identifiers.
32GlyphId[] ids = encoder.Encode(message, font);
33Console.WriteLine("Glyph IDs: " + string.Join(", ", ids.Select(id => id.ToString())));
34
35// Decode a plain Morse string.
36string morse = "... --- ...";
37string decoded = decoder.Decode(morse, MorseAlphabets.Latin);
38Console.WriteLine($"Decoded '{morse}' -> {decoded}");

组合示例将 callme.png 保存到 OutputDir,输出编码后的字形标识符,并将 ... --- ... 解码为 SOS。请使用上文所示的命名空间和目录设置。

在线演示

Aspose 提供了 免费网络演示 来尝试翻译器而无需安装任何东西:

演示的特点:

发行说明

Aspose.Font for .NET 23.10 新增 API:TextUtilsFactory、IFontMorseEncoder、IFontMorseDecoder、IMorseEncoder、IMorseDecoder、枚举类型 MorseAlphabets。

何时使用哪种重载

应用场景推荐重载
仅需字形标识符进行后续处理Encode(string text, IFont font)(或使用显式字母表)
无需自行编写渲染代码即可获取 PNG 流使用接受 fontSize、lineSpacingType、lineSpacingValue 和 maxWidth 的重载
自定义单词分隔符设置 inputSeparator 和 outputSeparator;这些参数不会更改点或划符号
源语言使用非拉丁字母且自动检测失败显式传递相应的 MorseAlphabets 值

总结

Aspose.Font 现在包含一个 完整的摩尔斯电码翻译引擎,可在 字形级别 和 位图级别 运行。使用简单的工厂方法,您可以立即开始编码、解码和渲染受支持字母表中的文本,而无需编写自己的映射表或处理低级渲染。

无论您正在构建:

API 为您提供完全控制,同时保持代码整洁、跨平台和完全支持。