Lavorare con i modelli di tratteggio in PostScript | C++

Aggiungi un pattern di tratteggio in un documento PS

Un pattern di tratteggio è un pattern di texture a mosaico, solitamente rappresentato da una piccola immagine semplice a 2 colori (solitamente bianco e nero). Il contenuto principale di queste piccole immagini è costituito da vari tratteggi.

Per disegnare tramite tratteggi, la piattaforma C++ dispone di una classe separata, derivata da System.Drawing.Brush, ovvero System.Drawing.HatchBrush. La sua differenza rispetto a System.Drawing.TextureBrush è che dispone di stili predefiniti che definiscono l’immagine da utilizzare per il mosaico. La piattaforma C++ offre 53 stili di tratteggio e tutti i 52 stili possono essere utilizzati per il riempimento o il contorno (contorno) in PSDocument.

Per dipingere oggetti grafici con un motivo di tratteggio nella libreria Aspose.Page per C++, è sufficiente passare System.Drawing.HatchBrush a SetPaint() o a uno dei metodi FillText() o FillAndStrokeText() che accettano System.Drawing.Brush come parametro.

Per delineare oggetti grafici con un motivo di tratteggio nella libreria Aspose.Page per C++, è necessario creare un nuovo System.Drawing.Pen con System.Drawing.HacthBrush e passarlo a SetStroke() o a uno dei metodi OutlineText() o FillAndStrokeText() che accettano System.Drawing.Pen come parametro.

Nell’esempio seguente mostriamo, innanzitutto, come riempire una forma con un motivo di tratteggio, poi tutti i diversi stili di tratteggio in C++ e, infine, come riempire e delineare un testo con un motivo di tratteggio.

Un algoritmo per dipingere oggetti grafici con un motivo a tratteggio in un nuovo documento PS comprende i seguenti passaggi:

  1. Creare un flusso di output per il file PS risultante.
  2. Creare PsSaveOptions.
  3. Creare PsDocument con il flusso di output già creato e le opzioni di salvataggio.
  4. Creare il percorso grafico o il font necessari in base all’oggetto che si desidera riempire o delineare.
  5. Creare un oggetto di System.Drawing.HatchBrush con lo stile desiderato.
  6. Impostare il pennello tratteggio come pennello corrente in PsDocument.
  7. Riempire il percorso grafico con il pennello corrente o riempire un testo. Se si utilizza uno dei metodi per riempire un testo che accetta System.Drawing.Brush come parametro, il punto precedente può essere ignorato.
  8. Chiudere la pagina.
  9. Salvare il documento.

Se desideriamo delineare (contornare) oggetti grafici con un motivo di tratteggio invece degli ultimi 4 punti, il seguente sarà:

  1. Creare l’oggetto System.Drawing.Pen con il pennello di tratteggio.

  2. Impostare questa penna come tratto corrente in PsDocument.

  3. Delineare il tracciato grafico con il tratto corrente o il testo. Se utilizziamo uno dei metodi per delineare il testo che accetta System.Drawing.Pen come parametro, il punto precedente può essere ignorato.

  4. Chiudere la pagina.

  5. Salvare il documento.

 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    }

Vedi come lavorare con un pattern di tratteggio in un documento PS in Java.


Il risultato dell’esecuzione di questo codice viene visualizzato come

Aggiungi pattern di tratteggio

È possibile scaricare esempi e file di dati da GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.