mirror of
https://github.com/zoriya/react-native-svg.git
synced 2025-12-05 22:56:11 +00:00
# Summary
There are two main things going on with the PR. Both involve a reworking
of the new arch implementation of rn-svg for windows.
The current implementation attempts to implement a svg renderer from
scratch. There are numerous edge cases that it wasn't handling
correctly, and the performance had some serious issues. This
implementation switches to use the svg rendering path built into
Direct2D. This brings significant performance improvements.
The 2nd issue is there have been various breaking changes in
react-native-windows for how new arch native components are implemented.
This brings the rn-svg implementation in line with those latest changes.
## Test Plan
Primary testing right now is loading up the example app in the repo.
New arch on react-native-windows is still in somewhat early days - so
there are not really current users of this code. I am integrating this
code into Microsoft Office, where I have tested some scenarios. But we
will get expanded testing as we roll out the new arch. I expect there to
be some follow-ups as we expand our usage. The version of rn-svg before
this PR doesn't build with the latest new arch react-native-windows
versions. - So its hard to get worse than that.
### What's required for testing (prerequisites)?
### What are the steps to reproduce (after prerequisites)?
## Compatibility
| OS | Implemented |
| ------- | :---------: |
| iOS | N/A |
| MacOS | N/A |
| Android | N/A |
| Web | ✅ |
## Checklist
- [x] I have tested this on a device and a simulator
- [ ] I added documentation in `README.md`
- [ ] I updated the typed files (typescript)
- [ ] I added a test for the API in the `__tests__` folder
129 lines
4.2 KiB
C++
129 lines
4.2 KiB
C++
#include "pch.h"
|
|
#include "ImageView.h"
|
|
|
|
namespace winrt::RNSVG::implementation {
|
|
|
|
REACT_STRUCT(ImageSource)
|
|
struct ImageSource {
|
|
REACT_FIELD(uri)
|
|
std::wstring uri{};
|
|
REACT_FIELD(width)
|
|
float width{0.0f};
|
|
REACT_FIELD(height)
|
|
float height{0.0f};
|
|
|
|
bool operator==(const ImageSource &rhs) const {
|
|
return uri == rhs.uri && width == rhs.width && height == rhs.height;
|
|
}
|
|
|
|
bool operator!=(const ImageSource &rhs) const {
|
|
return !(*this == rhs);
|
|
}
|
|
};
|
|
|
|
REACT_STRUCT(ImageProps)
|
|
struct ImageProps : winrt::implements<ImageProps, winrt::Microsoft::ReactNative::IComponentProps> {
|
|
ImageProps(const winrt::Microsoft::ReactNative::ViewProps &props, const winrt::Microsoft::ReactNative::IComponentProps& cloneFrom) REACT_SVG_RENDERABLE_COMMON_PROPS_INIT
|
|
{
|
|
REACT_BEGIN_SVG_RENDERABLE_COMMON_PROPS_CLONE(ImageProps)
|
|
x = cloneFromProps->x;
|
|
y = cloneFromProps->y;
|
|
width = cloneFromProps->width;
|
|
height = cloneFromProps->height;
|
|
src = cloneFromProps->src;
|
|
align = cloneFromProps->align;
|
|
meetOrSlice = cloneFromProps->meetOrSlice;
|
|
REACT_END_SVG_RENDERABLE_COMMON_PROPS_CLONE
|
|
}
|
|
|
|
void SetProp(uint32_t hash, winrt::hstring propName, winrt::Microsoft::ReactNative::IJSValueReader value) noexcept {
|
|
winrt::Microsoft::ReactNative::ReadProp(hash, propName, value, *this);
|
|
}
|
|
|
|
REACT_SVG_RENDERABLE_COMMON_PROPS;
|
|
|
|
REACT_FIELD(x)
|
|
std::optional<D2D1_SVG_LENGTH> x;
|
|
REACT_FIELD(y)
|
|
std::optional<D2D1_SVG_LENGTH> y;
|
|
REACT_FIELD(width)
|
|
std::optional<D2D1_SVG_LENGTH> width;
|
|
REACT_FIELD(height)
|
|
std::optional<D2D1_SVG_LENGTH> height;
|
|
REACT_FIELD(src)
|
|
ImageSource src;
|
|
REACT_FIELD(align)
|
|
std::optional<std::string> align{""};
|
|
REACT_FIELD(meetOrSlice)
|
|
std::optional<MeetOrSlice> meetOrSlice;
|
|
};
|
|
|
|
struct ImageView : winrt::implements<ImageView, winrt::Windows::Foundation::IInspectable, RenderableView> {
|
|
public:
|
|
ImageView() = default;
|
|
|
|
const wchar_t *GetSvgElementName() noexcept override {
|
|
return L"image";
|
|
}
|
|
|
|
void UpdateProps(
|
|
const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
const winrt::Microsoft::ReactNative::IComponentProps &newProps,
|
|
const winrt::Microsoft::ReactNative::IComponentProps &oldProps) noexcept {
|
|
RenderableView::UpdateProps(view, newProps, oldProps);
|
|
|
|
auto props = newProps.as<ImageProps>();
|
|
|
|
if (!props->align) {
|
|
m_aspectAlign = AlignToAspectAlign(props->align.value());
|
|
} else {
|
|
m_aspectAlign = D2D1_SVG_ASPECT_ALIGN::D2D1_SVG_ASPECT_ALIGN_NONE;
|
|
}
|
|
}
|
|
|
|
void OnRender(const SvgView &svgView, ID2D1SvgDocument &document, ID2D1SvgElement &element) noexcept override {
|
|
auto props = m_props.as<ImageProps>();
|
|
SetCommonSvgProps(svgView, document, element, *props);
|
|
|
|
if (props->x) {
|
|
element.SetAttributeValue(SvgStrings::xAttributeName, props->x.value());
|
|
}
|
|
if (props->y) {
|
|
element.SetAttributeValue(SvgStrings::yAttributeName, props->y.value());
|
|
}
|
|
if (props->width) {
|
|
element.SetAttributeValue(SvgStrings::widthAttributeName, props->width.value());
|
|
}
|
|
if (props->height) {
|
|
element.SetAttributeValue(SvgStrings::heightAttributeName, props->height.value());
|
|
}
|
|
|
|
if (props->align != std::nullopt || props->meetOrSlice != std::nullopt) {
|
|
D2D1_SVG_PRESERVE_ASPECT_RATIO preserveAspectRatio;
|
|
preserveAspectRatio.defer = false;
|
|
preserveAspectRatio.align = m_aspectAlign;
|
|
|
|
preserveAspectRatio.meetOrSlice = props->meetOrSlice.value() == MeetOrSlice::Meet
|
|
? D2D1_SVG_ASPECT_SCALING::D2D1_SVG_ASPECT_SCALING_MEET
|
|
: D2D1_SVG_ASPECT_SCALING::D2D1_SVG_ASPECT_SCALING_SLICE;
|
|
element.SetAttributeValue(SvgStrings::preserveAspectRatioAttributeName, preserveAspectRatio);
|
|
}
|
|
|
|
if (!props->src.uri.empty()) {
|
|
element.SetAttributeValue(
|
|
SvgStrings::xlinkhrefAttributeName,
|
|
D2D1_SVG_ATTRIBUTE_STRING_TYPE::D2D1_SVG_ATTRIBUTE_STRING_TYPE_SVG,
|
|
props->src.uri.c_str());
|
|
}
|
|
}
|
|
|
|
private:
|
|
D2D1_SVG_ASPECT_ALIGN m_aspectAlign;
|
|
};
|
|
|
|
void RegisterImageComponent(const winrt::Microsoft::ReactNative::IReactPackageBuilderFabric &builder) noexcept {
|
|
RegisterRenderableComponent<winrt::RNSVG::implementation::ImageProps, ImageView>(L"RNSVGImage", builder);
|
|
}
|
|
|
|
} // namespace winrt::RNSVG::implementation
|