Repository navigation
Add interactive forms - #8912
diogotcorreia wants to merge 7 commits into
Conversation
84e3719 to
74687b2
Compare
d5346b7 to
d99088b
Compare
fb4293b to
85613d1
Compare
laurmaedje
left a comment
There was a problem hiding this comment.
The code looks very solid overall, nice work!
The architecture was discussed ahead and seems to work well, so this review is mostly minor things, style nitpicks, and stuff like that.
I should note that I haven't looked much at the default show rules yet because they are WIP afaict. I'm happy to discuss how we can deal with the width / height issues the current ones have. I also haven't looked much at the PDF export code either, as I would defer that to @saecki. Finally, I should note that I mostly experimented with PDF, not with HTML because the latter is more straightforward.
Compatibility notes
- Typing an "ä" does not work in Acrobat in a textbox, but in non-Typst forms I tested, it worked
- A choice field with
multiple: truedoes not display out of the box in Acrobat - A textbox does not display its default value in Acrobat
These were just a few things I noticed, I did not check comprehensively. I think we need a document that exercises all features and test it in all viewers on all platforms.
Comments that aren't about a particular part of the code
- It would be nice if
set text(fill: ..)would configure the variable text appearance color (via krilla'sVariableAppearance)
cdbc60b to
1caa177
Compare
diogotcorreia
left a comment
There was a problem hiding this comment.
I think I addressed all or most of the comments. I haven't tried to fix compatibility with Acrobat yet, but will investigate that next.
|
|
||
| const FORM_BUTTON_RULE: ShowFn<FormButton> = |elem, engine, styles| { | ||
| let span = elem.span(); | ||
| if styles.get_ref(FormElem::form).is_none() { |
There was a problem hiding this comment.
I named it FormElem::try_from_styles, but I'm not sure if it should be part of FormElem or just a standalone function. It's kinda weird that it's on FormElem but then returns a &Packed<FormElem>
| if fixed_width && max_length.is_none() { | ||
| bail!(span, "`max-length` must be set when `fixed-width: true`"); | ||
| } | ||
|
|
There was a problem hiding this comment.
HTML does not support fixed-width so this doesn't exist there?
| } | ||
| } | ||
| FrameItem::Text(..) | FrameItem::Shape(..) | FrameItem::Image(..) => {} | ||
| FrameItem::Text(..) |
There was a problem hiding this comment.
I gave my shot at this with the measure locator, but it doesn't seem to be working 100% yet. I think it's best we define a few test cases and the expected outcome.
| /// An internal or external link to a destination. | ||
| Link(Destination, Size), | ||
| /// A representation of an interactive form field. | ||
| FormField(FieldWidget), |
There was a problem hiding this comment.
I added Arc to FieldWidget::appearance, the struct no longer increases in size.
|
|
||
| #[derive(Debug, Clone, Hash)] | ||
| pub struct FieldWidget { | ||
| pub span: Span, |
There was a problem hiding this comment.
Hopefully everything is documented now (except for the user-facing docs)
827e0ce to
2aa504e
Compare
2aa504e to
cc4259f
Compare
cc4259f to
e4072ca
Compare
saecki
left a comment
There was a problem hiding this comment.
Here's the review of the pdf export.
It's one question about the alt_name of form fields, on performance concern, otherwise mostly stylistic nits :)
laurmaedje
left a comment
There was a problem hiding this comment.
Here's some more comments. :)
| if disabled { | ||
| styles::set_disabled_styles(&mut out); | ||
| } | ||
| out.set(RectElem::inset, Sides::splat(Some(Abs::pt(5.0).into()))); |
There was a problem hiding this comment.
The inset is set for the second time here, so the inner 0.5pt from set_rect_stroke are overridden.
There was a problem hiding this comment.
It was intentional, but I can see that it can be confusing, especially because I'm setting it back to the default (5pt). It might make more sense to set the inner stroke without the inset, so I'll just inline the function, sadly.
There was a problem hiding this comment.
I was a bit confused because I would think to get the inner stroke plus the standard padding, it should really be 5.5pt, shouldn't it?
There was a problem hiding this comment.
It's such a small difference anyway, but I'm setting the inset to 5pt + 0.05em now then.
| if is_leaf { | ||
| let name = field.name.clone(); | ||
| if fields.insert(path_segment.into(), Node::Leaf(field)).is_some() { | ||
| bail!( |
There was a problem hiding this comment.
Agreed. And if we know about the field, we could also be smarter about the hint for dotted paths instead of saying generically that it might happen with {name}.something-else.
e4072ca to
d0e079e
Compare
There was a problem hiding this comment.
I've resolved a bunch of comments and the commits I just pushed include a few other changes as well, most notably:
form.cursoris gone, as we discussedform.signaturehas been added- WIP reference and guide docs have been added
- replaced textbox's
multilineandfixed-widthwithkindstring enum - refactored
crates/typst-layout/src/form.rsso a lot more code is reused between each field type - variable text field's quadding is now inferred from horizontal alignment
I've replied to all unresolved comments as well. Some I left unresolved because I wasn't sure my changes were the right ones, but for others I have not changed anything and I await further feedback/discussion.
| let widget = FieldWidget { | ||
| span, | ||
| field_location: elem.location().unwrap(), | ||
| size, |
There was a problem hiding this comment.
How should we handle checkbox/radio? Expand + off state normal's size?
let size = region.expand.select(region.size, body_off.normal.size());I'm going with this, let me know if you want to revert to region on these two
| let styles = StyleChain::default(); | ||
| let span = field.span(); | ||
|
|
||
| let get_alt_text = || { |
There was a problem hiding this comment.
Done. Also added .filter(|s| !s.trim().is_empty()) so that we don't set whitespace-only alt names.
| #form.textbox() | ||
| ] | ||
|
|
||
| --- form-hover-down-styles-pdf-a pdf --- |
There was a problem hiding this comment.
Is turning the form-counter test into PDF A/4 enough? Seems to pass
d0e079e to
2720e0e
Compare
| #import "../../components/index.typ": docs-chapter, docs-table, short-or-long | ||
|
|
||
| #show: docs-chapter.with( | ||
| title: "Form Guide", |
There was a problem hiding this comment.
| title: "Form Guide", | |
| title: "Forms Guide", |
|
|
||
| = #short-or-long[Form Fields][Which form fields does Typst support?] <form-fields> | ||
|
|
||
| Typst has support for five kinds of form fields where users can input information, |
There was a problem hiding this comment.
| Typst has support for five kinds of form fields where users can input information, | |
| Typst has support for five kinds of form fields where users can enter information, |
| Each field kind is better suited for a different data type that you might want to collect | ||
| and the medium you want to distribute your form in. |
There was a problem hiding this comment.
| Each field kind is better suited for a different data type that you might want to collect | |
| and the medium you want to distribute your form in. | |
| The suitability of each field kind depends on which type of data you want to collect and in which medium you want to distribute your form. |
| ] | ||
| ``` | ||
|
|
||
| - *@form.radio-group[Radio]:* Select at most one option among multiple options. |
There was a problem hiding this comment.
| - *@form.radio-group[Radio]:* Select at most one option among multiple options. | |
| - *@form.radio-group[Radio]:* Select at most one option among multiple. |
| When only a single choice is allowed, this field kind is displayed as | ||
| a dropdown. For multiple choices, it is displayed as a listbox instead. | ||
| Choice fields work great digitally, although with limited styling options in PDF. | ||
| However, they are not usable in print. |
There was a problem hiding this comment.
| However, they are not usable in print. | |
| When printed, they do not work. |
(especially dropdowns, I could see someone circling a listbox option)
| use crate::text::TextElem; | ||
| use crate::visualize::{CircleElem, Paint, RectElem}; | ||
|
|
||
| /// A form with interactive and fillable fields. |
There was a problem hiding this comment.
Perhaps this should describe that multiple forms are allowed, that forms must not nest (perhaps on the body argument), and that when submitting, all field values in the form submit?
| /// We highly recommend explicitly setting each form field's name when you | ||
| /// expect the form to be submitted. Otherwise, fields won't be submitted at | ||
| /// all in HTML and will have auto-generated names in PDF. |
There was a problem hiding this comment.
@laurmaedje should we document that auto-generated name changes will not be considered a breaking change or are non-breaking breaking changes a fanciful mirage anyways?
| /// A textbox can be used when you want the user to input arbitrary text | ||
| /// into a field, and it comes in three variants: | ||
| /// | ||
| /// - *single-line*, where the value cannot contain line breaks; |
There was a problem hiding this comment.
Capitalize at start of list, add following punctuation to boldface or italics
| /// When used within a PDF file, this field allows the user to cryptographically | ||
| /// sign the present document and optionally place a visual signature where | ||
| /// this field is. |
There was a problem hiding this comment.
From a compat standpoint, more like the reverse
| ) | ||
| } | ||
|
|
||
| /// Fragment element to allow styling of form fields in a specific state. |
There was a problem hiding this comment.
Is it a fragment or a pseudo element (cc @laurmaedje)
MDLC01
left a comment
There was a problem hiding this comment.
Just a few details I noticed
| /// - When `false`, this field is displayed as a dropdown. | ||
| /// - When `true`, this field is displayed as a listbox. |
There was a problem hiding this comment.
| /// - When `false`, this field is displayed as a dropdown. | |
| /// - When `true`, this field is displayed as a listbox. | |
| /// - When `{false}`, this field is displayed as a dropdown. | |
| /// - When `{true}`, this field is displayed as a listbox. |
| /// set your own click listener that performs another action instead. | ||
| pub action: Option<ButtonAction>, | ||
|
|
||
| /// Whether this button is disabled. When true, interacting with it does not |
There was a problem hiding this comment.
| /// Whether this button is disabled. When true, interacting with it does not | |
| /// Whether this button is disabled. When `{true}`, interacting with it does not |
Same in other places below.
| pub struct FormChoice { | ||
| /// The name of this field. | ||
| /// | ||
| /// If `{auto}`, will be automatically generated as `field-<n>` |
There was a problem hiding this comment.
| /// If `{auto}`, will be automatically generated as `field-<n>` | |
| /// If `{auto}`, will be automatically generated as `{"field-<n>"}` |
Also in one other place below.
| /// this field is. | ||
| /// | ||
| /// This form field is not supported in HTML and some PDF readers. See the | ||
| /// @form:compatibility[compatibility section] for more details. |
There was a problem hiding this comment.
| /// @form:compatibility[compatibility section] for more details. | |
| /// @form:compatibility[Compatibility section] for more details. |
| the document, attesting its validity. | ||
| Users of a supported PDF reader can sign the document using their digital signature. | ||
| Only certain PDF readers are supported – check the | ||
| @form:compatibility[compatibility table] for full details. |
There was a problem hiding this comment.
I may be mistaken, but I believe the question is whether the signature functionality is supported by the PDF reader, whereas this is phrased in a way that makes it seems like something (Typst?) needs to support each individual PDF reader.
I am unsure how to phrase it better without making the text too cumbersome. Maybe something like this?
Users of a PDF reader that supports the feature can sign the document using...
Not all PDF readers support digital signatures...
Related to #1765 #2421 #4368
This PR only contains the library changes and the HTML export. The paged export will come in a separate PR.PR includes both HTML and paged export.