Skip to content

Commit

Permalink
GTK rendering support
Browse files Browse the repository at this point in the history
Upstream PR: Redth#798

Fixes Redth#797
  • Loading branch information
knocte committed Aug 14, 2022
1 parent 034bb00 commit 5cbbb55
Show file tree
Hide file tree
Showing 45 changed files with 311 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Samples/Sample.Android/Sample.Android.csproj
Expand Up @@ -93,8 +93,8 @@
<PackageReference Include="Xamarin.AndroidX.Browser">
<Version>1.3.0.4</Version>
</PackageReference>
<PackageReference Include="Xamarin.Essentials">
<Version>1.6.0</Version>
<PackageReference Include="DotNetEssentials">
<Version>1.6.0--date20220814-0324.git-7f6e466</Version>
</PackageReference>
</ItemGroup>
</Project>
Expand Up @@ -57,8 +57,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.1874" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.0" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2515" />
<PackageReference Include="DotNetEssentials" Version="1.6.0--date20220814-0324.git-7f6e466" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
Expand Down
Expand Up @@ -26,7 +26,7 @@
<ExcludeAssets>Runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Tizen.NET.Sdk" Version="1.1.6" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.1874" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2515" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\ZXing.Net.Mobile\ZXing.Net.Mobile.csproj" />
Expand Down
4 changes: 2 additions & 2 deletions Samples/Sample.Forms/Sample.Forms.UWP/Sample.Forms.UWP.csproj
Expand Up @@ -143,9 +143,9 @@
</Page>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.1874" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2515" />
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.11" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.0" />
<PackageReference Include="DotNetEssentials" Version="1.6.0--date20220814-0324.git-7f6e466" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\ZXing.Net.Mobile.Forms\ZXing.Net.Mobile.Forms.csproj">
Expand Down
4 changes: 2 additions & 2 deletions Samples/Sample.Forms/Sample.Forms.iOS/Sample.Forms.iOS.csproj
Expand Up @@ -126,8 +126,8 @@
<Reference Include="System.Numerics.Vectors" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.1874" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.0" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2515" />
<PackageReference Include="DotNetEssentials" Version="1.6.0--date20220814-0324.git-7f6e466" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\ZXing.Net.Mobile.Forms\ZXing.Net.Mobile.Forms.csproj">
Expand Down
4 changes: 2 additions & 2 deletions Samples/Sample.Forms/Sample.Forms/Sample.Forms.csproj
Expand Up @@ -11,8 +11,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.1874" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.0" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2515" />
<PackageReference Include="DotNetEssentials" Version="1.6.0--date20220814-0324.git-7f6e466" />
<PackageReference Include="ZXing.Net" Version="0.16.6" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions Samples/Sample.Uwp/Sample.Uwp.csproj
Expand Up @@ -160,8 +160,8 @@
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.11</Version>
</PackageReference>
<PackageReference Include="Xamarin.Essentials">
<Version>1.6.0</Version>
<PackageReference Include="DotNetEssentials">
<Version>1.6.0--date20220814-0324.git-7f6e466</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Samples/Sample.iOS/Sample.iOS.csproj
Expand Up @@ -103,8 +103,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Essentials">
<Version>1.6.0</Version>
<PackageReference Include="DotNetEssentials">
<Version>1.6.0--date20220814-0324.git-7f6e466</Version>
</PackageReference>
<PackageReference Include="Xamarin.TestCloud.Agent" Version="0.22.1" />
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions ZXing.Net.Mobile.Forms/Platform.gtk.cs
@@ -0,0 +1,12 @@
using System;

namespace ZXing.Net.Mobile.Forms.GTK
{
public class Platform
{
public static void Init ()
{
ZXing.Net.Mobile.Forms.GTK.ZXingBarcodeImageViewRenderer.Init();
}
}
}
22 changes: 18 additions & 4 deletions ZXing.Net.Mobile.Forms/ZXing.Net.Mobile.Forms.csproj
@@ -1,6 +1,6 @@
<Project Sdk="MSBuild.Sdk.Extras/3.0.23">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;Xamarin.Mac20;MonoAndroid10.0;tizen40</TargetFrameworks>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;Xamarin.Mac20;MonoAndroid10.0;tizen40;net461</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);uap10.0.16299;</TargetFrameworks>
<AssemblyName>ZXing.Net.Mobile.Forms</AssemblyName>
<RootNamespace>ZXing.Net.Mobile.Forms</RootNamespace>
Expand Down Expand Up @@ -86,12 +86,26 @@
<Compile Include="**\*.tizen.cs" />
<Compile Include="**\*.tizen.*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('net461')) ">
<PackageReference Include="DotNetForms.Platform.GTK" Version="5.0.0.2515--date20220812-0259.git-cf6f7a9" />
<Reference Include="glib-sharp">
<HintPath>..\ZXing.Net.Mobile\GTK\Libs\gtk-sharp\gtk-sharp-2.0\glib-sharp.dll</HintPath>
</Reference>
<Reference Include="gtk-sharp">
<HintPath>..\ZXing.Net.Mobile\GTK\Libs\gtk-sharp\gtk-sharp-2.0\gtk-sharp.dll</HintPath>
</Reference>
<Reference Include="gdk-sharp">
<HintPath>..\ZXing.Net.Mobile\GTK\Libs\gtk-sharp\gtk-sharp-2.0\gdk-sharp.dll</HintPath>
</Reference>
<Compile Include="**\*.gtk.cs" />
<Compile Include="**\*.gtk.*.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Essentials" Version="1.6.0" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.1874" />
<PackageReference Include="DotNetEssentials" Version="1.6.0--date20220814-0324.git-7f6e466" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2515" />
<PackageReference Include="ZXing.Net" Version="0.16.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZXing.Net.Mobile\ZXing.Net.Mobile.csproj" />
</ItemGroup>
</Project>
</Project>
73 changes: 73 additions & 0 deletions ZXing.Net.Mobile.Forms/ZXingBarcodeImageViewRenderer.gtk.cs
@@ -0,0 +1,73 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.GTK;
using ZXing.Net.Mobile.Forms;
using ZXing.Net.Mobile.Forms.GTK;
using ZXing.Net.Mobile.GTK;
using Image = Gtk.Image;

