Rendering Backend

The backend layer defines the low-level contracts used by Docraft to render documents. It is organized around small capability interfaces for drawing text, shapes, lines, images, and pages, plus provider interfaces that group related capabilities.

Concrete backends, such as the libharu-based PDF backend, implement these contracts and expose them through capability providers.

Capability Providers

Capability providers group backend functionality by responsibility. They allow Docraft services to request only the capabilities they need while keeping the individual rendering interfaces independent and focused.

IDocraftRenderingCapabilityProvider

Provides access to rendering capabilities for lines, text, shapes, images, and pages.

class IDocraftRenderingCapabilityProvider

Rendering capability contract (geometry, text, images, pages).

Subclassed by docraft::backend::pdf::DocraftHaruBackend

Public Functions

virtual ~IDocraftRenderingCapabilityProvider() = default
virtual const IDocraftLineRenderingBackend *line_rendering() const = 0
virtual IDocraftLineRenderingBackend *edit_line_rendering() = 0
virtual const IDocraftTextRenderingBackend *text_rendering() const = 0
virtual IDocraftTextRenderingBackend *edit_text_rendering() = 0
virtual const IDocraftShapeRenderingBackend *shape_rendering() const = 0
virtual IDocraftShapeRenderingBackend *edit_shape_rendering() = 0
virtual const IDocraftImageRenderingBackend *image_rendering() const = 0
virtual IDocraftImageRenderingBackend *edit_image_rendering() = 0
virtual const IDocraftPageRenderingBackend *page_rendering() const = 0
virtual IDocraftPageRenderingBackend *edit_page_rendering() = 0

IDocraftResourceCapabilityProvider

Provides access to resource-related capabilities, such as font handling.

class IDocraftResourceCapabilityProvider

Resource capability contract (fonts and related resources).

Subclassed by docraft::backend::pdf::DocraftHaruBackend

Public Functions

virtual ~IDocraftResourceCapabilityProvider() = default
virtual const IDocraftFontBackend *font_backend() const = 0
virtual IDocraftFontBackend *edit_font_backend() = 0

IDocraftLifecycleCapabilityProvider

Provides access to document lifecycle capabilities, such as output persistence and metadata handling.

class IDocraftLifecycleCapabilityProvider

Document lifecycle capability contract (metadata and persistence).

Subclassed by docraft::backend::pdf::DocraftHaruBackend

Public Functions

virtual ~IDocraftLifecycleCapabilityProvider() = default
virtual const IDocraftOutputBackend *output_backend() const = 0
virtual IDocraftOutputBackend *edit_output_backend() = 0
virtual const IDocraftMetadataBackend *metadata_backend() const = 0
virtual IDocraftMetadataBackend *edit_metadata_backend() = 0

Rendering Capability Interfaces

Rendering capability interfaces are standalone and chain-free. Each interface covers one drawing responsibility.

IDocraftTextRenderingBackend

class IDocraftTextRenderingBackend

Interface for text rendering backends used by Docraft.

Subclassed by docraft::backend::pdf::DocraftHaruTextBackend

Public Functions

virtual ~IDocraftTextRenderingBackend() = default

Virtual destructor.

virtual void begin_text() const = 0

Initializes the text rendering context.

Must be called before any text drawing operations.

virtual void end_text() const = 0

Finalizes the text rendering context.

Must be called after all text drawing operations are completed.

virtual void draw_text(const std::string &text, float x, float y) const = 0

Draws the specified text at the given coordinates on the PDF page.

Parameters:
  • text – The text to be drawn.

  • x – The x-coordinate where the text should start.

  • y – The y-coordinate where the text should start.

virtual void set_text_color(float r, float g, float b) const = 0

Sets the fill color used for subsequent text drawing.

Parameters:
  • r – Red component in [0,1].

  • g – Green component in [0,1].

  • b – Blue component in [0,1].

virtual void draw_text_matrix(const std::string &text, float scale_x, float skew_x, float skew_y, float scale_y, float translate_x, float translate_y) const = 0

Draws the specified text using a custom transformation matrix.

Parameters:
  • text – The text to be drawn.

  • scale_x – The horizontal scaling factor.

  • skew_x – The horizontal skewing factor.(skew=tan(angle),represents the tangent of the skew angle)

  • skew_y – The vertical skewing factor.(skew=tan(angle),represents the tangent of the skew angle)

  • scale_y – The vertical scaling factor.

  • translate_x – The horizontal translation (movement) in points.

  • translate_y – The vertical translation (movement) in points.

virtual float measure_text_width(const std::string &text) const = 0

Measures the width of the specified text using the current font settings.

Parameters:

text – The text to be measured.

Returns:

The width of the text in points.

IDocraftShapeRenderingBackend

class IDocraftShapeRenderingBackend

Interface for shape rendering backends used by Docraft.

Subclassed by docraft::backend::pdf::DocraftHaruShapeBackend

Public Functions

virtual ~IDocraftShapeRenderingBackend() = default

Virtual destructor.

