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 | IMorseDecoder returns plain text; IFontMorseDecoder returns glyph identifiers or a PNG stream using a supplied font. |
| Render text as Morse code in PNG format | Rendering overloads accept a font, font size, line spacing, and maximum width, and return a PNG stream. |
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 text → Morse 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 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 overview
1 Factory – entry point
Create a TextUtilsFactory instance with new TextUtilsFactory(), then call its instance methods:
| Method | Returns | Description |
|---|---|---|
GetFontMorseEncoder() | IFontMorseEncoder | Encodes text as glyph identifiers or a PNG stream using a supplied font |
GetMorseEncoder() | IMorseEncoder | Encodes text as a plain Morse string without a font |
GetFontMorseDecoder() | IFontMorseDecoder | Decodes Morse into glyph identifiers or a PNG stream using a supplied font |
GetMorseDecoder() | IMorseDecoder | Decodes a Morse string to plain text without a font |
The factory lives in Aspose.Font.Factories.TextUtilsFactory (added in 23.10).
2 Core interfaces
IFontMorseEncoder
The glyph overloads return GlyphId[]. The rendering overloads return a PNG Stream, which the caller should dispose after copying or consuming it.
| Result | Parameters |
|---|---|
GlyphId[] | Text and an IFont, with either automatic alphabet detection or an explicit MorseAlphabets value |
PNG Stream | Text, an IFont, fontSize, RenderingUtils.LineSpacingType, lineSpacingValue, and maxWidth, with an optional explicit alphabet overload |
LineSpacingType supports Pixels and PercentOfFontHeight. The renderer calculates the image height; there is no fixed-height parameter.
The optional inputSeparator and outputSeparator characters delimit words. For encoding, their defaults are a space and /, respectively. They do not replace the Morse dot and dash symbols.
IFontMorseDecoder
IFontMorseDecoder accepts a Morse string, a font, and an explicit MorseAlphabets value. It returns either GlyphId[] for the decoded text or a PNG Stream when rendering parameters are supplied. It does not return plain text.
Use IMorseDecoder to obtain a decoded string without loading a font. For decoding, inputSeparator defaults to / and outputSeparator defaults to a space.
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
The font-based examples use TTF/LatoWeb-Regular.ttf. Set DataDir to the directory containing the TTF folder and OutputDir to an existing output directory. Plain-text decoding does not require a font.
- Encode to glyph IDs
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())));Result: An array of GlyphId objects representing the Morse symbols, suitable for custom drawing pipelines.
- Encode and render a PNG
This fragment reuses the font and encoder from the preceding example. It saves morse.png in 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}The PNG visualises Morse code using dot (.) and dash (-) glyphs from the font. The character parameters control word separators, not alternative dot/dash symbols.
- Decode back to plain text
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); // SOSWhen a custom alphabet was used during encoding, pass the same MorseAlphabets value to the decoder.
- Combined example (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}");The combined example saves callme.png in OutputDir, prints the encoded glyph identifiers, and decodes ... --- ... to SOS. Use the namespaces and directory setup shown above.
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 a PNG stream without handling rendering code | Use the overload with fontSize, lineSpacingType, lineSpacingValue, and maxWidth |
| Custom word separators | Set inputSeparator and outputSeparator; these parameters do not change dot/dash symbols |
| 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 text in the supported 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.