-
Notifications
You must be signed in to change notification settings - Fork 56
/
Program.cs
170 lines (145 loc) · 6.68 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
namespace DrawingTextAlongAPath
{
static class Program
{
static void Main(string[] args)
{
System.IO.Directory.CreateDirectory("output");
using (Image img = new Image<Rgba32>(1500, 500))
{
PathBuilder pathBuilder = new PathBuilder();
pathBuilder.SetOrigin(new PointF(500, 0));
pathBuilder.AddCubicBezier(new PointF(50, 450), new PointF(200, 50), new PointF(300, 50), new PointF(450, 450));
// Add more complex paths and shapes here.
IPath path = pathBuilder.Build();
// For production application we would recomend you create a FontCollection
// singleton and manually install the ttf fonts yourself as using SystemFonts
// can be expensive and you risk font existing or not existing on a deployment
// by deployment basis.
var font = SystemFonts.CreateFont("Segoe UI", 39, FontStyle.Regular);
const string text = "Hello World Hello World Hello World Hello World Hello World";
// Draw the text along the path wrapping at the end of the line
var textOptions = new TextOptions(font)
{
WrappingLength = path.ComputeLength(),
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Left,
};
// Let's generate the text as a set of vectors drawn along the path
var glyphs = TextBuilder.GenerateGlyphs(text, path, textOptions);
img.Mutate(ctx => ctx
.Fill(Color.White) // white background image
.Draw(Color.Gray, 3, path) // draw the path so we can see what the text is supposed to be following
.Fill(Color.Black, glyphs));
img.Save("output/wordart.png");
}
}
public static IImageProcessingContext ApplyScalingWaterMark(this IImageProcessingContext processingContext,
Font font,
string text,
Color color,
float padding,
bool wordwrap)
{
if (wordwrap)
{
// return processingContext.ApplyScalingWaterMarkWordWrap(font, text, color, padding);
}
//else
{
return processingContext.ApplyScalingWaterMarkSimple(font, text, color, padding);
}
}
private static IImageProcessingContext ApplyScalingWaterMarkSimple(
this IImageProcessingContext processingContext,
Font font,
string text,
Color color,
float padding)
{
Size imgSize = processingContext.GetCurrentSize();
// Measure the text size
FontRectangle size = TextMeasurer.MeasureSize(text, new TextOptions(font));
// Find out how much we need to scale the text to fill the space (up or down)
float scalingFactor = Math.Min(imgSize.Width - padding / size.Width, imgSize.Height - padding / size.Height);
// Create a new font
Font scaledFont = new Font(font, scalingFactor * font.Size);
var center = new PointF(imgSize.Width / 2, imgSize.Height / 2);
var textOptions = new RichTextOptions(scaledFont)
{
Origin = center,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
return processingContext.DrawText(textOptions, text, color);
}
//private static IImageProcessingContext ApplyScalingWaterMarkWordWrap(
// this IImageProcessingContext processingContext,
// Font font,
// string text,
// Color color,
// float padding)
//{
// Size imgSize = processingContext.GetCurrentSize();
// float targetWidth = imgSize.Width - (padding * 2);
// float targetHeight = imgSize.Height - (padding * 2);
// float targetMinHeight = imgSize.Height - (padding * 3); // must be with in a margin width of the target height
// // now we are working i 2 dimensions at once and can't just scale because it will cause the text to
// // reflow we need to just try multiple times
// var scaledFont = font;
// FontRectangle s = new FontRectangle(0, 0, float.MaxValue, float.MaxValue);
// float scaleFactor = (scaledFont.Size / 2);// everytime we change direction we half this size
// int trapCount = (int)scaledFont.Size * 2;
// if (trapCount < 10)
// {
// trapCount = 10;
// }
// bool isTooSmall = false;
// while ((s.Height > targetHeight || s.Height < targetMinHeight) && trapCount > 0)
// {
// if (s.Height > targetHeight)
// {
// if (isTooSmall)
// {
// scaleFactor = scaleFactor / 2;
// }
// scaledFont = new Font(scaledFont, scaledFont.Size - scaleFactor);
// isTooSmall = false;
// }
// if (s.Height < targetMinHeight)
// {
// if (!isTooSmall)
// {
// scaleFactor = scaleFactor / 2;
// }
// scaledFont = new Font(scaledFont, scaledFont.Size + scaleFactor);
// isTooSmall = true;
// }
// trapCount--;
// s = TextMeasurer.Measure(text, new RendererOptions(scaledFont)
// {
// WrappingWidth = targetWidth
// });
// }
// var center = new PointF(padding, imgSize.Height / 2);
// var textGraphicsOptions = new TextGraphicsOptions()
// {
// TextOptions = {
// HorizontalAlignment = HorizontalAlignment.Left,
// VerticalAlignment = VerticalAlignment.Center,
// WrapTextWidth = targetWidth
// }
// };
// return processingContext.DrawText(textGraphicsOptions, text, scaledFont, color, center);
//}
}
}