[assembly:ExportRenderer(typeof(ZXingBarcodeImageView), typeof(ZXingBarcodeImageViewRenderer))]
namespace ZXing.Net.Mobile.Forms.GTK
{
public class ZXingBarcodeImageViewRenderer : ViewRenderer<ZXingBarcodeImageView, Image>
{
public static void Init()
{
var temp = DateTime.Now;
}

ZXingBarcodeImageView formsView;
Image gtkImage;

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// in GTK there are a way to many properties that are changed compared to other platforms
if (e.PropertyName == ZXingBarcodeImageView.BarcodeValueProperty.PropertyName ||
e.PropertyName == ZXingBarcodeImageView.BarcodeFormatProperty.PropertyName ||
e.PropertyName == ZXingBarcodeImageView.BarcodeOptionsProperty.PropertyName)
{
Regenerate();
}

base.OnElementPropertyChanged(sender, e);
}

protected override void OnElementChanged(ElementChangedEventArgs<ZXingBarcodeImageView> e)
{
formsView = Element;

if (gtkImage == null)
{
gtkImage = new Image();

base.SetNativeControl(gtkImage);
}

Regenerate();

base.OnElementChanged(e);
}

void Regenerate ()
{
if (formsView != null && formsView.BarcodeValue != null)
{
var writer = new BarcodeWriter();

if (formsView != null && formsView.BarcodeOptions != null)
writer.Options = formsView.BarcodeOptions;
if (formsView != null && formsView.BarcodeFormat != null)
writer.Format = formsView.BarcodeFormat;

var value = formsView != null ? formsView.BarcodeValue : string.Empty;

Device.BeginInvokeOnMainThread(() =>
{
var pixBuf = writer.Write(value);
gtkImage.Pixbuf = pixBuf;
});
}
}
}
}
13 changes: 13 additions & 0 deletions ZXing.Net.Mobile/GTK/BarcodeWriter.gtk.cs
@@ -0,0 +1,13 @@
using Gdk;

namespace ZXing.Net.Mobile.GTK
{
public class BarcodeWriter : BarcodeWriter<Pixbuf>, IBarcodeWriter
{
public BarcodeWriter()
{
Renderer = new BitmapRenderer();
}
}
}

52 changes: 52 additions & 0 deletions ZXing.Net.Mobile/GTK/BitmapRenderer.gtk.cs
@@ -0,0 +1,52 @@
using Cairo;
using Gdk;
using ZXing.Common;
using ZXing.Rendering;

namespace ZXing.Net.Mobile.GTK
{
public class BitmapRenderer : IBarcodeRenderer<Pixbuf>
{
public Pixbuf Render(BitMatrix matrix, BarcodeFormat format, string content)
{
var black = new Cairo.Color(0, 0, 0);
var white = new Cairo.Color(1, 1, 1);
using (var surface = new ImageSurface(Format.RGB24, matrix.Width, matrix.Height))
{
using (var cr = new Context(surface))
{
for (var x = 0; x < matrix.Width; x++)
{
for (var y = 0; y < matrix.Height; y++)
{
SetSourceColor(cr, matrix[x, y] ? black : white);
cr.MoveTo(x, y);
cr.LineTo(x + 1, y);
cr.Stroke();
}
}

const int onlyBitsPerSampleValueSupportedByGdk = 8;
return new Pixbuf(surface.Data,
Colorspace.Rgb,
true,
onlyBitsPerSampleValueSupportedByGdk,
matrix.Width,
matrix.Height,
surface.Stride);
}
}
}

private void SetSourceColor(Context context, Cairo.Color color)
{
context.SetSourceRGB(color.R, color.G, color.B);
}

public Pixbuf Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options)
{
return Render(matrix, format, content);
}
}
}

