odin-blend2d

Odin bindings to Blend2D
Log | Files | Refs | README | LICENSE

bl_sample_8.cpp (1252B)


      1 #include <blend2d.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 int main() {
      6   BLImage img(480, 480, BL_FORMAT_PRGB32);
      7   BLContext ctx(img);
      8 
      9   const char font_name[] = "ABeeZee-Regular.ttf";
     10   const char* str = "Hello Blend2D!\n"
     11                     "I'm a simple multiline text example\n"
     12                     "that uses GlyphBuffer and GlyphRun!";
     13   BLRgba32 color(0xFFFFFFFFu);
     14 
     15   BLFontFace face;
     16   BLResult result = face.create_from_file(font_name);
     17   if (result != BL_SUCCESS) {
     18     printf("Failed to load a face (err=%u)\n", result);
     19     return 1;
     20   }
     21 
     22   BLFont font;
     23   font.create_from_face(face, 20.0f);
     24 
     25   BLGlyphBuffer gb;
     26   BLTextMetrics tm;
     27   BLFontMetrics fm = font.metrics();
     28   double y = 190.0 + double(fm.ascent);
     29 
     30   ctx.clear_all();
     31   do {
     32     const char* nl = strchr(str, '\n');
     33     gb.set_utf8_text(str, nl ? (size_t)(nl - str) : SIZE_MAX);
     34     font.shape(gb);
     35     font.get_text_metrics(gb, tm);
     36 
     37     double x = (tm.bounding_box.x1 - tm.bounding_box.x0);
     38     ctx.fill_glyph_run(BLPoint((480.0 - x) / 2, y),
     39                        font, gb.glyph_run(), color);
     40 
     41     y += double(fm.ascent + fm.descent + fm.line_gap);
     42     str = nl ? nl + 1 : nullptr;
     43   } while (str);
     44   ctx.end();
     45 
     46   img.write_to_file("bl_sample_8.png");
     47   return 0;
     48 }