latexml_core/binding/def/
traits.rs1use std::collections::VecDeque;
4
5use crate::{
6 common::{
7 arena, arena::SymHashMap as HashMap, color::Color, error::*, glue::Glue,
8 mudimension::MuDimension, muglue::MuGlue, number::Number, store::Stored,
9 },
10 definition::{Reversion, SizingClosure, argument::ArgWrap, register::*},
11 keyvals::KeyVals,
12 list::List,
13 state::{Scope, lookup_font},
14 token::*,
15 tokens::TeXString,
16 whatsit::Whatsit,
17 *,
18};
19
20fn sizer_options_from_whatsit(w: &Whatsit) -> HashMap<Stored> {
24 let mut options: HashMap<Stored> = HashMap::default();
25 for key in ["width", "height", "depth", "vattach", "layout", "mode"] {
26 if let Some(v) = w.get_property(key) {
27 options.insert(key, v.into_owned());
28 }
29 }
30 options
31}
32
33enum SizerRef {
35 Arg(usize),
36 Prop(String),
37}
38
39pub trait IntoOption<T>: Sized {
43 fn into_option(self) -> T;
45}
46
47impl IntoOption<Option<String>> for &str {
48 fn into_option(self) -> Option<String> { Some(self.to_string()) }
49}
50
51impl<T> IntoOption<Option<T>> for Option<T> {
52 fn into_option(self) -> Option<T> { self }
53}
54
55impl IntoOption<bool> for bool {
56 fn into_option(self) -> bool { self }
57}
58impl IntoOption<Option<bool>> for bool {
59 fn into_option(self) -> Option<bool> { Some(self) }
60}
61
62impl<T> IntoOption<Option<Vec<T>>> for Vec<T> {
63 fn into_option(self) -> Option<Vec<T>> { Some(self) }
64}
65
66impl<T> IntoOption<Option<VecDeque<T>>> for VecDeque<T> {
67 fn into_option(self) -> Option<VecDeque<T>> { Some(self) }
68}
69impl IntoOption<Option<usize>> for usize {
70 fn into_option(self) -> Option<usize> { Some(self) }
71}
72
73impl IntoOption<Option<Reversion>> for Tokens {
74 fn into_option(self) -> Option<Reversion> { Some(Reversion::Tokens(self)) }
75}
76impl IntoOption<Option<Reversion>> for &str {
77 fn into_option(self) -> Option<Reversion> {
78 if self.is_empty() {
79 Some(Reversion::Tokens(Tokens!()))
80 } else {
81 Some(Reversion::Tokens(
82 mouth::tokenize_internal(TeXString::assembled(self.to_string()))
88 .pack_parameters()
89 .ok()
90 .unwrap(),
91 ))
92 }
93 }
94}
95
96impl IntoOption<Option<Scope>> for &str {
97 fn into_option(self) -> Option<Scope> {
98 match self {
99 "" => None,
100 "local" => Some(Scope::Local),
101 "Local" => Some(Scope::Local),
102 "LOCAL" => Some(Scope::Local),
103 "global" => Some(Scope::Global),
104 "Global" => Some(Scope::Global),
105 "GLOBAL" => Some(Scope::Global),
106 other => Some(Scope::Named(arena::pin(other))),
107 }
108 }
109}
110impl IntoOption<Option<Scope>> for String {
111 fn into_option(self) -> Option<Scope> {
112 match self.as_ref() {
113 "" => None,
114 "local" => Some(Scope::Local),
115 "Local" => Some(Scope::Local),
116 "LOCAL" => Some(Scope::Local),
117 "global" => Some(Scope::Global),
118 "Global" => Some(Scope::Global),
119 "GLOBAL" => Some(Scope::Global),
120 _ => Some(Scope::Named(arena::pin(self))),
121 }
122 }
123}
124
125impl IntoOption<Option<SizingClosure>> for i64 {
127 fn into_option(self) -> Option<SizingClosure> {
128 Some(Rc::new(move |_| {
129 Ok((
130 Dimension::new(self),
131 Dimension::new(self),
132 Dimension::new(self),
133 ))
134 }))
135 }
136}
137impl IntoOption<Option<SizingClosure>> for &str {
138 fn into_option(self) -> Option<SizingClosure> {
139 if self.is_empty() {
140 None
141 } else if self == "0" {
142 Some(Rc::new(|_| {
143 Ok((
144 Dimension::default(),
145 Dimension::default(),
146 Dimension::default(),
147 ))
148 }))
149 } else if self.starts_with('#') {
150 let mut refs: Vec<SizerRef> = Vec::new();
153 let mut rest = self;
154 while let Some(stripped) = rest.strip_prefix('#') {
155 let end = stripped.find('#').unwrap_or(stripped.len());
156 let name = &stripped[..end];
157 if let Ok(n) = name.parse::<usize>() {
158 refs.push(SizerRef::Arg(n));
159 } else {
160 refs.push(SizerRef::Prop(name.to_string()));
161 }
162 rest = &stripped[end..];
163 }
164 Some(Rc::new(move |w| {
165 let mut boxes: Vec<Digested> = Vec::with_capacity(refs.len());
166 for r in &refs {
167 match r {
168 SizerRef::Arg(n) => {
169 if let Some(arg) = w.get_arg(*n) {
170 boxes.push(arg.clone());
171 }
172 },
173 SizerRef::Prop(name) => {
174 if let Some(Stored::Digested(d)) = w.get_property(name).as_deref() {
175 boxes.push(d.clone());
176 }
177 },
178 }
179 }
180 if boxes.len() == 1 {
181 let mut options = sizer_options_from_whatsit(w);
190 options.remove("height");
191 options.remove("depth");
192 if !matches!(boxes[0].data(), DigestedData::List(_)) {
193 options.remove("width");
194 }
195 boxes[0].compute_size(options)
196 } else if boxes.is_empty() {
197 Ok((
198 Dimension::default(),
199 Dimension::default(),
200 Dimension::default(),
201 ))
202 } else {
203 let font = match w.get_property("font").as_deref() {
204 Some(Stored::Font(font)) => font.clone(),
205 _ => lookup_font().unwrap(),
206 };
207 let mut options = sizer_options_from_whatsit(w);
210 options.remove("width");
211 font.compute_boxes_size(&boxes, options)
212 }
213 }))
214 } else {
215 let sized_data = String::from(self);
217 Some(Rc::new(move |w| {
218 let font = match *w.get_property("font").unwrap() {
219 Stored::Font(ref font) => font.clone(),
220 _ => lookup_font().unwrap(),
221 };
222 let options = sizer_options_from_whatsit(w);
223 font.compute_boxes_size(
224 &[Digested::from(Tbox {
225 text: arena::pin(&sized_data),
226 ..Tbox::default()
227 })],
228 options,
229 )
230 }))
231 }
232 }
233}
234
235pub trait IntoTokensResult<T>: Sized {
238 fn into_tokens_result(self) -> Result<Tokens>;
240}
241
242impl IntoTokensResult<Result<Tokens>> for Token {
243 fn into_tokens_result(self) -> Result<Tokens> { Ok(Tokens!(self)) }
244}
245
246impl IntoTokensResult<Result<Tokens>> for Vec<Token> {
247 fn into_tokens_result(self) -> Result<Tokens> { Ok(Tokens::new(self)) }
248}
249
250impl IntoTokensResult<Result<Tokens>> for Tokens {
251 fn into_tokens_result(self) -> Result<Tokens> { Ok(self) }
252}
253
254impl IntoTokensResult<Result<Tokens>> for Result<Tokens> {
255 fn into_tokens_result(self) -> Result<Tokens> { self }
256}
257
258impl IntoTokensResult<Result<Tokens>> for Result<()> {
259 fn into_tokens_result(self) -> Result<Tokens> {
260 match self {
261 Ok(()) => Ok(Tokens!()),
262 Err(e) => Err(e),
263 }
264 }
265}
266
267impl IntoTokensResult<Result<Tokens>> for () {
268 fn into_tokens_result(self) -> Result<Tokens> { Ok(Tokens!()) }
269}
270
271impl IntoTokensResult<Result<Tokens>> for ArgWrap {
272 fn into_tokens_result(self) -> Result<Tokens> { Ok(self.owned_tokens().unwrap_or_default()) }
274}
275impl IntoTokensResult<Result<Tokens>> for Result<ArgWrap> {
276 fn into_tokens_result(self) -> Result<Tokens> {
278 self.map(|w| w.owned_tokens().unwrap_or_default())
279 }
280}
281
282pub trait IntoResultArgWrap<T>: Sized {
285 fn into_result_argwrap(self) -> Result<ArgWrap>;
287}
288
289impl IntoResultArgWrap<Result<ArgWrap>> for Error {
290 fn into_result_argwrap(self) -> Result<ArgWrap> { Err(self) }
291}
292
293impl<T> IntoResultArgWrap<Result<ArgWrap>> for Result<T>
294where T: Into<ArgWrap> + Sized
295{
296 fn into_result_argwrap(self) -> Result<ArgWrap> { self.map(|v| v.into()) }
297}
298
299impl<T> IntoResultArgWrap<Result<ArgWrap>> for T
300where T: Into<ArgWrap> + Sized
301{
302 fn into_result_argwrap(self) -> Result<ArgWrap> { Ok(self.into()) }
303}
304
305impl IntoResultArgWrap<Result<ArgWrap>> for Vec<Token> {
306 fn into_result_argwrap(self) -> Result<ArgWrap> { Ok(ArgWrap::Tokens(Tokens::new(self))) }
307}
308
309pub trait IntoBoolResult<T>: Sized {
311 fn into_bool_result(self) -> Result<bool>;
313}
314impl IntoBoolResult<Result<bool>> for bool {
315 fn into_bool_result(self) -> Result<bool> { Ok(self) }
316}
317impl IntoBoolResult<Result<bool>> for Result<bool> {
318 fn into_bool_result(self) -> Result<bool> { self }
319}
320
321pub trait IntoDigestedResult<T>: Sized {
323 fn into_digested_result(self) -> Result<Vec<Digested>>;
325}
326impl IntoDigestedResult<Result<Vec<Digested>>> for () {
327 fn into_digested_result(self) -> Result<Vec<Digested>> { Ok(Vec::new()) }
328}
329impl IntoDigestedResult<Result<Vec<Digested>>> for Error {
330 fn into_digested_result(self) -> Result<Vec<Digested>> { Err(self) }
331}
332impl IntoDigestedResult<Result<Vec<Digested>>> for Result<()> {
333 fn into_digested_result(self) -> Result<Vec<Digested>> { self.map(|_| Vec::new()) }
334}
335impl IntoDigestedResult<Result<Vec<Digested>>> for Tbox {
336 fn into_digested_result(self) -> Result<Vec<Digested>> { Ok(vec![self.into()]) }
337}
338impl IntoDigestedResult<Result<Vec<Digested>>> for Result<Tbox> {
339 fn into_digested_result(self) -> Result<Vec<Digested>> { self.map(|tb| vec![tb.into()]) }
340}
341
342impl IntoDigestedResult<Result<Vec<Digested>>> for Whatsit {
343 fn into_digested_result(self) -> Result<Vec<Digested>> { Ok(vec![self.into()]) }
344}
345
346impl IntoDigestedResult<Result<Vec<Digested>>> for List {
347 fn into_digested_result(self) -> Result<Vec<Digested>> { Ok(vec![self.into()]) }
348}
349
350impl IntoDigestedResult<Result<Vec<Digested>>> for Digested {
351 fn into_digested_result(self) -> Result<Vec<Digested>> { Ok(vec![self]) }
352}
353
354impl IntoDigestedResult<Result<Vec<Digested>>> for Vec<Digested> {
355 fn into_digested_result(self) -> Result<Vec<Digested>> { Ok(self) }
356}
357
358impl IntoDigestedResult<Result<Vec<Digested>>> for Result<Vec<Digested>> {
359 fn into_digested_result(self) -> Result<Vec<Digested>> { self }
360}
361impl IntoDigestedResult<Result<Vec<Digested>>> for Result<Digested> {
362 fn into_digested_result(self) -> Result<Vec<Digested>> { self.map(|d| vec![d]) }
363}
364
365pub trait IntoRegisterValueOption<T>: Sized {
368 fn into_register_value_option(self) -> Option<RegisterValue>;
369}
370impl IntoRegisterValueOption<Option<RegisterValue>> for () {
371 fn into_register_value_option(self) -> Option<RegisterValue> { None }
372}
373impl IntoRegisterValueOption<Option<RegisterValue>> for Option<RegisterValue> {
374 fn into_register_value_option(self) -> Option<RegisterValue> { self }
375}
376impl IntoRegisterValueOption<Option<RegisterValue>> for usize {
377 fn into_register_value_option(self) -> Option<RegisterValue> {
378 Some(RegisterValue::Number(Number(self as i64)))
379 }
380}
381impl IntoRegisterValueOption<Option<RegisterValue>> for Number {
382 fn into_register_value_option(self) -> Option<RegisterValue> { Some(RegisterValue::Number(self)) }
383}
384impl IntoRegisterValueOption<Option<RegisterValue>> for Dimension {
385 fn into_register_value_option(self) -> Option<RegisterValue> {
386 Some(RegisterValue::Dimension(self))
387 }
388}
389impl IntoRegisterValueOption<Option<RegisterValue>> for MuDimension {
390 fn into_register_value_option(self) -> Option<RegisterValue> {
391 Some(RegisterValue::MuDimension(self))
392 }
393}
394impl IntoRegisterValueOption<Option<RegisterValue>> for Glue {
395 fn into_register_value_option(self) -> Option<RegisterValue> { Some(RegisterValue::Glue(self)) }
396}
397impl IntoRegisterValueOption<Option<RegisterValue>> for MuGlue {
398 fn into_register_value_option(self) -> Option<RegisterValue> { Some(RegisterValue::MuGlue(self)) }
399}
400impl IntoRegisterValueOption<Option<RegisterValue>> for Token {
401 fn into_register_value_option(self) -> Option<RegisterValue> { Some(RegisterValue::Token(self)) }
402}
403impl IntoRegisterValueOption<Option<RegisterValue>> for Option<Token> {
404 fn into_register_value_option(self) -> Option<RegisterValue> { self.map(RegisterValue::Token) }
405}
406impl IntoRegisterValueOption<Option<RegisterValue>> for Tokens {
407 fn into_register_value_option(self) -> Option<RegisterValue> { Some(RegisterValue::Tokens(self)) }
408}
409
410impl IntoRegisterValueOption<Option<RegisterValue>> for Option<Number> {
411 fn into_register_value_option(self) -> Option<RegisterValue> { self.map(RegisterValue::Number) }
412}
413
414pub trait IntoDigestedOptionResult<T>: Sized {
416 fn into_digested_option_result(self) -> Result<Option<Digested>>;
417}
418
419impl IntoDigestedOptionResult<Result<Option<Digested>>> for () {
420 fn into_digested_option_result(self: ()) -> Result<Option<Digested>> { Ok(None) }
421}
422
423impl IntoDigestedOptionResult<Result<Option<Digested>>> for Glue {
424 fn into_digested_option_result(self) -> Result<Option<Digested>> {
425 RegisterValue::Glue(self).into_digested_option_result()
426 }
427}
428impl IntoDigestedOptionResult<Result<Option<Digested>>> for MuGlue {
429 fn into_digested_option_result(self) -> Result<Option<Digested>> {
430 RegisterValue::MuGlue(self).into_digested_option_result()
431 }
432}
433impl IntoDigestedOptionResult<Result<Option<Digested>>> for Dimension {
434 fn into_digested_option_result(self) -> Result<Option<Digested>> {
435 RegisterValue::Dimension(self).into_digested_option_result()
436 }
437}
438impl IntoDigestedOptionResult<Result<Option<Digested>>> for MuDimension {
439 fn into_digested_option_result(self) -> Result<Option<Digested>> {
440 RegisterValue::MuDimension(self).into_digested_option_result()
441 }
442}
443
444impl IntoDigestedOptionResult<Result<Option<Digested>>> for Number {
445 fn into_digested_option_result(self) -> Result<Option<Digested>> {
446 RegisterValue::Number(self).into_digested_option_result()
447 }
448}
449
450impl IntoDigestedOptionResult<Result<Option<Digested>>> for RegisterValue {
451 fn into_digested_option_result(self) -> Result<Option<Digested>> { Ok(Some(self.into())) }
452}
453impl IntoDigestedOptionResult<Result<Option<Digested>>> for Option<Digested> {
454 fn into_digested_option_result(self) -> Result<Option<Digested>> { Ok(self) }
455}
456impl IntoDigestedOptionResult<Result<Option<Digested>>> for Result<Option<Digested>> {
457 fn into_digested_option_result(self) -> Result<Option<Digested>> { self }
458}
459impl IntoDigestedOptionResult<Result<Option<Digested>>> for KeyVals {
460 fn into_digested_option_result(self) -> Result<Option<Digested>> {
461 Ok(Some(Digested::from(self)))
462 }
463}
464impl IntoDigestedOptionResult<Result<Option<Digested>>> for Option<KeyVals> {
465 fn into_digested_option_result(self) -> Result<Option<Digested>> {
466 match self {
467 None => Ok(None),
468 Some(kv) => kv.into(),
469 }
470 }
471}
472impl IntoDigestedOptionResult<Result<Option<Digested>>> for List {
473 fn into_digested_option_result(self) -> Result<Option<Digested>> {
474 Ok(Some(Digested::from(self)))
475 }
476}
477
478pub trait IntoPropertiesResult {
479 fn into_properties_result(self) -> Result<HashMap<Stored>>;
480}
481impl IntoPropertiesResult for HashMap<Stored> {
482 fn into_properties_result(self) -> Result<HashMap<Stored>> { Ok(self) }
483}
484impl IntoPropertiesResult for Result<HashMap<Stored>> {
485 fn into_properties_result(self) -> Result<HashMap<Stored>> { self }
486}
487
488pub trait IntoFontField<T>: Sized {
489 fn into_font_field(self) -> T;
490}
491
492impl IntoFontField<Option<bool>> for bool {
493 fn into_font_field(self) -> Option<bool> { Some(self) }
494}
495
496impl IntoFontField<bool> for bool {
497 fn into_font_field(self) -> bool { self }
498}
499
500impl IntoFontField<Option<Cow<'static, str>>> for &'static str {
501 fn into_font_field(self) -> Option<Cow<'static, str>> { Some(Cow::Borrowed(self)) }
502}
503impl IntoFontField<Option<f64>> for f64 {
504 fn into_font_field(self) -> Option<f64> { Some(self) }
505}
506impl IntoFontField<Option<f64>> for i32 {
507 fn into_font_field(self) -> Option<f64> { Some(self as f64) }
508}
509impl IntoFontField<Option<Color>> for Color {
510 fn into_font_field(self) -> Option<Color> { Some(self) }
511}