Working with Textures in PostScript | C++
Add Texture Tiling Pattern in PS Document
A texture tiling pattern is an image that is used for filling or drawing objects: shapes or text. If the size of the image is less than the size of the object it is repeated in X and Y directions for covering all necessary areas.
The process of repetition of the image inside graphics objects is called tiling. In order to set paint or stroke in PsDocument we must pass an object of System.Drawing.Brush class for a painting and an object of System.Drawing.Pen for stroking into respective methods.
Aspose.Page for C++ library processes all subclasses of System.Drawing.Brush that is offered by the C++ platform. These are System.Drawing.SolidBrush, System.Drawing.TextureBrush, System.Drawing.LinearGradientBrush, System.Drawing.PathGradientBrush and System.Drawing.HatchBrush. System.Drawing.Pen class cannot be extended because it is sealed, but it contains System.Drawing.Brush as a property and, thus, Aspose.Page for C++ library can also use a complete set of brushes also for drawing lines and outlining shapes and text.
In order to paint graphics objects with a textured pattern in Aspose.Page for C++ library it is enough simply to pass System.Drawing.TextureBrush to SetPaint() or one of the FillText() or FillAndStrokeText() methods which accepts System.Drawing.Brush as a parameter.
In order to outline graphics objects with a textured pattern in Aspose.Page for C++ library you should create new System.Drawing.Pen with System.Drawing.TextureBrush and pass it to SetStroke() or one of the OutlineText() or FillAndStrokeText() methods which accepts System.Drawing.Pen as a parameter.
In the example below we demonstrate how to fill a shape and a text and outline a text with a texture tiling pattern.
Description of steps of working with Texture Pattern and PsDocument in the example:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a new graphics state and translate it to the necessary position.
- Create System.Drawing.Bitmap from image file.
- Create System.Drawing.TextureBrush from the image.
- Set the necessary transformation in the texture brush.
- Set the texture brush as current paint in the current graphics state of PsDocument.
- Create a rectangle path.
- Fill the rectangle with the texture brush.
- Save current paint as a local variable for future use.
- Set the current stroke with a red pen.
- Outline the rectangle with a current pen.
- Exit from the current graphics state to the upper-level graphics state.
- Create system font.
- Fill and stroke (outline) text. For filling the texture brush is used, and for stroking black pen is used.
- Outline the text in the other position with the new System.Drawing.Pen created with texture brush as Brush.
- Close the page.
- Save the document.
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 }
The result of running this code is appeared as
You can download examples and data files from GitHub.