typst 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/ext/typst/src/lib.wip.rs +203 -0
  3. metadata +5 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43d8c84a75186f58f150a1b401f023269a87b65ff4181622d12ee0605e44e12c
4
- data.tar.gz: b04f05668004a681b3dbf23cd8c5741e7a5d88418b1b282144b8a855575e9ae1
3
+ metadata.gz: 85a0ad3847a1a278f6f47ab94fc447860fb583736774ab4c7c81e5fc871d8d7f
4
+ data.tar.gz: e9a4f127a9f9b2780a2b58efc413f3cbef4f62b0fbae1d2649e670ef552a87da
5
5
  SHA512:
6
- metadata.gz: 90852c841c355b42fd6f305627d6ce61e3ad2321e56e8a45bab1eff715888f232f76779eb217375e0da4855c24fd139cb14a86dea2a4aa53aeacccc816ece7da
7
- data.tar.gz: 2e661ba34cdef35d3e9911b461cbd06815c31a2ac4408fa6be4a3395519f8a159bf3a3e31ae6cc573f78dbbe14a8818f0089dacf59f6ad6d779e80c79d02733d
6
+ metadata.gz: b75ae52ebb916a9a9fd34b7a77816ddf86f38aaeb3878a1135e135bbb468f712751b2bcd288dc947d884b8cdca64a3fd267b38f405dc8f12976dda9d8e66d059
7
+ data.tar.gz: bccf2c3206193e12ae7ec4805b61c1d953abda079866eca4063d221973d6491cee3d8c959fb66cfc3b4b5994cd915f85d19e748617f998c4c816ddd67dac3205
@@ -0,0 +1,203 @@
1
+ use std::path::PathBuf;
2
+ use std::env;
3
+
4
+ use std::{
5
+ cell::RefCell,
6
+ fmt,
7
+ hash::{Hash, Hasher},
8
+ };
9
+
10
+ use magnus::r_string::IntoRString;
11
+ //use magnus::{function, exception, Error, IntoValue};
12
+ use magnus::{
13
+ class, define_class, exception, method, module, function,
14
+ prelude::*,
15
+ scan_args::{get_kwargs, scan_args},
16
+ typed_data, error::Error, Value, IntoValue, Symbol
17
+ };
18
+
19
+ use world::SystemWorld;
20
+
21
+ mod compiler;
22
+ mod download;
23
+ mod fonts;
24
+ mod package;
25
+ mod world;
26
+
27
+ fn compile(
28
+ input: PathBuf,
29
+ output: Option<PathBuf>,
30
+ root: Option<PathBuf>,
31
+ font_paths: Vec<PathBuf>,
32
+ ) -> Result<magnus::Value, Error> {
33
+ let input = input.canonicalize()
34
+ .map_err(|err| magnus::Error::new(exception::arg_error(), err.to_string()))?;
35
+
36
+ let root = if let Some(root) = root {
37
+ root.canonicalize()
38
+ .map_err(|err| magnus::Error::new(exception::arg_error(), err.to_string()))?
39
+ } else if let Some(dir) = input.parent() {
40
+ dir.into()
41
+ } else {
42
+ PathBuf::new()
43
+ };
44
+
45
+ let resource_path = env::current_dir()
46
+ .map_err(|err| magnus::Error::new(exception::arg_error(), err.to_string()))?;
47
+
48
+ let mut default_fonts = Vec::new();
49
+ for entry in walkdir::WalkDir::new(resource_path.join("fonts")) {
50
+ let path = entry
51
+ .map_err(|err| magnus::Error::new(exception::arg_error(), err.to_string()))?
52
+ .into_path();
53
+ let Some(extension) = path.extension() else {
54
+ continue;
55
+ };
56
+ if extension == "ttf" || extension == "otf" {
57
+ default_fonts.push(path);
58
+ }
59
+ }
60
+
61
+ let mut world = SystemWorld::builder(root, input)
62
+ .font_paths(font_paths)
63
+ .font_files(default_fonts)
64
+ .build()
65
+ .map_err(|msg| magnus::Error::new(exception::arg_error(), msg.to_string()))?;
66
+
67
+ let pdf_bytes = world
68
+ .compile()
69
+ .map_err(|msg| magnus::Error::new(exception::arg_error(), msg.to_string()))?;
70
+
71
+ if let Some(output) = output {
72
+ std::fs::write(output, pdf_bytes)
73
+ .map_err(|_| magnus::Error::new(exception::arg_error(), "error"))?;
74
+
75
+ let value = true.into_value();
76
+ Ok(value)
77
+ } else {
78
+ let value = pdf_bytes.into_value();
79
+ Ok(value)
80
+ }
81
+ }
82
+
83
+ #[magnus::wrap(class = "Typst", free_immediately, size)]
84
+ #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd)]
85
+ struct Typst {
86
+ input: PathBuf,
87
+ output: Option<PathBuf>,
88
+ root: PathBuf,
89
+ font_paths: Vec<PathBuf>,
90
+ }
91
+
92
+ // can't derive this due to needing to use RefCell to get mutability
93
+ impl Hash for Typst {
94
+ fn hash<H: Hasher>(&self, state: &mut H) {
95
+ self.input.hash(state)
96
+ }
97
+ }
98
+
99
+ //#[magnus::wrap(class = "Typst")]
100
+ impl Typst {
101
+ fn initialize(rb_self: typed_data::Obj<Self>, args: &[Value]) -> Result<Value, Error> {
102
+ let args = scan_args::<_, _, (), (), _, ()>(args)?;
103
+ let (input,): (PathBuf,) = args.required;
104
+ let (output,): (
105
+ Option<PathBuf>,
106
+ ) = args.optional;
107
+
108
+ let kw = get_kwargs::<_, (), (Option<PathBuf>, Option<Vec<PathBuf>>), ()>(args.keywords, &[], &["root", "font_paths"])?;
109
+ let (root, font_paths) = kw.optional;
110
+
111
+ //*rb_self.input.borrow_mut() = input;
112
+ //*rb_self.output.borrow_mut() = output;
113
+ rb_self.input = input;
114
+ rb_self.output = output;
115
+
116
+ rb_self.root = if let Some(root) = root {
117
+ root
118
+ } else {
119
+ PathBuf::new()
120
+ };
121
+
122
+ rb_self.font_paths = if let Some(font_paths) = font_paths {
123
+ font_paths
124
+ } else {
125
+ let font_paths:Vec<PathBuf> = Vec::new();
126
+ font_paths
127
+ };
128
+
129
+ Ok(rb_self.into_value())
130
+ }
131
+
132
+ fn compile(rb_self: typed_data::Obj<Self>, args: &[Value]) -> Result<Value, Error> {
133
+ let resource_path = env::current_dir()
134
+ .map_err(|err| magnus::Error::new(exception::arg_error(), err.to_string()))?;
135
+
136
+ let mut default_fonts = Vec::new();
137
+ for entry in walkdir::WalkDir::new(resource_path.join("fonts")) {
138
+ let path = entry
139
+ .map_err(|err| magnus::Error::new(exception::arg_error(), err.to_string()))?
140
+ .into_path();
141
+ let Some(extension) = path.extension() else {
142
+ continue;
143
+ };
144
+ if extension == "ttf" || extension == "otf" {
145
+ default_fonts.push(path);
146
+ }
147
+ }
148
+
149
+ //let input = PathBuf::from(*rb_self.input.borrow());
150
+ //let root = PathBuf::from(*rb_self.root.borrow());
151
+ let input = PathBuf::from(&rb_self.input);
152
+ let root = PathBuf::from(&rb_self.root);
153
+ let font_paths: Vec<PathBuf> = Vec::new();
154
+ let mut world = SystemWorld::builder(root, input)
155
+ .font_paths(font_paths)
156
+ .font_files(default_fonts)
157
+ .build()
158
+ .map_err(|msg| magnus::Error::new(exception::arg_error(), msg.to_string()))?;
159
+
160
+ let pdf_bytes = world
161
+ .compile()
162
+ .map_err(|msg| magnus::Error::new(exception::arg_error(), msg.to_string()))?;
163
+
164
+ //let op: Option<PathBuf> = *rb_self.output.borrow();
165
+ //if let Some(output) = op {
166
+ // std::fs::write(output, pdf_bytes)
167
+ // .map_err(|_| magnus::Error::new(exception::arg_error(), "error"))?;
168
+
169
+ // let value = true.into_value();
170
+ // Ok(value)
171
+ //} else {
172
+ let value = pdf_bytes.into_value();
173
+ Ok(value)
174
+ //}
175
+ }
176
+ }
177
+
178
+ impl fmt::Display for Typst {
179
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180
+ write!(f, "{}", self.input.display())
181
+ }
182
+ }
183
+
184
+ #[magnus::init]
185
+ fn init() -> Result<(), Error> {
186
+ let class = define_class("Typst", class::object())?;
187
+
188
+ // Define alloc func based on the Default impl, plus an initialize method,
189
+ // rather than overwriting `new`, to allow class to be subclassed from Ruby
190
+ class.define_alloc_func::<Typst>();
191
+ class.define_method("initialize", method!(Typst::initialize, -1))?;
192
+
193
+ //class.define_singleton_method("foo", function!(foo, -1))?;
194
+
195
+ class.define_method("inspect",method!(<Typst as typed_data::Inspect>::inspect, 0))?;
196
+
197
+ class.define_method("to_s", method!(Typst::to_string, 0))?;
198
+ // class.define_method("barf", method!(Typst::barf, 0))?;
199
+
200
+ //let module = magnus::define_module("Typst").unwrap();
201
+ //module.define_module_function("compile", function!(compile, 4)).unwrap();
202
+ Ok(())
203
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typst
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flinn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-26 00:00:00.000000000 Z
11
+ date: 2023-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb_sys
@@ -42,6 +42,7 @@ files:
42
42
  - ext/typst/src/download.rs
43
43
  - ext/typst/src/fonts.rs
44
44
  - ext/typst/src/lib.rs
45
+ - ext/typst/src/lib.wip.rs
45
46
  - ext/typst/src/package.rs
46
47
  - ext/typst/src/world.rs
47
48
  - lib/fonts/DejaVuSansMono-Bold.ttf
@@ -71,14 +72,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
72
  requirements:
72
73
  - - ">="
73
74
  - !ruby/object:Gem::Version
74
- version: 3.2.2
75
+ version: 3.0.0
75
76
  required_rubygems_version: !ruby/object:Gem::Requirement
76
77
  requirements:
77
78
  - - ">="
78
79
  - !ruby/object:Gem::Version
79
80
  version: '0'
80
81
  requirements: []
81
- rubygems_version: 3.4.22
82
+ rubygems_version: 3.2.3
82
83
  signing_key:
83
84
  specification_version: 4
84
85
  summary: Ruby binding to typst, a new markup-based typesetting system that is powerful