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

توفر مكتبة Aspose.Slides FOSS للغة C++ تنسيق نص دقيق من خلال الفئة PortionFormat. الـ 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() يرجع مؤشرًا إلى TextFrame الخاص بالشكل. استخدم -> لاستدعاء الأساليب عليه.

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

يحتوي 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

انظر أيضًا

 العربية