CairoPlatform.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Avalonia.Cairo.Media;
  7. using Avalonia.Cairo.Media.Imaging;
  8. using Avalonia.Media;
  9. using Avalonia.Platform;
  10. using Avalonia.Controls;
  11. namespace Avalonia
  12. {
  13. public static class GtkApplicationExtensions
  14. {
  15. public static T UseCairo<T>(this T builder) where T : AppBuilderBase<T>, new()
  16. {
  17. builder.UseRenderingSubsystem(Cairo.CairoPlatform.Initialize, "Cairo");
  18. return builder;
  19. }
  20. }
  21. }
  22. namespace Avalonia.Cairo
  23. {
  24. using System.IO;
  25. using global::Cairo;
  26. public class CairoPlatform : IPlatformRenderInterface
  27. {
  28. private static readonly CairoPlatform s_instance = new CairoPlatform();
  29. private static readonly Pango.Context s_pangoContext = CreatePangoContext();
  30. public static void Initialize() => AvaloniaLocator.CurrentMutable.Bind<IPlatformRenderInterface>().ToConstant(s_instance);
  31. public IBitmapImpl CreateBitmap(int width, int height)
  32. {
  33. return new BitmapImpl(new Gdk.Pixbuf(Gdk.Colorspace.Rgb, true, 32, width, height));
  34. }
  35. public IFormattedTextImpl CreateFormattedText(
  36. string text,
  37. string fontFamily,
  38. double fontSize,
  39. FontStyle fontStyle,
  40. TextAlignment textAlignment,
  41. Avalonia.Media.FontWeight fontWeight,
  42. TextWrapping wrapping)
  43. {
  44. return new FormattedTextImpl(s_pangoContext, text, fontFamily, fontSize, fontStyle, textAlignment, fontWeight);
  45. }
  46. public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
  47. {
  48. var accessor = surfaces?.OfType<Func<Gdk.Drawable>>().FirstOrDefault();
  49. if(accessor!=null)
  50. return new RenderTarget(accessor);
  51. throw new NotSupportedException(string.Format(
  52. "Don't know how to create a Cairo renderer from any of the provided surfaces."));
  53. }
  54. public IRenderTargetBitmapImpl CreateRenderTargetBitmap(int width, int height)
  55. {
  56. return new RenderTargetBitmapImpl(new ImageSurface(Format.Argb32, width, height));
  57. }
  58. public IStreamGeometryImpl CreateStreamGeometry()
  59. {
  60. return new StreamGeometryImpl();
  61. }
  62. public IBitmapImpl LoadBitmap(string fileName)
  63. {
  64. var pixbuf = new Gdk.Pixbuf(fileName);
  65. return new BitmapImpl(pixbuf);
  66. }
  67. public IBitmapImpl LoadBitmap(Stream stream)
  68. {
  69. var pixbuf = new Gdk.Pixbuf(stream);
  70. return new BitmapImpl(pixbuf);
  71. }
  72. private static Pango.Context CreatePangoContext()
  73. {
  74. Gtk.Application.Init();
  75. return new Gtk.Invisible().CreatePangoContext();
  76. }
  77. public IBitmapImpl LoadBitmap(PixelFormat format, IntPtr data, int width, int height, int stride)
  78. {
  79. throw new NotSupportedException("No proper control over pixel format with Cairo, use Skia backend instead");
  80. }
  81. public IWritableBitmapImpl CreateWritableBitmap(int width, int height, PixelFormat? fmt)
  82. {
  83. throw new NotSupportedException("No proper support with Cairo, use Skia backend instead");
  84. }
  85. }
  86. }