From 5d2dcf91480d4f9a69b153b73ebe4e00b33b03fe Mon Sep 17 00:00:00 2001 From: Somya Sharma Date: Tue, 23 Apr 2024 15:56:42 -0700 Subject: [PATCH 1/2] Read svg file using http request --- .../uwp/SharedRenderer/dll/CppWinRTIncludes.h | 17 +++--- .../lib/AdaptiveImageRenderer.cpp | 53 +++++++++++-------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/source/uwp/SharedRenderer/dll/CppWinRTIncludes.h b/source/uwp/SharedRenderer/dll/CppWinRTIncludes.h index f90844d443..06a890db0b 100644 --- a/source/uwp/SharedRenderer/dll/CppWinRTIncludes.h +++ b/source/uwp/SharedRenderer/dll/CppWinRTIncludes.h @@ -77,6 +77,15 @@ namespace winrt { using namespace ::winrt::AdaptiveCards::Rendering::Xaml_Rendering::implementation; } + + // using namespace winrt::Windows::Data::Xml::Dom + using XmlDocument = ::winrt::Windows::Data::Xml::Dom::XmlDocument; + using IXmlNode = ::winrt::Windows::Data::Xml::Dom::IXmlNode; + + // using namespace winrt::Windows::Web::Http + using HttpProgress = ::winrt::Windows::Web::Http::HttpProgress; + using HttpClient = ::winrt::Windows::Web::Http::HttpClient; + using HttpResponseMessage = ::winrt::Windows::Web::Http::HttpResponseMessage; #ifdef USE_WINUI3 using MediaElement = xaml::Controls::MediaPlayerElement; @@ -97,17 +106,9 @@ namespace winrt // using namespace winrt::Windows::Data::Json using JsonObject = ::winrt::Windows::Data::Json::JsonObject; - // using namespace winrt::Windows::Data::Xml::Dom - using XmlDocument = ::winrt::Windows::Data::Xml::Dom::XmlDocument; - using IXmlNode = ::winrt::Windows::Data::Xml::Dom::IXmlNode; - // using namespace winrt::Windows::Globalization::DateTimeFormatting; using DateTimeFormatter = ::winrt::Windows::Globalization::DateTimeFormatting::DateTimeFormatter; - // using namespace winrt::Windows::Web::Http - using HttpProgress = ::winrt::Windows::Web::Http::HttpProgress; - using HttpClient = ::winrt::Windows::Web::Http::HttpClient; - // using namespace winrt::Windows::Web::Http::HttpFilters using HttpBaseProtocolFilter = ::winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter; diff --git a/source/uwp/SharedRenderer/lib/AdaptiveImageRenderer.cpp b/source/uwp/SharedRenderer/lib/AdaptiveImageRenderer.cpp index d7277deb6c..871dfb96f2 100644 --- a/source/uwp/SharedRenderer/lib/AdaptiveImageRenderer.cpp +++ b/source/uwp/SharedRenderer/lib/AdaptiveImageRenderer.cpp @@ -414,36 +414,45 @@ namespace AdaptiveCards::Rendering::Xaml_Rendering if (imgProperties.isImageSvg) { - // If we have an SVG, we need to try to parse for the image size before setting the image source - auto svgDocumentLoadOperation = winrt::XmlDocument::LoadFromUriAsync(imageUrl); + winrt::HttpClient httpClient; + auto getOperation = httpClient.GetAsync(imageUrl); - svgDocumentLoadOperation.Completed( - [weakThis = this->get_weak(), - weakImageSource = winrt::make_weak(image.as()), - imageUrl](auto const& operation, auto status) -> void + getOperation.Completed([weakThis = this->get_weak(), + weakImageSource = winrt::make_weak(image.as()), + imageUrl](auto const& operation, auto status) + { + if (status == winrt::AsyncStatus::Completed) { - auto strongThis = weakThis.get(); - auto strongImageSource = weakImageSource.get(); + winrt::HttpResponseMessage response = operation.GetResults(); - if (strongThis && strongImageSource) + if (response.IsSuccessStatusCode()) { - if (status == winrt::AsyncStatus::Completed) - { - auto success = strongThis->ParseXmlForHeightAndWidth(operation.GetResults(), strongImageSource); + auto readOperation = response.Content().ReadAsStringAsync(); - if (success) + readOperation.Completed([weakThis, weakImageSource, imageUrl](auto const& operation, auto status) { - // Now that we've parsed the height and width successfully, we can set the image source - strongThis->SetSvgUriSource(strongImageSource, imageUrl); + auto strongThis = weakThis.get(); + auto strongImageSource = weakImageSource.get(); + + if (strongThis && strongImageSource) + { + if (status == winrt::AsyncStatus::Completed) + { + // Read SVG xml + winrt::XmlDocument xmlDoc; + xmlDoc.LoadXml(operation.GetResults()); + auto success = strongThis->ParseXmlForHeightAndWidth(xmlDoc, strongImageSource); + if (success) + { + // Now that we've parsed the height and width successfully, we can set the image source + strongThis->SetSvgUriSource(strongImageSource, imageUrl); + } + } } - } - else if (status == winrt::AsyncStatus::Error) - { - // Handle error - winrt::hresult error = operation.ErrorCode(); - } + }); } - }); + } + }); } else { From 6a5d902484670486b7fc85e2a881944baa4ca8ef Mon Sep 17 00:00:00 2001 From: Somya Sharma Date: Wed, 24 Apr 2024 16:44:34 -0700 Subject: [PATCH 2/2] Clean up --- .../uwp/SharedRenderer/dll/CppWinRTIncludes.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/source/uwp/SharedRenderer/dll/CppWinRTIncludes.h b/source/uwp/SharedRenderer/dll/CppWinRTIncludes.h index 06a890db0b..447ff5c5bf 100644 --- a/source/uwp/SharedRenderer/dll/CppWinRTIncludes.h +++ b/source/uwp/SharedRenderer/dll/CppWinRTIncludes.h @@ -77,15 +77,6 @@ namespace winrt { using namespace ::winrt::AdaptiveCards::Rendering::Xaml_Rendering::implementation; } - - // using namespace winrt::Windows::Data::Xml::Dom - using XmlDocument = ::winrt::Windows::Data::Xml::Dom::XmlDocument; - using IXmlNode = ::winrt::Windows::Data::Xml::Dom::IXmlNode; - - // using namespace winrt::Windows::Web::Http - using HttpProgress = ::winrt::Windows::Web::Http::HttpProgress; - using HttpClient = ::winrt::Windows::Web::Http::HttpClient; - using HttpResponseMessage = ::winrt::Windows::Web::Http::HttpResponseMessage; #ifdef USE_WINUI3 using MediaElement = xaml::Controls::MediaPlayerElement; @@ -106,9 +97,18 @@ namespace winrt // using namespace winrt::Windows::Data::Json using JsonObject = ::winrt::Windows::Data::Json::JsonObject; + // using namespace winrt::Windows::Data::Xml::Dom + using XmlDocument = ::winrt::Windows::Data::Xml::Dom::XmlDocument; + using IXmlNode = ::winrt::Windows::Data::Xml::Dom::IXmlNode; + // using namespace winrt::Windows::Globalization::DateTimeFormatting; using DateTimeFormatter = ::winrt::Windows::Globalization::DateTimeFormatting::DateTimeFormatter; + // using namespace winrt::Windows::Web::Http + using HttpProgress = ::winrt::Windows::Web::Http::HttpProgress; + using HttpClient = ::winrt::Windows::Web::Http::HttpClient; + using HttpResponseMessage = ::winrt::Windows::Web::Http::HttpResponseMessage; + // using namespace winrt::Windows::Web::Http::HttpFilters using HttpBaseProtocolFilter = ::winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter;