تقديم النص باستخدام الخط في جافا

في المقالة السابقة ، رأيت كيفية تحميل خطوط CFF و TrueType و Type1 برمجيًا باستخدام Java. اليوم ، سنناقش ميزة أخرى مثيرة للاهتمام لواجهة برمجة تطبيقات معالجة الخطوط بجافا - عرض النص باستخدام الخطوط. بنهاية هذه المقالة ، ستكون قادرًا على عرض النص باستخدام خطوط TrueType و Type1 من داخل تطبيقات Java الخاصة بك. دعنا نبدأ.

Java Font Manipulation API

يوفر لك Aspose.Font for Java ميزات تحميل الخطوط وحفظها بالإضافة إلى الحصول على مقاييس لأنواع الخطوط الشائعة بما في ذلك CFF و TrueType و OpenType و Type1. بالإضافة إلى ذلك ، تتيح لك واجهة برمجة التطبيقات (API) عرض النص باستخدام خطوط TrueType أو Type1 المتوفرة. يمكنك إما تثبيت API باستخدام تكوينات Maven أو تنزيل JAR الخاص بواجهة برمجة التطبيقات.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-font</artifactId>
    <version>20.10</version>
</dependency>

تطبيق طريقة عرض النص

من أجل تقديم النص ، يتطلب منك Aspose.Font for Java تنفيذ طريقة drawText() التي سترسم النص المقدم. فيما يلي التعريف الكامل لطريقة drawText().

// للحصول على أمثلة وملفات بيانات كاملة ، يرجى الانتقال إلى https://github.com/aspose-font/Aspose.Font-for-Java
static void drawText(String text, IFont font, double fontSize,
           Paint backgroundBrush, Paint textBrush, String outFile) throws Exception
{
    //احصل على معرفات الحروف الرسومية لكل رمز في سطر النص
    GlyphId[] gids = new GlyphId[text.length()];
   for (int i = 0; i < text.length(); i++)
        gids[i] = font.getEncoding().decodeToGid(text.charAt(i));
    // تعيين إعدادات الرسم المشتركة
    double dpi = 300;
	
    double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
    // تحضير الصورة النقطية الإخراج
    BufferedImage outBitmap = new BufferedImage(960, 720, BufferedImage.TYPE_INT_BGR);
    //outBitmap.getRaster().SetResolution ((عائم) نقطة في البوصة ، (عائم) نقطة في البوصة) ؛
    java.awt.Graphics2D outGraphics = (java.awt.Graphics2D) outBitmap.getGraphics();
    outGraphics.setPaint(backgroundBrush);
    outGraphics.fillRect(0, 0, outBitmap.getWidth(), outBitmap.getHeight());
    outGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    //إعلان متغيرات الإحداثيات والدليل السابق
    GlyphId previousGid = null;
    double glyphXCoordinate = 0;
    double glyphYCoordinate = fontSize * resolutionCorrection;
    //الحلقة التي ترسم كل حرف رسومي في gids
   for (GlyphId gid : gids)
    {
        // إذا كان الخط يحتوي على gid
        if (gid != null)
        {
            Glyph glyph = font.getGlyphAccessor().getGlyphById(gid);
            if (glyph == null)
                continue;
	
            // المسار الذي يقبل تعليمات الرسم
            GeneralPath path = new GeneralPath();
	
            // إنشاء تطبيق IGlyphOutlinePainter
            GlyphOutlinePainter outlinePainter = new GlyphOutlinePainter(path);
	
            // قم بإنشاء العارض
            IGlyphRenderer renderer = new GlyphOutlineRenderer(outlinePainter);
	
            // الحصول على خصائص الصورة الرمزية المشتركة
            double kerning = 0;
	
            // الحصول على قيمة تقنين الأحرف
            if (previousGid != null)
            {
                kerning = (font.getMetrics().getKerningValue(previousGid, gid) /
                           glyph.getSourceResolution()) * fontSize * resolutionCorrection;
                kerning += fontWidthToImageWdith(font.getMetrics().getGlyphWidth(previousGid),
                        glyph.getSourceResolution(), fontSize, 300);
            }
	
            // تحديد موضع الصورة الرمزية - زيادة إحداثيات الحرف الرسومي X وفقًا لمسافة المسافة بين الحروف
            glyphXCoordinate += kerning;
	
            // مصفوفة وضع الصورة الرمزية
            TransformationMatrix glyphMatrix =
                new TransformationMatrix(
                    new double[]
                            {
                                    fontSize*resolutionCorrection,
                                    0,
                                    0,
                                // سالب بسبب نظام إحداثيات الصورة النقطية يبدأ من الأعلى
                                    - fontSize*resolutionCorrection,
                                    glyphXCoordinate,
                                    glyphYCoordinate
                            });
	
            // تقديم الصورة الرمزية الحالية
            renderer.renderGlyph(font, gid, glyphMatrix);
            // املأ المسار
            path.setWindingRule(GeneralPath.WIND_NON_ZERO);
            outGraphics.setPaint(textBrush);
            outGraphics.fill(path);
        }
        //اضبط gid الحالي على أنه سابق للحصول على المسافات الصحيحة بين الحروف للصورة الرمزية التالية
        previousGid = gid;
    }
    //حفظ النتائج
    ImageIO.write(outBitmap, "jpg", new File(outFile));
}

