Lavorare con le texture in PostScript | C++

Aggiungi un pattern di texture a mosaico in un documento PS

Un pattern di texture a mosaico è un’immagine utilizzata per riempire o disegnare oggetti: forme o testo. Se le dimensioni dell’immagine sono inferiori a quelle dell’oggetto, viene ripetuta nelle direzioni X e Y per coprire tutte le aree necessarie.

Il processo di ripetizione dell’immagine all’interno di oggetti grafici è chiamato mosaico. Per impostare il colore o il tratto in PsDocument, dobbiamo passare un oggetto della classe System.Drawing.Brush per un disegno e un oggetto della classe System.Drawing.Pen per il tratto ai rispettivi metodi.

La libreria Aspose.Page per C++ elabora tutte le sottoclassi di System.Drawing.Brush offerte dalla piattaforma C++. Questi sono System.Drawing.SolidBrush, System.Drawing.TextureBrush, System.Drawing.LinearGradientBrush, System.Drawing.PathGradientBrush e System.Drawing.HatchBrush. La classe System.Drawing.Pen non può essere estesa perché è sealed, ma contiene System.Drawing.Brush come proprietà e, pertanto, la libreria Aspose.Page per C++ può utilizzare anche un set completo di pennelli per disegnare linee e delineare forme e testo.

Per dipingere oggetti grafici con un pattern texturizzato nella libreria Aspose.Page per C++, è sufficiente passare System.Drawing.TextureBrush a SetPaint() o a uno dei metodi FillText() o FillAndStrokeText() che accetta System.Drawing.Brush come parametro.

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

Nell’esempio seguente mostriamo come riempire una forma e un testo e come delineare un testo con un pattern texture a mosaico.

Descrizione dei passaggi per lavorare con Texture Pattern e PsDocument nell’esempio:

  1. Crea un flusso di output per il file PS risultante.
  2. Crea un oggetto PsSaveOptions con le opzioni predefinite.
  3. Crea un PsDocument a pagina singola con un flusso di output già creato e le relative opzioni di salvataggio.
  4. Crea un nuovo stato grafico e traslalo nella posizione desiderata.
  5. Crea System.Drawing.Bitmap dal file immagine.
  6. Crea System.Drawing.TextureBrush dall’immagine.
  7. Imposta la trasformazione necessaria nel pennello texture.
  8. Imposta il pennello texture come pennello corrente nello stato grafico corrente del PsDocument.
  9. Crea un tracciato rettangolare.
  10. Riempi il rettangolo con il pennello texture.
  11. Salva il pennello corrente come variabile locale per un utilizzo futuro. 12. Imposta il tratto corrente con una penna rossa.
  12. Contorna il rettangolo con la penna corrente.
  13. Esci dallo stato grafico corrente e torna allo stato grafico di livello superiore.
  14. Crea un font system.
  15. Riempi e traccia (contorna) il testo. Per il riempimento si utilizza il pennello texture e per il contorno si utilizza una penna nera.
  16. Contorna il testo nell’altra posizione con il nuovo System.Drawing.Pen creato con il pennello texture come Brush.
  17. Chiudi la pagina.
  18. Salva il documento.
 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    }

Vedi come lavorare con le texture nei documenti PS in .NET o Java.

Il risultato dell’esecuzione di questo codice viene visualizzato come

Aggiungi pattern di affiancamento texture

È 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.