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

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

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

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

目標仕組み
プレーンテキストをフォント (またはビットマップ画像) のモールス信号グリフにエンコードしますIFontMorseEncoder は、文字を ドット (.) と ダッシュ (-) 記号のシーケンスに変換し、指定された IFont のグリフ識別子にマッピングします。
すでにモールス信号が含まれている文字列を読み取り可能なテキストにデコードしますIFontMorseDecoder は逆の操作を実行します。
エンコードされたグリフを PNG (または Aspose.Font でサポートされている任意のラスター形式) に直接レンダリングしますレンダリング パラメーター (LineSpacingType、画像サイズなど) を受け入れるオーバーロードは、画像を作成します。

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

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

はじめに

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

1dotnet add package Aspose.Font

2 名前空間の追加 (C#)

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

APIの概要

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

メソッド戻り値説明
TextUtilsFactory.GetFontMorseEncoder()IFontMorseEncoderIFont インスタンスで動作するエンコーダー
TextUtilsFactory.GetMorseEncoder()IMorseEncoderプレーンなモールス信号文字列 (フォントなし) を生成するエンコーダー
TextUtilsFactory.GetFontMorseDecoder()IFontMorseDecoderグリフをテキストに読み込むデコーダー
TextUtilsFactory.GetMorseDecoder()IMorseDecoderプレーンなモールス信号文字列用のデコーダー

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

2 コアインターフェイス

IFontモールスエンコーダ

 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

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

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

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

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

実践例

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

結果: カスタム描画パイプラインに供給できる整数グリフ識別子のリスト。

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

PNG は、デフォルトのドット (.) とダッシュ (-) を使用してモールス信号を視覚化します。代替記号 ('・''—' など) の dot/dash パラメーターを変更します。

  1.  デコードしてプレーンテキストに戻す
1var decoder = TextUtilsFactory.GetFontMorseDecoder();
2
3string morse   = "... --- ...";   // classic SOS
4string result  = decoder.Decode(morse, font);
5
6Console.WriteLine(result);        // prints: SOS

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

  1.  完全なエンドツーエンド サンプル (C# コンソール)
 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}

任意の .NET Core / .NET Framework プロジェクトで実行します。唯一の外部依存関係は Aspose.Font です。

オンラインデモ

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

デモの特徴:

リリースノート

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

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

状況推奨されるオーバーロード
以降の処理にグリフ識別子のみが必要な場合Encode(string text, IFont font) (または明示的なアルファベットを使用)
レンダリング処理コードなしで画像が必要な場合Encode(string text, IFont font, double size, LineSpacingType spaces, int w, int h)
カスタムのドット/ダッシュ記号 (例: Unicode “·” & “—”)char dot および char dash を受け入れるオーバーロードを使用する場合
ソース言語で非ラテン文字が使用されており、自動検出が失敗する適切な MorseAlphabets 値を明示的に渡す場合

概要

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

構築しているかどうか:

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

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.