Binary file added ZXing.Net.Mobile/GTK/Libs/OpenTK/OpenTK.dll
Binary file not shown.
25 changes: 25 additions & 0 deletions ZXing.Net.Mobile/GTK/Libs/OpenTK/OpenTK.dll.config
@@ -0,0 +1,25 @@
<configuration>
<dllmap os="linux" dll="opengl32.dll" target="libGL.so.1"/>
<dllmap os="linux" dll="glu32.dll" target="libGLU.so.1"/>
<dllmap os="linux" dll="openal32.dll" target="libopenal.so.1"/>
<dllmap os="linux" dll="alut.dll" target="libalut.so.0"/>
<dllmap os="linux" dll="opencl.dll" target="libOpenCL.so"/>
<dllmap os="linux" dll="libX11" target="libX11.so.6"/>
<dllmap os="linux" dll="libXi" target="libXi.so.6"/>
<dllmap os="linux" dll="SDL2.dll" target="libSDL2-2.0.so.0"/>
<dllmap os="osx" dll="opengl32.dll" target="/System/Library/Frameworks/OpenGL.framework/OpenGL"/>
<dllmap os="osx" dll="openal32.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
<dllmap os="osx" dll="alut.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
<dllmap os="osx" dll="libGLES.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="libGLESv1_CM.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="libGLESv2.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="opencl.dll" target="/System/Library/Frameworks/OpenCL.framework/OpenCL"/>
<dllmap os="osx" dll="SDL2.dll" target="libSDL2.dylib"/>
<!-- XQuartz compatibility (X11 on Mac) -->
<dllmap os="osx" dll="libGL.so.1" target="/usr/X11/lib/libGL.dylib"/>
<dllmap os="osx" dll="libX11" target="/usr/X11/lib/libX11.dylib"/>
<dllmap os="osx" dll="libXcursor.so.1" target="/usr/X11/lib/libXcursor.dylib"/>
<dllmap os="osx" dll="libXi" target="/usr/X11/lib/libXi.dylib"/>
<dllmap os="osx" dll="libXinerama" target="/usr/X11/lib/libXinerama.dylib"/>
<dllmap os="osx" dll="libXrandr.so.2" target="/usr/X11/lib/libXrandr.dylib"/>
</configuration>
1 change: 1 addition & 0 deletions ZXing.Net.Mobile/GTK/Libs/OpenTK/README.txt
@@ -0,0 +1 @@
https://github.com/opentk/opentk
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions ZXing.Net.Mobile/GTK/Libs/gtk-sharp/README.txt
@@ -0,0 +1 @@
https://github.com/mono/gtk-sharp
Binary file not shown.
@@ -0,0 +1,4 @@
<configuration>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/5.12.0/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libatk-1.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/5.12.0/lib/libatk-1.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
<configuration>
<dllmap dll="libglib-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglib-2.0.0.dylib"/>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libgdk-win32-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgdk-quartz-2.0.0.dylib"/>
<dllmap dll="libgdk_pixbuf-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgdk_pixbuf-2.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
<configuration>
<dllmap dll="libglade-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglade-2.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,5 @@
<configuration>
<dllmap dll="libglib-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglib-2.0.0.dylib"/>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libgthread-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgthread-2.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
<configuration>
<dllmap dll="libgdk-win32-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgdk-quartz-2.0.0.dylib"/>
</configuration>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,7 @@
<configuration>
<dllmap dll="libglib-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglib-2.0.0.dylib"/>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libatk-1.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libatk-1.0.0.dylib"/>
<dllmap dll="libgtk-win32-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgtk-quartz-2.0.0.dylib"/>
<dllmap dll="libgdk-win32-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgdk-quartz-2.0.0.dylib"/>
</configuration>
Binary file not shown.
@@ -0,0 +1,6 @@
<configuration>
<dllmap dll="libglib-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libglib-2.0.0.dylib"/>
<dllmap dll="libgobject-2.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libgobject-2.0.0.dylib"/>
<dllmap dll="libpango-1.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libpango-1.0.0.dylib"/>
<dllmap dll="libpangocairo-1.0-0.dll" target="/Library/Frameworks/Mono.framework/Versions/Current/lib/libpangocairo-1.0.0.dylib"/>
</configuration>
Binary file not shown.
1 change: 1 addition & 0 deletions ZXing.Net.Mobile/GTK/Libs/webkit-sharp/README.txt
@@ -0,0 +1 @@
https://github.com/mono/webkit-sharp
Binary file not shown.
@@ -0,0 +1,5 @@
<configuration>
<dllmap dll="webkit-1.0" target="libwebkitgtk-1.0.0.dylib"/>
<dllmap dll="webkit-1.0" os="linux" target="libwebkitgtk-1.0.so.0"/>
<dllmap dll="webkit-1.0" os="windows" target="libwebkitgtk-1.0-0.dll"/>
</configuration>

0 comments on commit 5cbbb55

Please sign in to comment.