SixLabors.ImageSharp.Drawing@1.0.0-beta15

+ clean useSystemFont + adjust vertical offset
This commit is contained in:
Julien Richard 2022-10-10 22:49:08 +02:00
commit 8fc49c47a9
5 changed files with 15 additions and 28 deletions

View file

@ -20,16 +20,11 @@ namespace Greenshot.Editor.Controls
set { SetValue(EmojiProperty, value); } set { SetValue(EmojiProperty, value); }
} }
private static void OnUseSystemFontPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((EmojiControl)d).Source = null;
}
protected override void OnRender(DrawingContext dc) protected override void OnRender(DrawingContext dc)
{ {
if (Source == null && !string.IsNullOrEmpty(Emoji)) if (Source == null && !string.IsNullOrEmpty(Emoji))
{ {
Source = EmojiRenderer.GetBitmapSource(Emoji, iconSize: 48, useSystemFont: false); Source = EmojiRenderer.GetBitmapSource(Emoji, iconSize: 48);
} }
base.OnRender(dc); base.OnRender(dc);

View file

@ -155,7 +155,7 @@ namespace Greenshot.Editor.Drawing
var iconSize = Math.Min(Bounds.Width, Bounds.Height); var iconSize = Math.Min(Bounds.Width, Bounds.Height);
if (iconSize <= 0) return null; if (iconSize <= 0) return null;
var image = EmojiRenderer.GetBitmap(Emoji, iconSize, useSystemFont: false); var image = EmojiRenderer.GetBitmap(Emoji, iconSize);
if (RotationAngle != 0) if (RotationAngle != 0)
{ {
var newImage = image.Rotate(RotationAngle); var newImage = image.Rotate(RotationAngle);

View file

@ -30,30 +30,22 @@ namespace Greenshot.Editor.Drawing
return fontFamily; return fontFamily;
}); });
private static Lazy<FontFamily> _segoeUI = new Lazy<FontFamily>(() => public static Image<Rgba32> GetImage(string emoji, int iconSize)
{
using var fileStream = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"Fonts\seguiemj.ttf"), FileMode.Open, FileAccess.Read);
_fontCollection.Add(fileStream);
_fontCollection.TryGet("Segoe UI Emoji", out var fontFamily);
return fontFamily;
});
public static Image<Rgba32> GetImage(string emoji, int iconSize, bool useSystemFont)
{ {
var image = new Image<Rgba32>(iconSize, iconSize); var image = new Image<Rgba32>(iconSize, iconSize);
RenderEmoji(emoji, iconSize, image, useSystemFont); RenderEmoji(emoji, iconSize, image);
return image; return image;
} }
private static void RenderEmoji(string emoji, int iconSize, SixLabors.ImageSharp.Image image, bool useSystemFont) private static void RenderEmoji(string emoji, int iconSize, SixLabors.ImageSharp.Image image)
{ {
var fontFamily = useSystemFont ? _segoeUI.Value : _twemoji.Value; var fontFamily = _twemoji.Value;
var font = fontFamily.CreateFont(useSystemFont ? iconSize * 0.95f : iconSize, FontStyle.Regular); var font = fontFamily.CreateFont(iconSize, FontStyle.Regular);
var verticalOffset = useSystemFont ? -font.Size * 0.050f : font.Size * 0.045f; var verticalOffset = font.Size * 0.045f;
var textOptions = new TextOptions(font) var textOptions = new TextOptions(font)
{ {
Origin = new SixLabors.ImageSharp.PointF(font.Size / 2.0f, font.Size / 2.0f - verticalOffset), Origin = new SixLabors.ImageSharp.PointF(font.Size / 2.0f, font.Size / 2.0f + verticalOffset),
HorizontalAlignment = HorizontalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center VerticalAlignment = VerticalAlignment.Center
}; };
@ -61,7 +53,7 @@ namespace Greenshot.Editor.Drawing
image.Mutate(x => x.DrawText(textOptions, emoji, SixLabors.ImageSharp.Color.Black)); image.Mutate(x => x.DrawText(textOptions, emoji, SixLabors.ImageSharp.Color.Black));
} }
public static Image GetBitmap(string emoji, int iconSize, bool useSystemFont) public static Image GetBitmap(string emoji, int iconSize)
{ {
int width = iconSize; int width = iconSize;
int height = iconSize; int height = iconSize;
@ -74,7 +66,7 @@ namespace Greenshot.Editor.Drawing
unsafe unsafe
{ {
var image = SixLabors.ImageSharp.Image.WrapMemory<Bgra32>((void*)bitmapData.Scan0, width: width, height: height); var image = SixLabors.ImageSharp.Image.WrapMemory<Bgra32>((void*)bitmapData.Scan0, width: width, height: height);
RenderEmoji(emoji, iconSize, image, useSystemFont); RenderEmoji(emoji, iconSize, image);
} }
} }
finally finally
@ -85,7 +77,7 @@ namespace Greenshot.Editor.Drawing
return bitmap; return bitmap;
} }
public static BitmapSource GetBitmapSource(string emoji, int iconSize, bool useSystemFont) public static BitmapSource GetBitmapSource(string emoji, int iconSize)
{ {
var pixelFormat = PixelFormats.Bgra32; var pixelFormat = PixelFormats.Bgra32;
int width = iconSize; int width = iconSize;
@ -95,7 +87,7 @@ namespace Greenshot.Editor.Drawing
var image = SixLabors.ImageSharp.Image.WrapMemory<Bgra32>(byteMemory: pixels, width: width, height: height); var image = SixLabors.ImageSharp.Image.WrapMemory<Bgra32>(byteMemory: pixels, width: width, height: height);
RenderEmoji(emoji, iconSize, image, useSystemFont); RenderEmoji(emoji, iconSize, image);
var source = BitmapSource.Create(pixelWidth: width, pixelHeight: height, dpiX: 96, dpiY: 96, pixelFormat: pixelFormat, palette: null, pixels: pixels, stride: stride); var source = BitmapSource.Create(pixelWidth: width, pixelHeight: height, dpiX: 96, dpiY: 96, pixelFormat: pixelFormat, palette: null, pixels: pixels, stride: stride);
source.Freeze(); source.Freeze();

View file

@ -452,7 +452,7 @@ namespace Greenshot.Editor.Forms {
// //
this.btnEmoji.CheckOnClick = true; this.btnEmoji.CheckOnClick = true;
this.btnEmoji.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.btnEmoji.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.btnEmoji.Image = EmojiRenderer.GetBitmap("🙂", 32, useSystemFont: false); this.btnEmoji.Image = EmojiRenderer.GetBitmap("🙂", 32);
this.btnEmoji.ImageTransparentColor = System.Drawing.Color.Magenta; this.btnEmoji.ImageTransparentColor = System.Drawing.Color.Magenta;
this.btnEmoji.Text = "Emoji (M)"; this.btnEmoji.Text = "Emoji (M)";
this.btnEmoji.Name = "btnEmoji"; this.btnEmoji.Name = "btnEmoji";

View file

@ -8,7 +8,7 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" /> <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>