Add offscreen text rendering.

This commit is contained in:
jmsgrogan 2023-01-12 10:58:43 +00:00
parent c63138c455
commit 076e32b1d6
17 changed files with 156 additions and 101 deletions

View file

@ -35,17 +35,26 @@ ID2D1DeviceContext2* DirectX2dInterface::getContext() const
return mDeviceContext.Get(); return mDeviceContext.Get();
} }
void DirectX2dInterface::initializeStandalone(IWICBitmap* bitmap, bool useSoftware) ID2D1RenderTarget* DirectX2dInterface::getRenderTarget() const
{
if (mRenderTarget)
{
return mRenderTarget.Get();
}
else
{
return mDeviceContext.Get();
}
}
void DirectX2dInterface::initialize(IWICBitmap* bitmap, bool useSoftware)
{ {
D2D1_FACTORY_OPTIONS d2dFactoryOptions = {}; D2D1_FACTORY_OPTIONS d2dFactoryOptions = {};
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory3), &d2dFactoryOptions, &mFactory); D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory3), &d2dFactoryOptions, &mFactory);
auto hr = mFactory->CreateWicBitmapRenderTarget(bitmap, D2D1::RenderTargetProperties(), &mRenderTarget); auto hr = mFactory->CreateWicBitmapRenderTarget(bitmap, D2D1::RenderTargetProperties(), &mRenderTarget);
}
ID2D1RenderTarget* DirectX2dInterface::getRenderTarget() const initializeDirectWrite();
{
return mRenderTarget.Get();
} }
bool DirectX2dInterface::initialize(IDXGIDevice* device) bool DirectX2dInterface::initialize(IDXGIDevice* device)

View file

@ -25,7 +25,7 @@ public:
ID2D1RenderTarget* getRenderTarget() const; ID2D1RenderTarget* getRenderTarget() const;
bool initialize(IDXGIDevice* device); bool initialize(IDXGIDevice* device);
void initializeStandalone(IWICBitmap* bitmap, bool useSoftware = true); void initialize(IWICBitmap* bitmap, bool useSoftware = true);
bool isValid() const; bool isValid() const;

View file

@ -17,7 +17,8 @@ void DirectX2dPainter::finishDrawing()
D2D1::ColorF DirectX2dPainter::toD2dColor(const Color& color) D2D1::ColorF DirectX2dPainter::toD2dColor(const Color& color)
{ {
return D2D1::ColorF(color.getR() / 255.0, color.getG() / 255.0, color.getB() / 255.0, static_cast<float>(color.getAlpha())); return D2D1::ColorF(static_cast<float>(color.getR() / 255.0), static_cast<float>(color.getG() / 255.0),
static_cast<float>(color.getB() / 255.0), static_cast<float>(color.getAlpha()));
} }
void DirectX2dPainter::clearBackground(const Color& color) void DirectX2dPainter::clearBackground(const Color& color)

View file

