使用 Aspose.Font 进行摩尔斯电码翻译 | .NET
使用 Aspose.Font for .NET 进行摩尔斯电码翻译
什么是摩尔斯电码翻译?
该功能可让您:
| 目标 | 工作原理 |
|---|---|
| 编码 纯文本到字体(或位图图像)的莫尔斯电码字形 | IFontMorseEncoder 将字符转换为点 (.) 和划 (-) 符号序列,并将其映射到所提供 IFont 的字形标识符。 |
| 解码 已包含莫尔斯电码符号的字符串,使其解码回可读文本 | IFontMorseDecoder 执行相反的操作。 |
| 渲染 编码后的字形直接渲染为 PNG(或 Aspose.Font 支持的任何光栅格式) | 接受渲染参数(LineSpacingType、图像大小等)的重载方法会自动创建图像。 |
该功能在 Aspose.Fontfor.NET23.10 中引入(FONTNET-602 – 支持摩尔斯电码编码)。
为什么要使用它?
- 纯 C# 解决方案 – 没有外部库,没有操作系统级依赖项。
- 适用于包含所需字形的任何字体(TrueType、OpenType、CFF …)。
- 当您省略时,内置启发式算法会自动选择正确的字母(拉丁语、希腊语、西里尔语等)。
- 文本 → 字形 ID(对于自定义渲染管道很有用)和 字形 ID → PNG 都是开箱即用的。
- Aspose 的支持完全覆盖(论坛、文档、现场演示)。
开始使用
1 安装NuGet包
1dotnet add package Aspose.Font2 添加命名空间 (C#)
1using Aspose.Font;
2using Aspose.Font.TextUtils;
3using Aspose.Font.Renderers; // for PNG rendering
4using Aspose.Font.Rendering; // for LineSpacingTypeAPI概览
1 个工厂 – 入口点
| 方法 | 返回值 | 描述 |
|---|---|---|
TextUtilsFactory.GetFontMorseEncoder() | IFontMorseEncoder | 使用 IFont 实例的编码器 |
TextUtilsFactory.GetMorseEncoder() | IMorseEncoder | 生成纯莫尔斯电码字符串(无字体)的编码器 |
TextUtilsFactory.GetFontMorseDecoder() | IFontMorseDecoder | 将字形解码回文本的解码器 |
TextUtilsFactory.GetMorseDecoder() | IMorseDecoder | 用于解码纯莫尔斯电码字符串的解码器 |
该工厂位于 Aspose.Font.Factories.TextUtilsFactory (在 23.10 中添加)。
2 核心接口
IFontMorseEncoder
1public interface IFontMorseEncoder
2{
3 IEnumerable<int> Encode(string text, IFont font,
4 char dot = '.', char dash = '-');
5
6 IEnumerable<int> Encode(string text, IFont font,
7 MorseAlphabets alphabet,
8 char dot = '.', char dash = '-');
9
10 byte[] Encode(string text, IFont font,
11 double fontSize,
12 LineSpacingType spacing,
13 int width, int height,
14 char dot = '.', char dash = '-');
15
16 byte[] Encode(string text, IFont font,
17 double fontSize,
18 LineSpacingType spacing,
19 int width, int height,
20 MorseAlphabets alphabet,
21 char dot = '.', char dash = '-');
22}IFontMorseDecoder
1public interface IFontMorseDecoder
2{
3 string Decode(string morseText, IFont font,
4 char dot = '.', char dash = '-');
5
6 string Decode(string morseText, IFont font,
7 MorseAlphabets alphabet,
8 char dot = '.', char dash = '-');
9}IMorseEncoder / IMorseDecoder
这些仅适用于原始字符串(不涉及字体)。当您只需要“点划”序列时使用它们。
3 支持枚举 – MorseAlphabets
| Value | Script |
|---|---|
Latin | 英语和大多数西方语言 |
Greek | 希腊字母 |
Cyrillic | 俄语、保加利亚语等 |
Hebrew | 希伯来字母 |
Arabic | 阿拉伯文字 |
Portuguese | 葡萄牙语特有符号 |
Kurdish | 库尔德语字母 |
Persian | 波斯语/法尔西语字母 |
如果省略枚举,编码器会尝试自动检测。
实际例子
- 编码为字形 ID
1// Load any TrueType/OpenType font.
2IFont font = FontFactory.Open("arial.ttf");
3
4// Get the encoder.
5var encoder = TextUtilsFactory.GetFontMorseEncoder();
6
7// Encode “HELLO WORLD”.
8IEnumerable<int> glyphIds = encoder.Encode(
9 "HELLO WORLD",
10 font);
11
12Console.WriteLine(string.Join(", ", glyphIds));结果:可以输入到自定义绘图管道的整数字形标识符列表。
- 编码并渲染 PNG
1double fontSize = 48; // points
2var spacing = RenderingUtils.LineSpacingType.Auto;
3int imgWidth = 800;
4int imgHeight = 200;
5
6byte[] pngBytes = encoder.Encode(
7 "SOS HELP",
8 font,
9 fontSize,
10 spacing,
11 imgWidth,
12 imgHeight);
13
14File.WriteAllBytes("morse.png", pngBytes);PNG 使用默认的点 (.) 和破折号 (-) 来可视化摩尔斯电码。更改替代符号的“点”/“破折号”参数(“··”、“—”等)。
- 解码回纯文本
1var decoder = TextUtilsFactory.GetFontMorseDecoder();
2
3string morse = "... --- ..."; // classic SOS
4string result = decoder.Decode(morse, font);
5
6Console.WriteLine(result); // prints: SOS当编码期间使用自定义字母表时,将相同的“MorseAlphabets”值传递给解码器。
- 完整的端到端示例(C# 控制台)
1using System;
2using System.IO;
3using Aspose.Font;
4using Aspose.Font.TextUtils;
5using Aspose.Font.Rendering;
6
7class Program
8{
9 static void Main()
10 {
11 // Load a font.
12 IFont font = FontFactory.Open("CourierNew.ttf");
13
14 // Factories.
15 var factory = TextUtilsFactory.Instance;
16 var encoder = factory.GetFontMorseEncoder();
17 var decoder = factory.GetFontMorseDecoder();
18
19 const string message = "CALL ME AT 5PM";
20
21 // Encode → PNG.
22 byte[] png = encoder.Encode(
23 message,
24 font,
25 fontSize: 36,
26 spacing: RenderingUtils.LineSpacingType.Auto,
27 width: 900,
28 height: 150);
29
30 File.WriteAllBytes("callme.png", png);
31 Console.WriteLine("PNG created.");
32
33 // Encode → glyph IDs.
34 var ids = encoder.Encode(message, font);
35 Console.WriteLine("Glyph IDs: " + string.Join(", ", ids));
36
37 // Decode a plain Morse string.
38 string morse = "... --- ...";
39 string decoded = decoder.Decode(morse, font);
40 Console.WriteLine($"Decoded '{morse}' → {decoded}");
41 }
42}在任何 .NET Core / .NET Framework 项目上运行它 - 唯一的外部依赖项是 Aspose.Font。
在线演示
Aspose 提供了 免费网络演示 来尝试翻译器而无需安装任何东西:
演示的特点:
- 文本 ↔︎ 莫尔斯电码 ↔︎ 字形渲染
- 选择自定义点/划字符
- 下载生成的 PNG 或查看字形 ID
发行说明
Aspose.Font for .NET 23.10 新增 API:
TextUtilsFactory、IFontMorseEncoder、IFontMorseDecoder、IMorseEncoder、IMorseDecoder、枚举类型MorseAlphabets。
何时使用哪种重载
| 应用场景 | 推荐重载 |
|---|---|
| 仅需字形标识符进行后续处理 | Encode(string text, IFont font)(或使用显式字母表) |
| 需要图像而无需处理渲染代码 | Encode(string text, IFont font, double size, LineSpacingType spacing, int w, int h) |
| 自定义点/划符号(例如,Unicode “·” 和 “—”) | 使用接受 char dot 和 char dash 的重载 |
| 源语言使用非拉丁字母且自动检测失败 | 显式传递相应的 MorseAlphabets 值 |
总结
Aspose.Font 现在包含一个 完整的摩尔斯电码翻译引擎,可在 字形级别 和 位图级别 运行。使用简单的工厂方法,您可以立即开始编码、解码和渲染数十种字母表中的任何文本,而无需编写自己的映射表或处理低级渲染。
无论您正在构建:
- 自定义信号可视化组件
- 教授莫尔斯通讯的教育工具
- 将信息存储为风格化字体的数据混淆步骤
API 为您提供完全控制,同时保持代码整洁、跨平台和完全支持。