# Text style

> Set text color, background color, font family, font size, or line height.

Text style adds the `textStyle` mark. It can store multiple style properties on the same text, such as a custom color and font size.

## Try it

[Try the text-style editor example](https://gpdoc.dev/docs/plugins/formatting/text-style/#text-style-example)

## Install

```sh
cargo add gpdoc-plugin-text-style
```

## Example

Register the text style formatter and the color and font size properties in the [Quickstart editor configuration](/docs/quickstart/#configure-the-editor). The mark and each property install their own default views.

```rust
use gpdoc::{EditorConfig, PluginViewExt};
use gpdoc_plugin_text_style::{color, font_size, text_style, text_style_format};
use gpdoc_plugin_paragraph::paragraph;


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(text_style().with_view(|| text_style_format(["textStyle"])))
    .plugin(color())
    .plugin(font_size())
    .build()
    .expect("configure editor");
```

Use `setColor` and `setFontSize` on the same selection to apply both properties. Clearing the size with `unsetFontSize` keeps the color.

## Settings

### `text_style_kit`

Use `.plugins(text_style_kit())` to install the mark and all five properties with their default views. For a smaller config, register `text_style()` with one or more of `color()`, `background_color()`, `font_family()`, `font_size()`, and `line_height()`.

```rust
use gpdoc::EditorConfig;
use gpdoc_plugin_text_style::{color, text_style};


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(text_style())
    .plugin(color())
    .build()?;
```

### `StyleProperty::on`

Choose which registered node or mark types accept an attribute. `on(self, types: impl IntoIterator<Item = impl Into<String>>)` accepts the variants `Color`, `BackgroundColor`, `FontFamily`, `FontSize`, and `LineHeight`. Include `textStyle` to use that property with the text commands below.

```rust
use gpdoc::EditorConfig;
use gpdoc_plugin_text_style::{text_style, StyleProperty};


let color = StyleProperty::Color.on(["textStyle", "paragraph"]);
let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(text_style())
    .plugin(color)
    .build()?;
```

## Commands

Each setter accepts `Some(value)` to assign a string or `None` to clear it. A setter preserves the other properties. An unset command clears its property and runs `removeEmptyTextStyle`. `editor.run` returns `Result<CommandOutcome, Error>`. A setter returns `NotApplicable` when the selection cannot take the style.

### `setColor`

Set the foreground color of the selected text.

```rust
editor.run(gpdoc_plugin_text_style::SetColor(Some("#2456aa".into())))?;
```

### `unsetColor`

Clear its foreground color.

```rust
editor.run(gpdoc_plugin_text_style::UnsetColor)?;
```

### `setBackgroundColor`

Set the background behind selected characters.

```rust
editor.run(gpdoc_plugin_text_style::SetBackgroundColor(Some("#ffe887".into())))?;
```

### `unsetBackgroundColor`

Clear the background color from selected characters.

```rust
editor.run(gpdoc_plugin_text_style::UnsetBackgroundColor)?;
```

### `setFontFamily`

Set the font family. Supply a CSS family name or a comma-separated list.

```rust
editor.run(gpdoc_plugin_text_style::SetFontFamily(Some("Georgia, serif".into())))?;
```

### `unsetFontFamily`

Restore the inherited font family.

```rust
editor.run(gpdoc_plugin_text_style::UnsetFontFamily)?;
```

### `setFontSize`

Set the font size using a CSS length. GPUI renders common lengths such as `px`, `em`, and `rem`. Your host must resolve expressions such as `calc()` and `var()`.

```rust
editor.run(gpdoc_plugin_text_style::SetFontSize(Some("18px".into())))?;
```

### `unsetFontSize`

Restore the inherited font size.

```rust
editor.run(gpdoc_plugin_text_style::UnsetFontSize)?;
```

### `setLineHeight`

Set line height using a CSS value. A unitless number specifies a multiple of the font size.

```rust
editor.run(gpdoc_plugin_text_style::SetLineHeight(Some("1.5".into())))?;
```

### `unsetLineHeight`

Restore the inherited line height.

```rust
editor.run(gpdoc_plugin_text_style::UnsetLineHeight)?;
```

### `toggleTextStyle`

Set the style fields to match. If all eligible text already matches, remove the whole mark. Otherwise merge the supplied values with existing styles.

```rust
editor.run(gpdoc_plugin_text_style::Toggle {
    font_size: Some("18px".into()),
    ..Default::default()
})?;
```

### `removeEmptyTextStyle`

Remove marks in the selection when all their properties are null or empty strings.

```rust
editor.run(gpdoc_plugin_text_style::RemoveEmpty)?;
```