Skip to content

Commit

Permalink
Add .NET Interactive Support (#1710)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored May 30, 2021
1 parent 1916562 commit 11591db
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 124 deletions.
3 changes: 3 additions & 0 deletions nuget/SkiaSharp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ Please visit https://go.microsoft.com/fwlink/?linkid=868517 to view the release
<!-- watchos -->
<file platform="macos" src="build/xamarinwatchos1.0/libSkiaSharp.framework/**/*" target="build/xamarinwatchos1.0/libSkiaSharp.framework" />

<!-- .NET interactive -->
<file src="interactive-extensions/dotnet/SkiaSharp.DotNet.Interactive.dll" />

<!-- legal -->
<file src="LICENSE.txt" />
<file src="THIRD-PARTY-NOTICES.txt" />
Expand Down
33 changes: 33 additions & 0 deletions source/SkiaSharp.DotNet.Interactive/ColorRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Html;

using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags;

namespace SkiaSharp.DotNet.Interactive
{
public static class ColorRenderer
{
public static IHtmlContent Render(this SKColor color)
{
var colorString = color.Alpha == 255
? $"rgb({color.Red},{color.Green},{color.Blue})"
: $"rgba({color.Red},{color.Green},{color.Blue},{color.Alpha / 255.0:0.0##})";

return GetHtml(colorString);
}

public static IHtmlContent Render(this SKColorF color) =>
((SKColor)color).Render();

private static IHtmlContent GetHtml(string colorString) =>
div(
span[style:
$"width: 2em; " +
$"background: {colorString}; " +
$"display: inline-block; " +
$"border: 1px solid black; "](
new HtmlString("&nbsp;")),
span(
new HtmlString("&nbsp;"),
colorString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Reflection;
using System.Resources;

[assembly: AssemblyTitle("SkiaSharp.Workbooks")]
[assembly: AssemblyDescription("SkiaSharp.Workbooks adds functionality for SkiaSharp to Xamarin.Workbooks.")]
[assembly: AssemblyTitle("SkiaSharp.DotNet.Interactive")]
[assembly: AssemblyDescription("SkiaSharp.DotNet.Interactive adds functionality for SkiaSharp to .NET Interactive notebooks.")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyProduct("SkiaSharp.Workbooks")]
[assembly: AssemblyProduct("SkiaSharp.DotNet.Interactive")]
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
[assembly: NeutralResourcesLanguage("en")]

Expand Down
48 changes: 48 additions & 0 deletions source/SkiaSharp.DotNet.Interactive/RasterRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Microsoft.AspNetCore.Html;

using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags;

namespace SkiaSharp.DotNet.Interactive
{
public static class RasterRenderer
{
public static IHtmlContent Render(this SKBitmap bitmap)
{
using var image = SKImage.FromBitmap(bitmap);
return Render(image);
}

public static IHtmlContent Render(this SKPixmap pixmap)
{
using var image = SKImage.FromPixels(pixmap);
return Render(image);
}

public static IHtmlContent Render(this SKPicture picture)
{
using var image = SKImage.FromPicture(picture, picture.CullRect.Size.ToSizeI());
return Render(image);
}

public static IHtmlContent Render(this SKSurface surface)
{
using var image = surface.Snapshot();
return Render(image);
}

public static IHtmlContent Render(this SKImage image)
{
using var data = image.Encode(SKEncodedImageFormat.Png, 100);
return GetHtml(data);
}

private static IHtmlContent GetHtml(SKData data)
{
var base64 = Convert.ToBase64String(data.AsSpan());

return div(
img[src: "data:image/png;base64," + base64]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>SkiaSharp.DotNet.Interactive</RootNamespace>
<AssemblyName>SkiaSharp.DotNet.Interactive</AssemblyName>
<MDocDocumentationDirectory></MDocDocumentationDirectory>
<SignAssembly>false</SignAssembly>
<PackagingGroup>SkiaSharp</PackagingGroup>
<PackagingLocation>interactive-extensions\dotnet</PackagingLocation>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="microsoft.dotnet.interactive" Version="1.0.0-beta.21255.3" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\binding\SkiaSharp\SkiaSharp.csproj" />
</ItemGroup>

<!-- uncomment this block for local testing -->
<!--<PropertyGroup>
<IncludeBuildOutput>false</IncludeBuildOutput>
<IsPackable>true</IsPackable>
<PackageVersion>0.0.1-preview.$([System.DateTime]::Now.ToString('yyyy.M.d.H.m.s'))</PackageVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Update="..\..\binding\SkiaSharp\SkiaSharp.csproj" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\output\native\osx\libSkiaSharp*" Pack="true" PackagePath="runtimes\osx\native\%(Filename)%(Extension)" />
<None Include="..\..\output\native\windows\x64\libSkiaSharp*" Pack="true" PackagePath="runtimes\win-x64\native\%(Filename)%(Extension)" />
<None Include="..\..\output\native\windows\x86\libSkiaSharp*" Pack="true" PackagePath="runtimes\win-x86\native\%(Filename)%(Extension)" />
<None Include="..\..\output\native\windows\arm64\libSkiaSharp*" Pack="true" PackagePath="runtimes\win-arm64\native\%(Filename)%(Extension)" />
<None Include="..\..\output\native\linux\x64\libSkiaSharp*" Pack="true" PackagePath="runtimes\linux-x64\native\%(Filename)%(Extension)" />
<None Include="..\..\output\native\linux\x86\libSkiaSharp*" Pack="true" PackagePath="runtimes\linux-x86\native\%(Filename)%(Extension)" />
<None Include="..\..\output\native\linux\arm64\libSkiaSharp*" Pack="true" PackagePath="runtimes\linux-arm64\native\%(Filename)%(Extension)" />
<None Include="..\..\output\native\linux\arm\libSkiaSharp*" Pack="true" PackagePath="runtimes\linux-arm\native\%(Filename)%(Extension)" />
<None Include="..\..\output\SkiaSharp\nuget\lib\netstandard2.0\SkiaSharp.dll" Pack="true" PackagePath="lib\netstandard2.0\SkiaSharp.dll" />
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="$(PackagingLocation)" />
</ItemGroup>-->

</Project>
27 changes: 27 additions & 0 deletions source/SkiaSharp.DotNet.Interactive/SkiaSharpKernelExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Formatting;

namespace SkiaSharp.DotNet.Interactive
{
public class SkiaSharpKernelExtension : IKernelExtension
{
public Task OnLoadAsync(Kernel kernel)
{
// colors
Formatter.Register<SKColor>((color, writer) => writer.Write(color.Render()), "text/html");
Formatter.Register<SKColorF>((color, writer) => writer.Write(color.Render()), "text/html");

// "images"
Formatter.Register<SKBitmap>((bmp, writer) => writer.Write(bmp.Render()), "text/html");
Formatter.Register<SKPixmap>((pix, writer) => writer.Write(pix.Render()), "text/html");
Formatter.Register<SKPicture>((pic, writer) => writer.Write(pic.Render()), "text/html");
Formatter.Register<SKSurface>((surface, writer) => writer.Write(surface.Render()), "text/html");
Formatter.Register<SKImage>((img, writer) => writer.Write(img.Render()), "text/html");

// TODO: colorspaces and other things

return Task.CompletedTask;
}
}
}
17 changes: 0 additions & 17 deletions source/SkiaSharp.Workbooks/SkiaSharp.Workbooks.csproj

This file was deleted.

92 changes: 0 additions & 92 deletions source/SkiaSharp.Workbooks/SkiaSharpRepresentationProvider.cs

This file was deleted.

12 changes: 0 additions & 12 deletions source/SkiaSharp.Workbooks/SkiaWorkbooksIntegration.cs

This file was deleted.

9 changes: 9 additions & 0 deletions source/SkiaSharpSource.Linux.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Views.Uno.Wasm",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Views.Uno.Reference", "SkiaSharp.Views.Uno\SkiaSharp.Views.Uno.Reference\SkiaSharp.Views.Uno.Reference.csproj", "{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SkiaSharp.DotNet.Interactive", "SkiaSharp.DotNet.Interactive", "{E0EDA875-C25F-4727-BB98-6EF7BBAFEB4B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.DotNet.Interactive", "SkiaSharp.DotNet.Interactive\SkiaSharp.DotNet.Interactive.csproj", "{03A0F2E5-345D-4FA2-B1F8-45D2D7EEA3EB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -113,6 +117,10 @@ Global
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB}.Release|Any CPU.Build.0 = Release|Any CPU
{03A0F2E5-345D-4FA2-B1F8-45D2D7EEA3EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03A0F2E5-345D-4FA2-B1F8-45D2D7EEA3EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03A0F2E5-345D-4FA2-B1F8-45D2D7EEA3EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03A0F2E5-345D-4FA2-B1F8-45D2D7EEA3EB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -133,6 +141,7 @@ Global
{4606F63B-F0B1-418E-8B45-844361E17EB1} = {66A0800A-CA70-4AC2-88B7-8E7C4CA7C914}
{08599A28-FADE-43B5-95A0-BDCB22B97D3A} = {00C9099A-BE4F-40D0-9401-295618368370}
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB} = {00C9099A-BE4F-40D0-9401-295618368370}
{03A0F2E5-345D-4FA2-B1F8-45D2D7EEA3EB} = {E0EDA875-C25F-4727-BB98-6EF7BBAFEB4B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B4229170-607D-4886-9990-8FD5D86220B9}
Expand Down
9 changes: 9 additions & 0 deletions source/SkiaSharpSource.Mac.sln
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Views.Uno.Wasm",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Views.Uno.Reference", "SkiaSharp.Views.Uno\SkiaSharp.Views.Uno.Reference\SkiaSharp.Views.Uno.Reference.csproj", "{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.DotNet.Interactive", "SkiaSharp.DotNet.Interactive\SkiaSharp.DotNet.Interactive.csproj", "{A23C2451-BBCE-4309-B80F-CEE482A140C7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SkiaSharp.DotNet.Interactive", "SkiaSharp.DotNet.Interactive", "{A54F6AE5-2921-4C68-846A-25D38F1C2E7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -227,6 +231,10 @@ Global
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB}.Release|Any CPU.Build.0 = Release|Any CPU
{A23C2451-BBCE-4309-B80F-CEE482A140C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A23C2451-BBCE-4309-B80F-CEE482A140C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A23C2451-BBCE-4309-B80F-CEE482A140C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A23C2451-BBCE-4309-B80F-CEE482A140C7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -266,6 +274,7 @@ Global
{E0FF9BFF-2253-4486-A8E2-94312D09D056} = {E53351EA-FE40-47D2-B388-4A75DA6CCD71}
{242213FA-1F0E-40BB-A9B2-6FEE82B5D216} = {E53351EA-FE40-47D2-B388-4A75DA6CCD71}
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB} = {E53351EA-FE40-47D2-B388-4A75DA6CCD71}
{A23C2451-BBCE-4309-B80F-CEE482A140C7} = {A54F6AE5-2921-4C68-846A-25D38F1C2E7E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4B8BED07-EA83-4BC1-B3BB-6B8EBC54C25F}
Expand Down
9 changes: 9 additions & 0 deletions source/SkiaSharpSource.Windows.sln
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Views.WinUI", "Sk
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SkiaSharp.Views.WinUI", "SkiaSharp.Views.WinUI", "{32AE56A0-F3E1-4721-9BED-B1F609DEA8E8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SkiaSharp.DotNet.Interactive", "SkiaSharp.DotNet.Interactive", "{B7F572BF-AD3F-4AC8-A654-3ED241E3ECA4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.DotNet.Interactive", "SkiaSharp.DotNet.Interactive\SkiaSharp.DotNet.Interactive.csproj", "{73185ECB-5A38-447C-95CF-D32D6784B97D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -187,6 +191,10 @@ Global
{F50AE765-D9B9-4025-88A8-E8B080D5D624}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F50AE765-D9B9-4025-88A8-E8B080D5D624}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F50AE765-D9B9-4025-88A8-E8B080D5D624}.Release|Any CPU.Build.0 = Release|Any CPU
{73185ECB-5A38-447C-95CF-D32D6784B97D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73185ECB-5A38-447C-95CF-D32D6784B97D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73185ECB-5A38-447C-95CF-D32D6784B97D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73185ECB-5A38-447C-95CF-D32D6784B97D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -219,6 +227,7 @@ Global
{F675A841-ECDB-4571-8AA3-7D4642FD57DB} = {E53351EA-FE40-47D2-B388-4A75DA6CCD71}
{D5C7D157-609E-4D64-8E42-C1DB0FAA86EB} = {E53351EA-FE40-47D2-B388-4A75DA6CCD71}
{F50AE765-D9B9-4025-88A8-E8B080D5D624} = {32AE56A0-F3E1-4721-9BED-B1F609DEA8E8}
{73185ECB-5A38-447C-95CF-D32D6784B97D} = {B7F572BF-AD3F-4AC8-A654-3ED241E3ECA4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {63A8C36E-4917-46DB-9AAD-522B8EC00C23}
Expand Down
Loading

0 comments on commit 11591db

Please sign in to comment.