在 PostScript 中使用文本 | C++
在 PS 文档中添加文本
本文将探讨如何在 PS 文档中添加文本。
显然,文本应该使用某种字体。字体可以存储在系统文件夹中,在这种情况下,我们称之为系统字体;也可以存储在自定义文件夹中,自定义文件夹是用于存放特定用途字体的文件夹。通常,这些字体在系统文件夹中不存在。我们称之为自定义字体。 Aspose.Page for C++ 库提供了使用系统和自定义字体的方法。
要使用系统字体,我们只需使用原生 C++ 的 System.Drawing.Font 函数填充或勾勒文本轮廓即可。方法调用中的数字是文本左上角的 x 和 y 坐标。
1document->FillText(str, System::MakeObject<System::Drawing::Font>(u"Times New Roman", static_cast<float>(fontSize), System::Drawing::FontStyle::Bold), 50.0f, 100.0f);
要使用自定义字体,首先在 PsSaveOptions 中添加 custom fonts 文件夹,然后获取 Aspose.Page.Font.DrFont。最后使用此 DrFont 对象填充或勾勒文本轮廓。
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);
另一种选择是使用也属于字体的文本。用于在 PostScript 文档中打印文本的字体可以嵌入到此文件中,也可以不嵌入。 在第一种情况下,文本将始终呈现在任何 PostScript 查看器或编辑器中,因为它始终与文本一起存在。在第二种情况下,我们应该预期所使用的字体存在于目标主机的系统文件夹中。如果所使用的字体不存在,PostScript 解释器可能会抛出错误。
第三种选择也与字体有关,因为字体是添加文本的关键。用于填充、绘制(或剪切)文本的字体可以以各种形式嵌入到 PS 文件中。现在支持嵌入 TrueType 和 Type3 字体类型。
第四种选择是可以使用指定的步长(宽度)写入文本的字形。这可以为文本增添一些艺术效果。
在下面的示例中,我们演示了使用 Aspose.Page for C++ 库在 PS 文档中添加文本的各种方法。
在新的 PS 文档中添加文本的算法包括以下步骤:
- 为生成的 PS 文件创建输出流。
- 创建 PsSaveOptions。如果我们使用自定义字体,请在保存选项中添加自定义字体文件夹。
- 使用已创建的输出流和保存选项创建 PsDocument。
- 创建必要的字体:系统 或 自定义。
- 使用创建的字体填充或勾勒文本轮廓。我们可以根据填充还是绘制文本来指定 System.Drawing.Brush 或 System.Drawing.Pen。或者,我们可以在一个方法中同时填充和勾勒文本轮廓。如果我们使用不带System.Drawing.Brush或System.Drawing.Pen的方法,文本将使用当前图形状态下的当前绘画/描边进行填充或勾勒轮廓。
- 关闭页面。
- 保存文档。
我们将示例代码拆分成几个小的代码片段,以便区分 PS 文档的初始准备、各种添加文本方法的使用以及文档的最终完成。
在这段代码中,我们创建了一个输出流和 PsSaveOptions,添加了一个自定义字体文件夹以便在代码中使用自定义字体,创建了一个 PsDocument 对象,将所有方法的通用文本设置为字符串变量,并创建了 fontSize 变量(该变量在每个方法中都会用到)。
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);
这里我们演示了如何使用系统字体用图形状态的当前颜色(即黑色)和新的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 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
结果
现在我们用自定义字体填充文本。
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 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
结果
这里我们演示了如何使用系统字体,通过当前图形状态的笔触(即黑色)和新的画笔来勾勒文本轮廓。
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 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
结果
现在我们用自定义字体勾勒出文本轮廓。
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 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
结果
这里我们演示了如何使用系统字体,通过新的SolidBrush和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));
结果
现在我们用自定义字体填充文本并勾勒出轮廓。
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));
结果
最后,我们用指定的字形宽度填充文本。宽度的数量必须等于字形的数量。
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()));
结果
关闭当前页面,保存文档。
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 }
请参阅在 Java 中处理 PS 文档中的文本。
在上面的示例中,使用的字体是 PostScript 文件中嵌入的 TrueType 字体,因为这是 PsDocument 类中保存字体的默认行为。如果您需要更改此行为,请使用 PsSaveOptions,如下所示:
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);
您可以从 GitHub下载示例和数据文件。