High Quality 2D

Blend2D is a high-quality 2D vector graphics engine written in C++. It has a built-in JIT compiler that generates optimized pipelines at runtime. In addition, the engine features a brand new rasterizer that has been written from scratch to deliver superior quality and performance. The quality is comparable to rasterizers used by AGG and FreeType and the performance is much better due to 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.

Easy to Use 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.
}

Rich Styling

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 its beta phase soon. At the moment there is only few remaining tasks to be done before the source code goes public. All public parts of Blend2D were rewritten to offer both C and C++ APIs and this task took a lot of time to plan and execute. At the moment it's almost done and most time is spent on improving text support and vector geometry processing.

Beta release will be the first open-source version of Blend2D released under ZLIB license and is expected to happen at the end of February or beginning of March 2019.