# core/component/shadowtext (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/component/shadowtext/

Single-line text with a shadow. (ShadowText in vanilla.js core/component/shadowtext.js)
- Laying a shadow with two text nodes (the same text in black below, the text on top) makes the two show through separately, so when the node is faded the shadow beneath shows through the faded text.
  This component bakes the look of the text placed over the shadow into one image and draws that single sheet, so the node's opacity applies to the text and shadow as one sheet.
- Horizontal alignment is left, center or right within the node, and vertically it is the node's center.
- If the text exceeds the node width, it shrinks from the initial size down to the minimum ratio (set_fit_minimum_ratio) to fit.
- The shadow can be blurred with three box blurs (close to Gaussian). The baked image edge is widened by the blur amount.
- Rebakes when the bake scale, text, style, text direction or node width changes, or when a new font is loaded. If a bake of the same look already exists, it is shared. (sharedbakecache.rs)
- The text direction (set_text_direction, the original setTextDirection, initially Ltr) drives bidirectional ordering and shaping, and resolves alignment Start / End.
- Off-screen culling is judged by the rectangle where the last baked sheet is placed (including overflowing text and the shadow margin). It is not culled if it has not been baked yet since a setter changed it, or if the node width or bake scale has changed.
- Italic, underline, strikethrough (set_italic, set_underline, set_strikethrough): the line position and thickness are the same as Text's middle baseline, and the lines are baked together with the text so the shadow lies beneath them too.
  It is single-line text shrunk to fit, so there is no wrap mode.
- On the web, as in the original, it sets the shadow (shadowColor, shadowBlur, shadowOffsetY) on a canvas and bakes with fillText in the text color. (bake_shadow_text in platform/web/systemfont.rs, the margin and position calculations match the original)
  Desktop gets the shape with the text cache's rasterize_text and computes the shadow with three box blurs.
