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

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

그림자를 깐 한 줄 글. (vanilla.js core/component/shadowtext.js 의 ShadowText)
- 글자 노드 두 벌(같은 글을 검게 밑에, 글을 위에)로 그림자를 깔면 둘이 따로 비쳐, 노드를 흐리게 할 때 흐린 글 사이로 밑의 그림자가 보인다.
  이 컴포넌트는 그림자 위에 글을 얹은 모습을 그림 한 장에 구워 그 한 장을 그리므로, 노드의 불투명도가 글과 그림자에 한 장째로 먹는다.
- 가로 정렬은 노드 안에서 왼쪽, 가운데, 오른쪽이고, 세로는 노드의 가운데다.
- 글이 노드 너비를 넘으면 처음 크기에서 최소 비율(set_fit_minimum_ratio)까지 줄여 맞춘다.
- 그림자는 상자 흐림 세 번(가우시안에 가까움)으로 번지게 할 수 있다. 번지는 만큼 구운 그림의 가장자리를 넓힌다.
- 굽는 배율, 글, 꼴, 글 방향, 노드 너비가 바뀌거나 글꼴이 새로 올라오면 다시 굽는다. 같은 모양을 구운 것이 있으면 나눠 쓴다. (sharedbakecache.rs)
- 글 방향(set_text_direction, 원본 setTextDirection, 처음 Ltr)으로 양방향 정렬과 모양 짓기를 하고, 정렬 Start / End 를 푼다.
- 화면 밖 거르기는 마지막으로 구운 한 장이 놓이는 사각형(넘친 글, 그림자 여백 포함)으로 판정한다. 세터로 바꾼 뒤 아직 굽지 않았거나 노드 너비나 굽는 배율이 달라졌으면 거르지 않는다.
- 기울임, 밑줄, 취소선(set_italic, set_underline, set_strikethrough): 선의 자리, 두께는 Text 의 가운데 기준선과 같고, 선도 글과 함께 구워 그림자가 깔린다.
  한 줄을 줄여 맞추는 글이라 줄바꿈 방식은 두지 않는다.
- 웹은 원본처럼 캔버스에 그림자(shadowColor, shadowBlur, shadowOffsetY)를 걸고 글자 색으로 fillText 해 굽는다. (platform/web/systemfont.rs 의 bake_shadow_text, 여백과 자리 셈이 원본과 같다)
  데스크탑은 글자 캐시의 rasterize_text 로 모양을 받아 상자 흐림 세 번으로 그림자를 셈한다.
- 색 글리프(이모지)는 제 색에 글자 색의 알파만 곱해 얹고, 그림자는 이모지 모양까지 드리운다. (원본 fillStyle 과 캔버스 그림자)
- 원본과 다른 것: 원본 setFontFace(FontFace) 는 set_font_family(올린 글꼴 이름), 그림자의 색과 불투명도(setShadow 의 색 글과 opacity)는 Color 하나(알파가 불투명도)로 받는다.
  원본 getBakeCanvas 는 구운 캔버스 대신 구운 Image 를 돌려주는 get_bake_image 다.

## ShadowText (struct) (Default, Component)

그림자를 깐 한 줄 글 컴포넌트. (그림자 위에 글을 얹은 모습을 그림 한 장에 구워 그린다)
- 노드의 불투명도가 글과 그림자에 한 장째로 먹는다. 가로는 정렬대로, 세로는 노드 가운데에 놓는다.
- 글이 노드 너비를 넘으면 처음 크기에서 최소 비율까지 줄여 맞춘다.
- 처음은 빈 글, 글꼴 default, 크기 16, 가운데 정렬, Ltr, 흰 글자, 검은 그림자(내림 0, 번짐 0), 최소 비율 0.6.

예제: 굵은 제목에 흐린 그림자

```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): 생성.
  - returns ShadowText: 빈 글, 글꼴 default, 크기 16, 가운데 정렬, 흰 글자, 검은 그림자인 새 컴포넌트.
- `pub fn create_with_text(text: &str, font_size: f64) -> ShadowText` (static, since 0.4.0): 글로 생성.
  - `text` &str: 그릴 한 줄 글.
  - `font_size` f64: 처음 글자 크기. (픽셀)
  - returns ShadowText: 그 글과 크기로 만든 새 컴포넌트. (나머지는 new 와 같다)

  예제: 점수 글 만들기

  ```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): 출력.
  - 글이 비면 그리지 않는다. 굽는 배율, 글, 꼴, 색, 그림자, 최소 비율, 노드 너비, 글 방향, 올린 글꼴 수가 지난번과 다르면 다시 굽는다.
  - 글꼴 모양을 받지 못해 굽지 못하면 그림자 없이 Graphic 의 글로 그린다. (밑줄, 취소선은 선으로 긋는다)
  - `transform` &NodeTransform: 그릴 노드 트랜스폼. (content_size 를 읽는다)
  - `graphic` &mut Graphic: 그릴 그래픽. (노드 트랜스폼이 걸린 상태, 굽는 배율과 글자 캐시에 쓴다)
