Load, Extract Data and Save from TTF | C++
Contents
[
Hide
Show
]Load TrueType Font from Disc
TrueType fonts can be stored to disc in files. Aspose.Font for C++ API can load such files and read font information from these. The following steps can be used to read TrueType font file in C++ applications.
- Create a new object of FontDefinition class
- Use FontType as TTF and FontFileDefinition as ttf
- Declare a TtfFont object and open the font file from FontDefinition object defined in the earlier step
1For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
2System::String fileName = dataDir + u"Montserrat-Regular.ttf";
3//Font file name with full path
4
5System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName)));
6System::SharedPtr<TtfFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
Save TTF Font
Aspose.Font for C++ provides the capability to edit font files and save back the changes to disc. The following C++ code sample shows how to save an updated TTF font file to disc.
- Define a new object of FontDefinition class
- Specify FontType as TTF and FontFileDefinition as ttf
- Create a TtfFont object and open the font file from FontDefinition object defined in the earlier step
- Make the required updates and save the updated Font file to disc using the TtfFont’s Save method
1For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
2//byte array to load Font from
3System::String dataDir = RunExamples::GetDataDir_Data();
4
5System::ArrayPtr<uint8_t> fontMemoryData = System::IO::File::ReadAllBytes(dataDir + u"Montserrat-Regular.ttf");
6System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<ByteContentStreamSource>(fontMemoryData)));
7System::SharedPtr<TtfFont> ttfFont = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd));
8
9//Work with data from just loaded TtfFont object
10
11//Save CffFont to disk
12//Output Font file name with full path
13System::String outputFile = RunExamples::GetDataDir_Data() + u"Montserrat-Regular_out.ttf";
14
15ttfFont->Save(outputFile);
For more examples go to Aspose.Font.Examples.sln solution, in the net-examples folder of the Aspose.Font Documentation Github repository.
You can also check our free online Font Manipulation Applications to see how the functionality can be implemented in a web solution.