PostScript でのテクスチャ操作 | C++

PSドキュメントにテクスチャタイリングパターンを追加する

テクスチャタイリングパターンは、図形やテキストなどのオブジェクトの塗りつぶしや描画に使用される画像です。画像のサイズがオブジェクトのサイズより小さい場合は、必要な領域をすべて覆うためにX方向とY方向に繰り返し配置されます。

グラフィックオブジェクト内で画像を繰り返す処理はタイリングと呼ばれます。 PsDocumentでペイントやストロークを設定するには、ペイントの場合はSystem.Drawing.Brushクラスのオブジェクト、ストロークの場合はSystem.Drawing.Penクラスのオブジェクトをそれぞれのメソッドに渡す必要があります。

Aspose.Page for C++ ライブラリは、C++ プラットフォームで提供される System.Drawing.Brush のすべてのサブクラスを処理します。これらのサブクラスは、System.Drawing.SolidBrushSystem.Drawing.TextureBrushSystem.Drawing.LinearGradientBrushSystem.Drawing.PathGradientBrush、および System.Drawing.HatchBrush です。System.Drawing.Pen クラスはシールされているため拡張できませんが、System.Drawing.Brush をプロパティとして含んでいるため、Aspose.Page for C++ ライブラリでは、直線の描画や図形やテキストのアウトラインにも使用できるブラシの完全なセットを使用できます。

Aspose.Page for C++ ライブラリでテクスチャ パターンを使用してグラフィック オブジェクトを ペイント するには、System.Drawing.TextureBrushSetPaint() に渡すか、System.Drawing.Brush をパラメーターとして受け入れる FillText() または FillAndStrokeText() メソッドのいずれかに渡すだけで十分です。

Aspose.Page for C++ ライブラリでテクスチャ パターンを使用してグラフィック オブジェクトを アウトライン するには、System.Drawing.TextureBrush を使用して新しい System.Drawing.Pen を作成し、それを SetStroke() に渡すか、System.Drawing.Pen をパラメーターとして受け入れる OutlineText() または FillAndStrokeText() メソッドのいずれかに渡す必要があります。

以下の例では、テクスチャタイリングパターンを使用して図形とテキストを塗りつぶし、テキストのアウトラインを作成する方法を示します。

この例でテクスチャパターンと PsDocument を使用する手順について説明します。

  1. 生成される PS ファイルの出力ストリームを作成します。
  2. デフォルトのオプションを使用して PsSaveOptions オブジェクトを作成します。
  3. 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
  4. 新しいグラフィックス状態を作成し、必要な位置に移動します。
  5. 画像ファイルから System.Drawing.Bitmap を作成します。
  6. 画像から System.Drawing.TextureBrush を作成します。
  7. テクスチャ ブラシに必要な変換を設定します。
  8. PsDocument の現在のグラフィック ステートで、テクスチャ ブラシを現在のペイントとして設定します。
  9. 四角形のパスを作成します。
  10. 四角形をテクスチャ ブラシで塗りつぶします。
  11. 現在のペイントを将来使用するためにローカル変数として保存します。
  12. 赤いペンで現在のストロークを設定します。
  13. 現在のペンで四角形の輪郭を描きます。
  14. 現在のグラフィック ステートから上位レベルのグラフィック ステートへ移動します。
  15. システム フォントを作成します。
  16. テキストを塗りつぶし、ストローク (アウトライン) します。塗りつぶしにはテクスチャ ブラシを使用し、ストロークには黒のペンを使用します。
  17. テクスチャ ブラシをブラシとして作成した新しい System.Drawing.Pen を使用して、他の位置のテキストのアウトラインを描きます。
  18. ページを閉じます。
  19. ドキュメントを保存します。
 1    // The path to the documents directory.
 2    System::String dataDir = RunExamples::GetDataDir_WorkingWithTextures();
 3    
 4    //Create output stream for PostScript document
 5    {
 6        System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddTextureTilingPattern_outPS.ps", System::IO::FileMode::Create);
 7        // Clearing resources under 'using' statement
 8        System::Details::DisposeGuard<1> __dispose_guard_1({ outPsStream});
 9        // ------------------------------------------
10        
11        try
12        {
13            //Create save options with A4 size
14            System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15            
16            // Create new 1-paged PS Document
17            System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18            
19            
20            document->WriteGraphicsSave();
21            document->Translate(200.0f, 100.0f);
22            
23            //Create a Bitmap object from image file
24            {
25                System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"TestTexture.bmp");
26                // Clearing resources under 'using' statement
27                System::Details::DisposeGuard<1> __dispose_guard_0({ image});
28                // ------------------------------------------
29                
30                try
31                {
32                    //Create texture brush from the image
33                    System::SharedPtr<System::Drawing::TextureBrush> brush = System::MakeObject<System::Drawing::TextureBrush>(image, System::Drawing::Drawing2D::WrapMode::Tile);
34                    
35                    //Add scaling in X direction to the mattern
36                    System::SharedPtr<System::Drawing::Drawing2D::Matrix> transform = System::MakeObject<System::Drawing::Drawing2D::Matrix>(2.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
37                    brush->set_Transform(transform);
38                    
39                    //Set this texture brush as current paint
40                    document->SetPaint(brush);
41                }
42                catch(...)
43                {
44                    __dispose_guard_0.SetCurrentException(std::current_exception());
45                }
46            }
47            
48            //Create rectangle path
49            System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
50            path->AddRectangle(System::Drawing::RectangleF(0.0f, 0.0f, 200.0f, 100.0f));
51            
52            //Fill rectangle
53            document->Fill(path);
54            
55            //Get current paint
56            System::SharedPtr<System::Drawing::Brush> paint = document->GetPaint();
57            
58            //Set red stroke
59            document->SetStroke(System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Red()), 2.0f));
60            //Stroke the rectangle
61            document->Draw(path);
62            
63            document->WriteGraphicsRestore();
64            
65            //Fill text with texture pattern                
66            System::SharedPtr<System::Drawing::Font> font = System::MakeObject<System::Drawing::Font>(u"Arial", 96.0f, System::Drawing::FontStyle::Bold);
67            document->FillAndStrokeText(u"ABC", font, 200.0f, 300.0f, paint, System::MakeObject<System::Drawing::Pen>(System::Drawing::Color::get_Black(), 2.0f));
68            
69            //Outline text with texture pattern
70            document->OutlineText(u"ABC", font, 200.0f, 400.0f, System::MakeObject<System::Drawing::Pen>(paint, 5.0f));
71            
72            //Close current page
73            document->ClosePage();
74            
75            //Save the document
76            document->Save();
77        }
78        catch(...)
79        {
80            __dispose_guard_1.SetCurrentException(std::current_exception());
81        }
82    }

PS ドキュメント内のテクスチャの操作については、 .NET または Java を参照してください。

このコードを実行した結果は次のように表示されます。

Add Texture Tiling Pattern

サンプルとデータ ファイルは GitHub からダウンロードできます。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.