[Windows] Port to Direct2D to remove win2d dependency (#2052)

This change removes the win2d (Direct2D wrapper) dependency by using D2D directly. This removes the manual step of adding the win2d to any new react-native-windows projects that want to use react-native-svg. It is also a stepping stone to an easier Fabric implementation for windows.
This commit is contained in:
Marlene Cota
2023-11-14 05:33:19 -05:00
committed by GitHub
parent 22f47333fb
commit f88532d195
66 changed files with 1420 additions and 711 deletions

View File

@@ -6,7 +6,6 @@
#include "Utils.h"
using namespace winrt;
using namespace Microsoft::Graphics::Canvas;
using namespace Microsoft::ReactNative;
namespace winrt::RNSVG::implementation {
@@ -30,18 +29,29 @@ void LineView::UpdateProperties(IJSValueReader const &reader, bool forceUpdate,
__super::UpdateProperties(reader, forceUpdate, invalidate);
}
void LineView::CreateGeometry(UI::Xaml::CanvasControl const &canvas) {
auto const &resourceCreator{canvas.try_as<ICanvasResourceCreator>()};
void LineView::CreateGeometry() {
auto const &root{SvgRoot()};
float x1{Utils::GetAbsoluteLength(m_x1, canvas.Size().Width)};
float y1{Utils::GetAbsoluteLength(m_y1, canvas.Size().Height)};
float x2{Utils::GetAbsoluteLength(m_x2, canvas.Size().Width)};
float y2{Utils::GetAbsoluteLength(m_y2, canvas.Size().Height)};
float x1{Utils::GetAbsoluteLength(m_x1, root.ActualWidth())};
float y1{Utils::GetAbsoluteLength(m_y1, root.ActualHeight())};
float x2{Utils::GetAbsoluteLength(m_x2, root.ActualWidth())};
float y2{Utils::GetAbsoluteLength(m_y2, root.ActualHeight())};
auto const &pathBuilder{Geometry::CanvasPathBuilder(resourceCreator)};
pathBuilder.BeginFigure(x1, y1);
pathBuilder.AddLine (x2, y2);
pathBuilder.EndFigure(Geometry::CanvasFigureLoop::Open);
Geometry(Geometry::CanvasGeometry::CreatePath(pathBuilder));
com_ptr<ID2D1DeviceContext> deviceContext{get_self<D2DDeviceContext>(root.DeviceContext())->Get()};
com_ptr<ID2D1Factory> factory;
deviceContext->GetFactory(factory.put());
com_ptr<ID2D1PathGeometry> geometry;
check_hresult(factory->CreatePathGeometry(geometry.put()));
com_ptr<ID2D1GeometrySink> sink;
check_hresult(geometry->Open(sink.put()));
sink->BeginFigure({x1, y1}, D2D1_FIGURE_BEGIN_FILLED);
sink->AddLine({x2, y2});
sink->EndFigure(D2D1_FIGURE_END_OPEN);
Geometry(make<RNSVG::implementation::D2DGeometry>(geometry.as<ID2D1Geometry>()));
}
} // namespace winrt::RNSVG::implementation