virtual void save_state() const = 0

Saves the current graphics state.

virtual void restore_state() const = 0

Restores the previous graphics state.

virtual void set_fill_color(float r, float g, float b) const = 0

Sets the fill color used for subsequent fills.

Parameters:
  • r – Red component in [0,1].

  • g – Green component in [0,1].

  • b – Blue component in [0,1].

virtual void set_fill_alpha(float alpha) const = 0

Sets the alpha used for subsequent fills.

Parameters:

alpha – Alpha in [0,1].

virtual void set_stroke_alpha(float alpha) const = 0

Sets the alpha used for subsequent strokes.

Parameters:

alpha – Alpha in [0,1].

virtual void draw_rectangle(float x, float y, float width, float height) const = 0

Defines a rectangle path with the given dimensions.

Parameters:
  • x – The x-coordinate of the bottom-left corner.

  • y – The y-coordinate of the bottom-left corner.

  • width – The rectangle width.

  • height – The rectangle height.

virtual void draw_circle(float center_x, float center_y, float radius) const = 0

Defines a circle path with the given center and radius.

Parameters:
  • center_x – The x-coordinate of the center.

  • center_y – The y-coordinate of the center.

  • radius – The circle radius.

virtual void draw_polygon(const std::vector<model::DocraftPoint> &points) const = 0

Defines a polygon path with the given points.

Parameters:

points – Polygon points in document coordinates.

virtual void fill() const = 0

Fills the current path.

virtual void stroke() const = 0

Strokes the current path.

virtual void fill_stroke() const = 0

Fills and strokes the current path.

IDocraftLineRenderingBackend

class IDocraftLineRenderingBackend

Interface for line rendering backends used by Docraft.

Subclassed by docraft::backend::pdf::DocraftHaruLineBackend

Public Functions

virtual ~IDocraftLineRenderingBackend() = default

Virtual destructor.

virtual void set_stroke_color(float r, float g, float b) const = 0

Sets the stroke color used for subsequent line drawing.

Parameters:
  • r – Red component in [0,1].

  • g – Green component in [0,1].

  • b – Blue component in [0,1].

virtual void set_line_width(float thickness) const = 0

Sets the line width used for subsequent line drawing.

Parameters:

thickness – Line width in points.

virtual void draw_line(float x1, float y1, float x2, float y2) const = 0

Draws a line between two points using the current stroke settings.

Parameters:
  • x1 – The x-coordinate of the line start.

  • y1 – The y-coordinate of the line start.

  • x2 – The x-coordinate of the line end.

  • y2 – The y-coordinate of the line end.

IDocraftImageRenderingBackend

class IDocraftImageRenderingBackend

Interface for image rendering backends used by Docraft.

Subclassed by docraft::backend::pdf::DocraftHaruImageBackend

Public Functions

virtual ~IDocraftImageRenderingBackend() = default

Virtual destructor.

virtual void draw_png_image(const std::string &path, float x, float y, float width, float height) const = 0

Draws a PNG image from file.

virtual void draw_png_image_from_memory(const unsigned char *data, std::size_t size, float x, float y, float width, float height) const = 0

Draws a PNG image from memory.

virtual void draw_jpeg_image(const std::string &path, float x, float y, float width, float height) const = 0

Draws a JPEG image from file.

virtual void draw_jpeg_image_from_memory(const unsigned char *data, std::size_t size, float x, float y, float width, float height) const = 0

Draws a JPEG image from memory.

virtual void draw_raw_rgb_image(const std::string &path, int pixel_width, int pixel_height, float x, float y, float width, float height) const = 0

Draws a raw RGB image from file.

virtual void draw_raw_rgb_image_from_memory(const unsigned char *data, int pixel_width, int pixel_height, float x, float y, float width, float height) const = 0

Draws a raw RGB image from memory.

IDocraftPageRenderingBackend

class IDocraftPageRenderingBackend

Interface for page-level operations and page metadata.

Subclassed by docraft::backend::pdf::DocraftHaruPageBackend

Public Functions

virtual ~IDocraftPageRenderingBackend() = default

Virtual destructor.

virtual float page_width() const = 0

Returns the current page width in points.

Returns:

Page width in points.

virtual float page_height() const = 0

Returns the current page height in points.

Returns:

Page height in points.

virtual void add_new_page() = 0

Adds a new page to the document.

virtual void move_to_next_page() = 0

Moves the cursor to the next page.

virtual void go_to_page(std::size_t page_number) = 0

Navigates to a specific page (0-based index).

Parameters:

page_number – Destination page index.

virtual void go_to_first_page() = 0

Navigates to the first page.

virtual void go_to_previous_page() = 0

Navigates to the previous page.

virtual void go_to_last_page() = 0

Navigates to the last page.

virtual void set_page_format(model::DocraftPageSize size, model::DocraftPageOrientation orientation) = 0

Sets the page size and orientation.

Parameters:
  • size – Page size.

  • orientation – Page orientation.

