Files
pdf/engine/include/pdfengine/display_list.hpp
T
2026-05-22 15:49:10 +05:30

89 lines
2.5 KiB
C++

#pragma once
#include <vector>
#include <memory>
#include <string>
#include <pdfengine/graphics_state.hpp>
namespace pdfengine {
class CommandVisitor;
// Base class for all drawing commands
struct Command {
virtual ~Command() = default;
virtual void accept(CommandVisitor& visitor) const = 0;
};
// --- Specific Command Types ---
struct SaveStateCommand : public Command {
void accept(CommandVisitor& visitor) const override;
};
struct RestoreStateCommand : public Command {
void accept(CommandVisitor& visitor) const override;
};
struct SetTransformCommand : public Command {
Matrix matrix;
explicit SetTransformCommand(const Matrix& m) : matrix(m) {}
void accept(CommandVisitor& visitor) const override;
};
struct FillRectCommand : public Command {
float x, y, width, height;
FillRectCommand(float x, float y, float w, float h) : x(x), y(y), width(w), height(h) {}
void accept(CommandVisitor& visitor) const override;
};
struct DrawTextCommand : public Command {
std::string text;
float x, y;
// We would eventually have a font reference here too
DrawTextCommand(std::string text, float x, float y) : text(std::move(text)), x(x), y(y) {}
void accept(CommandVisitor& visitor) const override;
};
// --- Visitor Interface ---
// The visitor interface that the renderer (or replay engine) implements
class CommandVisitor {
public:
virtual ~CommandVisitor() = default;
virtual void visit(const SaveStateCommand& cmd) = 0;
virtual void visit(const RestoreStateCommand& cmd) = 0;
virtual void visit(const SetTransformCommand& cmd) = 0;
virtual void visit(const FillRectCommand& cmd) = 0;
virtual void visit(const DrawTextCommand& cmd) = 0;
};
// --- Display List Container ---
// A container that stores a sequence of drawing commands.
class DisplayList {
public:
DisplayList() = default;
// Add commands directly
void addCommand(std::unique_ptr<Command> cmd);
// Replay the commands to a visitor (renderer)
void replay(CommandVisitor& visitor) const;
// Helper methods to easily append common commands
void saveState();
void restoreState();
void setTransform(const Matrix& m);
void fillRect(float x, float y, float w, float h);
void drawText(const std::string& text, float x, float y);
[[nodiscard]] size_t size() const noexcept { return m_commands.size(); }
void clear() { m_commands.clear(); }
private:
std::vector<std::unique_ptr<Command>> m_commands;
};
} // namespace pdfengine