# core/component/shadowtext (vanilla.js 0.4.0)

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

그림자를 깐 한 줄 글.

## ShadowText extends Component

그림자를 깐 한 줄 글.
- 글자 노드 두 벌(같은 글을 검게 밑에, 글을 위에)로 그림자를 깔면 둘이 따로 비쳐, 노드를 흐리게 할 때 흐린 글 사이로 밑의 그림자가 보인다.
  이 컴포넌트는 그림자 위에 글을 얹은 모습을 캔버스 한 장에 구워 그 한 장을 그리므로, 노드의 불투명도가 글과 그림자에 한 장째로 먹는다.
- 가로 정렬과 놓는 자리는 Graphic.drawTextEntry 와 같은 셈이다. 세로는 노드의 가운데다.
- 글이 노드 너비를 넘으면 처음 크기에서 최소 비율(setFitMinimumRatio)까지 줄여 맞춘다.
- 그리는 배율(기기의 DPR, 뷰 배율), 글, 꼴, 노드 너비가 바뀌거나 웹 글꼴의 조각이 새로 받아지면 다시 굽는다.
- 그림자는 캔버스의 그림자로 칠해 번지게 할 수 있다. 번지는 만큼 구운 캔버스의 가장자리를 넓힌다.
- 기울임, 밑줄, 취소선(setItalic, setUnderline, setStrikethrough): 선의 자리, 두께는 Text 의 가운데 기준선과 같고, 선도 글과 함께 구워 그림자가 깔린다.
- 글 방향은 "ltr", "rtl", "auto"(글의 첫 강한 글자로) 이다.

예제: 그림자를 깐 제목 글