- `pub fn set_text(&mut self, text: &str)` (method, since 0.4.0): 글 설정.
  - `text` &str: 그릴 한 줄 글. (빈 글이면 그리지 않는다)
- `pub fn get_text(&self) -> &str` (method, since 0.4.0): 글 반환.
  - returns &str: 그리는 글. (정하지 않았으면 빈 글)
- `pub fn set_font_family(&mut self, font_family: &str)` (method, since 0.4.0): 글꼴 이름 설정. (엔진에 올린 글꼴 이름, 처음은 default. 원본 setFontFace)
  - `font_family` &str: 엔진에 올린 글꼴 이름.
- `pub fn get_font_family(&self) -> &str` (method, since 0.4.0): 글꼴 이름 반환. (원본 getFontFace, 원본은 FontFace 객체라 반사 저장이 적지 않아 이것도 저장에서 뺀다)
  - returns &str: 글꼴 이름.
- `pub fn get_font_size(&self) -> f64` (method, since 0.4.0): 처음 글자 크기 반환. (원본 getFontSize)
  - returns f64: 처음 글자 크기. (픽셀, 넘쳐 줄인 크기가 아니다)
- `pub fn set_text_align(&mut self, text_align: TextAlign)` (method, since 0.4.0): 가로 정렬 설정.
  - `text_align` TextAlign: 노드 안 가로 정렬. (Left, Center, Right, Start / End 는 글 방향으로 푼다, 처음 Center)
- `pub fn get_text_align(&self) -> TextAlign` (method, since 0.4.0): 가로 정렬 반환.
  - returns TextAlign: 가로 정렬.
- `pub fn set_text_direction(&mut self, text_direction: TextDirection)` (method, since 0.4.0): 글 방향 설정. (Ltr, Rtl, Auto. 원본 setTextDirection("ltr" / "rtl"), Inherit 는 Ltr 로 본다)
  - `text_direction` TextDirection: 글 방향. (Auto 는 글의 첫 강한 글자로 정한다, 처음 Ltr)
- `pub fn get_text_direction(&self) -> TextDirection` (method, since 0.4.0): 글 방향 반환.
  - returns TextDirection: 설정한 글 방향. (Auto, Inherit 를 푼 값이 아니다)
- `pub fn is_bold(&self) -> bool` (method, since 0.4.0): 굵게 여부.
  - returns bool: 굵게 그리면 true.
- `pub fn set_text_style(&mut self, font_size: f64, is_bold: bool, text_color: impl Into<Color>)` (method, since 0.4.0): 글의 꼴 설정. (처음 크기, 굵기, 색)
  - `font_size` f64: 처음 글자 크기. (픽셀, 넘치면 이 크기에서 줄인다)
  - `is_bold` bool: 굵게 그릴지.
  - `text_color` impl Into<Color>: 글자 색. (Color 또는 "#rrggbb" 같은 문자열, 알파가 글자 불투명도)
- `pub fn set_italic(&mut self, is_italic: bool)` (method, since 0.4.0): 기울임 설정.
  - `is_italic` bool: 기울여 그릴지. (처음 false)
- `pub fn is_italic(&self) -> bool` (method, since 0.4.0): 기울임 여부.
  - returns bool: 기울여 그리면 true.
- `pub fn set_underline(&mut self, is_underline: bool)` (method, since 0.4.0): 밑줄 설정.
  - `is_underline` bool: 밑줄을 그을지. (처음 false)
- `pub fn is_underline(&self) -> bool` (method, since 0.4.0): 밑줄 여부.
  - returns bool: 밑줄을 그으면 true.
- `pub fn set_strikethrough(&mut self, is_strikethrough: bool)` (method, since 0.4.0): 취소선 설정.
  - `is_strikethrough` bool: 취소선을 그을지. (처음 false)
- `pub fn is_strikethrough(&self) -> bool` (method, since 0.4.0): 취소선 여부.
  - returns bool: 취소선을 그으면 true.
