แสดงผลแบบอักษร truetype c ++

ตัวอักษรเป็นสัญลักษณ์ที่อธิบายความหมายของคำต่างๆ คุณสามารถแสดงอักขระข้อความต่างๆ ด้วย TrueType fonts โดยใช้ C++ ด้วย Aspose.Font for C++ API รองรับคุณสมบัติการแสดงผลข้อความต่างๆ รวมถึง TrueType, Type1, OTF, EOT และแบบอักษรอื่นๆ ให้เราสำรวจวิธีใช้คุณสมบัติการแสดงข้อความด้วยการเรียก API ง่ายๆ ไม่กี่ขั้นตอน เราจะครอบคลุมหัวข้อภายใต้หัวข้อต่อไปนี้:

C ++ TrueType Font Rendering API – การติดตั้ง

เมื่อพิจารณาถึงความนิยมและความต้องการของแพลตฟอร์ม C++ เราได้แนะนำคุณสมบัติที่มีประโยชน์มากใน Aspose.Font for C++ API ในการแสดงฟอนต์ TrueType ในแอปพลิเคชันของคุณ คุณต้องกำหนดค่า API โดยดาวน์โหลดจาก New Releases หรือคุณสามารถติดตั้งผ่าน NuGet โดยใช้คำสั่งต่อไปนี้:

Install-Package Aspose.Font.Cpp

ใช้ส่วนต่อประสานสำหรับการวาด Glyph เพื่อแสดงผลแบบอักษร TrueType โดยใช้ C ++

ก่อนอื่น คุณต้องใช้ IGlyphOutlinePainter ภายใต้ Aspose.Font.Rendering namespace ทำตามขั้นตอนด้านล่างสำหรับการปรับใช้เมธอดและคลาส:

  1. สร้างคลาสชื่อ GlyphOutlinePainter
  2. ใช้ 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 โดยมีขั้นตอนต่อไปนี้:

  1. วนซ้ำสัญลักษณ์ในสตริงข้อความ
  2. รับ GID เป็นสัญลักษณ์ที่ระบุสำหรับแต่ละสัญลักษณ์
  3. สร้าง GlyphOutlinePainter และส่งต่อไปยังวัตถุ GlyphOutlineRenderer
  4. ระบุพิกัดสัญลักษณ์ด้วยวัตถุ 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;
}

Render ข้อความด้วย TrueType Font โดยใช้ C++

เราได้ใช้ฟังก์ชันที่จำเป็นสำหรับการแสดงข้อความด้วยฟอนต์ 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");

บทสรุป

ในบทความนี้ คุณได้เรียนรู้วิธีแสดงข้อความโดยใช้ฟอนต์ TrueType ด้วย C++ สนใจที่จะเรียนรู้เพิ่มเติมเกี่ยวกับฟีเจอร์ที่นำเสนอโดย Aspose.Font for C++ API หรือไม่ คุณสามารถดูรายละเอียดเพิ่มเติมได้โดยไปที่ ข้อมูลอ้างอิง API หรือ เอกสารประกอบผลิตภัณฑ์ อย่างไรก็ตาม หากคุณพบความคลุมเครือหรือต้องการความช่วยเหลือ โปรดติดต่อเราผ่าน ฟอรัมสนับสนุนฟรี เราหวังว่าจะมีส่วนร่วมกับคุณ!

ดูสิ่งนี้ด้วย