Skip to content

Commit ee025d5

Browse files
Merge pull request #37 from SixLabors/better-draw-on-path-api
Use more effecient draw text apis that support emojii
2 parents 7a94930 + 6168903 commit ee025d5

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

ImageSharp/DrawingTextAlongAPath/DrawingTextAlongAPath.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,14 @@
1010
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.3" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<None Update="Roboto-Regular.ttf">
15+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Twemoji Mozilla.ttf">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
1322

1423
</Project>

ImageSharp/DrawingTextAlongAPath/Program.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static class Program
1414
{
1515
static void Main(string[] args)
1616
{
17+
1718
System.IO.Directory.CreateDirectory("output");
1819

1920
using Image img = new Image<Rgba32>(1500, 500);
@@ -24,19 +25,20 @@ static void Main(string[] args)
2425
// Add more complex paths and shapes here.
2526
IPath path = pathBuilder.Build();
2627

27-
// For production application we would recommend you create a FontCollection
28-
// singleton and manually install the ttf fonts yourself as using SystemFonts
29-
// can be expensive and you risk font existing or not existing on a deployment
30-
// by deployment basis.
31-
Font font = SystemFonts.CreateFont("Segoe UI", 39, FontStyle.Regular);
32-
const string text = "Hello World Hello World Hello World Hello World Hello World";
28+
var fonts = new FontCollection();
29+
var mainFont = fonts.Add("Roboto-Regular.ttf");
30+
var emojiiFont = fonts.Add("Twemoji Mozilla.ttf");
31+
32+
const string text = "Hello World 🎉🎉 Hello World Hello World Hello World Hello World";
3333

3434
// Draw the text along the path wrapping at the end of the line
35-
TextOptions textOptions = new(font)
35+
RichTextOptions textOptions = new(mainFont.CreateFont(39, FontStyle.Regular))
3636
{
3737
WrappingLength = path.ComputeLength(),
3838
VerticalAlignment = VerticalAlignment.Bottom,
3939
HorizontalAlignment = HorizontalAlignment.Left,
40+
FallbackFontFamilies = [emojiiFont],
41+
Path = path
4042
};
4143

4244
// Let's generate the text as a set of vectors drawn along the path
@@ -45,7 +47,7 @@ static void Main(string[] args)
4547
img.Mutate(ctx => ctx
4648
.Fill(Color.White) // white background image
4749
.Draw(Color.Gray, 3, path) // draw the path so we can see what the text is supposed to be following
48-
.Fill(Color.Black, glyphs));
50+
.DrawText(textOptions, text, new SolidBrush(Color.Gray)));
4951

5052
img.Save("output/wordart.png");
5153
}
164 KB
Binary file not shown.
1.37 MB
Binary file not shown.

0 commit comments

Comments
 (0)