Blend2D


Blend2D is a high performance 2D vector graphics engine written in C++ and released under the Zlib license. It has a built-in JIT compiler that generates optimized pipelines at runtime. Additionally, the engine features a new rasterizer that has been written from scratch. It delivers superior performance while quality is comparable to rasterizers used by AGG and FreeType. The performance has been optimized by using an innovative approach to index data that is built during rasterization and scanned during composition. The rasterizer is robust and excels in rendering complex vector art and text.

Blend2D is written in C++ but it provides both C and C++ APIs. Public functionality is provided through C interface so that each feature of the library is accessible to both C and C++ users. The C API makes it possible to use Blend2D from many programming languages which are able to interface with C (either natively or through FFI). The primary goal of the C++ API is to make the library easy-to-use in C++ projects without the need of managing resources manually. It is built on top of the C API and turns all objects requiring initialization and finalization into smart objects that handle it in their constructors and destructors.

The snippets below provide an insight into the C and C++ APIs provided by Blend2D.

#include <blend2d.h> // Each C API struct that requires a lifetime management
// ends with 'Core'. Such structs must be initialized by
// 'Init()' and finalized by 'Reset()' or by function
// that resets implicitly like 'blContextEnd()'.
void render(BLImageCore* img) { BLResult r; BLContextCore ctx; BLGradientCore gradient; r = blImageInitAs(img, 256, 256, BL_FORMAT_PRGB32); if (r != BL_SUCCESS) return; r = blContextInitAs(&ctx, img, NULL); if (r != BL_SUCCESS) return; BLLinearGradientValues values = { 0, 0, 256, 256 }; blGradientInitAs(&gradient, BL_GRADIENT_TYPE_LINEAR, &values, BL_EXTEND_MODE_PAD, NULL, 0, NULL); blGradientAddStopRgba32(&gradient, 0.0, 0x00000000); blGradientAddStopRgba32(&gradient, 1.0, 0xFFFFFFFF); blContextSetCompOp(&ctx, BL_COMP_OP_SRC_COPY); blContextSetFillStyle(&ctx, &gradient); blContextFillAll(&ctx); blContextEnd(&ctx); // Manual cleanup is necessary in C. blGradientReset(&gradient);
}
#include <blend2d.h> // C++ API is automatically provided in C++ mode. Each
// 'Core' struct has also its C++ counterpart that
// doesn't have the suffix (BLImageCore -> BLImage),
// which provides RAII and acts as a smart object.
void render(BLImage& img) noexcept { BLResult r; BLContext ctx; BLGradient gradient; r = img.create(256, 256, BL_FORMAT_PRGB32); if (r != BL_SUCCESS) return; r = ctx.create(img); if (r != BL_SUCCESS) return; // There are many overloads available in C++ API. gradient.create( BLLinearGradientValues(0, 0, 256, 256), BL_EXTEND_MODE_PAD); gradient.addStop(0.0, BLRgba32(0x00000000)); gradient.addStop(1.0, BLRgba32(0xFFFFFFFF)); ctx.setCompOp(BL_COMP_OP_SRC_COPY); ctx.setFillStyle(gradient); ctx.fillAll(); ctx.end(); // C++ smart objects take care of cleanup // automatically when they go out of scope.
}

More C++ examples and their corresponding outputs can be seen on Getting Started page.

Blend2D offers similar paint styles as defined by SVG and HTML <canvas> including solid colors, gradients, and patterns. The rendering context uses a separate options for styling fill and stroke operations. Gradients and patterns also support extend modes so that it is possible to specify whether they should be padded, repeated, or reflected. Extend modes of patterns can be configured independently of X and Y.

Blend2D supports all Porter & Duff compositing operators and a wide range of blend modes defined by SVG, CSS, and PDF specifications. Composition and blending modes can be applied to any rendering operation including fills, strokes, and image blits.

Blend2D is currently in a beta testing mode, which means that you can download and use the library, but a certain functionality may be limited or not working as expected. Project status and roadmap can be tracked via status page and github issues page. We are interested in your feedback! So give it a try, download Blend2D, and let us know what should be improved.

Want to Know More?

  • About - Information about the history of Blend2D and its authors
  • Performance - Comparision of Blend2D performance with other libraries
  • Support - Contacts, donation options and commercial support possibilities

Documentation

Download & Build