Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/475ee235c59099937496d1571abde8fd to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/475ee235c59099937496d1571abde8fd to your computer and use it in GitHub Desktop.
Morse code encoding and decoding, alphabets, separators and application scenarios.

Morse Code Translation Examples

Morse code encoding and decoding, alphabets, separators and application scenarios.

Status

Four documentation examples are implemented: encoding to glyph identifiers, rendering a PNG, decoding to plain text, and a combined scenario. Runtime verification is pending the user-run tests.

Running the examples

From the repository root:

dotnet test src/Aspose.Font.Net/Aspose.Font.Tests.csproj --filter "TestCategory=MorseCodeExamples"

Input under tests-data/Aspose.Font: TTF/LatoWeb-Regular.ttf. Plain-text decoding does not require a font. BaseTests supplies the existing license setup. Output files under tests-out/Aspose.Font.Net/MorseCode: morse.png and callme.png. The PNG fragment continues the font/encoder setup shown in the preceding glyph example; the test supplies that setup outside its snippet.

The current API returns GlyphId[] for glyph encoding and Stream for PNG rendering. Rendering uses pixel line spacing and maximum width; image height is calculated automatically. Character parameters are word separators, not replacement dot/dash symbols. Use IMorseDecoder for plain text; IFontMorseDecoder returns glyph identifiers or a rendered stream.

Related Documentation

Related Resources

Requirements

  • .NET 5.0+, .NET Core, or .NET Framework
  • Aspose.Font for .NET library
Aspose.Font for .NET – Morse Code Translation Examples
// Decode a Morse-code string to plain text
// Learn more: https://docs.aspose.com/font/net/developer-guide/morse-code-translation/
// Plain-text decoding does not require a font.
IMorseDecoder decoder = new TextUtilsFactory().GetMorseDecoder();
string morse = "... --- ...";
string result = decoder.Decode(morse, MorseAlphabets.Latin);
Console.WriteLine(result); // SOS
// Encode text as Morse-code glyph identifiers
// Learn more: https://docs.aspose.com/font/net/developer-guide/morse-code-translation/
// Load a font containing the Morse-code symbols.
string fontPath = Path.Combine(DataDir, "TTF", "LatoWeb-Regular.ttf");
Font font = Font.Open(new FontDefinition(
FontType.TTF, "ttf", new FileSystemStreamSource(fontPath)));
// Create the factory and get the font encoder.
var factory = new TextUtilsFactory();
IFontMorseEncoder encoder = factory.GetFontMorseEncoder();
// Encode HELLO WORLD using automatic alphabet detection.
GlyphId[] glyphIds = encoder.Encode("HELLO WORLD", font);
Console.WriteLine(string.Join(", ", glyphIds.Select(id => id.ToString())));
// Render Morse code, obtain glyph identifiers and decode a message
// Learn more: https://docs.aspose.com/font/net/developer-guide/morse-code-translation/
// Load a font.
string fontPath = Path.Combine(DataDir, "TTF", "LatoWeb-Regular.ttf");
Font font = Font.Open(new FontDefinition(
FontType.TTF, "ttf", new FileSystemStreamSource(fontPath)));
var factory = new TextUtilsFactory();
IFontMorseEncoder encoder = factory.GetFontMorseEncoder();
IMorseDecoder decoder = factory.GetMorseDecoder();
const string message = "CALL ME AT 5PM";
// Encode to PNG. Height is calculated by the renderer.
using (Stream pngStream = encoder.Encode(
message, font,
fontSize: 36,
lineSpacingType: RenderingUtils.LineSpacingType.Pixels,
lineSpacingValue: 10,
maxWidth: 900))
{
if (pngStream.CanSeek)
pngStream.Position = 0;
using (FileStream output = File.Create(Path.Combine(OutputDir, "callme.png")))
{
pngStream.CopyTo(output);
}
}
Console.WriteLine("PNG created.");
// Encode to glyph identifiers.
GlyphId[] ids = encoder.Encode(message, font);
Console.WriteLine("Glyph IDs: " + string.Join(", ", ids.Select(id => id.ToString())));
// Decode a plain Morse string.
string morse = "... --- ...";
string decoded = decoder.Decode(morse, MorseAlphabets.Latin);
Console.WriteLine($"Decoded '{morse}' -> {decoded}");
// Encode text as Morse code and save a PNG
// Learn more: https://docs.aspose.com/font/net/developer-guide/morse-code-translation/
double fontSize = 48;
var spacing = RenderingUtils.LineSpacingType.Pixels;
int lineSpacing = 10;
int maxWidth = 800;
// The renderer returns a stream and determines the image height.
using (Stream pngStream = encoder.Encode(
"SOS HELP", font, fontSize, spacing, lineSpacing, maxWidth))
{
if (pngStream.CanSeek)
pngStream.Position = 0;
using (FileStream output = File.Create(Path.Combine(OutputDir, "morse.png")))
{
pngStream.CopyTo(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment