aboutsummaryrefslogtreecommitdiffstatsci
path: root/Backends/CairoBackend/src/Device.cs
diff options
context:
space:
mode:
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>2021-11-24 03:00:25 +0100
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>2021-11-24 03:00:25 +0100
commitf005d76468dc4c6a368b6f1f6eca69da25214e80 (patch)
tree14a70c7fb9a58be9083dbd7492ecd94d030c18a9 /Backends/CairoBackend/src/Device.cs
parent362d83b9556355b1d3dfb6cb892896ed33371521 (diff)
default Crow.CairoBackend ok
Diffstat (limited to 'Backends/CairoBackend/src/Device.cs')
-rw-r--r--Backends/CairoBackend/src/Device.cs149
1 files changed, 101 insertions, 48 deletions
diff --git a/Backends/CairoBackend/src/Device.cs b/Backends/CairoBackend/src/Device.cs
index 5191a23d..4cbb180f 100644
--- a/Backends/CairoBackend/src/Device.cs
+++ b/Backends/CairoBackend/src/Device.cs
@@ -28,33 +28,17 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
+using System.IO;
+using System.Runtime.InteropServices;
using Drawing2D;
using Glfw;
namespace Crow.CairoBackend
{
- public class Device : IDevice
- {
- IntPtr handle = IntPtr.Zero;
- /// <summary> Global font rendering settings for Cairo </summary>
- FontOptions FontRenderingOptions;
- /// <summary> Global font rendering settings for Cairo </summary>
- Antialias Antialias = Antialias.Subpixel;
-
- protected Device()
- {
- FontRenderingOptions = new FontOptions ();
- FontRenderingOptions.Antialias = Antialias.Subpixel;
- FontRenderingOptions.HintMetrics = HintMetrics.On;
- FontRenderingOptions.HintStyle = HintStyle.Full;
- FontRenderingOptions.SubpixelOrder = SubpixelOrder.Default;
- }
-
- protected Device (IntPtr ptr) : this (ptr, true)
- {
- }
-
- protected Device (IntPtr handle, bool owner)
+ public abstract class CairoDevice : Device {
+ protected IntPtr handle = IntPtr.Zero;
+ public IntPtr Handle => handle;
+ protected CairoDevice (IntPtr handle, bool owner = true)
{
this.handle = handle;
if (!owner)
@@ -62,25 +46,11 @@ namespace Crow.CairoBackend
if (CairoDebug.Enabled)
CairoDebug.OnAllocated (handle);
}
-
- ~Device ()
- {
- Dispose (false);
- }
-
- public IntPtr Handle {
- get {
- return handle;
- }
- }
public string Status {
get {
return System.Runtime.InteropServices.Marshal.PtrToStringAuto(NativeMethods.cairo_status_to_string (NativeMethods.cairo_device_status (handle)));
}
}
- public void SetThreadAware (bool value){
- NativeMethods.cairo_gl_device_set_thread_aware (handle, value ? 1 : 0);
- }
public Status Acquire()
{
return NativeMethods.cairo_device_acquire (handle);
@@ -90,14 +60,10 @@ namespace Crow.CairoBackend
NativeMethods.cairo_device_release (handle);
}
- public void Dispose ()
+ protected override void Dispose (bool disposing)
{
- Dispose (true);
- GC.SuppressFinalize (this);
- }
+ base.Dispose (disposing);
- protected virtual void Dispose (bool disposing)
- {
if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<Device> (handle, disposing);
@@ -106,24 +72,46 @@ namespace Crow.CairoBackend
NativeMethods.cairo_device_destroy (handle);
- FontRenderingOptions.Dispose ();
handle = IntPtr.Zero;
}
+ }
+ public class Device : IDevice
+ {
+ /// <summary> Global font rendering settings for Cairo </summary>
+ FontOptions FontRenderingOptions;
+ /// <summary> Global font rendering settings for Cairo </summary>
+ Antialias Antialias = Antialias.Subpixel;
- public void GetDpy(out int hdpy, out int vdpy)
+ public Device()
{
- throw new NotImplementedException();
+ FontRenderingOptions = new FontOptions ();
+ FontRenderingOptions.Antialias = Antialias.Subpixel;
+ FontRenderingOptions.HintMetrics = HintMetrics.On;
+ FontRenderingOptions.HintStyle = HintStyle.Full;
+ FontRenderingOptions.SubpixelOrder = SubpixelOrder.Default;
}
- public void SetDpy(int hdpy, int vdpy)
+ ~Device ()
+ {
+ Dispose (false);
+ }
+
+ #region IDevice implementation
+ public void GetDpy(out int hdpy, out int vdpy)
{
throw new NotImplementedException();
}
- public virtual ISurface CreateSurface(int width, int height)
+ public void SetDpy(int hdpy, int vdpy)
{
throw new NotImplementedException();
}
+ public IRegion CreateRegion () => new Region ();
+ public virtual ISurface CreateSurface(int width, int height)
+ => new ImageSurface (Format.ARGB32, width, height);
+ public virtual ISurface CreateSurface(byte[] data, int width, int height)
+ => new ImageSurface (data, Format.ARGB32, width, height, 4 * width);
+
public ISurface CreateSurface (IntPtr nativeWindoPointer, int width, int height) {
switch (Environment.OSVersion.Platform) {
case PlatformID.Unix:
@@ -147,8 +135,73 @@ namespace Crow.CairoBackend
Context gr = new Context (surf);
gr.FontOptions = FontRenderingOptions;
gr.Antialias = Antialias;
- throw new NotImplementedException();
+ return gr;
+ }
+ public byte[] LoadBitmap (Stream stream, out Size dimensions) {
+ byte[] image;
+#if STB_SHARP
+ StbImageSharp.ImageResult stbi = StbImageSharp.ImageResult.FromStream (stream, StbImageSharp.ColorComponents.RedGreenBlueAlpha);
+ image = new byte[stbi.Data.Length];
+ //rgba to argb for cairo.
+ for (int i = 0; i < stbi.Data.Length; i += 4) {
+ image[i] = stbi.Data[i + 2];
+ image[i + 1] = stbi.Data[i + 1];
+ image[i + 2] = stbi.Data[i];
+ image[i + 3] = stbi.Data[i + 3];
+ }
+ dimensions = new Size (stbi.Width, stbi.Height);
+#else
+ using (StbImage stbi = new StbImage (stream)) {
+ image = new byte [stbi.Size];
+ for (int i = 0; i < stbi.Size; i+=4) {
+ //rgba to argb for cairo. ???? looks like bgra to me.
+ image [i] = Marshal.ReadByte (stbi.Handle, i + 2);
+ image [i + 1] = Marshal.ReadByte (stbi.Handle, i + 1);
+ image [i + 2] = Marshal.ReadByte (stbi.Handle, i);
+ image [i + 3] = Marshal.ReadByte (stbi.Handle, i + 3);
+ }
+ dimensions = new Size (stbi.Width, stbi.Height);
+ }
+#endif
+ return image;
+ }
+ public ISvgHandle LoadSvg(Stream stream)
+ {
+ using (BinaryReader sr = new BinaryReader (stream))
+ return new SvgHandle (sr.ReadBytes ((int)stream.Length));
+ }
+
+ public ISvgHandle LoadSvg(string svgFragment) =>
+ new SvgHandle (System.Text.Encoding.Unicode.GetBytes (svgFragment));
+
+ public IGradient CreateGradient (GradientType gradientType, Rectangle bounds) {
+ switch (gradientType) {
+ case GradientType.Vertical:
+ return new LinearGradient (bounds.Left, bounds.Top, bounds.Left, bounds.Bottom);
+ case GradientType.Horizontal:
+ return new LinearGradient (bounds.Left, bounds.Top, bounds.Right, bounds.Top);
+ case GradientType.Oblic:
+ return new LinearGradient (bounds.Left, bounds.Top, bounds.Right, bounds.Bottom);
+ case GradientType.Radial:
+ throw new NotImplementedException ();
+ }
+ return null;
+ }
+ #endregion
+
+ #region IDispose implementation
+ public void Dispose ()
+ {
+ Dispose (true);
+ GC.SuppressFinalize (this);
+ }
+
+ protected virtual void Dispose (bool disposing)
+ {
+ if (disposing)
+ FontRenderingOptions.Dispose ();
}
+ #endregion
}
}