Jump to content

Recommended Posts

Posted (edited)

Hi everyone, apart of L2, I also work on many different things and I'd like to share one example I wrote (because it's not so easy finding out how to  do it properly).

 

When you need to render some text that requires more complex rendering than Latin - for example Hebrew, Arabic and Devanagari (Hindi) - you'll have to do some text shaping. It's not so difficult for Hebrew and Arabic (if you don't mind sometimes wrong niqqud, you can implement subset of OpenType and use fribidi) but for Devanagari it's almost impossible to implement again (too much work).

 

If you're on Windows platform only, you can use Microsoft's Uniscribe which works perfectly, on OS X/iOS you can use Apple's Coretext which works very well too.

 

But what if you need it to be platform agnostic? Then you can use FreeType and HarfBuzz :) Also, HarfBuzz can use native (Uniscribe/Coretext) shapers on given platform (depends how you compile it) instead of it's own shaper.

 

So the example here, as for fonts, you can download them from Google, if you want to see output without compiling, scroll down.


#include <iostream>
#include <harfbuzz/hb.h>
#include <harfbuzz/hb-ft.h>
#include <ft2build.h>
#include <freetype.h>

// Input texts in UTF-8 (without Byte Order Mask)
static const char *texts[] = {
    "\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d \xd7\xa2\xd7\x95\xd7\x9c\xd7\x9d",
    "\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7 \xd8\xa8\xd8\xa7\xd9\x84\xd8\xb9\xd8\xa7\xd9\x84\xd9\x85",
    "\xe0\xa4\xa8\xe0\xa4\xae\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4\xe0\xa5\x87 \xe0\xa4\xa6\xe0\xa5\x81\xe0\xa4\xa8\xe0\xa4\xbf\xe0\xa4\xaf\xe0\xa4\xbe"
};

// Font sizes in points
static const size_t fontSizes[] = {
    16,
    16,
    16
};

// Font filenames
static const char *fontFiles[] = {
    "NotoSansHebrew-Regular.ttf",
    "NotoSansArabic-Regular.ttf",
    "NotoSansDevanagari-Regular.ttf"
};