virtual std::size_t current_page_number() const = 0

Returns the current page number.

Returns:

Current page number.

virtual std::size_t total_page_count() const = 0

Returns the total number of pages in the document.

Returns:

Total page count.

Resource and Lifecycle Interfaces

These interfaces define supporting backend responsibilities used by document rendering and export.

IDocraftFontBackend

class IDocraftFontBackend

Capability interface for backend font registration and selection.

Subclassed by docraft::backend::pdf::DocraftHaruFontBackend

Public Functions

virtual ~IDocraftFontBackend() = default
virtual const char *register_ttf_font_from_file(const std::string &path, bool embed) const = 0

Registers a TTF font and returns the internal name.

Parameters:
  • path – Font file path.

  • embed – Whether to embed the font in the document.

Returns:

Backend internal font name.

virtual bool can_use_font(const std::string &internal_name, const char *encoder) const = 0

Checks whether a font can be used with the given encoder.

Parameters:
  • internal_name – Backend internal font name.

  • encoder – Backend encoder name.

Returns:

true if the font can be used.

virtual void set_font(const std::string &internal_name, float size, const char *encoder) const = 0

Sets the current font and size.

Parameters:
  • internal_name – Backend internal font name.

  • size – Font size in points.

  • encoder – Backend encoder name.

IDocraftOutputBackend

class IDocraftOutputBackend

Capability interface for document output operations.

Subclassed by docraft::backend::pdf::DocraftHaruOutputBackend

Public Functions

virtual ~IDocraftOutputBackend() = default
virtual void save_to_file(const std::string &path) const = 0

Saves the document to a file path.

Parameters:

path – Output file path.

virtual std::string file_extension() const = 0

Returns the preferred file extension for this backend.

Returns:

Extension with or without leading dot (e.g. “.pdf” or “pdf”).

IDocraftMetadataBackend

class IDocraftMetadataBackend

Capability interface for document metadata support.

Subclassed by docraft::backend::pdf::DocraftHaruMetadataBackend

Public Functions

virtual ~IDocraftMetadataBackend() = default
virtual void set_document_metadata(const DocraftDocumentMetadata &metadata) = 0

Applies document metadata to the backend document.

Parameters:

metadata – Metadata values to apply.

Provider Factories

Factories create the provider set used by the rendering service.

DocraftCapabilityProviders

struct DocraftCapabilityProviders

Public Members

std::shared_ptr<IDocraftRenderingCapabilityProvider> rendering_provider
std::shared_ptr<IDocraftResourceCapabilityProvider> resource_provider
std::shared_ptr<IDocraftLifecycleCapabilityProvider> lifecycle_provider

IDocraftCapabilityProvidersFactory

class IDocraftCapabilityProvidersFactory

Subclassed by docraft::backend::pdf::DocraftHaruCapabilityProvidersFactory

Public Functions

virtual ~IDocraftCapabilityProvidersFactory() = default
virtual DocraftCapabilityProviders create_capability_providers() const = 0

Concrete Backends

DocraftHaruBackend

PDF backend implementation using libharu. It composes capability-focused internal objects and exposes them through the backend provider model.

class DocraftHaruBackend : public docraft::backend::IDocraftRenderingCapabilityProvider, public docraft::backend::IDocraftResourceCapabilityProvider, public docraft::backend::IDocraftLifecycleCapabilityProvider

This class is responsible for managing the Haru PDF document and providing an interface for rendering operations.

Public Functions

DocraftHaruBackend()

Creates a Haru PDF backend with a new document and page.

~DocraftHaruBackend() override

Releases Haru resources.

virtual const docraft::backend::IDocraftLineRenderingBackend *line_rendering() const override
virtual docraft::backend::IDocraftLineRenderingBackend *edit_line_rendering() override
virtual const docraft::backend::IDocraftTextRenderingBackend *text_rendering() const override
virtual docraft::backend::IDocraftTextRenderingBackend *edit_text_rendering() override
virtual const docraft::backend::IDocraftShapeRenderingBackend *shape_rendering() const override
virtual docraft::backend::IDocraftShapeRenderingBackend *edit_shape_rendering() override
virtual const docraft::backend::IDocraftImageRenderingBackend *image_rendering() const override
virtual docraft::backend::IDocraftImageRenderingBackend *edit_image_rendering() override
virtual const docraft::backend::IDocraftPageRenderingBackend *page_rendering() const override
virtual docraft::backend::IDocraftPageRenderingBackend *edit_page_rendering() override
virtual const docraft::backend::IDocraftOutputBackend *output_backend() const override
virtual docraft::backend::IDocraftOutputBackend *edit_output_backend() override
virtual const docraft::backend::IDocraftFontBackend *font_backend() const override
virtual docraft::backend::IDocraftFontBackend *edit_font_backend() override
virtual const docraft::backend::IDocraftMetadataBackend *metadata_backend() const override
virtual docraft::backend::IDocraftMetadataBackend *edit_metadata_backend() override
HPDF_Doc pdf_document() const