- `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): 그림자 설정. (색, 불투명도, 아래로 내려가는 만큼, 번지는 만큼, 원본 setShadow(색, 불투명도, 내려가는 만큼, 번지는 만큼) 과 같다)
  - `shadow_color` impl Into<Color>: 그림자 색. (Color 또는 "#rrggbb" 같은 문자열, 색의 알파는 읽지 않는다, 처음 검정)
  - `shadow_opacity` f64: 그림자 불투명도. (0 ~ 1, 처음 1)
  - `shadow_offset` f64: 그림자가 아래로 내려가는 만큼. (노드 좌표, 처음 0)
  - `shadow_blur` f64: 그림자가 번지는 만큼. (노드 좌표, 0 보다 작으면 0, 처음 0)
- `pub fn set_fit_minimum_ratio(&mut self, fit_minimum_ratio: f64)` (method, since 0.4.0): 넘칠 때 줄이는 가장 작은 비율 설정. (1 이면 줄이지 않는다)
  - `fit_minimum_ratio` f64: 처음 글자 크기에 대한 가장 작은 비율. (0.01 ~ 1 로 맞춘다, 처음 0.6)

  예제: 줄이지 않게 하기와 아래 한계

  ```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 f64: 처음 글자 크기에 대한 가장 작은 비율. (0.01 ~ 1)
- `pub fn get_bake_image(&self) -> Option<&Rc<Image>>` (method, since 0.4.0): 구운 그림 반환. (아직 굽지 않았거나 그림자 없이 엔진의 글로 그렸으면 None, 원본 getBakeCanvas)
  - returns Option<&Rc<Image>>: 마지막으로 구운 그림. 아직 굽지 않았거나 굽지 못해 엔진의 글로 그렸으면 None.

## Functions and constants

- `pub fn blur_coverage(coverage: &[u8], width: i32, height: i32, radius: f64) -> Vec<u8>` (method, since 0.4.0): 덮인 정도(0 ~ 255)를 상자 흐림 세 번으로 번지게 하기. (반지름은 픽셀, 0 이하면 그대로)
  - 가로, 세로 상자 흐림을 세 번 거듭해 가우시안에 가깝게 한다. 상자 반지름은 표준편차를 radius / 2 로 보고 셈하며 1 이상이다.
  - 그림 밖은 0 으로 쳐서 가장자리 쪽은 옅어진다.
  - coverage 가 width x height 보다 짧으면 패닉한다. (radius 가 0 이하이거나 width, height 가 0 이하면 읽지 않고 그대로 돌려준다)
  - `coverage` &[u8]: 덮인 정도. (0 ~ 255, 한 줄 width 개씩 height 줄)
  - `width` i32: 너비. (픽셀)
  - `height` i32: 높이. (픽셀)
  - `radius` f64: 흐림 반지름. (픽셀, 0 이하면 흐리지 않는다)
  - returns Vec<u8>: 번진 덮인 정도. (coverage 와 같은 길이, 0 ~ 255)

  예제: 한 점을 번지게 하기

  ```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): 그림자 위에 모양을 얹은 RGBA 그림 짓기. (모양의 덮인 정도와 색, 그림자의 덮인 정도와 색과 아래로 내림, 곧은 알파)
  - 그림자는 shape 를 흐려 shadow_offset_y 만큼 내려 깔고, 그 위에 모양을 알파로 얹는다.
  - shape_pixels 를 주면 모양의 색을 픽셀마다 쓴다. (그림, None 이면 shape_color 한 빛)
  - 내린 그림자가 그림 밖으로 나간 부분은 버린다. 길이가 모자라면(shape_coverage 가 width x height, shape_pixels 가 width x height x 4 보다 짧으면) 패닉한다.
  - `shape_coverage` &[u8]: 모양의 덮인 정도. (0 ~ 255, width x height) 그림자는 이것을 흐려 짓고, shape_pixels 가 None 이면 모양의 알파도 이것이다.
  - `shape_pixels` Option<&[u8]>: 모양의 곧은 알파 RGBA 픽셀. (width x height x 4, None 이면 shape_color 한 빛)
  - `width` i32: 너비. (픽셀)
  - `height` i32: 높이. (픽셀)
  - `shape_color` Color: shape_pixels 가 None 일 때 모양 색. (알파를 덮인 정도에 곱한다)
  - `shadow_color` Color: 그림자 색. (알파가 불투명도)
  - `shadow_blur` f64: 그림자 흐림 반지름. (픽셀, blur_coverage 의 radius, 0 이하면 흐리지 않는다)
  - `shadow_offset_y` i32: 그림자를 아래로 내리는 만큼. (픽셀, 음수면 위로)
  - returns Rc<Image>: 지은 새 그림. (width x height)

  예제: 흰 점 아래 한 칸에 검은 그림자

  ```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);
  ```

