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

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

什么是摩尔斯电码翻译?

该功能可让您:

目标工作原理
编码 纯文本到字体(或位图图像)的莫尔斯电码字形IFontMorseEncoder 将字符转换为点 (.) 和划 (-) 符号序列,并将其映射到所提供 IFont 的字形标识符。
解码 已包含莫尔斯电码符号的字符串,使其解码回可读文本IFontMorseDecoder 执行相反的操作。
渲染 编码后的字形直接渲染为 PNG(或 Aspose.Font 支持的任何光栅格式)接受渲染参数(LineSpacingType、图像大小等)的重载方法会自动创建图像。

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

为什么要使用它?

开始使用

1 安装NuGet包

1dotnet add package Aspose.Font

2 添加命名空间 (C#)

1using Aspose.Font;
2using Aspose.Font.TextUtils;
3using Aspose.Font.Renderers;   // for PNG rendering
4using Aspose.Font.Rendering;   // for LineSpacingType

API概览

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

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

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

实际例子

  1.  编码为字形 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));

结果:可以输入到自定义绘图管道的整数字形标识符列表。

  1.  编码并渲染 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 使用默认的点 (.) 和破折号 (-) 来可视化摩尔斯电码。更改替代符号的“点”/“破折号”参数(“··”、“—”等)。

  1.  解码回纯文本
1var decoder = TextUtilsFactory.GetFontMorseDecoder();
2
3string morse   = "... --- ...";   // classic SOS
4string result  = decoder.Decode(morse, font);
5
6Console.WriteLine(result);        // prints: SOS

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

  1.  完整的端到端示例(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 提供了 免费网络演示 来尝试翻译器而无需安装任何东西:

演示的特点:

发行说明

Aspose.Font for .NET 23.10 新增 APITextUtilsFactoryIFontMorseEncoderIFontMorseDecoderIMorseEncoderIMorseDecoder、枚举类型 MorseAlphabets

何时使用哪种重载

应用场景推荐重载
仅需字形标识符进行后续处理Encode(string text, IFont font)(或使用显式字母表)
需要图像而无需处理渲染代码Encode(string text, IFont font, double size, LineSpacingType spacing, int w, int h)
自定义点/划符号(例如,Unicode “·” 和 “—”)使用接受 char dotchar dash 的重载
源语言使用非拉丁字母且自动检测失败显式传递相应的 MorseAlphabets

总结

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

无论您正在构建:

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

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.