Working with Text in PostScript | C++
Add Text in PS Document
In this article, we consider the ways how a text can be added to a PS document.
Obviously, the text should be written in a document with some font. Fonts can be stored in system folders, and in this case, we call them system fonts and can be stored in custom folders, which are folders in which someone puts fonts for particular usage. Usually, these fonts are absent in system folders. We call such fonts custom fonts. Aspose.Page for C++ library offers methods for using both system and custom fonts.
For using system fonts we just fill or outline a text with native C++’s System.Drawing.Font. Numbers in the method’s call are the x and y coordinates of the top-left corner of the text.
1document->FillText(str, System::MakeObject<System::Drawing::Font>(u"Times New Roman", static_cast<float>(fontSize), System::Drawing::FontStyle::Bold), 50.0f, 100.0f);
For using custom fonts, first add custom fonts folder in PsSaveOptions, then fetch Aspose.Page.Font.DrFont. And finally fill or outline text with this DrFont object.
1options->set_AdditionalFontsFolders(System::MakeArray<System::String>({FONTS_FOLDER}));
2System::SharedPtr<DrFont> drFont = ExternalFontCache::FetchDrFont(u"Palatino Linotype", static_cast<float>(fontSize), System::Drawing::FontStyle::Regular);
3document->FillText(str, drFont, 50.0f, 200.0f);
Another option is working with text that also belongs to a font. The font is used to print text in the PostScript document can be embedded in this file or not embedded. In the first case, the text will always be rendered in any PostScript viewer or editor because it always lies with the text. In the second case, we should expect that the used font exists in system folders on the target host. If the used font doesn’t exist PostScript interpreter can throw an error.
The third option is also about a font because a font is the main thing in adding text. A font used for filling or drawing (or clipping) a text can be embedded in a PS file in various forms. Now TrueType and Type3 font types in embedding are supported.
The fourth option is the possibility to write the glyphs of the text with assigned advances (widths). It allows adding some artistry to the text.
In the example below we demonstrate the usage of various methods of adding text in a PS document with Aspose.Page for C++ library.
An algorithm for adding a text in new PS document includes the following steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions. If we use a custom font we add custom fonts folder(s) in the save options.
- Create PsDocument with the already created output stream and save options.
- Create the necessary font, system, or custom.
- Fill or outline the text with the created font. Here we can assign System.Drawing.Brush or System.Drawing.Pen depending on whether we fill or draw the text. Or we can fill and outline the text in one method. If we use the method without System.Drawing.Brush or System.Drawing.Pen, the text will be filled or outlined with current paint/stroke in the current graphics state.
- Close the page.
- Save the document.
We split the example code into little code snippets in order to separate the initial preparation for the PS document, the usage of every method for adding text, and the completion of the document.
In this piece of code, we create an output stream and PsSaveOptions, add a custom fonts folder for using the custom font in the code, create a PsDocument object, set common for all methods text as a string variable, and create fontSize variable that is also used in every used method.
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithText();
3
4 System::String FONTS_FOLDER = RunExamples::GetDataDir_Data() + u"necessary_fonts/";
5
6 //Create output stream for PostScript document
7 {
8 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddText_outPS.ps", System::IO::FileMode::Create);
9 // Clearing resources under 'using' statement
10 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
11 // ------------------------------------------
12
13 try
14 {
15 //Create save options with A4 size
16 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
17 // Set custom fonts folder. It will be added to system fonts folders for finding needed font.
18 options->set_AdditionalFontsFolders(System::MakeArray<System::String>({FONTS_FOLDER}));
19 //A text to write to PS file
20 System::String str = u"ABCDEFGHIJKLMNO";
21 int32_t fontSize = 48;
22
23 // Create new 1-paged PS Document
24 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
Here we demonstrate the usage of the system font for filling the text with the current color of the graphics state (that is black) and with the new SolidBrush.
1 ////////////////////////////////////// Using sysem font (located in system fonts folders) for filling text //////////////////
2 System::SharedPtr<System::Drawing::Font> font = System::MakeObject<System::Drawing::Font>(u"Times New Roman", static_cast<float>(fontSize), System::Drawing::FontStyle::Bold);
3 //Fill text with default or already defined color. In given case it is black.
4 document->FillText(str, font, 50.0f, 100.0f);
5 //Fill text with Blue color.
6 document->FillText(str, font, 50.0f, 150.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()));
7 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The result
Now we fill the text with the custom font.
1 ////////////////////////////////////// Using custom font (located in custom fonts folders) for filling text /////////////////
2 System::SharedPtr<DrFont> drFont = ExternalFontCache::FetchDrFont(u"Palatino Linotype", static_cast<float>(fontSize), System::Drawing::FontStyle::Regular);
3 //Fill text with default or already defined color. In given case it is black.
4 document->FillText(str, drFont, 50.0f, 200.0f);
5 //Fill text with Blue color.
6 document->FillText(str, drFont, 50.0f, 250.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()));
7 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The result
Here we demonstrate the usage of the system font for outlining the text with the current stroke of graphics state (that is black) and with the new Pen.
1 ////////////////////////////////////// Using sysem font (located in system fonts folders) for outlining text ////////////////
2 //Outline text with default or already defined pen. In given case it is black colored 1-points width pen.
3 document->OutlineText(str, font, 50.0f, 300.0f);
4 //Outline text with blue-violet colored 2-points width pen.
5 document->OutlineText(str, font, 50.0f, 350.0f, System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f));
6 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The result
Now we outline the text with the custom font.
1 ////////////////////////////////////// Using custom font (located in custom fonts folders) for outlining text /////////////////
2 //Outline text with default or already defined pen. In given case it is black colored 1-points width pen.
3 document->OutlineText(str, drFont, 50.0f, 450.0f);
4 //Outline text with blue-violet colored 2-points width pen.
5 document->OutlineText(str, drFont, 50.0f, 500.0f, System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f));
6 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The result
Here we demonstrate the usage of system font for filling and outlining the text with new SolidBrush and Pen.
1 //Fill text with orange color and stroke with blue colored 2-points width pen.
2 document->FillAndStrokeText(str, font, 50.0f, 400.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Yellow()), System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f));
The result
Now we fill and an outline the text with the custom font.
1 //Fill text with orange color and stroke with blue colored 2-points width pen.
2 document->FillAndStrokeText(str, drFont, 50.0f, 550.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()), System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()), 2.0f));
The result
And finally we fill the text with assigning glyphs widths. The number of widths must be equal to the number of glyphs.
1 //Glyphs widths
2 System::ArrayPtr<float> widths = System::MakeArray<float>({87, 87, 87, 87, 34, 87, 87});
3 //Fill ASCII text using with assigning glyphs widths.
4 document->FillText(u"BAMBOOK", widths, drFont, 50.0f, 600.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()));
5
6 //Glyphs widths
7 widths = System::MakeArray<float>({87, 34, 87, 87, 87, 87, 87});
8 //Fill Unicode text using with assigning glyphs widths.
9 document->FillText(u"ЗООПАРК", widths, drFont, 50.0f, 650.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()));
The result
Close the current page, save the document.
1 //Close current page
2 document->ClosePage();
3
4 //Save the document
5 document->Save();
6 }
7 catch(...)
8 {
9 __dispose_guard_0.SetCurrentException(std::current_exception());
10 }
11 }
See working with a text in PS documents in Java.
In the example above, there are used fonts embedded in the PostScript file as TrueType fonts, because it is the default behavior of saving fonts in PsDocument class. If you need to change this behavior, please, use PsSaveOptions like the following:
1 //Do not embed used fonts.
2 options->set_EmbedFonts(false);
1 //Embed used fonts as Type3 fonts.
2 options->set_EmbedFontsAs(FontConstants::EMBED_FONTS_TYPE3);
You can download examples and data files from GitHub.