Files
pdf/engine/include/pdfengine/display_list.hpp
T

156 lines
4.7 KiB
C++
Raw Normal View History

2026-05-22 15:49:10 +05:30
#pragma once
#include <vector>
#include <memory>
#include <string>
#include <pdfengine/graphics_state.hpp>
2026-06-11 11:24:35 +05:30
#include <pdfengine/path.hpp>
#include <pdfengine/image.hpp>
2026-08-01 16:19:11 +05:30
#include <pdfengine/image_object.hpp>
2026-05-22 15:49:10 +05:30
namespace pdfengine {
class CommandVisitor;
struct Command {
virtual ~Command() = default;
virtual void accept(CommandVisitor& visitor) const = 0;
};
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;
2026-07-23 11:31:26 +05:30
FillRectCommand(float _x, float _y, float w, float h) : x(_x), y(_y), width(w), height(h) {}
2026-05-22 15:49:10 +05:30
void accept(CommandVisitor& visitor) const override;
};
struct DrawTextCommand : public Command {
std::string text;
float x, y;
2026-07-23 11:31:26 +05:30
DrawTextCommand(std::string _text, float _x, float _y) : text(std::move(_text)), x(_x), y(_y) {}
2026-05-22 15:49:10 +05:30
void accept(CommandVisitor& visitor) const override;
};
2026-06-11 11:24:35 +05:30
struct FillPathCommand : public Command {
Path path;
FillRule rule;
2026-07-23 11:31:26 +05:30
explicit FillPathCommand(Path _path, FillRule _rule = FillRule::NonZero) : path(std::move(_path)), rule(_rule) {}
2026-06-11 11:24:35 +05:30
void accept(CommandVisitor& visitor) const override;
};
struct StrokePathCommand : public Command {
Path path;
2026-07-23 11:31:26 +05:30
explicit StrokePathCommand(Path _path) : path(std::move(_path)) {}
2026-06-11 11:24:35 +05:30
void accept(CommandVisitor& visitor) const override;
};
struct FillStrokePathCommand : public Command {
Path path;
FillRule rule;
2026-07-23 11:31:26 +05:30
explicit FillStrokePathCommand(Path _path, FillRule _rule = FillRule::NonZero) : path(std::move(_path)), rule(_rule) {}
void accept(CommandVisitor& visitor) const override;
};
2026-08-01 16:19:11 +05:30
enum class BlendMode {
Normal,
Multiply,
Screen,
Overlay,
Darken,
Lighten,
ColorBurn,
SoftLight,
Difference
};
enum class RenderingIntent {
RelativeColorimetric,
Perceptual,
AbsoluteColorimetric,
Saturation
};
2026-06-11 11:24:35 +05:30
struct DrawImageCommand : public Command {
2026-08-01 16:19:11 +05:30
std::shared_ptr<const ImageObject> imageObject;
2026-06-11 11:24:35 +05:30
ImageInfo image;
2026-06-18 16:14:29 +05:30
Matrix matrix;
2026-08-01 16:19:11 +05:30
float opacity = 1.0f;
BlendMode blendMode = BlendMode::Normal;
RenderingIntent renderingIntent = RenderingIntent::RelativeColorimetric;
bool interpolate = false;
2026-06-18 16:14:29 +05:30
2026-08-01 16:19:11 +05:30
DrawImageCommand(std::shared_ptr<const ImageObject> imgObj, Matrix m, float op = 1.0f,
BlendMode blend = BlendMode::Normal, bool interp = false)
: imageObject(std::move(imgObj)), matrix(m), opacity(op), blendMode(blend), interpolate(interp) {
if (imageObject) {
image.width = imageObject->geometry().width;
image.height = imageObject->geometry().height;
image.channels = imageObject->pixels().channels();
image.pixelData = imageObject->pixels().vector();
}
}
DrawImageCommand(ImageInfo img, Matrix m, float op = 1.0f)
2026-06-18 16:14:29 +05:30
: image(std::move(img)), matrix(m), opacity(op) {}
2026-08-01 16:19:11 +05:30
2026-06-11 11:24:35 +05:30
void accept(CommandVisitor& visitor) const override;
};
2026-06-18 16:14:29 +05:30
2026-05-22 15:49:10 +05:30
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;
2026-06-11 11:24:35 +05:30
virtual void visit(const FillPathCommand& cmd) = 0;
virtual void visit(const StrokePathCommand& cmd) = 0;
virtual void visit(const FillStrokePathCommand& cmd) = 0;
2026-06-11 11:24:35 +05:30
virtual void visit(const DrawImageCommand& cmd) = 0;
2026-05-22 15:49:10 +05:30
};
class DisplayList {
public:
DisplayList() = default;
void addCommand(std::unique_ptr<Command> cmd);
void replay(CommandVisitor& visitor) const;
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);
void fillPath(const Path& path, FillRule rule = FillRule::NonZero);
2026-06-11 11:24:35 +05:30
void strokePath(const Path& path);
void fillStrokePath(const Path& path, FillRule rule = FillRule::NonZero);
2026-06-18 16:14:29 +05:30
void drawImage(const ImageInfo& image, const Matrix& m, float opacity = 1.0f);
2026-08-01 16:19:11 +05:30
void drawImage(std::shared_ptr<const ImageObject> image, const Matrix& m, float opacity = 1.0f, BlendMode blend = BlendMode::Normal);
2026-05-22 15:49:10 +05:30
[[nodiscard]] size_t size() const noexcept { return m_commands.size(); }
void clear() { m_commands.clear(); }
private:
std::vector<std::unique_ptr<Command>> m_commands;
};
2026-06-22 15:18:47 +05:30
}