كيفية تنسيق النص في C++

Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat فئة. A Portion هي أصغر وحدة مستقلة من النص؛ تُطابق تشغيل تنسيق واحد داخل الفقرة. يوضح هذا الدليل كيفية تطبيق التنسيق الغامق، المائل، حجم الخط، وتنسيق اللون على النص في عرض تقديمي.

دليل خطوة بخطوة

الخطوة 1: بناء وربط المكتبة

git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake .. && cmake --build .

الخطوة 2: إضافة شكل مع إطار نص

قبل تنسيق النص، أضف شكلاً واضبط محتوى نصه عبر shape.text_frame()->set_text().

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Default text: will be formatted");
    prs.save("output.pptx", asf::SaveFormat::PPTX);
    return 0;
}

الخطوة 3: الوصول إلى TextFrame

shape.text_frame() يرجع مؤشرًا إلى shape’s TextFrame. استخدم -> لاستدعاء الأساليب عليه.

auto* tf = shape.text_frame();          // pointer to the shape's text frame
tf->set_text("your text here");

A TextFrame يحتوي على Paragraph كائنات (tf->paragraphs()). كل Paragraph يحتوي Portion الكائنات (paragraph.portions()).


الخطوة 4: تطبيق تنسيق الغامق والمائل

استخدم portion_format().set_font_bold() و portion_format().set_font_italic(). تقبل هذه الأساليب NullableBool::TRUE, NullableBool::FALSE, أو NullableBool::NOT_DEFINED (ورث من master).

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Bold and italic text");
    auto* tf = shape.text_frame();

    auto& fmt = tf->paragraphs()[0].portions()[0].portion_format();
    fmt.set_font_bold(asf::NullableBool::TRUE);
    fmt.set_font_italic(asf::NullableBool::TRUE);

    prs.save("bold-italic.pptx", asf::SaveFormat::PPTX);
    return 0;
}

الخطوة 5: ضبط حجم الخط واللون

عيّن portion_format().set_font_height() لحجم (بالنقاط) واستخدم fill_format() للون.

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Large corporate-blue heading");
    auto* tf = shape.text_frame();

    auto& fmt = tf->paragraphs()[0].portions()[0].portion_format();
    fmt.set_font_height(32);                               // 32pt font
    fmt.set_font_bold(asf::NullableBool::TRUE);
    fmt.fill_format().set_fill_type(asf::FillType::SOLID);
    fmt.fill_format().solid_fill_color().set_color(
        asf::Color::from_argb(255, 0, 70, 127));

    prs.save("colored-text.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Color::from_argb(alpha, red, green, blue) يقبل القيم من 0 إلى 255 لكل قناة.


الخطوة 6: عدة أجزاء في فقرة واحدة

يمكن لفقرة واحدة أن تحتوي على عدة أجزاء بتنسيق مختلف. أضف جديدًا Portion إلى فقرة portions() مجموعة:

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 600, 100);
    shape.text_frame()->set_text(""); // start with empty text
    auto* tf = shape.text_frame();

    auto& paragraph = tf->paragraphs()[0];

    // First portion: normal text
    auto& portion1 = paragraph.portions()[0];
    portion1.set_text("Normal text followed by ");
    portion1.portion_format().set_font_height(20);

    // Second portion: bold red text
    asf::Portion portion2;
    portion2.set_text("bold red text");
    portion2.portion_format().set_font_height(20);
    portion2.portion_format().set_font_bold(asf::NullableBool::TRUE);
    portion2.portion_format().fill_format().set_fill_type(asf::FillType::SOLID);
    portion2.portion_format().fill_format().solid_fill_color().set_color(
        asf::Color::from_argb(255, 200, 0, 0));
    paragraph.portions().add(portion2);

    prs.save("mixed-format.pptx", asf::SaveFormat::PPTX);
    return 0;
}

المشكلات الشائعة والحلول

النص يظهر باللون الأسود حتى بعد ضبط اللون

تأكد fill_format().set_fill_type(FillType::SOLID) يتم تعيينه قبل تعيين اللون. بدون تعيين نوع التعبئة، قد لا يكون لتغيير اللون أي تأثير.

NullableBool::TRUE مقابل true

portion_format().set_font_bold() يتوقع NullableBool::TRUE,، وليس C++ true. تمرير true الاستدعاء مباشرة لن يتم تجميعه أو سيؤدي إلى سلوك غير معرف اعتمادًا على حل التحميل الزائد.

الخط لا يظهر في الملف المحفوظ

ال set_latin_font() الطريقة تُعيّن عائلة الخط اللاتيني. إذا لم تُعيّن، يُستخدم خط سمة العرض. يجب تضمين الخطوط المخصَّصة أو أن تكون متاحة على جهاز العرض.


الأسئلة المتكررة

كيف يمكنني تغيير عائلة الخط؟?

تعيين portion_format().set_latin_font():

fmt.set_latin_font(asf::FontData("Arial"));

FontData يقبل اسم عائلة الخط كسلسلة.

كيف يمكنني ضبط محاذاة الفقرة؟?

استخدام paragraph_format().set_alignment():

tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);

القيم المدعومة: LEFT, CENTER, RIGHT, JUSTIFY.

كيف يمكنني ضبط تباعد الأسطر؟?

استخدام paragraph_format().set_space_before() (نقاط قبل الفقرة) أو paragraph_format().set_space_after() (نقاط بعد الفقرة):

tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6);   // 6pt after

انظر أيضًا

 العربية