Skip to content

Commit

Permalink
use harfbuzz for text rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
chinatsu committed Nov 28, 2024
1 parent 0cef06d commit 28c58c1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/FMBot.Bot/FMBot.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<PackageReference Include="Serilog.Sinks.Discord" Version="1.2.1" />
<PackageReference Include="Serilog.Sinks.Seq" Version="8.0.0" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SkiaSharp.HarfBuzz" Version="2.88.8" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />
<PackageReference Include="SkiaSharp.Views.Desktop.Common" Version="2.88.8" />
<PackageReference Include="SpotifyAPI.Web" Version="7.1.1" />
Expand Down
1 change: 1 addition & 0 deletions src/FMBot.Bot/Models/ChartModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Discord;
using FMBot.Domain.Models;
using SkiaSharp;

using Color = System.Drawing.Color;

namespace FMBot.Bot.Models;
Expand Down
9 changes: 5 additions & 4 deletions src/FMBot.Bot/Services/ChartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using FMBot.Domain.Models;
using Serilog;
using SkiaSharp;
using SkiaSharp.HarfBuzz;
using Color = System.Drawing.Color;

namespace FMBot.Bot.Services;
Expand Down Expand Up @@ -525,14 +526,14 @@ private void AddTitleToChartImage(SKBitmap chartImage, bool largerImages, TopAlb

bitmapCanvas.DrawRoundRect(backgroundRectangle, 4, 4, rectanglePaint);

bitmapCanvas.DrawText(artistName, (float)chartImage.Width / 2,
bitmapCanvas.DrawShapedText(artistName, (float)chartImage.Width / 2,
-artistBounds.Top + chartImage.Height -
(albumName != null ? largerImages ? 39 : 26 : largerImages ? 20 : 13),
textPaint);

if (albumName != null)
{
bitmapCanvas.DrawText(albumName, (float)chartImage.Width / 2,
bitmapCanvas.DrawShapedText(albumName, (float)chartImage.Width / 2,
-albumBounds.Top + chartImage.Height - (largerImages ? 20 : 13),
textPaint);
}
Expand All @@ -559,8 +560,8 @@ private static void AddClassicTitleToChartImage(SKBitmap chartImage, TopAlbum al
textPaint.MeasureText(album.ArtistName, ref artistBounds);
textPaint.MeasureText(album.AlbumName, ref albumBounds);

bitmapCanvas.DrawText(album.ArtistName, 4, 12, textPaint);
bitmapCanvas.DrawText(album.AlbumName, 4, 22, textPaint);
bitmapCanvas.DrawShapedText(album.ArtistName, 4, 12, textPaint);
bitmapCanvas.DrawShapedText(album.AlbumName, 4, 22, textPaint);
}

public ChartSettings SetSettings(ChartSettings currentChartSettings, string[] extraOptions,
Expand Down
1 change: 1 addition & 0 deletions src/FMBot.Images/FMBot.Images.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />
<PackageReference Include="SkiaSharp.Views.Desktop.Common" Version="2.88.8" />
<PackageReference Include="SkiaSharp.HarfBuzz" Version="2.88.8" />
</ItemGroup>

<ItemGroup>
Expand Down
19 changes: 10 additions & 9 deletions src/FMBot.Images/Generators/PuppeteerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using PuppeteerSharp;
using Serilog;
using SkiaSharp;
using SkiaSharp.HarfBuzz;

namespace FMBot.Images.Generators;

Expand Down Expand Up @@ -585,7 +586,7 @@ private static void AddCountryListToMap(SKBitmap chartImage, List<GroupedCountri

bitmapCanvas.DrawRoundRect(backgroundRectangle, 12, 12, rectanglePaint);

bitmapCanvas.DrawText($"Artists per country", rectangleLeft + 170, rectangleTop + 58, textPaint);
bitmapCanvas.DrawShapedText($"Artists per country", rectangleLeft + 170, rectangleTop + 58, textPaint);

for (var index = 0; index < lines.Count; index++)
{
Expand All @@ -608,12 +609,12 @@ private static void AddCountryListToMap(SKBitmap chartImage, List<GroupedCountri
bitmapCanvas.DrawRoundRect(colorRectangle, 8, 8, colorRectanglePaint);

var text = line.MinAmount == line.MaxAmount ? $"{line.MinAmount}" : $"{line.MinAmount} - {line.MaxAmount}";
bitmapCanvas.DrawText(text, rectangleLeft + 200, rectangleTop + 129 + (index * 65), textPaint);
bitmapCanvas.DrawShapedText(text, rectangleLeft + 200, rectangleTop + 129 + (index * 65), textPaint);
}

bitmapCanvas.DrawText($"{lines.SelectMany(s => s.CountryCodes).Count()} countries", rectangleLeft + 170,
bitmapCanvas.DrawShapedText($"{lines.SelectMany(s => s.CountryCodes).Count()} countries", rectangleLeft + 170,
rectangleTop + 140 + ((lines.Count) * lineHeight), textPaint);
bitmapCanvas.DrawText($"Generated by .fmbot", rectangleLeft + 170,
bitmapCanvas.DrawShapedText($"Generated by .fmbot", rectangleLeft + 170,
rectangleTop + 170 + ((lines.Count) * lineHeight), promoTextPaint);
}

Expand Down Expand Up @@ -676,13 +677,13 @@ public void CreatePopularityIcebergImage(SKBitmap chartImage, string username, s
Typeface = typeface
};

bitmapCanvas.DrawText($"{username}'s", 590, 198, titlePaint);
bitmapCanvas.DrawShapedText($"{username}'s", 590, 198, titlePaint);

titlePaint.TextSize = 60;
bitmapCanvas.DrawText($"iceberg", 830, 262, titlePaint);
bitmapCanvas.DrawShapedText($"iceberg", 830, 262, titlePaint);

titlePaint.TextSize = 42;
bitmapCanvas.DrawText(timePeriod, 980, 415, titlePaint);
bitmapCanvas.DrawShapedText(timePeriod, 980, 415, titlePaint);

using var artistTextPaint = new SKPaint
{
Expand Down Expand Up @@ -736,10 +737,10 @@ public void CreatePopularityIcebergImage(SKBitmap chartImage, string username, s
artistTextPaint.Color = SKColors.White;
artistStrokePaint.Color = SKColors.Black;

bitmapCanvas.DrawText(sectionArtists[i].Name, position.X, position.Y, artistStrokePaint);
bitmapCanvas.DrawShapedText(sectionArtists[i].Name, position.X, position.Y, artistStrokePaint);
}

bitmapCanvas.DrawText(sectionArtists[i].Name, position.X, position.Y, artistTextPaint);
bitmapCanvas.DrawShapedText(sectionArtists[i].Name, position.X, position.Y, artistTextPaint);
}
}
}
Expand Down

0 comments on commit 28c58c1

Please sign in to comment.