فيما يلي طريقة مساعدة تُستخدم لحساب عرض الصورة الرمزية لنظام إحداثيات الصورة النقطية.

// للحصول على أمثلة وملفات بيانات كاملة ، يرجى الانتقال إلى https://github.com/aspose-font/Aspose.Font-for-Java
static double fontWidthToImageWidth(double width, int fontSourceResulution, double fontSize, double dpi)
{
    double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
    return (width / fontSourceResulution) * fontSize * resolutionCorrection;
}

خطوات تقديم النص باستخدام الخط في Java

فيما يلي خطوات كيفية عرض نص محدد كملف صورة باستخدام طريقة darwText() المذكورة أعلاه.

  • قم بتحميل الخط باستخدام فئة FontDefinition.
  • قم بالوصول إلى الخط باستخدام الفئة مثل Type1Font أو TtfFont.
  • قم باستدعاء طريقة drawText() من خلال توفير التفاصيل في شكل معاملاتها.

عرض النص باستخدام خط TrueType باستخدام Java

يُظهر نموذج التعليمات البرمجية التالي كيفية استخدام أسلوب drawText() لتقديم نص باستخدام خط تروتايب. سيتم إنشاء نتائج التجسيد كصورة JPEG.

// للحصول على أمثلة وملفات بيانات كاملة ، يرجى الانتقال إلى https://github.com/aspose-font/Aspose.Font-for-Java
String fileName1 = Utils.getDataDir() + "Montserrat-Bold.ttf"; //Font file name with full path
      FontDefinition fd1 = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName1)));
      TtfFont font1 = (TtfFont) Font.open(fd1);
      
      String fileName2 = Utils.getDataDir() + "Lora-Bold.ttf"; //Font file name with full path
      FontDefinition fd2 = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName2)));
      TtfFont font2 = (TtfFont) Font.open(fd2);
      
      try {
       drawText("Hello world", font1, 14, java.awt.Color.WHITE, java.awt.Color.BLACK, Utils.getDataDir() + "hello1_montserrat_out.jpg");
       drawText("Hello world", font2, 14, java.awt.Color.YELLOW, java.awt.Color.RED, Utils.getDataDir() + "hello2_lora_out.jpg");
      } catch (Exception ex) {
      	ex.printStackTrace();
      }

عرض النص باستخدام خطوط Type1 في Java

يوضح نموذج التعليمات البرمجية التالي كيفية تقديم نص إلى صورة JPEG بخط Type1 باستخدام Java.

// للحصول على أمثلة وملفات بيانات كاملة ، يرجى الانتقال إلى https://github.com/aspose-font/Aspose.Font-for-Java
String fileName = Utils.getDataDir() + "courier.pfb"; //Font file name with full path

FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
      Type1Font font = (Type1Font) Font.open(fd);
      
      try {
       drawText("Hello world", font, 14, java.awt.Color.WHITE, java.awt.Color.BLACK, Utils.getDataDir() + "hello1_type1_out.jpg");
       drawText("Hello world", font, 14, java.awt.Color.YELLOW, java.awt.Color.RED, Utils.getDataDir() + "hello2_type1_out.jpg");
      } catch (Exception ex) {
      	ex.printStackTrace();
      }

استنتاج

في هذه المقالة ، رأيت كيفية عرض النص على هيئة صور JPEG باستخدام خط TrueType أو Type1 من داخل تطبيقات Java. لمعرفة المزيد حول واجهة برمجة تطبيقات معالجة خطوط جافا ، يمكنك زيارة الوثائق وتقييم ميزات واجهة برمجة التطبيقات باستخدام نماذج التعليمات البرمجية المصدر.