int main(int argc, char **argv)
{
    // Freetype library handle
    FT_Library library;

    // Initialize FreeType
    if (FT_Init_FreeType(&library)) {
        std::cerr << "Can't initialize FreeType" << std::endl;
        return 1;
    }

    // Go through all input texts
    for (size_t textIndex = 0 ; textIndex < sizeof(texts) / sizeof(texts[0]) ; ++textIndex) {
        // Current input text
        const char *text = texts[textIndex];
        const size_t fontSize = fontSizes[textIndex];
        const char *fontFile = fontFiles[textIndex];

        // FreeType font face handle
        FT_Face face;

        // Load font
        if (FT_New_Face(library, fontFile, 0, &face)) {
            std::cerr << "Can't load font " << fontFile << std::endl;
            return 1;
        }

        // Set character size
        if (FT_Set_Char_Size(face, fontSize << 6, fontSize << 6, 0, 0)) {
            std::cerr << "Can't set character size" << std::endl;
            return 1;
        }

        // Set no transform (identity)
        FT_Set_Transform(face, 0, 0);

        // Load font into HarfBuzz
        hb_font_t *hbFont = hb_ft_font_create(face, 0);

        // Create buffer for our text
        hb_buffer_t *hbBuffer = hb_buffer_create();

        // Add our text to buffer
        hb_buffer_add_utf8(hbBuffer, text, -1, 0, -1);

        // Detect direction etc
        hb_buffer_guess_segment_properties(hbBuffer);

        // Shape our text
        hb_shape(hbFont, hbBuffer, 0, 0);

        // Shaped text info
        unsigned int len = hb_buffer_get_length(hbBuffer);
        hb_glyph_info_t *info = hb_buffer_get_glyph_infos(hbBuffer, 0);
        hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(hbBuffer, 0);

        // Compute text width and origin (from min and max X and Y drawing coordinate)
        int originX = 0, originY = 0;
        int minX = INT_MAX, maxX = INT_MIN, minY = INT_MAX, maxY = INT_MIN;

        // Go through all glyphs and find minimum and maximum X and Y coordinate
        for (size_t i = 0 ; i < len ; ++i) {
            // Load glyph
            if (FT_Load_Glyph(face, info[i].codepoint, FT_LOAD_RENDER)) {
                std::cerr << "Can't load glyph " << info[i].codepoint << std::endl;
                return 1;
            }

            // Glyph data
            FT_GlyphSlot slot = face->glyph;

            // Get X and Y offset
            int offsetX = ((pos[i].x_offset + slot->metrics.horiBearingX) >> 6);
            int offsetY = ((pos[i].y_offset + slot->metrics.horiBearingY) >> 6);

            // Compute minimum and maximum X and Y for this glyph
            int glyphMinX = originX + offsetX;
            int glyphMaxX = originX + slot->bitmap.width + offsetX;
            int glyphMinY = originY - slot->bitmap.rows + offsetY;
            int glyphMaxY = originY + offsetY;

            // Update minimum and maximum X and Y for text
            if (glyphMinX < minX) minX = glyphMinX;
            if (glyphMaxX > maxX) maxX = glyphMaxX;
            if (glyphMinY < minY) minY = glyphMinY;
            if (glyphMaxY > maxY) maxY = glyphMaxY;

            // Advance
            originX += pos[i].x_advance >> 6;
        }

        // Text will start on 0
        originX = -minX;
        originY = -minY;

        // Compute width and height
        size_t width = maxX - minX + 1;
        size_t height = maxY - minY + 1;

        // Allocate buffer for image
        unsigned char *image = new unsigned char[width * height];

        // Clear image bufer
        memset(image, 0, width * height);

        // Go through glyphs and draw them
        for (size_t i = 0 ; i < len ; ++i) {
            // Load glyph
            if (FT_Load_Glyph(face, info[i].codepoint, FT_LOAD_RENDER)) {
                std::cerr << "Can't load glyph " << info[i].codepoint << std::endl;
                return 1;
            }

            // Glyph data
            FT_GlyphSlot slot = face->glyph;

            // Pointer to bitmap data
            unsigned char *ptr = slot->bitmap.buffer;

            // Get real offset
            int drawX = originX + ((pos[i].x_offset + slot->metrics.horiBearingX) >> 6);
            int drawY = originY + ((pos[i].y_offset + slot->metrics.horiBearingY) >> 6);

            // Copy bitmap
            for (size_t y = 0 ; y < slot->bitmap.rows ; ++y) {
                // Copy row
                for (size_t x = 0 ; x < slot->bitmap.width ; ++x) {
                    if (drawX + x < 0) {
                        std::cerr << "drawX (" << drawX << ") + x (" << x << ") < 0" << std::endl;
                        abort();
                    }
                    if (drawX + x >= width) {
                        std::cerr << "drawX (" << drawX << ") + x (" << x << ") > width (" << width << ")" << std::endl;
                        abort();
                    }
                    if (drawY - y < 0) {
                        std::cerr << "drawY (" << drawY << ") - y (" << y << ") < 0" << std::endl;
                        abort();
                    }
                    if (drawY - y >= height) {
                        std::cerr << "drawY (" << drawY << ") - y (" << y << ") > height (" << height << ")" << std::endl;
                        abort();
                    }
                    image[(drawY - y) * width + drawX + x] = ptr[x];
                }
                // Advance pointer
                ptr += slot->bitmap.pitch;
            }

            // Advance
            originX += pos[i].x_advance >> 6;
        }

        // Output rendered text
        for (size_t y = 0 ; y < height ; ++y) {
            for (size_t x = 0 ; x < width ; ++x) {
                unsigned char value = image[(height - y - 1) * width + x];
                if (value >= 0x80) {
                    std::cout << "XX"; // if it's 128+
                } else if (value >= 0x40) {
                    std::cout << ".."; // if it's 64+
                } else {
                    std::cout << "  "; // if its under 64
                }
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;

        // Delete image buffer
        delete [] image;

        // Destroy buffer for text
        hb_buffer_destroy(hbBuffer);

        // Destroy HarfBuzz font
        hb_font_destroy(hbFont);

        // Destroy FreeType font
        FT_Done_Face(face);
    }

    // Destroy FreeType
    FT_Done_FreeType(library);

    return 0;
}
                  XXXX                                                                          XXXX
                  XXXX                                                                          XXXX
                  XXXX                                                                          XXXX
XXXXXXXXXXXXXX    XXXXXXXXXXXXXXXX  XXXX    XXXX          XX          XXXXXXXXXXXXXX      XXXX  XXXXXXXXXXXXXXXX..XX      XXXX    XXXX
XXXX........XXXX  ............XX..  XXXX    XXXX        ..XX          XXXX........XXXX    XXXX  ............XX..  XX      XXXX    XXXX
XXXX        ..XX            ..XX    XXXX    ..XX        ..XX          XXXX        ..XX    XXXX            ..XX    XX..    XX..    XXXX
XXXX        ..XX            XXXX    XXXX      XX..      XXXX          XXXX        ..XX    XXXX            XXXX    XX..  ..XX      XX..
XXXX        ..XX            XX..    XXXX      XXXX      XXXX          XXXX        ..XX    XXXX            XX..    XXXXXXXXXX      XX..
XXXX        ..XX          XXXX      XXXX      XXXX      XXXX          XXXX        ..XX    XXXX          XXXX      XXXXXX        XXXX
XXXX        ..XX          XXXX      XXXX      ..XX    XXXX            XXXX        ..XX    XXXX          XXXX      XXXX        ..XXXX
XXXX........XXXX        ..XX        XXXX    ....XXXXXXXX..            XXXX........XXXX    XXXX        ..XX        XXXX......XXXXXX
XXXXXXXXXXXXXXXX        XXXX        XXXX  XXXXXXXXXXXX                XXXXXXXXXXXXXXXX    XXXX        XXXX        XXXXXXXXXXXX..


                  ..XX    XX..                    ..XX    XX..                    XX..
                  ..XX    XXXX                    ..XX    XXXX                    XXXX
                  ..XX    XXXX                    ..XX    XXXX                    XXXX
                  ..XX    XXXX                    ..XX    XXXX                    XXXX
                  ..XX    XXXX      XXXXXXXX..    ..XX    XXXX      ..            XXXX
      ..XXXX..      XX    XXXX    XXXX....XXXX      XX    XXXX    ..XX            XXXX              XXXXXXXX..          ..          XXXXXX
    XXXX..XXXX      XX    XXXX    XX..      XX      XX    XXXX      XX..          XXXX      ....    ......XXXXXX      ..XX        ..XX..XXXX
  XXXX      XX..    XX    XXXX    ..XX..  XXXX      XX    XXXX      XX..          XXXX      XXXX              XX        XXXX      XX..    XX..
  XXXXXX    XXXX    XX    XXXX      ..XXXXXX        XX    XXXX      XX..          XXXX      XX..          ..XXXX        ..XX    ..XX      XX..
XXXX  XXXX..XXXX..XXXX    ..XXXX....XXXXXXXXXX....XXXX    ..XXXX....XX            ..XXXX..XXXXXX......XXXXXX..          ..XXXX..XXXXXX....XX
XXXX    XXXXXXXXXXXX..      XXXXXXXXXX..  XXXXXXXXXX..      XXXXXXXX..              XXXXXXXX..XXXXXXXXXX..              XXXXXXXXXX  XXXXXXXX
XX..                                                                                                                  ..XX
XXXX                                                                                                                ..XX..
XXXX                                                              ..XX                      ..XX                  XXXX..
..XX                                                                                                              ..
  XX
  XX..




                                                  ..XXXXXX                                  XXXXXXXXXX..
                                                        XX..                              XXXX..  ..XXXXXX
                                                        ..XX                              XX            XXXX
                                                          XX                              XX..            XXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX        ..  XX..      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..
            XXXX    XX..    ..XX..      XXXX              XXXX                  ..XX      XXXX              XXXX    XXXX    XXXX    XXXX
            XX..    XX..      XX        XXXX              XXXX                  ..XX      XXXX              XX..    ..XX    ..XX    XXXX
            XX..    XX..      XX        XXXX              XXXX              XXXXXXXX      XXXX              XX..    XXXX    ..XX    XXXX
..XXXXXXXXXXXX..    XX..      XX  ..XXXXXX..    ..XXXXXXXXXXXX            XXXX..          XXXX  ..XXXXXXXXXXXX..  XXXX..    ..XX    XXXX
  XXXX..    XX....XXXXXXXXXXXXXX    XXXXXXXXXX  XXXX      XXXX            XX              XXXX    XXXX..    XX..  XX..      XXXX    XXXX
  ..XX      XX..  XXXX..      XX    ..XX        XX        XXXX            XX      XXXX    XXXX    ..XX      XX..  XXXX    ..XXXX    XXXX
            XX..    XX        XX      XXXX      XX..      XXXX            XXXX    XXXX..  XXXX              XX..    XXXXXXXXXXXX    XXXX
            XX..              XX        XXXX    ..XX..    XXXX              XXXXXXXXXX    XXXX              XX..            ..XX    XXXX
            XX..              XX          XXXX    ..XX    XXXX                      XX    XXXX              XX..            ..XX    XXXX
                                                                                  XXXXXX..
                                                                          ..XX        ..XX
                                                                            XXXX..    XXXX
                                                                              ..XXXXXXXX


 

Edited by eressea
  • Thanks 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Posts

    • Yeah inside router i had to enable udnp services 
    • Hello cheaters, As a team of avid developers and enthusiasts of Lineage 2, we are excited to present the L2 Control Hub, a groundbreaking plugin designed by myself and my collaborator, StinkyMadness. This innovative tool equips server administrators with powerful automation capabilities directly within the game's community board. L2 Control Hub simplifies the creation and management of automations, enabling you to customize your server operations without the need to modify the source code.   Key Features of L2 Control Hub: Robust Automation Triggers: Select from a plethora of triggers currently available, with continuous additions in the works to enhance your control options. Dynamic Conditions and Actions: Tailor your server operations with an extensive range of conditions and actions, ensuring flexible and precise control over game events and player interactions. Customizable Variables: Easily integrate server-specific variables from your database to further personalize and streamline your automations. Utilize these variables across various automation scenarios to cater to your specific server requirements. JavaScript Integration: Execute custom JavaScript codes that interact seamlessly with Java classes, bringing advanced functionalities to your server's ecosystem.   Explore L2 Control Hub in Action: We've prepared a series of video tutorials to demonstrate the capabilities of L2 Control Hub: Control Hub - Create a Simple Flow with 1 Condition and 1 Action: Get started with basic automations. Control Hub - Multiple Conditions with Multiple Actions: Explore more complex automations for detailed server management. Control Hub - Using Variables: Discover how to implement and use custom variables for tailored automations. Control Hub - Using JavaScript: Experience the power of custom scripts in enhancing your server functionality.   L2 Control Hub is currently about 70% complete, and we are actively developing and refining features. We invite you to join our ➡️ Discord community ⬅️ to engage with the development process, provide feedback, and be the first to test new features. Additionally, any updates or changes to the plugin are seamlessly delivered to all customers directly from our web server, ensuring your system is always up-to-date without the need for manual downloads.   Your game, your rules, automated. Join us in redefining server management in Lineage 2 and elevate your gaming community with unmatched automation capabilities. For more details, contact us directly to get started with L2 Control Hub.   Currently, the plugin is developed using aCis sources. We will continue with these sources until we finalize all the necessary details before proceeding to integrate with the more prominent sources available.       The L2 Control Hub is designed to extend beyond mere functional additions to your server. We are in the process of implementing a suite of advanced mechanisms, such as a vote manager capable of interfacing with any Lineage 2 voting site without requiring configuration, live statistics to provide admins with real-time insights, and an event engine that can generate any desired event within seconds. All these features will be seamlessly integrated into the module, enhancing your server management experience significantly.     Please note that L2 Control Hub will be a premium tool, reflecting the extensive features and benefits it offers. While we are finalizing the pricing structure, rest assured that we aim to deliver great value for your investment. We will announce the cost details soon on our platforms to ensure everyone is well-informed and can plan accordingly. Join us to take your server management to the next level with L2 Control Hub.     
    • The link soucer and system are off, reup please, thanks very much @HypeH
    • DISCORD : utchiha_market telegram : https://t.me/utchiha_market SELLIX STORE : https://utchihamkt.mysellix.io/ Join our server for more products : https://discord.gg/hood-services https://campsite.bio/utchihaamkt  
    • Hola, Busco proveedores de adena en Reborn signature. Trabajo serio, Web de ventas Seria. Adena-Shop. Discord: susi007317   Hello, I am looking for adena suppliers in Reborn signature. Serious work, Serious sales Web. Adena-Shop. Discord: susi007317
  • Topics

×
×
  • Create New...