mirror of
https://github.com/typst/typst
synced 2025-05-13 12:36:23 +08:00
Documentation for measure, locate, and style
This commit is contained in:
parent
420eebe191
commit
22bf0117a0
@ -1,10 +1,39 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
/// Measure the size of content.
|
/// Measure the layouted size of content.
|
||||||
|
///
|
||||||
|
/// The `measure` function lets you determine the layouted size of content.
|
||||||
|
/// The same content can have a different size depending on the styles that
|
||||||
|
/// are active when it is layouted. For example, in the example below
|
||||||
|
/// `[#content]` is of course bigger when we increase the font size.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// #let content = [Hello!]
|
||||||
|
/// #content
|
||||||
|
/// #set text(14pt)
|
||||||
|
/// #content
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// To do a meaningful measurement, you therefore first need to retrieve the
|
||||||
|
/// active styles with the [`style`]($func/style) function. You can then pass
|
||||||
|
/// them to the `measure` function.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// #let thing(body) = style(styles => {
|
||||||
|
/// let size = measure(body, styles)
|
||||||
|
/// [Width of "#body" is #size.width]
|
||||||
|
/// })
|
||||||
|
///
|
||||||
|
/// #thing[Hey] \
|
||||||
|
/// #thing[Welcome]
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// The measure function returns a dictionary with the entries `width` and
|
||||||
|
/// `height`, both of type [`length`]($type/length).
|
||||||
///
|
///
|
||||||
/// Display: Measure
|
/// Display: Measure
|
||||||
/// Category: layout
|
/// Category: layout
|
||||||
/// Returns: array
|
/// Returns: dictionary
|
||||||
#[func]
|
#[func]
|
||||||
pub fn measure(
|
pub fn measure(
|
||||||
/// The content whose size to measure.
|
/// The content whose size to measure.
|
||||||
@ -16,5 +45,5 @@ pub fn measure(
|
|||||||
let styles = StyleChain::new(&styles);
|
let styles = StyleChain::new(&styles);
|
||||||
let frame = content.measure(&mut vm.vt, styles, pod)?.into_frame();
|
let frame = content.measure(&mut vm.vt, styles, pod)?.into_frame();
|
||||||
let Size { x, y } = frame.size();
|
let Size { x, y } = frame.size();
|
||||||
Value::Array(array![x, y])
|
dict! { "width" => x, "height" => y }.into()
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
/// Provide access to the location of content.
|
/// Provides access to the location of content.
|
||||||
|
///
|
||||||
|
/// This is useful in combination with [queries]($func/query),
|
||||||
|
/// [counters]($func/counter), [state]($func/state), and [links]($func/link).
|
||||||
|
/// See their documentation for more details.
|
||||||
///
|
///
|
||||||
/// Display: Locate
|
/// Display: Locate
|
||||||
/// Category: meta
|
/// Category: meta
|
||||||
/// Returns: content
|
/// Returns: content
|
||||||
#[func]
|
#[func]
|
||||||
pub fn locate(
|
pub fn locate(
|
||||||
/// The function to call with the location.
|
/// A function that receives a `location`. Its return value is displayed
|
||||||
|
/// in the document.
|
||||||
|
///
|
||||||
|
/// This function is called once for each time the content returned by
|
||||||
|
/// `locate` appears in the document. That makes it possible to generate
|
||||||
|
/// content that depends on its own location in the document.
|
||||||
func: Func,
|
func: Func,
|
||||||
) -> Value {
|
) -> Value {
|
||||||
LocateElem::new(func).pack().into()
|
LocateElem::new(func).pack().into()
|
||||||
@ -15,7 +24,7 @@ pub fn locate(
|
|||||||
|
|
||||||
/// Executes a `locate` call.
|
/// Executes a `locate` call.
|
||||||
///
|
///
|
||||||
/// Display: Styled
|
/// Display: Locate
|
||||||
/// Category: special
|
/// Category: special
|
||||||
#[element(Locatable, Show)]
|
#[element(Locatable, Show)]
|
||||||
struct LocateElem {
|
struct LocateElem {
|
||||||
@ -35,14 +44,24 @@ impl Show for LocateElem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provide access to active styles.
|
/// Provides access to active styles.
|
||||||
///
|
///
|
||||||
/// Display: Styled
|
/// The styles are currently opaque and only useful in combination with the
|
||||||
/// Category: layout
|
/// [`measure`]($func/measure) function. See its documentation for more details.
|
||||||
|
/// In the future, the provided styles might also be directly accessed to look
|
||||||
|
/// up styles defined by [set rules]($styling/#set-rules).
|
||||||
|
///
|
||||||
|
/// Display: Style
|
||||||
|
/// Category: meta
|
||||||
/// Returns: content
|
/// Returns: content
|
||||||
#[func]
|
#[func]
|
||||||
pub fn style(
|
pub fn style(
|
||||||
/// The function to call with the styles.
|
/// A function to call with the styles. Its return value is displayed
|
||||||
|
/// in the document.
|
||||||
|
///
|
||||||
|
/// This function is called once for each time the content returned by
|
||||||
|
/// `style` appears in the document. That makes it possible to generate
|
||||||
|
/// content that depends on the style context it appears in.
|
||||||
func: Func,
|
func: Func,
|
||||||
) -> Value {
|
) -> Value {
|
||||||
StyleElem::new(func).pack().into()
|
StyleElem::new(func).pack().into()
|
||||||
|
@ -20,12 +20,12 @@ use crate::prelude::*;
|
|||||||
/// x = eval(
|
/// x = eval(
|
||||||
/// expr.replace("x", str(x))
|
/// expr.replace("x", str(x))
|
||||||
/// )
|
/// )
|
||||||
/// [New value is #x. \ ]
|
/// [New value is #x. ]
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// #compute("10")
|
/// #compute("10") \
|
||||||
/// #compute("x + 3")
|
/// #compute("x + 3") \
|
||||||
/// #compute("x * 2")
|
/// #compute("x * 2") \
|
||||||
/// #compute("x - 5")
|
/// #compute("x - 5")
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@ -83,12 +83,12 @@ use crate::prelude::*;
|
|||||||
/// #s.update(x =>
|
/// #s.update(x =>
|
||||||
/// eval(expr.replace("x", str(x)))
|
/// eval(expr.replace("x", str(x)))
|
||||||
/// )
|
/// )
|
||||||
/// New value is #s.display(). \
|
/// New value is #s.display().
|
||||||
/// ]
|
/// ]
|
||||||
///
|
///
|
||||||
/// #compute("10")
|
/// #compute("10") \
|
||||||
/// #compute("x + 3")
|
/// #compute("x + 3") \
|
||||||
/// #compute("x * 2")
|
/// #compute("x * 2") \
|
||||||
/// #compute("x - 5")
|
/// #compute("x - 5")
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@ -105,17 +105,17 @@ use crate::prelude::*;
|
|||||||
/// >>> #s.update(x =>
|
/// >>> #s.update(x =>
|
||||||
/// >>> eval(expr.replace("x", str(x)))
|
/// >>> eval(expr.replace("x", str(x)))
|
||||||
/// >>> )
|
/// >>> )
|
||||||
/// >>> New value is #s.display(). \
|
/// >>> New value is #s.display().
|
||||||
/// >>> ]
|
/// >>> ]
|
||||||
/// <<< ...
|
/// <<< ...
|
||||||
///
|
///
|
||||||
/// #let more = [
|
/// #let more = [
|
||||||
/// #compute("x * 2")
|
/// #compute("x * 2") \
|
||||||
/// #compute("x - 5")
|
/// #compute("x - 5")
|
||||||
/// ]
|
/// ]
|
||||||
///
|
///
|
||||||
/// #compute("10")
|
/// #compute("10") \
|
||||||
/// #compute("x + 3")
|
/// #compute("x + 3") \
|
||||||
/// #more
|
/// #more
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@ -137,7 +137,7 @@ use crate::prelude::*;
|
|||||||
/// >>> #s.update(x => {
|
/// >>> #s.update(x => {
|
||||||
/// >>> eval(expr.replace("x", str(x)))
|
/// >>> eval(expr.replace("x", str(x)))
|
||||||
/// >>> })
|
/// >>> })
|
||||||
/// >>> New value is #s.display(). \
|
/// >>> New value is #s.display().
|
||||||
/// >>> ]
|
/// >>> ]
|
||||||
/// <<< ...
|
/// <<< ...
|
||||||
///
|
///
|
||||||
@ -148,10 +148,10 @@ use crate::prelude::*;
|
|||||||
/// .location()
|
/// .location()
|
||||||
/// ))
|
/// ))
|
||||||
///
|
///
|
||||||
/// #compute("10")
|
/// #compute("10") \
|
||||||
/// #compute("x + 3")
|
/// #compute("x + 3") \
|
||||||
/// *Here.* <here> \
|
/// *Here.* <here> \
|
||||||
/// #compute("x * 2")
|
/// #compute("x * 2") \
|
||||||
/// #compute("x - 5")
|
/// #compute("x - 5")
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
Loading…
x
Reference in New Issue
Block a user