@ -28,8 +28,6 @@
#include <windows.h> #include <windows.h>
#include <iostream>
DirectXPainter::DirectXPainter(DrawingContext* context) DirectXPainter::DirectXPainter(DrawingContext* context)
: AbstractPainter(context), : AbstractPainter(context),
mMeshPainter(std::make_unique<DirectXMeshPainter>()), mMeshPainter(std::make_unique<DirectXMeshPainter>()),
@ -44,11 +42,6 @@ DirectXMeshPainter* DirectXPainter::getMeshPainter() const
return mMeshPainter.get(); return mMeshPainter.get();
} }
DirectXTextPainter* DirectXPainter::getTextPainter() const
{
return mTextPainter.get();
}
void DirectXPainter::setDxInterface(DirectXInterface* dxInterface) void DirectXPainter::setDxInterface(DirectXInterface* dxInterface)
{ {
mRawDxInterface = dxInterface; mRawDxInterface = dxInterface;
@ -72,11 +65,6 @@ void DirectXPainter::initializeMesh()
mMeshPainter->initializeD3d(); mMeshPainter->initializeD3d();
} }
void DirectXPainter::initializeText()
{
mTextPainter->initialize();
}
void DirectXPainter::updateMesh() void DirectXPainter::updateMesh()
{ {
mMeshPainter->updateBuffers(mDrawingContext); mMeshPainter->updateBuffers(mDrawingContext);
@ -89,7 +77,7 @@ void DirectXPainter::initializeDxInterface()
auto backing_image = mDrawingContext->getSurface()->getImage(); auto backing_image = mDrawingContext->getSurface()->getImage();
auto wic_bitmap = dynamic_cast<Win32WicImage*>(backing_image->getPlatformImage())->getBitmap(); auto wic_bitmap = dynamic_cast<Win32WicImage*>(backing_image->getPlatformImage())->getBitmap();
mDxInterface->getD2dInterface()->initializeStandalone(wic_bitmap); mDxInterface->getD2dInterface()->initialize(wic_bitmap);
resetPainters(); resetPainters();
} }
@ -115,7 +103,12 @@ void DirectXPainter::paint()
for (const auto item : scene->getItems()) for (const auto item : scene->getItems())
{ {
if (item->getType() == SceneItem::Type::MODEL && item->isVisible()) if (!item->isVisible())
{
continue;
}
if (item->getType() == SceneItem::Type::MODEL)
{ {
auto model = dynamic_cast<SceneModel*>(item); auto model = dynamic_cast<SceneModel*>(item);
if (model->getGeometry()) if (model->getGeometry())
@ -123,6 +116,12 @@ void DirectXPainter::paint()
m2dPainter->paint(model); m2dPainter->paint(model);
} }
} }
else if (item->getType() == SceneItem::Type::TEXT)
{
auto text = dynamic_cast<SceneText*>(item);
mTextPainter->paint(text, mDrawingContext);
}
} }
m2dPainter->finishDrawing(); m2dPainter->finishDrawing();
@ -139,19 +138,6 @@ void DirectXPainter::paintMesh(ID3D12GraphicsCommandList* commandList)
mMeshPainter->updateCommandList(commandList); mMeshPainter->updateCommandList(commandList);
} }
void DirectXPainter::paintText()
{
auto scene = mDrawingContext->getSurface()->getScene();
for (const auto item : scene->getItems())
{
if (item->getType() == SceneItem::Type::TEXT && item->isVisible())
{
auto text = dynamic_cast<SceneText*>(item);
mTextPainter->paint(text, mDrawingContext);
}
}
}
bool DirectXPainter::supportsGeometryPrimitives() const bool DirectXPainter::supportsGeometryPrimitives() const
{ {
return true; return true;

View file

@ -21,17 +21,11 @@ public:
DirectXPainter(DrawingContext* context); DirectXPainter(DrawingContext* context);
DirectXMeshPainter* getMeshPainter() const; DirectXMeshPainter* getMeshPainter() const;
DirectXTextPainter* getTextPainter() const;
void initializeMesh(); void initializeMesh();
void initializeText();
void paint() override; void paint() override;
void paintBackground(const D3D12_CPU_DESCRIPTOR_HANDLE& rtvHandle, ID3D12GraphicsCommandList* commandList); void paintBackground(const D3D12_CPU_DESCRIPTOR_HANDLE& rtvHandle, ID3D12GraphicsCommandList* commandList);
void paintMesh(ID3D12GraphicsCommandList* commandList); void paintMesh(ID3D12GraphicsCommandList* commandList);
void paintText();
void setDxInterface(DirectXInterface* dxInterface); void setDxInterface(DirectXInterface* dxInterface);

View file

@ -29,27 +29,18 @@ DirectXTextPainter::DirectXTextPainter()
void DirectXTextPainter::setD2dInterface(DirectX2dInterface* d2dIterface) void DirectXTextPainter::setD2dInterface(DirectX2dInterface* d2dIterface)
{ {
mD2dInterface = d2dIterface; mD2dInterface = d2dIterface;
mD2dInterface->getRenderTarget()->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mTextBrush);
} }
void DirectXTextPainter::initialize() void DirectXTextPainter::updateTextFormat(const FontItem& font)
{
initializeBrush();
}
void DirectXTextPainter::initializeBrush()
{
mD2dInterface->getContext()->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mTextBrush);
}
void DirectXTextPainter::updateTextFormat(float fontSize)
{ {
mD2dInterface->getDirectWriteFactory()->CreateTextFormat( mD2dInterface->getDirectWriteFactory()->CreateTextFormat(
L"Verdana", StringUtils::convert(font.getFaceName()).c_str(),
NULL, nullptr,
DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
fontSize, static_cast<float>(font.getSize()),
L"en-us", L"en-us",
&mTextFormat &mTextFormat
); );
@ -62,14 +53,11 @@ void DirectXTextPainter::paint(SceneText* text, DrawingContext* context)
const auto location = text->getTransform().getLocation(); const auto location = text->getTransform().getLocation();
D2D1_RECT_F textRect = D2D1::RectF(static_cast<float>(location.getX()), static_cast<float>(location.getY()), static_cast<float>(location.getX() + 200), static_cast<float>(location.getY() + 100)); D2D1_RECT_F textRect = D2D1::RectF(static_cast<float>(location.getX()), static_cast<float>(location.getY()), static_cast<float>(location.getX() + 200), static_cast<float>(location.getY() + 100));
updateTextFormat(static_cast<float>(text->getTextData().mFont.getSize())); updateTextFormat(text->getTextData().mFont);
auto content = StringUtils::convert(text->getTextData().mContent); auto content = StringUtils::convert(text->getTextData().mContent);
mD2dInterface->getContext()->BeginDraw(); mD2dInterface->getRenderTarget()->DrawText(content.c_str(), static_cast<UINT32>(content.size()), mTextFormat.Get(), &textRect, mTextBrush.Get());
mD2dInterface->getContext()->SetTransform(D2D1::Matrix3x2F::Identity());
mD2dInterface->getContext()->DrawText(content.c_str(), static_cast<UINT32>(content.size()), mTextFormat.Get(), &textRect, mTextBrush.Get());
mD2dInterface->getContext()->EndDraw();
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "FontItem.h"
#include <wrl.h> #include <wrl.h>
#include <dwrite.h> #include <dwrite.h>
#include <d2d1_3.h> #include <d2d1_3.h>
@ -25,15 +27,12 @@ class DirectXTextPainter
public: public:
DirectXTextPainter(); DirectXTextPainter();
void initialize();
void paint(SceneText* text, DrawingContext* context); void paint(SceneText* text, DrawingContext* context);
void setD2dInterface(DirectX2dInterface* d2dIterface); void setD2dInterface(DirectX2dInterface* d2dIterface);
private: private:
void initializeBrush(); void updateTextFormat(const FontItem& font);
void updateTextFormat(float fontSize);
DirectX2dInterface* mD2dInterface{ nullptr }; DirectX2dInterface* mD2dInterface{ nullptr };
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> mTextBrush; Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> mTextBrush;

View file

@ -0,0 +1,35 @@
#include "LatexSymbols.h"
#include "StringUtils.h"
std::unordered_map<std::string, std::wstring> LatexSymbolLookup::mSymbols = {
{"Psi", L"\u03A8"},
{"alpha", L"\u03B1"},
{"beta", L"\u03B2"},
{"gamma", L"\u03B3"},
{"psi", L"\u03C8"}
};
std::optional<std::string> LatexSymbolLookup::getSymbolUtf8(const std::string& tag)
{
if (auto entry = getSymbolUtf16(tag); entry)
{
return StringUtils::convert(*entry);
}
else
{
return std::nullopt;
}
}
std::optional<std::wstring> LatexSymbolLookup::getSymbolUtf16(const std::string& tag)
{
if (auto iter = mSymbols.find(tag); iter != mSymbols.end())
{
return iter->second;
}
else
{
return std::nullopt;
}
}

View file

@ -22,5 +22,10 @@ struct LatexMathSymbol
class LatexSymbolLookup class LatexSymbolLookup
{ {
public: public:
std::unordered_map<std::string, LatexMathSymbol> mTags; static std::optional<std::string> getSymbolUtf8(const std::string& tag);
static std::optional<std::wstring> getSymbolUtf16(const std::string& tag);
private:
static std::unordered_map<std::string, std::wstring> mSymbols;
}; };

View file

@ -79,6 +79,15 @@ void TextNode::setContent(const std::string& content)
} }
} }
void TextNode::setFont(const FontItem& font)
{
if (mTextData.mFont != font)
{
mTextData.mFont = font;
mContentIsDirty = true;
}
}
SceneItem* TextNode::getSceneItem(std::size_t idx) const SceneItem* TextNode::getSceneItem(std::size_t idx) const
{ {
if (idx == 0) if (idx == 0)

View file

@ -33,6 +33,8 @@ public:
void setContent(const std::string& content); void setContent(const std::string& content);
void setFont(const FontItem& font);
void update(SceneInfo* sceneInfo) override; void update(SceneInfo* sceneInfo) override;
private: private:

View file

@ -39,8 +39,7 @@ void DirectX2dIntegration::render()
// Render text directly to the back buffer. // Render text directly to the back buffer.
mDx2dInterface->getContext()->SetTarget(getD2dRenderTarget()); mDx2dInterface->getContext()->SetTarget(getD2dRenderTarget());
auto painter = dynamic_cast<DirectXPainter*>(mWindow->getDrawingContent()->getPainter()); mWindow->getDrawingContent()->getPainter()->paint();
painter->paintText();
// Release our wrapped render target resource. Releasing // Release our wrapped render target resource. Releasing
// transitions the back buffer resource to the state specified // transitions the back buffer resource to the state specified

View file

@ -256,9 +256,8 @@ bool Win32DxWindowInterface::loadAssets()
{ {
auto painter = dynamic_cast<DirectXPainter*>(mWindow->getDrawingContent()->getPainter()); auto painter = dynamic_cast<DirectXPainter*>(mWindow->getDrawingContent()->getPainter());
painter->initializeMesh(); painter->initializeMesh();
painter->initializeText();
painter->updateMesh(); painter->updateMesh();
return true; return true;
} }

View file

@ -1,38 +1,19 @@
#include "TestCase.h"
#include "TestCaseRunner.h"
#include "TestUiApplication.h"
#include "TestFramework.h" #include "TestFramework.h"
#include "TestUtils.h" #include "TestUtils.h"
#include "TestRenderUtils.h"
#include "DrawingSurface.h"
#include "DrawingContext.h"
#include "AbstractPainter.h"
#include "Image.h"
#include "PngWriter.h"
#include "RectangleNode.h" #include "RectangleNode.h"
#include "Scene.h"
TEST_CASE(TestD2dOffScreenRendering, "graphics") TEST_CASE(TestD2dOffScreenRendering, "graphics")
{ {
auto surface = std::make_unique<DrawingSurface>(); TestRenderer renderer(800, 800);
surface->setSize(800, 800);
auto drawing_context = std::make_unique<DrawingContext>(surface.get());
auto rect = std::make_unique<RectangleNode>(Point(10, 10), 200.0, 200.0); auto rect = std::make_unique<RectangleNode>(Point(10, 10), 200.0, 200.0);
auto scene = surface->getScene(); auto scene = renderer.getScene();
//scene->setBackgroundColor(Color(100, 100, 0)); //scene->setBackgroundColor(Color(100, 100, 0));
scene->addNode(rect.get()); scene->addNode(rect.get());
drawing_context->paint(); renderer.write(TestUtils::getTestOutputDir(__FILE__) / "out.png");
auto image = surface->getImage();
PngWriter writer;
writer.setPath(TestUtils::getTestOutputDir(__FILE__) / "out.png");
writer.write(image);
}; };

View file

@ -1,22 +1,29 @@
#include "TestFramework.h" #include "TestFramework.h"
#include "TestUtils.h" #include "TestUtils.h"
#include "TestRenderUtils.h"
#include "StringUtils.h" #include "StringUtils.h"
#include "File.h" #include "FontItem.h"
#include "TextNode.h"
#include <iostream> #include "LatexSymbols.h"
TEST_CASE(TestLatexConverter, "publishing") TEST_CASE(TestLatexConverter, "publishing")
{ {
std::wstring out = L"\u03A8"; FontItem font("Cambria Math", 14);
std::wcout << out << std::endl;
auto psi = LatexSymbolLookup::getSymbolUtf8("psi");
auto alpha = LatexSymbolLookup::getSymbolUtf8("alpha");
auto beta = LatexSymbolLookup::getSymbolUtf8("beta");
std::string out_c = StringUtils::convert(out); auto content = *psi + " " + *alpha + " " + *beta + " ";
File file(TestUtils::getTestOutputDir(__FILE__) / "utf8_render.dat"); TestRenderer renderer(800, 800);
file.writeText(out_c);
//std::cout << out_c << std::endl; auto text = std::make_unique<TextNode>(content, Point(10, 10));
text->setFont(font);
renderer.getScene()->addNode(text.get());
renderer.write(TestUtils::getTestOutputDir(__FILE__) / "out.png");
}; };

View file

@ -6,6 +6,7 @@ add_library(test_utils STATIC
TestCaseRunner.cpp TestCaseRunner.cpp
TestUiApplication.h TestUiApplication.h
TestUiApplication.cpp TestUiApplication.cpp
TestRenderUtils.h
) )
target_include_directories(test_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(test_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

View file

@ -0,0 +1,40 @@
#pragma once
#include "DrawingSurface.h"
#include "DrawingContext.h"
#include "AbstractPainter.h"
#include "Image.h"
#include "PngWriter.h"
#include "Scene.h"
class TestRenderer
{
public:
TestRenderer(unsigned width = 1000, unsigned height = 1000)
{
mSurface = std::make_unique<DrawingSurface>();
mSurface->setSize(width, height);
mDrawingContext = std::make_unique<DrawingContext>(mSurface.get());
}
Scene* getScene() const
{
return mSurface->getScene();
}
void write(const Path& path)
{
mDrawingContext->paint();
auto image = mSurface->getImage();
PngWriter writer;
writer.setPath(path);
writer.write(image);
}
private:
std::unique_ptr<DrawingSurface> mSurface;
std::unique_ptr<DrawingContext> mDrawingContext;
};