Skip to content

Commit

Permalink
Add SkiaDemo for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancheung committed Apr 14, 2024
1 parent cd4b4a8 commit 69ed029
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 3 deletions.
18 changes: 17 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Launch Generator (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "buildGenerator",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/FreeTypeSharp.Generator/bin/Debug/net8.0/FreeTypeSharp.Generator.dll",
"args": [
Expand All @@ -22,6 +22,22 @@
"console": "internalConsole",
"stopAtEntry": false
},
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": "Launch SkiaDemo (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/FreeTypeSharp.SkiaDemo/bin/Debug/net8.0/FreeTypeSharp.SkiaDemo.dll",
"args": [],
"cwd": "${workspaceFolder}/FreeTypeSharp.SkiaDemo/bin/Debug/net8.0",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"version": "2.0.0",
"tasks": [
{
"label": "buildGenerator",
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/FreeTypeSharp.Generator/FreeTypeSharp.Generator.csproj",
"${workspaceFolder}/FreeTypeSharp.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
Expand Down
20 changes: 20 additions & 0 deletions FreeTypeSharp.SkiaDemo/FreeTypeSharp.SkiaDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SixLabors.Fonts" Version="2.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FreeTypeSharp\FreeTypeSharp.csproj" />
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions FreeTypeSharp.SkiaDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.IO;
using SkiaSharp;
using SixLabors.Fonts;
using System.Runtime.InteropServices;
using System.Globalization;

namespace FreeTypeSharp.Demo
{
public class Program
{
static string? GetSystemFont(string fontName)
{
FontFamily search = new();
bool found = false;
foreach (var fontFamily in SystemFonts.Families)
{
if (fontFamily.Name == fontName)
{
search = fontFamily;
found = true;
}
else
{
if (fontFamily.TryGetMetrics(FontStyle.Regular, out var metrics))
{
if (metrics.Description.FontName(CultureInfo.CurrentCulture) == fontName)
{
search = fontFamily;
found = true;
}
}
}
}

if (found)
{
var font = search.CreateFont(0, FontStyle.Regular);
if (font.TryGetPath(out var path))
return path;
}

return null;
}
unsafe static void Main(string[] args)
{
var library = new FreeTypeLibrary();
FT_FaceRec_* face;

var fontPath = GetSystemFont("Microsoft Sans Serif");
var error = FT.FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi(fontPath), 0, &face);
error = FT.FT_Set_Char_Size(face, 0, 16 * 64, 300, 300);
var glyphIndex = FT.FT_Get_Char_Index(face, 'я');
error = FT.FT_Load_Glyph(face, glyphIndex, FT_LOAD.FT_LOAD_DEFAULT);
error = FT.FT_Render_Glyph(face->glyph, FT_Render_Mode_.FT_RENDER_MODE_NORMAL);
var bitmap = face->glyph->bitmap;

var skBitmap = new SKBitmap((int)bitmap.width, (int)bitmap.rows);
var canvas = new SKCanvas(skBitmap);

for (var i = 0; i != bitmap.width; i++)
{
for (var j = 0; j != bitmap.rows; j++)
{
canvas.DrawPoint(new SKPoint(i, j), new SKColor(bitmap.buffer[j*bitmap.pitch+i], bitmap.buffer[j * bitmap.pitch + i], bitmap.buffer[j * bitmap.pitch + i], bitmap.buffer[j * bitmap.pitch + i]));
}
}

using(var fileStream = File.OpenWrite("some.jpg"))
{
using (var wstream = new SKManagedWStream(fileStream))
{
skBitmap.Encode(wstream, SKEncodedImageFormat.Png, 100);
}
}
}
}
}
14 changes: 14 additions & 0 deletions FreeTypeSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeTypeSharp.Android.Test"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeTypeSharp.Generator", "FreeTypeSharp.Generator\FreeTypeSharp.Generator.csproj", "{B94334B4-904F-401C-840E-EB52A62A1F5F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeTypeSharp.SkiaDemo", "FreeTypeSharp.SkiaDemo\FreeTypeSharp.SkiaDemo.csproj", "{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -89,6 +91,18 @@ Global
{B94334B4-904F-401C-840E-EB52A62A1F5F}.Release|x64.Build.0 = Release|Any CPU
{B94334B4-904F-401C-840E-EB52A62A1F5F}.Release|x86.ActiveCfg = Release|Any CPU
{B94334B4-904F-401C-840E-EB52A62A1F5F}.Release|x86.Build.0 = Release|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Debug|x64.ActiveCfg = Debug|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Debug|x64.Build.0 = Debug|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Debug|x86.ActiveCfg = Debug|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Debug|x86.Build.0 = Debug|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Release|Any CPU.Build.0 = Release|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Release|x64.ActiveCfg = Release|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Release|x64.Build.0 = Release|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Release|x86.ActiveCfg = Release|Any CPU
{4A9BCD45-6E9F-41CB-93CC-9F91E51311F1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 69ed029

Please sign in to comment.