Kako renderirati LaTeX u PNG koristeći C++

Ova zanimljiva tema pruža demonstraciju kako renderirati LaTeX u PNG pomoću C++. LaTeX datoteke također nazivamo .tex datotekama i lako se može renderirati LaTeX u PNG u C++ s jednostavnim API svojstvima i metodama. Jedna dobra stvar kod korištenja ovog primjera je da API pozivi ne ovise o bilo kojem drugom aplikacijskom softveru ili alatima treće strane za njihovo izvršenje.

Koraci za renderiranje LaTeX-a u PNG pomoću C++

  1. Instalirajte Aspose.Tex.Cpp pomoću alata NuGet Package Manager
  2. Uključi referencu na prostore imena Aspose::TeX, Aspose::TeX::IO i Aspose::TeX::Presentation::Image
  3. Instancirajte objekt TeXOptions Class za postavljanje konfiguracija
  4. Instancirajte objekt PngSaveOptions Class za spremanje LaTeX slike u PNG
  5. Inicijalizirajte ImageDevice za iscrtavanje
  6. Koristite TexJob za renderiranje LaTeX-a u PNG sliku

LaTeX dokumenti namijenjeni su za znanstvene i istraživačke svrhe koje mogu koristiti tehnički korisnici i sadrže informacije u obliku običnog teksta. Kako bismo izvezli LaTeX u PNG sliku u C++, prvo ćemo stvoriti instancu TeXOptions klase za postavljanje konfiguracija koje uključuju ulazni direktorij, izlazni direktorij i opcije konzole. U sljedećim koracima postavit ćemo svojstva PngSaveOptions poput rezolucije slike. Na kraju, inicijalizirat ćemo ImageDevice i pomoću TexJob-a renderirati LaTeX u PNG sliku koristeći C++.

Kod za pretvaranje LaTeX-a u PNG u C++

#pragma once
#include <system/text/encoding.h>
#include <system/io/text_writer.h>
#include <system/io/memory_stream.h>
#include <system/array.h>
#include <Aspose.TeX.Cpp/TeXOptions.h>
#include <Aspose.TeX.Cpp/TeXConfig.h>
#include <Aspose.TeX.Cpp/TeXJob.h>
#include <Aspose.TeX.Cpp/Presentation/SaveOptions.h>
#include <Aspose.TeX.Cpp/Presentation/Image/PngSaveOptions.h>
#include <Aspose.TeX.Cpp/Presentation/Image/ImageDevice.h>
#include <Aspose.TeX.Cpp/IO/OutputFileSystemDirectory.h>
#include <Aspose.TeX.Cpp/IO/OutputConsoleTerminal.h>
#include <Aspose.TeX.Cpp/IO/IOutputWorkingDirectory.h>
#include <Aspose.TeX.Cpp/IO/IOutputTerminal.h>
#include <Aspose.TeX.Cpp/IO/InputFileSystemDirectory.h>
#include <Aspose.TeX.Cpp/IO/InputConsoleTerminal.h>
#include <Aspose.TeX.Cpp/IO/IInputTerminal.h>
#include <cstdint>
#include <Aspose.TeX.Cpp/License.h>
using namespace Aspose::TeX;
using namespace Aspose::TeX::IO;
using namespace Aspose::TeX::Presentation::Image;
class TexToPngConverter{
public:
static void TexToPNGRendering()
{
// Initialize license object
System::SharedPtr<License> TexLicense = System::MakeObject<License>();
// Applying license for rendering PNG
TexLicense->SetLicense(u"Aspose.Total.NET.lic");
// Instantiate TeXOptions object for configuring settings
System::SharedPtr<TeXOptions> RenderingOptions = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX());
// Specify the job name.
RenderingOptions->set_JobName(u"stream-image-out");
// Specify the input file working directory.
RenderingOptions->set_InputWorkingDirectory(System::MakeObject<InputFileSystemDirectory>(u""));
// Specify the output file working directory.
RenderingOptions->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(u""));
// Setting the console input terminal
RenderingOptions->set_TerminalIn(System::MakeObject<InputConsoleTerminal>());
// Setting the console output terminal
RenderingOptions->set_TerminalOut(System::MakeObject<OutputConsoleTerminal>());
// Creating and specifying PngSaveOptions
System::SharedPtr<PngSaveOptions> ImageOptions = System::MakeObject<PngSaveOptions>();
ImageOptions->set_Resolution(300);
RenderingOptions->set_SaveOptions(ImageOptions);
// Instantiating ImageDevice for rendering
System::SharedPtr<ImageDevice> ImageDevise = System::MakeObject<ImageDevice>();
// Running TexJob for rendering to PNG
TeXJob::TeXJob(System::MakeObject<System::IO::MemoryStream>(System::Text::Encoding::get_ASCII()->
GetBytes(u"\\hrule height 10pt width 95pt\\vskip10pt\\hrule height 5pt")),
ImageDevise, RenderingOptions);
// During executoion, when the console prompts the input,
// Step 1: Type "ABC", press Enter
// Step 2: Type "\end" and press Enter again
// In order to view the ouptut and to look write.
RenderingOptions->get_TerminalOut()->get_Writer()->WriteLine();
// Alternatively, you can also get the images in form of array of byte arrays
// The 0th index belong to first page.
System::ArrayPtr<System::ArrayPtr<uint8_t>> result = ImageDevise->get_Result();
}
};

U ovoj smo temi naučili kako C++ stvoriti PNG sliku iz LaTeX-a pomoću jednostavnih API poziva. Ako ste zainteresirani za učenje drugih značajki poput iscrtavanja MPP datoteka, pogledajte članak na kako pretvoriti MPP u XPS koristeći C++.

 Hrvatski