PostScript でのハッチパターンの操作 | C++
PSドキュメントにハッチパターンを追加する
ハッチパターンは、通常、小さな2色(通常は白黒)のシンプルな画像で表されるテクスチャタイリングパターンです。これらの小さな画像の主な内容は、様々なハッチです。
ハッチによる描画のために、C++プラットフォームにはSystem.Drawing.Brushから派生したSystem.Drawing.HatchBrushという別のクラスがあります。System.Drawing.TextureBrushとの違いは、タイリングに使用する画像を定義する名前付き定義済みスタイルが用意されていることです。C++プラットフォームには53種類のハッチスタイルが用意されており、これら52種類すべてをPsDocumentの塗りつぶしやストローク(アウトライン)に使用できます。
Aspose.Page for C++ ライブラリでグラフィック オブジェクトをハッチ パターンで 描画 するには、System.Drawing.HatchBrush を SetPaint() または、System.Drawing.Brush をパラメーターとして受け取る FillText() や FillAndStrokeText() メソッドのいずれかに渡すだけで十分です。
Aspose.Page for C++ ライブラリでグラフィック オブジェクトをハッチ パターンで アウトライン するには、System.Drawing.HacthBrush を持つ新しい System.Drawing.Pen を作成し、それを SetStroke() または、System.Drawing.Pen をパラメーターとして受け取る OutlineText() や FillAndStrokeText() メソッドのいずれかに渡す必要があります。
以下の例では、まずハッチパターンで図形を塗りつぶす方法、次にC++で様々なハッチスタイルを使用する方法、最後にハッチパターンでテキストを塗りつぶしてアウトライン化する方法について説明します。
新しいPSドキュメントでハッチパターンを使用してグラフィックオブジェクトを描画するアルゴリズムは、以下の手順で構成されます。
- 結果のPSファイル用の出力ストリームを作成します。
- PsSaveOptions を作成します。
- 既に作成した出力ストリームと保存オプションを使用して、 PsDocument を作成します。
- 塗りつぶしまたはアウトライン化するオブジェクトに応じて、必要なグラフィックパスまたはフォントを作成します。
- 希望するスタイルでSystem.Drawing.HatchBrushオブジェクトを作成します。
- PsDocument でハッチブラシを現在のペイントとして設定します。
- グラフィックパスを現在のペイントで塗りつぶすか、テキストを塗りつぶします。System.Drawing.Brush をパラメータとして受け入れるテキスト塗りつぶしメソッドのいずれかを使用する場合は、前の手順は無視できます。
- ページを閉じます。
- ドキュメントを保存します。
最後の4点の代わりにハッチパターンでグラフィックオブジェクトを*ストローク(アウトライン)*する必要がある場合は、以下の手順に従います。
ハッチブラシを使用してSystem.Drawing.Penオブジェクトを作成します。
このペンをPsDocumentで現在のストロークに設定します。
現在のストロークでグラフィックパスのアウトラインを作成するか、テキストのアウトラインを作成します。System.Drawing.Penをパラメータとして受け入れるテキストのアウトライン方法を使用する場合、前の点は無視されます。
ページを閉じます。
ドキュメントを保存します。
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithHatches();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddHatchPattern_outPS.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_0({ 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 int32_t x0 = 20;
20 int32_t y0 = 100;
21 int32_t squareSide = 32;
22 int32_t width = 500;
23 int32_t sumX = 0;
24
25 //Restore graphics state
26 document->WriteGraphicsSave();
27
28 //Translate to initial point
29 document->Translate(static_cast<float>(x0), static_cast<float>(y0));
30
31 //Create rectngle path for every pattern square
32 System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
33 path->AddRectangle(System::Drawing::RectangleF(0.0f, 0.0f, static_cast<float>(squareSide), static_cast<float>(squareSide)));
34
35 //Create pen for outlining pattern square
36 System::SharedPtr<System::Drawing::Pen> pen = System::MakeObject<System::Drawing::Pen>(System::Drawing::Color::get_Black(), 2.0f);
37
38 //For every hatch pattern style
39 for (System::Drawing::Drawing2D::HatchStyle hatchStyle = static_cast<System::Drawing::Drawing2D::HatchStyle>(0); hatchStyle <= (System::Drawing::Drawing2D::HatchStyle)52; hatchStyle++)
40 {
41 //Set paint with current hatch brush style
42 document->SetPaint(System::MakeObject<System::Drawing::Drawing2D::HatchBrush>(hatchStyle, System::Drawing::Color::get_Black(), System::Drawing::Color::get_White()));
43
44 //Calculate displacement in order to don't go beyond the page bounds
45 int32_t x = squareSide;
46 int32_t y = 0;
47 if (sumX >= width)
48 {
49 x = -(sumX - squareSide);
50 y += squareSide;
51 }
52 //Translate current graphics state
53 document->Translate(static_cast<float>(x), static_cast<float>(y));
54 //Fill pattern square
55 document->Fill(path);
56 //Set stroke
57 document->SetStroke(pen);
58 //Draw square outline
59 document->Draw(path);
60
61 //Calculate distance from X0
62 if (sumX >= width)
63 {
64 sumX = squareSide;
65 }
66 else
67 {
68 sumX += x;
69 }
70 }
71
72 //Restore graphics state
73 document->WriteGraphicsRestore();
74
75 //Fill text with hatch pattern
76 System::SharedPtr<System::Drawing::Drawing2D::HatchBrush> brush = System::MakeObject<System::Drawing::Drawing2D::HatchBrush>(System::Drawing::Drawing2D::HatchStyle::DiagonalCross, System::Drawing::Color::get_Red(), System::Drawing::Color::get_Yellow());
77 System::SharedPtr<System::Drawing::Font> font = System::MakeObject<System::Drawing::Font>(u"Arial", 96.0f, System::Drawing::FontStyle::Bold);
78 document->FillAndStrokeText(u"ABC", font, 200.0f, 300.0f, brush, pen);
79
80 //Outline text with hatch pattern
81 brush = System::MakeObject<System::Drawing::Drawing2D::HatchBrush>(System::Drawing::Drawing2D::HatchStyle::Percent50, System::Drawing::Color::get_Blue(), System::Drawing::Color::get_White());
82 document->OutlineText(u"ABC", font, 200.0f, 400.0f, System::MakeObject<System::Drawing::Pen>(brush, 5.0f));
83
84
85 //Close current page
86 document->ClosePage();
87
88 //Save the document
89 document->Save();
90 }
91 catch(...)
92 {
93 __dispose_guard_0.SetCurrentException(std::current_exception());
94 }
95 }
PSドキュメント内のハッチパターンの操作については、 Javaを参照してください。
このコードを実行すると、次のように表示されます。
サンプルとデータ ファイルは GitHub からダウンロードできます。