- Color glyphs (emoji) are placed in their own colors multiplied only by the text color's alpha, and the shadow is cast by the emoji shapes too. (the original fillStyle and canvas shadow)
- Differences from the original: the original setFontFace(FontFace) is set_font_family (a loaded font name), and the shadow color and opacity (setShadow's color string and opacity) are taken as one Color (alpha is the opacity).
  The original getBakeCanvas is get_bake_image, which returns the baked Image instead of the baked canvas.

## ShadowText (struct) (Default, Component)

Single-line text with a shadow component. (bakes the look of the text placed over the shadow into one image and draws it)
- The node's opacity applies to the text and shadow as one sheet. Horizontally placed by the alignment, vertically at the node's center.
- If the text exceeds the node width, it shrinks from the initial size down to the minimum ratio to fit.
- Initially empty text, font default, size 16, center alignment, Ltr, white text, a black shadow (drop 0, blur 0), minimum ratio 0.6.

Example: A blurred shadow under a bold title

```rust
use vanilla::{Color, NodeTree, ShadowText, TextAlign, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let title_id = node_tree.create_child(root_id, "Title");
node_tree[title_id].set_content_size(Vector2::create(320.0, 48.0));
let shadow_text = node_tree.add_component(title_id, ShadowText::create_with_text("Stage Clear", 32.0)).unwrap();
shadow_text.set_text_style(32.0, true, "#ffee88");
shadow_text.set_shadow(Color::black(), 0.6, 2.0, 3.0);
shadow_text.set_text_align(TextAlign::Left);

let shadow_text = node_tree[title_id].get_component::<ShadowText>().unwrap();
assert_eq!(shadow_text.get_text(), "Stage Clear");
assert!(shadow_text.is_bold());
assert_eq!(shadow_text.get_text_align(), TextAlign::Left);
```

- `pub fn new() -> ShadowText` (constructor, since 0.4.0): Constructor.
  - returns ShadowText: New component with empty text, font default, size 16, center alignment, white text and a black shadow.
- `pub fn create_with_text(text: &str, font_size: f64) -> ShadowText` (static, since 0.4.0): Constructor with text.
  - `text` &str: Single-line text to draw.
  - `font_size` f64: Initial font size. (pixels)
  - returns ShadowText: New component with that text and size. (the rest is the same as new)

  Example: Create score text

  ```rust
  use vanilla::ShadowText;
  
  let shadow_text = ShadowText::create_with_text("Score 100", 24.0);
  assert_eq!(shadow_text.get_text(), "Score 100");
  assert_eq!(shadow_text.get_font_size(), 24.0);
  assert_eq!(shadow_text.get_font_family(), "default");
  ```

- `pub fn draw_shadow_text(&mut self, transform: &NodeTransform, graphic: &mut Graphic)` (method, since 0.4.0): Draw.
  - Does not draw if the text is empty. Rebakes if the bake scale, text, style, color, shadow, minimum ratio, node width, text direction or number of loaded fonts differs from last time.
  - If it cannot bake because the font shapes could not be obtained, it draws with the Graphic's text without a shadow. (underline and strikethrough are drawn as lines)
  - `transform` &NodeTransform: Node transform to draw on. (reads content_size)
  - `graphic` &mut Graphic: Graphic to draw to. (with the node transform applied, used for the bake scale and the text cache)
- `pub fn set_text(&mut self, text: &str)` (method, since 0.4.0): Sets the text.
  - `text` &str: Single-line text to draw. (does not draw if empty)
- `pub fn get_text(&self) -> &str` (method, since 0.4.0): Returns the text.
  - returns &str: The text being drawn. (empty string if not set)
- `pub fn set_font_family(&mut self, font_family: &str)` (method, since 0.4.0): Sets the font name. (a font name loaded into the engine, initially default. The original setFontFace)
  - `font_family` &str: Font name loaded into the engine.
- `pub fn get_font_family(&self) -> &str` (method, since 0.4.0): Returns the font name. (the original getFontFace, in the original it is a FontFace object that reflection saving does not write, so this is also excluded from saving)
  - returns &str: Font name.
- `pub fn get_font_size(&self) -> f64` (method, since 0.4.0): Returns the initial font size. (the original getFontSize)
  - returns f64: Initial font size. (pixels, not the size shrunk for overflow)
- `pub fn set_text_align(&mut self, text_align: TextAlign)` (method, since 0.4.0): Sets the horizontal alignment.
  - `text_align` TextAlign: Horizontal alignment within the node. (Left, Center, Right, Start / End are resolved by the text direction, initially Center)
- `pub fn get_text_align(&self) -> TextAlign` (method, since 0.4.0): Returns the horizontal alignment.
  - returns TextAlign: Horizontal alignment.
- `pub fn set_text_direction(&mut self, text_direction: TextDirection)` (method, since 0.4.0): Sets the text direction. (Ltr, Rtl, Auto. The original setTextDirection("ltr" / "rtl"), Inherit is treated as Ltr)
  - `text_direction` TextDirection: Text direction. (Auto decides by the first strong character of the text, initially Ltr)
- `pub fn get_text_direction(&self) -> TextDirection` (method, since 0.4.0): Returns the text direction.
  - returns TextDirection: The set text direction. (not the value with Auto or Inherit resolved)
- `pub fn is_bold(&self) -> bool` (method, since 0.4.0): Whether bold.
  - returns bool: true if drawn bold.
- `pub fn set_text_style(&mut self, font_size: f64, is_bold: bool, text_color: impl Into<Color>)` (method, since 0.4.0): Sets the text style. (initial size, weight, color)
  - `font_size` f64: Initial font size. (pixels, shrinks from this size if it overflows)
  - `is_bold` bool: Whether to draw bold.
  - `text_color` impl Into<Color>: Text color. (a Color or a string such as "#rrggbb", alpha is the text opacity)
- `pub fn set_italic(&mut self, is_italic: bool)` (method, since 0.4.0): Sets italic.
  - `is_italic` bool: Whether to draw italic. (initially false)
- `pub fn is_italic(&self) -> bool` (method, since 0.4.0): Whether italic.
  - returns bool: true if drawn italic.
- `pub fn set_underline(&mut self, is_underline: bool)` (method, since 0.4.0): Sets underline.
  - `is_underline` bool: Whether to draw an underline. (initially false)
- `pub fn is_underline(&self) -> bool` (method, since 0.4.0): Whether underlined.
  - returns bool: true if an underline is drawn.
- `pub fn set_strikethrough(&mut self, is_strikethrough: bool)` (method, since 0.4.0): Sets strikethrough.
  - `is_strikethrough` bool: Whether to draw a strikethrough. (initially false)
- `pub fn is_strikethrough(&self) -> bool` (method, since 0.4.0): Whether strikethrough.
  - returns bool: true if a strikethrough is drawn.
- `pub fn set_shadow(&mut self, shadow_color: impl Into<Color>, shadow_opacity: f64, shadow_offset: f64, shadow_blur: f64)` (method, since 0.4.0): Shadow settings. (color, opacity, downward offset, blur amount; same as the original setShadow(color, opacity, downward offset, blur amount))
  - `shadow_color` impl Into<Color>: Shadow color. (a Color or a string like "#rrggbb"; the alpha of the color is not read; black at first)
  - `shadow_opacity` f64: Shadow opacity. (0 to 1, 1 at first)
  - `shadow_offset` f64: How far the shadow drops down. (node coordinates, initially 0)
  - `shadow_blur` f64: How far the shadow blurs. (node coordinates, 0 if less than 0, initially 0)
- `pub fn set_fit_minimum_ratio(&mut self, fit_minimum_ratio: f64)` (method, since 0.4.0): Sets the minimum ratio to shrink to on overflow. (no shrinking if 1)
  - `fit_minimum_ratio` f64: Smallest ratio to the initial font size. (fitted to 0.01 ~ 1, initially 0.6)

  Example: Prevent shrinking, and the lower limit

  ```rust
  use vanilla::ShadowText;
  
  let mut shadow_text = ShadowText::create_with_text("A very long caption", 20.0);
  assert_eq!(shadow_text.get_fit_minimum_ratio(), 0.6);
  shadow_text.set_fit_minimum_ratio(1.0);
  assert_eq!(shadow_text.get_fit_minimum_ratio(), 1.0);
  shadow_text.set_fit_minimum_ratio(0.0);
  assert_eq!(shadow_text.get_fit_minimum_ratio(), 0.01);
  ```

- `pub fn get_fit_minimum_ratio(&self) -> f64` (method, since 0.4.0): Returns the minimum ratio to shrink to on overflow.
  - returns f64: Smallest ratio to the initial font size. (0.01 ~ 1)
- `pub fn get_bake_image(&self) -> Option<&Rc<Image>>` (method, since 0.4.0): Returns the baked image. (None if not baked yet or if drawn with the engine's text without a shadow, the original getBakeCanvas)
  - returns Option<&Rc<Image>>: The last baked image. None if not baked yet or if it could not bake and drew with the engine's text.

## Functions and constants

- `pub fn blur_coverage(coverage: &[u8], width: i32, height: i32, radius: f64) -> Vec<u8>` (method, since 0.4.0): Blurs coverage (0 ~ 255) with three box blurs. (radius in pixels, unchanged if 0 or less)
  - Repeats horizontal and vertical box blurs three times to approximate a Gaussian. The box radius is computed treating the standard deviation as radius / 2, and is at least 1.
  - Outside the image counts as 0, so the edges fade.
  - Panics if coverage is shorter than width x height. (if radius is 0 or less, or width or height is 0 or less, returns it as is without reading)
  - `coverage` &[u8]: Coverage. (0 ~ 255, height rows of width values each)
  - `width` i32: Width. (pixels)
  - `height` i32: Height. (pixels)
  - `radius` f64: Blur radius. (pixels, no blur if 0 or less)
  - returns Vec<u8>: Blurred coverage. (same length as coverage, 0 ~ 255)

  Example: Blur a single point

  ```rust
  use vanilla::blur_coverage;
  
  let width = 9;
  let height = 9;
  let mut coverage = vec![0u8; (width * height) as usize];
  coverage[(4 * width + 4) as usize] = 255;
  let blurred = blur_coverage(&coverage, width, height, 4.0);
  assert_eq!(blurred.len(), coverage.len());
  assert!(blurred[(4 * width + 4) as usize] < 255);
  assert!(blurred[(4 * width + 5) as usize] > 0);
  assert_eq!(blur_coverage(&coverage, width, height, 0.0), coverage);
  ```

- `pub fn compose_shadow_image(shape_coverage: &[u8], shape_pixels: Option<&[u8]>, width: i32, height: i32, shape_color: Color, shadow_color: Color, shadow_blur: f64, shadow_offset_y: i32) -> Rc<Image>` (method, since 0.4.0): Builds an RGBA image with a shape placed over a shadow. (the shape's coverage and color, the shadow's color, blur and downward drop, straight alpha)
  - The shadow blurs shape and lays it down by shadow_offset_y, and the shape is placed over it with alpha.
  - If shape_pixels is given, the shape's color is taken per pixel. (an image, a single shape_color if None)
  - The part of the dropped shadow that goes outside the image is discarded. Panics if a length is short (shape_coverage shorter than width x height, shape_pixels shorter than width x height x 4).
  - `shape_coverage` &[u8]: Coverage of the shape. (0 ~ 255, width x height) The shadow is built by blurring this, and if shape_pixels is None it is also the shape's alpha.
  - `shape_pixels` Option<&[u8]>: Straight alpha RGBA pixels of the shape. (width x height x 4, a single shape_color if None)
  - `width` i32: Width. (pixels)
  - `height` i32: Height. (pixels)
  - `shape_color` Color: Shape color when shape_pixels is None. (its alpha is multiplied by the coverage)
  - `shadow_color` Color: Shadow color. (alpha is the opacity)
  - `shadow_blur` f64: Shadow blur radius. (pixels, the radius of blur_coverage, no blur if 0 or less)
  - `shadow_offset_y` i32: How far to drop the shadow down. (pixels, up if negative)
  - returns Rc<Image>: The new built image. (width x height)

  Example: A black shadow one cell below a white point

  ```rust
  use vanilla::{compose_shadow_image, Color};
  
  let shape_coverage = [255u8, 0, 0];
  let image = compose_shadow_image(&shape_coverage, None, 1, 3, Color::white(), Color::black(), 0.0, 1);
  let pixels = image.get_pixels();
  assert_eq!(&pixels[0..4], &[255, 255, 255, 255]);
  assert_eq!(&pixels[4..8], &[0, 0, 0, 255]);
  assert_eq!(pixels[11], 0);
  ```

