Aspose.Font を使用したモールス信号変換 | .NET

Aspose.Font for.NET を使用したモールス信号変換

モールス信号翻訳とは何ですか?

これは次のことを可能にする機能です。

目標仕組み
プレーンテキストをフォント (またはビットマップ画像) のモールス信号グリフにエンコードしますIFontMorseEncoder は、文字を ドット (.) と ダッシュ (-) 記号のシーケンスに変換し、指定された IFont のグリフ識別子にマッピングします。
モールス信号の文字列を読みやすいテキストにデコードIMorseDecoder はプレーンテキストを返します。IFontMorseDecoder は指定されたフォントを使用してグリフ識別子または PNG ストリームを返します。
テキストをモールス信号として PNG 形式で描画描画用オーバーロードはフォント、フォントサイズ、行間、最大幅を受け取り、PNG ストリームを返します。

この機能は Aspose.Fontfor.NET23.10 (FONTNET‑602 – モールス符号エンコーディングのサポート) で導入されました。

なぜそれを使うのでしょうか?

はじめに

1 NuGet パッケージをインストールする

1dotnet add package Aspose.Font

2 名前空間の追加 (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の概要

1 工場 – エントリーポイント

new TextUtilsFactory() で TextUtilsFactory のインスタンスを作成し、そのインスタンスメソッドを呼び出します。

メソッド戻り値説明
GetFontMorseEncoder()IFontMorseEncoder指定されたフォントを使用してテキストをグリフ識別子または PNG ストリームにエンコード
GetMorseEncoder()IMorseEncoderフォントを使用せずにテキストをモールス信号の文字列にエンコード
GetFontMorseDecoder()IFontMorseDecoder指定されたフォントを使用してモールス信号をグリフ識別子または PNG ストリームにデコード
GetMorseDecoder()IMorseDecoderフォントを使用せずにモールス信号の文字列をプレーンテキストにデコード

ファクトリは Aspose.Font.Factories.TextUtilsFactory にあります (23.10 で追加)。

2 コアインターフェイス

IFontモールスエンコーダ

グリフ用オーバーロードは GlyphId[] を返します。描画用オーバーロードは PNG の Stream を返します。呼び出し元は、コピーまたは処理の完了後にこのストリームを破棄する必要があります。

結果パラメーター
GlyphId[]テキストと IFont。アルファベットの自動検出、または明示的な MorseAlphabets 値を使用
PNG Streamテキスト、IFont、fontSize、RenderingUtils.LineSpacingType、lineSpacingValue、maxWidth。アルファベットを明示する別のオーバーロードも利用可能

LineSpacingType は Pixels と PercentOfFontHeight をサポートします。画像の高さは描画処理で計算され、固定の高さを指定するパラメーターはありません。

省略可能な文字パラメーター inputSeparator と outputSeparator は単語を区切ります。エンコード時の既定値はそれぞれ空白と / です。モールス信号の点と線を置き換えるものではありません。

IFontMorseDecoder

IFontMorseDecoder はモールス信号の文字列、フォント、明示的な MorseAlphabets 値を受け取ります。デコードされたテキストに対応する GlyphId[]、または描画パラメーターを指定した場合は PNG の Stream を返します。プレーンテキストは返しません。

フォントを読み込まずにデコードされた string を取得するには、IMorseDecoder を使用します。デコード時の既定値は、inputSeparator が /、outputSeparator が空白です。

IMorseEncoder / IMorseDecoder

これらは生の文字列のみを処理します (フォントは関係しません)。 「ドットダッシュ」シーケンスのみが必要な場合に使用します。

3 サポートする列挙型 – MorseAlphabets

ValueScript
Latin英語およびほとんどの西洋言語
Greekギリシャ文字
Cyrillicロシア語、ブルガリア語など
Hebrewヘブライ文字
Arabicアラビア文字
Portugueseポルトガル語固有の記号
Kurdishポルトガル語固有の記号
Persianペルシア文字 / ペルシア文字

enum を省略した場合、エンコーダは自動検出を試みます。

実践例

フォントを使用する例では TTF/LatoWeb-Regular.ttf を使用します。DataDir に TTF フォルダーを含むディレクトリを、OutputDir に既存の出力ディレクトリを指定してください。プレーンテキストへのデコードにはフォントは不要です。

  1.  グリフ ID にエンコードする
 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())));

結果: モールス信号の記号を表す GlyphId オブジェクトの配列です。独自の描画処理に使用できます。

  1.  PNG をエンコードしてレンダリングする

この断片は前の例の font と encoder を再利用し、OutputDir に morse.png を保存します。

 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}

PNG はフォントの点(.)と線(-)のグリフでモールス信号を表示します。文字パラメーターは単語の区切りを指定するもので、点や線の代替記号を指定するものではありません。

  1.  デコードしてプレーンテキストに戻す
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); // SOS

エンコード中にカスタム アルファベットが使用された場合は、同じ MorseAlphabets 値をデコーダに渡します。

  1. 組み合わせた例(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}");

この例は OutputDir に callme.png を保存し、エンコードされたグリフ識別子を出力して、... --- ... を SOS にデコードします。上記の名前空間とディレクトリ設定を使用してください。

オンラインデモ

Aspose は、何もインストールせずにトランスレーターを試すための 無料の Web デモ を提供しています。

デモの特徴:

リリースノート

Aspose.Font for .NET23.10 追加された API: TextUtilsFactory、IFontMorseEncoder、IFontMorseDecoder、IMorseEncoder、IMorseDecoder、列挙体 MorseAlphabets。 新機能: 「モールス信号エンコードのサポート」

どのオーバーロードをいつ使用するか

状況推奨されるオーバーロード
以降の処理にグリフ識別子のみが必要な場合Encode(string text, IFont font) (または明示的なアルファベットを使用)
独自の描画コードなしで PNG ストリームを取得fontSize、lineSpacingType、lineSpacingValue、maxWidth を受け取るオーバーロードを使用
単語区切りのカスタマイズinputSeparator と outputSeparator を設定します。これらは点や線の記号を変更しません
ソース言語で非ラテン文字が使用されており、自動検出が失敗する適切な MorseAlphabets 値を明示的に渡す場合

概要

Aspose.Font には、グリフ レベル と ビットマップ レベル の両方で動作する 完全なモールス信号変換エンジンが含まれるようになりました。シンプルなファクトリ メソッドを使用すると、独自のマッピング テーブルを作成したり、低レベルのレンダリングを処理したりすることなく、サポートされているアルファベットのテキストのエンコード、デコード、レンダリングを即座に開始できます。

構築しているかどうか:

API を使用すると、コードをクリーンに保ち、クロスプラットフォームで完全にサポートされながら、完全な制御が可能になります。