-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypst.cs
More file actions
45 lines (38 loc) · 1.28 KB
/
Typst.cs
File metadata and controls
45 lines (38 loc) · 1.28 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
namespace Altafraner.Typst;
using System.Collections.Concurrent;
using System.Text.Json;
using System.Threading;
using Microsoft.Extensions.Options;
///
public class Typst
{
private readonly TypstConfiguration _config;
private readonly int _threadCount;
private int _nextThread;
private readonly ConcurrentDictionary<string, CompilerSafe>[] _compilerCaches;
///
public Typst(IOptions<TypstConfiguration> typstConfiguration)
{
_config = typstConfiguration.Value;
_threadCount = _config.NumThreads;
_compilerCaches = new ConcurrentDictionary<string, CompilerSafe>[_threadCount];
for (int i = 0; i < _threadCount; i++)
{
_compilerCaches[i] = new ConcurrentDictionary<string, CompilerSafe>();
}
}
///
public byte[] GeneratePdf(string source, object inputData)
{
var threadIndex = (int)((uint)Interlocked.Increment(ref _nextThread) % _threadCount);
var cache = _compilerCaches[threadIndex];
var compiler = cache.GetOrAdd(source, s =>
new CompilerSafe(
_config.TypstResourcePath,
s,
_config.TypstFontPaths ?? []
)
);
return compiler.CompileWithInputs(JsonSerializer.Serialize(inputData));
}
}