Rendering Backend

The backend layer provides abstract interfaces for drawing primitives (text, shapes, images, pages) and a concrete implementation using libharu for PDF output.

IDocraftRenderingBackend

Aggregated interface inheriting all sub-backend interfaces.

class IDocraftRenderingBackend : public docraft::backend::IDocraftTextRenderingBackend, public docraft::backend::IDocraftShapeRenderingBackend, public docraft::backend::IDocraftImageRenderingBackend, public docraft::backend::IDocraftPageRenderingBackend

Aggregated rendering backend interface.

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

Public Functions

~IDocraftRenderingBackend() override = default

Virtual destructor.

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”).

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.

virtual void set_document_metadata(const DocraftDocumentMetadata &metadata) = 0

Applies document metadata to the backend document.

Parameters:

metadata – Metadata values to apply.

Sub-backend Interfaces

IDocraftTextRenderingBackend

class IDocraftTextRenderingBackend : public virtual docraft::backend::IDocraftLineRenderingBackend

Interface for text rendering backends used by Docraft.

Subclassed by docraft::backend::IDocraftRenderingBackend

Public Functions

~IDocraftTextRenderingBackend() override = 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 : public virtual docraft::backend::IDocraftLineRenderingBackend

Interface for shape rendering backends used by Docraft.

Subclassed by docraft::backend::IDocraftRenderingBackend

Public Functions

~IDocraftShapeRenderingBackend() override = 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::IDocraftShapeRenderingBackend, docraft::backend::IDocraftTextRenderingBackend

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::IDocraftRenderingBackend

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::IDocraftRenderingBackend

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.

Concrete Backends

DocraftHaruBackend

PDF backend implementation using libharu.

class DocraftHaruBackend : public docraft::backend::IDocraftRenderingBackend

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 void begin_text() const override

Begins a text object.

virtual void end_text() const override

Ends a text object.

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

Draws text at the given coordinates.

virtual void set_text_color(float r, float g, float b) const override

Sets text fill color.

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 override

Draws text with a custom transformation matrix.

virtual float measure_text_width(const std::string &text) const override

Measures text width using current font settings.

virtual void set_stroke_color(float r, float g, float b) const override

Sets stroke color for lines and shapes.

virtual void set_line_width(float thickness) const override

Sets line width in points.

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

Draws a line between two points.

virtual void save_state() const override

Saves the current graphics state.

virtual void restore_state() const override

Restores the previous graphics state.

virtual void set_fill_color(float r, float g, float b) const override

Sets fill color for shapes.

virtual void set_fill_alpha(float alpha) const override

Sets fill alpha for shapes.

virtual void set_stroke_alpha(float alpha) const override

Sets stroke alpha for shapes.

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

Adds a rectangle path.

virtual void draw_circle(float center_x, float center_y, float radius) const override

Adds a circle path.

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

Adds a polygon path.

virtual void fill() const override

Fills the current path.

virtual void stroke() const override

Strokes the current path.

virtual void fill_stroke() const override

Fills and strokes the current path.

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

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 override

Draws a PNG image from memory.

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

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 override

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 override

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 override

Draws a raw RGB image from memory.

virtual void save_to_file(const std::string &path) const override

Saves the document to a file path.

Parameters:

path – Output file path.

virtual std::string file_extension() const override

Returns the preferred file extension for this backend.

Returns:

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

virtual const char *register_ttf_font_from_file(const std::string &path, bool embed) const override

Registers a TTF font and returns the internal name.

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

Returns whether the backend can use a font with the given encoder.

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

Sets the current font and size.

virtual void set_document_metadata(const DocraftDocumentMetadata &metadata) override

Applies document metadata to the PDF info dictionary.

virtual float page_width() const override

Returns the current page width in points.

virtual float page_height() const override

Returns the current page height in points.

virtual void add_new_page() override

Adds a new page to the document and makes it the current page.

virtual void move_to_next_page() override

Moves the cursor to the next page if it exists.

Throws:

std::runtime_error – if already at the last page.

virtual void go_to_page(std::size_t page_number) override

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

Parameters:

page_number – Destination page index.

Throws:

std::runtime_error – if the page number is out of range.

virtual void go_to_first_page() override

Navigates to the first page.

virtual void go_to_previous_page() override

Navigates to the previous page.

Throws:

std::runtime_error – if already at the first page.

virtual void go_to_last_page() override

Navigates to the last page.

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

Sets the page size and orientation.

virtual std::size_t current_page_number() const override

Returns the current page number (1-based index).

virtual std::size_t total_page_count() const override

Returns the total number of pages in the document.