Aspose.Font を使用したモールス信号変換 | .NET
Aspose.Font for.NET を使用したモールス信号変換
モールス信号翻訳とは何ですか?
これは次のことを可能にする機能です。
| 目標 | 仕組み |
|---|---|
| プレーンテキストをフォント (またはビットマップ画像) のモールス信号グリフにエンコードします | IFontMorseEncoder は、文字を ドット (.) と ダッシュ (-) 記号のシーケンスに変換し、指定された IFont のグリフ識別子にマッピングします。 |
| すでにモールス信号が含まれている文字列を読み取り可能なテキストにデコードします | IFontMorseDecoder は逆の操作を実行します。 |
| エンコードされたグリフを PNG (または Aspose.Font でサポートされている任意のラスター形式) に直接レンダリングします | レンダリング パラメーター (LineSpacingType、画像サイズなど) を受け入れるオーバーロードは、画像を作成します。 |
この機能は Aspose.Fontfor.NET23.10 (FONTNET‑602 – モールス符号エンコーディングのサポート) で導入されました。
なぜそれを使うのでしょうか?
- Pure‑C# ソリューション – 外部ライブラリや OS レベルの依存関係はありません。
- 必要なグリフ (TrueType、OpenType、CFF など) を含む あらゆる フォントで動作します。
- 組み込みのヒューリスティックにより、正しい アルファベット (ラテン語、ギリシャ語、キリル文字など) を省略した場合に自動的に選択されます。
- テキスト → グリフ ID (カスタム レンダリング パイプラインに便利) と グリフ ID → PNG はすぐに使用できます。
- Aspose のサポート (フォーラム、ドキュメント、ライブ デモ) によって完全にカバーされています。
はじめに
1 NuGet パッケージをインストールする
1dotnet add package Aspose.Font2 名前空間の追加 (C#)
1using Aspose.Font;
2using Aspose.Font.TextUtils;
3using Aspose.Font.Renderers; // for PNG rendering
4using Aspose.Font.Rendering; // for LineSpacingTypeAPIの概要
1 工場 – エントリーポイント
| メソッド | 戻り値 | 説明 |
|---|---|---|
TextUtilsFactory.GetFontMorseEncoder() | IFontMorseEncoder | IFont インスタンスで動作するエンコーダー |
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
| Value | Script |
|---|---|
Latin | 英語およびほとんどの西洋言語 |
Greek | ギリシャ文字 |
Cyrillic | ロシア語、ブルガリア語など |
Hebrew | ヘブライ文字 |
Arabic | アラビア文字 |
Portuguese | ポルトガル語固有の記号 |
Kurdish | ポルトガル語固有の記号 |
Persian | ペルシア文字 / ペルシア文字 |
enum を省略した場合、エンコーダは自動検出を試みます。
実践例
- グリフ 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));結果: カスタム描画パイプラインに供給できる整数グリフ識別子のリスト。
- 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 パラメーターを変更します。
- デコードしてプレーンテキストに戻す
1var decoder = TextUtilsFactory.GetFontMorseDecoder();
2
3string morse = "... --- ..."; // classic SOS
4string result = decoder.Decode(morse, font);
5
6Console.WriteLine(result); // prints: SOSエンコード中にカスタム アルファベットが使用された場合は、同じ MorseAlphabets 値をデコーダに渡します。
- 完全なエンドツーエンド サンプル (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 デモ を提供しています。
デモの特徴:
- テキスト ↔︎ モールス信号 ↔︎ グリフ レンダリング
- カスタムのドット/ダッシュ文字を選択してください
- 生成された PNG をダウンロードするか、グリフ ID を表示します
リリースノート
Aspose.Font for .NET23.10 追加された API:
TextUtilsFactory、IFontMorseEncoder、IFontMorseDecoder、IMorseEncoder、IMorseDecoder、列挙体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 を使用すると、コードをクリーンに保ち、クロスプラットフォームで完全にサポートされながら、完全な制御が可能になります。