Working with Hatch Patterns in PostScript | C++

Add Hatch Pattern in PS Document

Hatch pattern is a texture tiling pattern usually represented by small 2-colors (usually black&white) simple image. The main content of these small images is various hatches.

For painting by hatches, the C++ platform has a separate class, derived from System.Drawing.Brush, System.Drawing.HatchBrush. Its difference from System.Drawing.TextureBrush is that it has named predefined styles defining what image to use for tiling. C++ platform offers 53 hatch styles and all 52 styles can be used for filling or stroking (outlining) in PsDocument.

In order to paint graphics objects with a hatch pattern in Aspose.Page for C++ library it is enough simply to pass System.Drawing.HatchBrush to SetPaint() or one of the FillText() or FillAndStrokeText() methods that accept System.Drawing.Brush as a parameter.

In order to outline graphics objects with a hatch pattern in Aspose.Page for C++ library someone should create new System.Drawing.Pen with System.Drawing.HacthBrush and pass it to SetStroke() or one of the OutlineText() or FillAndStrokeText() methods that accept System.Drawing.Pen as a parameter.

In the example below we demonstrate, firstly, how to fill a shape with a hatch pattern, then all variety of hatch styles in C++, and, finally, how to fill and outline a text with a hatch pattern.

An algorithm for painting graphics objects with a hatch pattern in a new PS document includes the following steps:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions.
  3. Create PsDocument with the already created output stream and save options.
  4. Create the necessary graphics path or font in dependence on what object we are going to fill or outline.
  5. Create an object of System.Drawing.HatchBrush with wishful style.
  6. Set the hatch brush as current paint in PsDocument
  7. Fill the graphics path with current paint or fill a text. If we use one of the methods for filling a text that accepts System.Drawing.Brush as a parameter, the previous point can be ignored.
  8. Close the page.
  9. Save the document.

If we need stroking (outlining) graphics objects with a hatch pattern instead of the last 4 points following will be:

  1. Create System.Drawing.Pen object with the hatch brush.

  2. Set this pen as current stroke in PsDocument.

  3. Outline the graphics path with current stroke or outline the text. If we use one of the methods for outlining the text that accepts System.Drawing.Pen as a parameter, previous point can be ignored.

  4. Close the page.

  5. Save the document.

 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    }

See working with a hatch pattern in PS document in Java.


The result of running this code is appeared as

Add hatch pattern

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.