-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormulaTextSource.cs
More file actions
170 lines (137 loc) · 5.93 KB
/
FormulaTextSource.cs
File metadata and controls
170 lines (137 loc) · 5.93 KB
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
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Vortice;
using Vortice.DCommon;
using Vortice.Direct2D1;
using Vortice.DXGI;
using Vortice.Mathematics;
using Vortice.WIC;
using WpfMath.Parsers;
using WpfMath.Rendering;
using XamlMath;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Player.Video;
namespace Formula
{
internal class FormulaTextSource : IShapeSource
{
readonly private IGraphicsDevicesAndContext devices;
readonly private FormulaTextParameter formulaTextParameter;
private double _size;
private string _text;
private System.Windows.Media.Color _color;
ID2D1CommandList? commandList;
public ID2D1Image Output => commandList ?? throw new Exception($"{nameof(commandList)}がnullです。事前にUpdateを呼び出す必要があります。");
public FormulaTextSource(IGraphicsDevicesAndContext devices, FormulaTextParameter formulaTextParameter)
{
this.devices = devices;
this.formulaTextParameter = formulaTextParameter;
_text = string.Empty;
}
public void Update(TimelineItemSourceDescription timelineItemSourceDescription)
{
var fps = timelineItemSourceDescription.FPS;
var frame = timelineItemSourceDescription.ItemPosition.Frame;
var length = timelineItemSourceDescription.ItemDuration.Frame;
var size = formulaTextParameter.Size.GetValue(frame, length, fps);
var text = formulaTextParameter.Text;
var color = formulaTextParameter.Color;
//サイズが変わっていない場合は何もしない
if (commandList != null && _size == size && _text == text && _color == color)
return;
var dc = devices.DeviceContext;
commandList?.Dispose();//新規作成前に、前回のCommandListを必ず破棄する
commandList = dc.CreateCommandList();
dc.Target = commandList;
dc.BeginDraw();
dc.Clear(null);
if (!string.IsNullOrWhiteSpace(text))
{
var formulaBitmap = CreateFormulaBitmap(dc, text, size, color);
if (formulaBitmap != null)
{
var sourceRect = new RawRectF(
0, 0,
formulaBitmap.PixelSize.Width,
formulaBitmap.PixelSize.Height
);
var drawRect = new RawRectF(
-(float)formulaBitmap.Size.Width / 2,
-(float)formulaBitmap.Size.Height / 2,
(float)formulaBitmap.Size.Width / 2,
(float)formulaBitmap.Size.Height / 2
);
dc.DrawBitmap(formulaBitmap, drawRect, 1.0f, Vortice.Direct2D1.BitmapInterpolationMode.Linear, sourceRect);
formulaBitmap.Dispose();
}
}
dc.EndDraw();
dc.Target = null;//Targetは必ずnullに戻す。
commandList.Close();//CommandListはEndDraw()の後に必ずClose()を呼んで閉じる必要がある
_size = size;
_text = text;
_color = color;
}
private static ID2D1Bitmap1 CreateFormulaBitmap(ID2D1DeviceContext dc, string tex, double targetSize, System.Windows.Media.Color color)
{
try
{
var parser = WpfTeXFormulaParser.Instance;
var formula = parser.Parse(tex);
var brush = new SolidColorBrush(color);
var environment = WpfTeXEnvironment.Create(TexStyle.Display, 30.0, "Arial", brush);
double baseDpi = 300;
double dynamicDpi = baseDpi * (targetSize / 30.0);
dynamicDpi = Math.Clamp(dynamicDpi, 1, int.MaxValue);
var originalBitmap = formula.RenderToBitmap(environment, dpi: dynamicDpi);
originalBitmap.Freeze();
var stream = new MemoryStream();
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(originalBitmap));
encoder.Save(stream);
stream.Position = 0;
var imagingFactory = new IWICImagingFactory();
var decoder = imagingFactory.CreateDecoderFromStream(stream, DecodeOptions.CacheOnLoad);
var frame = decoder.GetFrame(0);
var formatConverter = imagingFactory.CreateFormatConverter();
formatConverter.Initialize(frame, Vortice.WIC.PixelFormat.Format32bppPBGRA);
var bitmap = dc.CreateBitmapFromWicBitmap(formatConverter);
stream.Dispose();
formatConverter.Dispose();
frame.Dispose();
decoder.Dispose();
return bitmap;
}
catch (Exception)
{
var bmpProps = new BitmapProperties1(
new Vortice.DCommon.PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)
);
return dc.CreateBitmap(
new SizeI(1, 1),
IntPtr.Zero,
0,
bmpProps
);
}
}
private bool disposedValue;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
commandList?.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}