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:

GoalHow 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 textIFontMorseDecoder 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?

Getting started

1 Install the NuGet package

1dotnet add package Aspose.Font

2 Add namespaces (C#)

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

API overview

1 Factory – entry point

MethodReturnsDescription
TextUtilsFactory.GetFontMorseEncoder()IFontMorseEncoderEncoder that works with an IFont instance
TextUtilsFactory.GetMorseEncoder()IMorseEncoderEncoder that produces plain Morse strings (no font)
TextUtilsFactory.GetFontMorseDecoder()IFontMorseDecoderDecoder that reads glyphs back to text
TextUtilsFactory.GetMorseDecoder()IMorseDecoderDecoder 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

ValueScript
LatinEnglish & most Western languages
GreekGreek letters
CyrillicRussian, Bulgarian, etc.
HebrewHebrew letters
ArabicArabic script
PortuguesePortuguese specific symbols
KurdishKurdish letters
PersianPersian / Farsi letters

If you omit the enum, the encoder attempts automatic detection.

Practical examples

  1.  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.

  1.  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.).

  1.  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: SOS

When a custom alphabet was used during encoding, pass the same MorseAlphabets value to the decoder.

  1.  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:

Release notes

Aspose.Font for .NET 23.10
Added APIs: TextUtilsFactory, IFontMorseEncoder, IFontMorseDecoder, IMorseEncoder, IMorseDecoder, enumeration MorseAlphabets.
New Feature: “Support for Morse code encoding”.

When to use which overload

SituationRecommended overload
Need only glyph identifiers for further processingEncode(string text, IFont font) (or with explicit alphabet)
Want an image without handling rendering codeEncode(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 failsPass 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:

the API gives you full control while keeping the code clean, cross‑platform, and fully supported.

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.