字符是解釋不同單詞含義的符號。您可以使用帶有 Aspose.Font for C++ API 的 C++ 使用 TrueType 字體 渲染不同的文本字符。它支持不同的文本渲染功能,包括 TrueType、Type1、OTF、EOT 和其他字體。讓我們探索如何通過幾個簡單的 API 調用來使用文本呈現功能。我們將在以下標題下討論該主題:
C++ TrueType 字體渲染 API – 安裝
考慮到C++平台的流行和需求,我們在Aspose.Font for C++ API中引入了非常有用的特性。為了在您的應用程序中呈現 TrueType 字體,您需要通過從 New Releases 下載它來配置 API,或者您可以使用以下命令通過 NuGet 安裝它:
Install-Package Aspose.Font.Cpp
使用 C++ 實現繪製字形以呈現 TrueType 字體的接口
首先,您需要在 Aspose.Font.Rendering 命名空間下實現 IGlyphOutlinePainter。按照以下步驟實現方法和類:
- 創建一個名為 GlyphOutlinePainter 的類
- 使用System.Drawing.Drawing2D.GraphicsPath繪製圖形
下面是用於實現接口以繪製字形的 C++ 代碼片段:
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
RenderingText::GlyphOutlinePainter::GlyphOutlinePainter(System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path)
{
_path = path;
}
void RenderingText::GlyphOutlinePainter::MoveTo(System::SharedPtr<Aspose::Font::RenderingPath::MoveTo> moveTo)
{
_path->CloseFigure();
_currentPoint.set_X((float)moveTo->get_X());
_currentPoint.set_Y((float)moveTo->get_Y());
}
void RenderingText::GlyphOutlinePainter::LineTo(System::SharedPtr<Aspose::Font::RenderingPath::LineTo> lineTo)
{
float x = (float)lineTo->get_X();
float y = (float)lineTo->get_Y();
_path->AddLine(_currentPoint.get_X(), _currentPoint.get_Y(), x, y);
_currentPoint.set_X(x);
_currentPoint.set_Y(y);
}
void RenderingText::GlyphOutlinePainter::CurveTo(System::SharedPtr<Aspose::Font::RenderingPath::CurveTo> curveTo)
{
float x3 = (float)curveTo->get_X3();
float y3 = (float)curveTo->get_Y3();
_path->AddBezier(_currentPoint.get_X(), _currentPoint.get_Y(), (float)curveTo->get_X1(), (float)curveTo->get_Y1(), (float)curveTo->get_X2(), (float)curveTo->get_Y2(), x3, y3);
_currentPoint.set_X(x3);
_currentPoint.set_Y(y3);
}
void RenderingText::GlyphOutlinePainter::ClosePath()
{
_path->CloseFigure();
}
System::Object::shared_members_type Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::GetSharedMembers()
{
auto result = System::Object::GetSharedMembers();
result.Add("Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::_path", this->_path);
result.Add("Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::_currentPoint", this->_currentPoint);
return result;
}
其次,請按照以下步驟創建一個名為 DrawText 的方法:
- 遍歷文本字符串中的符號
- 獲取 GID 作為為每個符號標識的字形
- 創建 GlyphOutlinePainter 並傳遞給 GlyphOutlineRenderer 對象
- 使用 Matrix 對象指定字形坐標
此外,在完成所有這些步驟之後,創建用於計算圖像字體尺寸的實用方法,如以下 C++ 代碼片段中所述:
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
double RenderingText::FontWidthToImageWith(double width, int32_t fontSourceResulution, double fontSize, double dpi /* = 300*/)
{
double resolutionCorrection = dpi / 72;
// 72 是字體的內部 dpi
return (width / fontSourceResulution) * fontSize * resolutionCorrection;
}
使用 C++ 渲染帶有 TrueType 字體的文本
我們已經實現了使用 TrueType 字體呈現文本所需的功能。現在讓我們調用使用 Aspose.Font for C++ API 的方法作為下面的代碼片段:
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String dataDir = RunExamples::GetDataDir_Data();
System::String fileName1 = dataDir + u"Montserrat-Bold.ttf";
//帶完整路徑的字體文件名
System::SharedPtr<FontDefinition> fd1 = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName1)));
System::SharedPtr<TtfFont> ttfFont1 = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd1));
System::String fileName2 = dataDir + u"Lora-Bold.ttf";
//帶完整路徑的字體文件名
System::SharedPtr<FontDefinition> fd2 = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName2)));
System::SharedPtr<TtfFont> ttfFont2 = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd2));
DrawText(u"Hello world", ttfFont1, 14, System::Drawing::Brushes::get_White(), System::Drawing::Brushes::get_Black(), dataDir + u"hello1_montserrat_out.jpg");
DrawText(u"Hello world", ttfFont2, 14, System::Drawing::Brushes::get_Yellow(), System::Drawing::Brushes::get_Red(), dataDir + u"hello2_lora_out.jpg");
結論
在本文中,您學習瞭如何使用 C++ 使用 TrueType 字體呈現文本。有興趣了解更多關於 Aspose.Font for C++ API 提供的功能嗎?您可以通過瀏覽 API 參考 或 產品文檔 了解更多詳細信息。但是,如果您有任何疑問或需要幫助,請隨時通過 免費支持論壇 與我們聯繫。我們期待與您合作!