-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd4b4a8
commit 69ed029
Showing
5 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters