Morse‑Code Translation with Aspose.Font | .NET
Morse‑Code Translation with Aspose.Font for .NET
What is Morse‑Code translation?
This is functionality that lets you:
| Goal | How it works |
|---|---|
| Encode plain‑text into Morse‑code glyphs of a font (or a bitmap image) | IFontMorseEncoder converts characters to a sequence of dot (.) and dash (-) symbols and maps them to glyph identifiers of the supplied IFont. |
| Decode a string that already contains Morse symbols back into readable text | IFontMorseDecoder performs the reverse operation. |
| Render the encoded glyphs directly to PNG (or any raster format supported by Aspose.Font) | Overloads that accept rendering parameters (LineSpacingType, image size, etc.) create the image for you. |
The feature was introduced in Aspose.Font for .NET 23.10 (FONTNET‑602 – Support for Morse code encoding).
Why use it?
- Pure‑C# solution – no external libraries, no OS‑level dependencies.
- Works with any font that contains the required glyphs (TrueType, OpenType, CFF …).
- Built‑in heuristics automatically pick the correct alphabet (Latin, Greek, Cyrillic, etc.) when you omit it.
- Both text → glyph IDs (useful for custom rendering pipelines) and glyph IDs → PNG are available out‑of‑the‑box.
- Fully covered by Aspose’s support (forums, docs, live demo).
Getting started
1 Install the NuGet package
1dotnet add package Aspose.Font2 Add namespaces (C#)
1using Aspose.Font;
2using Aspose.Font.TextUtils;
3using Aspose.Font.Renderers; // for PNG rendering
4using Aspose.Font.Rendering; // for LineSpacingTypeAPI overview
1 Factory – entry point
| Method | Returns | Description |
|---|---|---|
TextUtilsFactory.GetFontMorseEncoder() | IFontMorseEncoder | Encoder that works with an IFont instance |
TextUtilsFactory.GetMorseEncoder() | IMorseEncoder | Encoder that produces plain Morse strings (no font) |
TextUtilsFactory.GetFontMorseDecoder() | IFontMorseDecoder | Decoder that reads glyphs back to text |
TextUtilsFactory.GetMorseDecoder() | IMorseDecoder | Decoder for plain Morse strings |
The factory lives in Aspose.Font.Factories.TextUtilsFactory (added in 23.10).
2 Core interfaces
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
These work with raw strings only (no font involved). Use them when you only need “dot‑dash” sequences.
3 Supporting enum – MorseAlphabets
| Value | Script |
|---|---|
Latin | English & most Western languages |
Greek | Greek letters |
Cyrillic | Russian, Bulgarian, etc. |
Hebrew | Hebrew letters |
Arabic | Arabic script |
Portuguese | Portuguese specific symbols |
Kurdish | Kurdish letters |
Persian | Persian / Farsi letters |
If you omit the enum, the encoder attempts automatic detection.
Practical examples
- Encode to glyph IDs
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));Result: A list of integer glyph identifiers that can be fed to custom drawing pipelines.
- Encode and render a 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);The PNG visualises Morse code using the default dot (.) and dash (-). Change dot/dash parameters for alternative symbols ('·', '—', etc.).
- Decode back to plain text
1var decoder = TextUtilsFactory.GetFontMorseDecoder();
2
3string morse = "... --- ..."; // classic SOS
4string result = decoder.Decode(morse, font);
5
6Console.WriteLine(result); // prints: SOSWhen a custom alphabet was used during encoding, pass the same MorseAlphabets value to the decoder.
- Full end‑to‑end sample (C# console)
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}Run it on any .NET Core / .NET Framework project – the only external dependency is Aspose.Font.
Online demo
Aspose provides a free web demo to try the translator without installing anything:
Features of the demo:
- Text ↔︎ Morse ↔︎ Glyph rendering
- Choose custom dot/dash characters
- Download generated PNG or view glyph IDs
Release notes
Aspose.Font for .NET 23.10
Added APIs:TextUtilsFactory,IFontMorseEncoder,IFontMorseDecoder,IMorseEncoder,IMorseDecoder, enumerationMorseAlphabets.
New Feature: “Support for Morse code encoding”.
When to use which overload
| Situation | Recommended overload |
|---|---|
| Need only glyph identifiers for further processing | Encode(string text, IFont font) (or with explicit alphabet) |
| Want an image without handling rendering code | Encode(string text, IFont font, double size, LineSpacingType spacing, int w, int h) |
| Custom dot/dash symbols (e.g., Unicode “·” & “—”) | Use overloads that accept char dot and char dash |
| Source language uses a non‑Latin script and auto‑detect fails | Pass the appropriate MorseAlphabets value explicitly |
Summary
Aspose.Font now includes a complete Morse‑code translation engine that operates at both the glyph level and the bitmap level. Using the simple factory methods you can instantly start encoding, decoding, and rendering any text in dozens of alphabets without writing your own mapping tables or dealing with low‑level rendering.
Whether you are building:
- A custom signal‑visualisation component
- An educational tool to teach Morse communication
- A data‑obfuscation step that stores information as stylised fonts
the API gives you full control while keeping the code clean, cross‑platform, and fully supported.