stuff-from-scratch/test/test_utils/TestRenderUtils.h

71 lines
1.3 KiB
C
Raw Normal View History

2023-01-12 10:58:43 +00:00
#pragma once
#include "DrawingSurface.h"
#include "DrawingContext.h"
#include "AbstractPainter.h"
2023-01-12 17:45:06 +00:00
#include "Scene.h"
2023-01-17 17:41:27 +00:00
#include "SvgPainter.h"
2023-01-12 17:45:06 +00:00
#include "SvgWriter.h"
#include "SvgDocument.h"
2023-01-12 10:58:43 +00:00
#include "Image.h"
#include "PngWriter.h"
2023-01-12 17:45:06 +00:00
#include "FontsManager.h"
2023-01-12 17:45:06 +00:00
#include "File.h"
2023-01-12 10:58:43 +00:00
class TestRenderer
{
public:
TestRenderer(unsigned width = 1000, unsigned height = 1000)
{
mFontsManager = std::make_unique<FontsManager>();
2023-01-12 10:58:43 +00:00
mSurface = std::make_unique<DrawingSurface>();
mSurface->setSize(width, height);
mDrawingContext = std::make_unique<DrawingContext>(mSurface.get());
getScene()->setFontsManager(mFontsManager.get());
2023-01-12 10:58:43 +00:00
}
Scene* getScene() const
{
return mSurface->getScene();
}
void write(const Path& path)
{
mDrawingContext->paint();
auto image = mSurface->getImage();
PngWriter writer;
2023-01-16 11:56:46 +00:00
writer.write(path, image);
2023-01-12 10:58:43 +00:00
}
2023-01-12 17:45:06 +00:00
void writeSvg(const Path& path)
2023-01-17 08:34:48 +00:00
{
writeSvg(path, mSurface->getScene());
}
static void writeSvg(const Path& path, Scene* scene)
2023-01-12 17:45:06 +00:00
{
2023-01-17 17:41:27 +00:00
SvgPainter painter;
auto svg_document = painter.paint(scene);
2023-01-12 17:45:06 +00:00
SvgWriter writer;
auto svg_content = writer.toString(svg_document.get());
File svg_file(path);
svg_file.writeText(svg_content);
}
2023-01-12 10:58:43 +00:00
private:
std::unique_ptr<DrawingSurface> mSurface;
std::unique_ptr<DrawingContext> mDrawingContext;
std::unique_ptr<FontsManager> mFontsManager;
2023-01-12 10:58:43 +00:00
};