BLEND 2D

next generation 2d vector graphics engine

About Blend2D

Blend2D is a next generation 2D vector graphics engine written in C++. It has been engineered from scratch to provide maximum performance of software based 2D rendering. It features a built-in JIT compiler that generates efficient pipelines at runtime and a completely new rasterizer that is both efficient and provides high quality anti-aliasing. The future of Blend2D is to offer asynchronous and multithreaded rendering for maximum performance.

Easy to Use C and C++ API

Blend2D is written in C++, but provides both C and C++ APIs. Public functions use only C interface so each feature of the library is directly available to both C and C++ users. The primary goal of C API is to make it possible to use Blend2D from programming languages that can interface with C either natively or through FFI. The primary goal of C++ API is to make the library easy-to-use in C++ projects without the need of manual resource management. The C++ API is built on top of the C API and turns all objects that require initialization and finalization into smart objects that handle it automatically. The C++ API should be considered zero-cost and it even allows to interchange C and C++ objects if that's what is needed.

The following snippets provide an insight into the C and C++ APIs provided by Blend2D. Please note that both snippets were written to be comparable line-by-line and designed to be as narrow as possible.

C API

#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 = blImageInitCreate(img, 256, 256, BL_FORMAT_PRGB32);
  if (r != BL_SUCCESS) return;

  r = blContextInitCreate(&ctx, img, NULL);
  if (r != BL_SUCCESS) return;

  BLLinearGradientValues values = { 0, 0, 256, 256 };
  blGradientInitCreate(&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);
}

C++ API

#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.
}

High Quality Rasterization

Blend2D features a brand new rasterizer that was written from scratch to deliver superior performance. It's an analytic high-quality rasterizer, which is comparable to rasterizers used by AGG and FreeType. It's more efficient as it uses an innovative approach to index data that it builds during rasterization and that it scans during composition. It has a stable memory footprint and performs rasterization and composition in bands instead of using a typical scanline-based approach as used by most 2D libraries. The rasterizer is robust and excels in rendering text and complex vector art.

Paint Styles

Blend2D provides similar paint styles as defined by SVG and HTML <canvas>, which include solid colors, gradients, and patterns. It uses a slot-based approach that allows to specify both fill and stroke styles separately. Gradients and patterns also support extend modes so it's possible to specify whether they should be padded, repeated, or reflected. Extend modes of patterns can be configured independently of X and Y.

Composition & Blending

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.

Beta Release

After a successful alpha testing Blend2D will enter beta phase soon. At the moment Blend2D is undergoing a transition from C++ API to C API to make it possible to use the library from C and other programming languages (as initially the library was C++ only). This was an important decision that caused postponing the release date by around 2 months, but as a result Blend2D will get a very stable API.

Beta release will be the first open-source version of Blend2D released under ZLIB license.