```js
import { Engine, EngineConfiguration, Scene, Vector2, WorldNode, ShadowText, FontAsset } from "./build/vanilla.js";

class MainScene extends Scene {
	async load(engine) {
		await super.load(engine);
		const fontAsset = new FontAsset();
		await fontAsset.loadFont("DOSGothic", "examples/assets/DOSGothic.woff");
		const titleNode = new WorldNode();
		titleNode.setContentSize(Vector2.create(400, 60));
		titleNode.setLocalPosition(Vector2.create(320, 180));
		const shadowText = titleNode.addComponent(ShadowText);
		shadowText.setFontFace(fontAsset.fontFace);
		shadowText.setTextStyle(40, true, "#ffffff");
		shadowText.setShadow("#000000", 0.8, 3, 4);
		shadowText.setText("Vanilla Engine");
		const root = this.getRoot();
		root.addChild(titleNode);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.4.0): 생성.
- `draw(graphic)` (method, since 0.4.0): 출력.
  - `graphic` Graphic: 그래픽.
- `calculateDrawnRect(rect)` (method, since 0.4.0): 그리는 사각형을 rect 에 적기. (오버라이드: 정렬로 놓은 글에 그림자의 번짐과 내림을 더한 사각형, 화면 밖 거르기와 하위 트리 그리기 경계가 쓴다)
  - 가로는 처음 글꼴 크기로 잰 글 너비(넘쳐 줄여도 이보다 좁다)를 정렬대로 놓고, 글꼴 크기의 반과 번짐의 두 배만큼 양옆에 더한다.
    "start" 와 "end" 는 그릴 때의 방향으로 왼쪽이나 오른쪽이 되므로 둘 다 품는다.
  - 세로는 내용 높이의 가운데에서 위아래로 글꼴 크기의 1.5 배와 번짐의 두 배씩, 아래는 그림자 내림만큼 더 잡는다. (밑줄, 취소선도 이 안)
    (캔버스 shadowBlur 는 표준 편차 blur / 2 의 가우스라 꼬리가 1.5 배까지 간다, 굽는 칸의 여유와 같이 두 배)
  - 글이 없으면 크기 0. 잴 수 없으면 false. (거르지 않는다)
  - `rect` Rect: 그리는 사각형을 적을 곳. (노드 내용 영역의 왼쪽 위가 원점)
  - returns boolean: 적었으면 참. (잴 수 없으면 거짓)

  예제: 그림자 글이 그리는 곳 보기

  ```js
  import { WorldNode, ShadowText, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(120, 30));
  const shadowText = node.addComponent(ShadowText);
  shadowText.setText("SCORE");
  shadowText.setShadow("#000000", 0.8, 3, 4);
  const drawnRect = Rect.zero();
  const isKnown = shadowText.calculateDrawnRect(drawnRect);
  console.log(isKnown, drawnRect.size.y > 30);
  ```

- `detach(node)` (method, since 0.4.0): 노드에서 떨어짐. (나눠 쓰던 구운 그림을 놓는다)
  - `node` ComponentNode: 떨어지는 노드.
- `bakeText(graphic, text, fitWidth, bakeScale, textDirection, fontFace)` (method, since 0.4.0): 굽기. (글을 한 번 칠하면 캔버스가 그 밑에 그림자를 먼저 칠한다)
  - 텍스처 캐시가 캔버스마다 한 번 올리므로 구울 때마다 새 캔버스를 만든다. (앞의 것은 수거되며 텍스처도 함께 수거된다)
  - `graphic` Graphic: 그래픽. (처음 크기의 글 너비를 잰다)
  - `text` string: 구울 글.
  - `fitWidth` number: 맞출 너비. (0 이하면 맞추지 않는다)
  - `bakeScale` number: 굽는 배율. (Graphic.calculateTextBakeScale)
  - `textDirection` string: 굽는 방향. ("ltr" 또는 "rtl")
  - `fontFace` FontFace | null: 글꼴. (null 이면 시스템 글꼴)
- `measureTextWidth(graphic)` (method, since 0.4.0): 글 너비 재기. (처음 크기로, Text 와 같은 재기, 내용 크기 단위)
  - `graphic` Graphic: 그래픽. (글 재기 캐시를 쓴다)
  - returns number: 글 너비. (내용 크기 단위, 글이 비었으면 0)
- `setText(text)` (method, since 0.4.0): 글 설정.
  - `text` string: 보일 한 줄 글.
- `getText()` (method, since 0.4.0): 글 반환.
  - returns string: 글.
- `setFontFace(fontFace)` (method, since 0.4.0): 글꼴 설정. (null 이면 시스템 글꼴)
  - `fontFace` FontFace | null: 글꼴. (null 이면 시스템 글꼴)
- `getFontFace()` (method, since 0.4.0): 글꼴 반환.
  - returns FontFace | null: 글꼴. (null 이면 시스템 글꼴)
- `setTextStyle(fontSize, isBold, textColor)` (method, since 0.4.0): 글의 꼴 설정. (처음 크기, 굵기, 색)
  - `fontSize` number: 처음 글자 크기. (내용 크기 단위, 넘치면 줄여 굽는다)
  - `isBold` boolean: 굵게 할지.
  - `textColor` Color | string: 글 색. (Color 나 "#rrggbb" 같은 색 글, 알파가 글자 불투명도)
- `getFontSize()` (method, since 0.4.0): 처음 글자 크기 반환.
  - returns number: 처음 글자 크기.
- `setTextAlign(textAlign)` (method, since 0.4.0): 가로 정렬 설정.
  - `textAlign` string: "left", "center", "right", "start", "end"
- `getTextAlign()` (method, since 0.4.0): 가로 정렬 반환.
  - returns string: 가로 정렬. ("left", "center", "right", "start", "end")
- `setTextDirection(textDirection)` (method, since 0.4.0): 글 방향 설정. ("ltr", "rtl", "auto")
  - `textDirection` string: 글 방향. ("ltr", "rtl", "auto", 그 밖의 값은 "ltr" 로 굽는다)
- `getTextDirection()` (method, since 0.4.0): 글 방향 반환.
  - returns string: 글 방향. ("ltr", "rtl", "auto")
- `resolveBakeTextDirection()` (method, since 0.4.0): 굽는 실제 방향. ("rtl" 이면 오른쪽에서 왼쪽, "auto" 는 글의 첫 강한 글자로, 그 밖은 "ltr")
  - returns string: "ltr" 또는 "rtl".
- `isBold()` (method, since 0.4.0): 굵게 여부.
  - returns boolean: 굵게면 참.
- `setItalic(isItalic)` (method, since 0.4.0): 기울임 설정.
  - `isItalic` boolean: 기울일지. (true 일 때만 기울인다)
- `isItalic()` (method, since 0.4.0): 기울임 여부.
  - returns boolean: 기울임이면 참.
- `setUnderline(isUnderline)` (method, since 0.4.0): 밑줄 설정.
  - `isUnderline` boolean: 밑줄을 그을지. (true 일 때만 긋는다)
- `isUnderline()` (method, since 0.4.0): 밑줄 여부.
  - returns boolean: 밑줄이면 참.
- `setStrikethrough(isStrikethrough)` (method, since 0.4.0): 취소선 설정.
  - `isStrikethrough` boolean: 취소선을 그을지. (true 일 때만 긋는다)
- `isStrikethrough()` (method, since 0.4.0): 취소선 여부.
  - returns boolean: 취소선이면 참.
- `setShadow(shadowColor, shadowOpacity, shadowOffset, shadowBlur = 0)` (method, since 0.4.0): 그림자 설정. (색, 불투명도, 글 아래로 내려가는 만큼, 번지는 만큼)
  - `shadowColor` Color | string: 그림자 색. (Color 나 "#rrggbb" 같은 색 글, 알파는 읽지 않는다)
  - `shadowOpacity` number: 그림자 불투명도. (0 ~ 1)
  - `shadowOffset` number: 그림자가 글 아래로 내려가는 만큼. (내용 크기 단위)
  - `shadowBlur` number: 0 이면 번지지 않는다.
- `setFitMinimumRatio(fitMinimumRatio)` (method, since 0.4.0): 넘칠 때 줄이는 가장 작은 비율 설정. (1 이면 줄이지 않는다)
  - `fitMinimumRatio` number: 넘칠 때 줄이는 가장 작은 비율. (0.01 ~ 1 로 자른다, 1 이면 줄이지 않는다)
- `getFitMinimumRatio()` (method, since 0.4.0): 넘칠 때 줄이는 가장 작은 비율 반환.
  - returns number: 넘칠 때 줄이는 가장 작은 비율.
- `getBakeCanvas()` (method, since 0.4.0): 구운 캔버스 반환. (아직 굽지 않았으면 null)
  - returns HTMLCanvasElement | null: 구운 캔버스. (아직 굽지 않았으면 null)
