Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active May 6, 2021 08:59
Show Gist options
  • Save aspose-com-gists/bf610decea4168f9c64482594164892b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/bf610decea4168f9c64482594164892b to your computer and use it in GitHub Desktop.
Example code snippets for Aspose.Font for C++
Aspose.Fonts for C++ examples
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::SharedPtr<License> lic = System::MakeObject<License>();
lic->SetLicense(u"path-to-licence-file.lic");
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String dataDir = u"c:\\temp\\";
// Load an existing Visio file in the stream
System::SharedPtr<System::IO::FileStream> LicStream = System::MakeObject<System::IO::FileStream>(dataDir + u"Aspose.Font.lic", System::IO::FileMode::Open);
System::SharedPtr<License> license = System::MakeObject<License>();
license->SetLicense(LicStream);
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::ArrayPtr<uint8_t> fontMemoryData = System::IO::File::ReadAllBytes(dataDir + u"OpenSans-Regular.cff");
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::CFF, System::MakeObject<FontFileDefinition>(u"cff", System::MakeObject<ByteContentStreamSource>(fontMemoryData)));
System::SharedPtr<CffFont> cffFont = System::DynamicCast_noexcept<Aspose::Font::Cff::CffFont>(Aspose::Font::Font::Open(fd));
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"OpenSans-Regular.cff";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::CFF, System::MakeObject<FontFileDefinition>(u"cff", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<CffFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Cff::CffFont>(Aspose::Font::Font::Open(fd));
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
//byte array to load Font from
System::String dataDir = RunExamples::GetDataDir_Data();
System::ArrayPtr<uint8_t> fontMemoryData = System::IO::File::ReadAllBytes(dataDir + u"OpenSans-Regular.cff");
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::CFF, System::MakeObject<FontFileDefinition>(u"cff", System::MakeObject<ByteContentStreamSource>(fontMemoryData)));
System::SharedPtr<CffFont> cffFont = System::DynamicCast_noexcept<Aspose::Font::Cff::CffFont>(Aspose::Font::Font::Open(fd));
//Work with data from just loaded TtfFont object
//Save TtfFont to disk
//Output Font file name with full path
System::String outputFile = RunExamples::GetDataDir_Data() + u"OpenSans-Regular_out.cff";
cffFont->Save(outputFile);
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"Montserrat-Regular.ttf";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<TtfFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
bool latinText = true;
for (uint32_t code = 65; code < static_cast<uint32_t>(123); code++)
{
System::SharedPtr<GlyphId> gid = ttfFont->get_Encoding()->DecodeToGid(code);
if (gid == nullptr || gid == System::StaticCast<System::Object>(GlyphUInt32Id::get_NotDefId()))
{
latinText = false;
}
}
if (latinText)
{
System::Console::WriteLine(System::String::Format(u"Font {0} supports latin symbols.", ttfFont->get_FontName()));
}
else
{
System::Console::WriteLine(System::String::Format(u"Latin symbols are not supported by font {0}.", ttfFont->get_FontName()));
}
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String dataDir = RunExamples::GetDataDir_Data();
//Font to check
System::String fileName = dataDir + u"Montserrat-Regular.ttf";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<TtfFont> font = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
System::SharedPtr<LicenseFlags> licenseFlags;
if (font->get_TtfTables()->get_Os2Table() != nullptr)
{
licenseFlags = font->get_TtfTables()->get_Os2Table()->GetLicenseFlags();
}
if (licenseFlags == nullptr || licenseFlags->get_FSTypeAbsent())
{
System::Console::WriteLine(System::String::Format(u"Font {0} has no embedded license restrictions", font->get_FontName()));
}
else
{
if (licenseFlags->get_IsEditableEmbedding())
{
System::Console::WriteLine(System::String::Format(u"Font {0} may be embedded, and may be temporarily loaded on other systems.", font->get_FontName()) + u" In addition, editing is permitted, including ability to format new text" + u" using the embedded font, and changes may be saved.");
}
else if (licenseFlags->get_IsInstallableEmbedding())
{
System::Console::WriteLine(System::String::Format(u"Font {0} may be embedded, and may be permanently installed", font->get_FontName()) + u" for use on a remote systems, or for use by other users.");
}
else if (licenseFlags->get_IsPreviewAndPrintEmbedding())
{
System::Console::WriteLine(System::String::Format(u"Font {0} may be embedded, and may be temporarily loaded", font->get_FontName()) + u" on other systems for purposes of viewing or printing the document.");
}
else if (licenseFlags->get_IsRestrictedLicenseEmbedding())
{
System::Console::WriteLine(System::String::Format(u"Font {0} must not be modified, embedded or exchanged in any manner", font->get_FontName()) + u" without first obtaining explicit permission of the legal owner.");
}
}
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"Montserrat-Regular.ttf";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<TtfFont> font = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
System::String name = font->get_FontName();
System::Console::WriteLine(System::String(u"Font name: ") + name);
System::Console::WriteLine(System::String(u"Glyph count: ") + font->get_NumGlyphs());
System::String metrics = System::String::Format(u"Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}", font->get_Metrics()->get_Ascender(), font->get_Metrics()->get_Descender(), font->get_Metrics()->get_TypoAscender(), font->get_Metrics()->get_TypoDescender(), font->get_Metrics()->get_UnitsPerEM());
System::Console::WriteLine(metrics);
//Get cmap unicode encoding table from font as object TtfCMapFormatBaseTable to access information about font glyph for symbol 'A'.
//Also check that font has object TtfGlyfTable (table 'glyf') to access glyph.
System::SharedPtr<Aspose::Font::TtfCMapFormats::TtfCMapFormatBaseTable> cmapTable;
if (font->get_TtfTables()->get_CMapTable() != nullptr)
{
cmapTable = font->get_TtfTables()->get_CMapTable()->FindUnicodeTable();
}
if (cmapTable != nullptr && font->get_TtfTables()->get_GlyfTable() != nullptr)
{
System::Console::WriteLine(System::String(u"Font cmap unicode table: PlatformID = ") + cmapTable->get_PlatformId() + u", PlatformSpecificID = " + cmapTable->get_PlatformSpecificId());
//Code for 'A' symbol
char16_t unicode = (char16_t)65;
//Glyph index for 'A'
uint32_t glIndex = cmapTable->GetGlyphIndex(unicode);
if (glIndex != static_cast<uint32_t>(0))
{
//Glyph for 'A'
System::SharedPtr<Glyph> glyph = font->GetGlyphById(glIndex);
if (glyph != nullptr)
{
//Print glyph metrics
System::Console::WriteLine(u"Glyph metrics for 'A' symbol:");
System::String bbox = System::String::Format(System::String(u"Glyph BBox: Xmin = {0}, Xmax = {1}") + u", Ymin = {2}, Ymax = {3}", glyph->get_GlyphBBox()->get_XMin(), glyph->get_GlyphBBox()->get_XMax(), glyph->get_GlyphBBox()->get_YMin(), glyph->get_GlyphBBox()->get_YMax());
System::Console::WriteLine(bbox);
System::Console::WriteLine(System::String(u"Width:") + font->get_Metrics()->GetGlyphWidth(System::MakeObject<GlyphUInt32Id>(glIndex)));
}
}
}
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"Montserrat-Regular.ttf";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<TtfFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
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";
//Font file name with full path
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";
//Font file name with full path
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");
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;
}
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
void RenderingText::DrawText(System::String text, System::SharedPtr<IFont> font, double fontSize, System::SharedPtr<System::Drawing::Brush> backgroundBrush, System::SharedPtr<System::Drawing::Brush> textBrush, System::String outFile)
{
//Get glyph identifiers for every symbol in text line
System::ArrayPtr<System::SharedPtr<GlyphId>> gids = System::MakeArray<System::SharedPtr<Aspose::Font::Glyphs::GlyphId>>(text.get_Length());
for (int32_t i = 0; i < text.get_Length(); i++)
{
gids[i] = font->get_Encoding()->DecodeToGid(text[i]);
}
// set common drawing settings
double dpi = 300;
double resolutionCorrection = dpi / 72;
// 72 is font's internal dpi
// prepare output bitmap
System::SharedPtr<System::Drawing::Bitmap> outBitmap = System::MakeObject<System::Drawing::Bitmap>(960, 720);
outBitmap->SetResolution((float)dpi, (float)dpi);
System::SharedPtr<System::Drawing::Graphics> outGraphics = System::Drawing::Graphics::FromImage(outBitmap);
outGraphics->FillRectangle(backgroundBrush, 0, 0, outBitmap->get_Width(), outBitmap->get_Height());
outGraphics->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality);
//declare coordinate variables and previous gid
System::SharedPtr<GlyphId> previousGid;
double glyphXCoordinate = 0;
double glyphYCoordinate = fontSize * resolutionCorrection;
//loop which paints every glyph in gids
{
for (System::SharedPtr<GlyphId> gid : gids)
{
// if the font contains the gid
if (gid != nullptr)
{
System::SharedPtr<Glyph> glyph = font->get_GlyphAccessor()->GetGlyphById(gid);
if (glyph == nullptr)
{
continue;
}
// path that accepts drawing instructions
System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
// Create IGlyphOutlinePainter implementation
System::SharedPtr<RenderingText::GlyphOutlinePainter> outlinePainter = System::MakeObject<RenderingText::GlyphOutlinePainter>(path);
// Create the renderer
System::SharedPtr<Aspose::Font::Renderers::IGlyphRenderer> renderer = System::MakeObject<Aspose::Font::Renderers::GlyphOutlineRenderer>(outlinePainter);
// get common glyph properties
double kerning = 0;
// get kerning value
if (previousGid != nullptr)
{
kerning = (font->get_Metrics()->GetKerningValue(previousGid, gid) / glyph->get_SourceResolution()) * fontSize * resolutionCorrection;
kerning += FontWidthToImageWith(font->get_Metrics()->GetGlyphWidth(previousGid), glyph->get_SourceResolution(), fontSize);
}
// glyph positioning - increase glyph X coordinate according to kerning distance
glyphXCoordinate += kerning;
// Glyph placement matrix
System::SharedPtr<TransformationMatrix> glyphMatrix = System::MakeObject<TransformationMatrix>(System::MakeArray<double>({fontSize * resolutionCorrection, 0, 0, -fontSize * resolutionCorrection, glyphXCoordinate, glyphYCoordinate}));
// render current glyph
renderer->RenderGlyph(font, gid, glyphMatrix);
// fill the path
path->set_FillMode(System::Drawing::Drawing2D::FillMode::Winding);
outGraphics->FillPath(textBrush, path);
}
//set current gid as previous to get correct kerning for next glyph
previousGid = gid;
}
}
//Save results
outBitmap->Save(outFile);
}
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 is font's internal dpi
return (width / fontSourceResulution) * fontSize * resolutionCorrection;
}
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String dataDir = RunExamples::GetDataDir_Data();
//byte array to load Font from file or another stream resource
System::ArrayPtr<uint8_t> fontMemoryData = System::IO::File::ReadAllBytes(dataDir + u"examplefont.eot");
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"eot", System::MakeObject<ByteContentStreamSource>(fontMemoryData)));
System::SharedPtr<TtfFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
System::String outputFile = RunExamples::GetDataDir_Data() + u"examplefont.ttf";
ttfFont->Save(outputFile);
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
//byte array to load Font from
System::String dataDir = RunExamples::GetDataDir_Data();
System::ArrayPtr<uint8_t> fontMemoryData = System::IO::File::ReadAllBytes(dataDir + u"Montserrat-Regular.ttf");
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<ByteContentStreamSource>(fontMemoryData)));
System::SharedPtr<TtfFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
//Work with data from just loaded TtfFont object
//Save CffFont to disk
//Output Font file name with full path
System::String outputFile = RunExamples::GetDataDir_Data() + u"Montserrat-Regular_out.ttf";
ttfFont->Save(outputFile);
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"courier.pfb";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(Aspose::Font::Font::Open(fd));
bool latinText = true;
for (uint32_t code = 65; code < static_cast<uint32_t>(123); code++)
{
System::SharedPtr<GlyphId> gid = font->get_Encoding()->DecodeToGid(code);
if (gid == nullptr || gid == System::StaticCast<System::Object>(GlyphUInt32Id::get_NotDefId()))
{
latinText = false;
}
}
if (latinText)
{
System::Console::WriteLine(System::String::Format(u"Font {0} supports latin symbols.", font->get_FontName()));
}
else
{
System::Console::WriteLine(System::String::Format(u"Latin symbols are not supported by font {0}.", font->get_FontName()));
}
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"courier.pfb";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(Aspose::Font::Font::Open(fd));
System::String name = font->get_FontName();
System::Console::WriteLine(System::String(u"Font name: ") + name);
System::Console::WriteLine(System::String(u"Glyph count: ") + font->get_NumGlyphs());
System::String metrics = System::String::Format(u"Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}", font->get_Metrics()->get_Ascender(), font->get_Metrics()->get_Descender(), font->get_Metrics()->get_TypoAscender(), font->get_Metrics()->get_TypoDescender(), font->get_Metrics()->get_UnitsPerEM());
System::Console::WriteLine(metrics);
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"courier.pfb";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(Aspose::Font::Font::Open(fd));
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String fileName = dataDir + u"courier.pfb";
//Font file name with full path
System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(Aspose::Font::Font::Open(fd));
DrawText(u"Hello world", font, 14, System::Drawing::Brushes::get_White(), System::Drawing::Brushes::get_Black(), dataDir + u"hello1_type1_out.jpg");
DrawText(u"Hello world", font, 14, System::Drawing::Brushes::get_Yellow(), System::Drawing::Brushes::get_Red(), dataDir + u"hello2_type1_out.jpg");
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::WorkingWithType1Fonts::RenderingText::GlyphOutlinePainter::GetSharedMembers()
{
auto result = System::Object::GetSharedMembers();
result.Add("Aspose::Font::Examples::WorkingWithType1Fonts::RenderingText::GlyphOutlinePainter::_path", this->_path);
result.Add("Aspose::Font::Examples::WorkingWithType1Fonts::RenderingText::GlyphOutlinePainter::_currentPoint", this->_currentPoint);
return result;
}
For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
void RenderingText::DrawText(System::String text, System::SharedPtr<IFont> font, double fontSize, System::SharedPtr<System::Drawing::Brush> backgroundBrush, System::SharedPtr<System::Drawing::Brush> textBrush, System::String outFile)
{
//Get glyph identifiers for every symbol in text line
System::ArrayPtr<System::SharedPtr<GlyphId>> gids = System::MakeArray<System::SharedPtr<Aspose::Font::Glyphs::GlyphId>>(text.get_Length());
for (int32_t i = 0; i < text.get_Length(); i++)
{
gids[i] = font->get_Encoding()->DecodeToGid(text[i]);
}
// set common drawing settings
double dpi = 300;
double resolutionCorrection = dpi / 72;
// 72 is font's internal dpi
// prepare output bitmap
System::SharedPtr<System::Drawing::Bitmap> outBitmap = System::MakeObject<System::Drawing::Bitmap>(960, 720);
outBitmap->SetResolution((float)dpi, (float)dpi);
System::SharedPtr<System::Drawing::Graphics> outGraphics = System::Drawing::Graphics::FromImage(outBitmap);
outGraphics->FillRectangle(backgroundBrush, 0, 0, outBitmap->get_Width(), outBitmap->get_Height());
outGraphics->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality);
//declare coordinate variables and previous gid
System::SharedPtr<GlyphId> previousGid;
double glyphXCoordinate = 0;
double glyphYCoordinate = fontSize * resolutionCorrection;
//loop which paints every glyph in gids
{
for (System::SharedPtr<GlyphId> gid : gids)
{
// if the font contains the gid
if (gid != nullptr)
{
System::SharedPtr<Glyph> glyph = font->get_GlyphAccessor()->GetGlyphById(gid);
if (glyph == nullptr)
{
continue;
}
// path that accepts drawing instructions
System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
// Create IGlyphOutlinePainter implementation
System::SharedPtr<RenderingText::GlyphOutlinePainter> outlinePainter = System::MakeObject<RenderingText::GlyphOutlinePainter>(path);
// Create the renderer
System::SharedPtr<Aspose::Font::Renderers::IGlyphRenderer> renderer = System::MakeObject<Aspose::Font::Renderers::GlyphOutlineRenderer>(outlinePainter);
// get common glyph properties
double kerning = 0;
// get kerning value
if (previousGid != nullptr)
{
kerning = (font->get_Metrics()->GetKerningValue(previousGid, gid) / glyph->get_SourceResolution()) * fontSize * resolutionCorrection;
kerning += FontWidthToImageWith(font->get_Metrics()->GetGlyphWidth(previousGid), glyph->get_SourceResolution(), fontSize);
}
// glyph positioning - increase glyph X coordinate according to kerning distance
glyphXCoordinate += kerning;
// Glyph placement matrix
System::SharedPtr<TransformationMatrix> glyphMatrix = System::MakeObject<TransformationMatrix>(System::MakeArray<double>({fontSize * resolutionCorrection, 0, 0, -fontSize * resolutionCorrection, glyphXCoordinate, glyphYCoordinate}));
// render current glyph
renderer->RenderGlyph(font, gid, glyphMatrix);
// fill the path
path->set_FillMode(System::Drawing::Drawing2D::FillMode::Winding);
outGraphics->FillPath(textBrush, path);
}
//set current gid as previous to get correct kerning for next glyph
previousGid = gid;
}
}
//Save results
outBitmap->Save(outFile);
}
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 is font's internal dpi
return (width / fontSourceResulution) * fontSize * resolutionCorrection;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment