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 다.
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.
ShadowTextstruct traits Default, Component
그림자를 깐 한 줄 글 컴포넌트. (그림자 위에 글을 얹은 모습을 그림 한 장에 구워 그린다)
- 노드의 불투명도가 글과 그림자에 한 장째로 먹는다. 가로는 정렬대로, 세로는 노드 가운데에 놓는다.
- 글이 노드 너비를 넘으면 처음 크기에서 최소 비율까지 줄여 맞춘다.
- 처음은 빈 글, 글꼴 default, 크기 16, 가운데 정렬, Ltr, 흰 글자, 검은 그림자(내림 0, 번짐 0), 최소 비율 0.6.
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.
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);생성.
Constructor.
반환ReturnsShadowText 빈 글, 글꼴 default, 크기 16, 가운데 정렬, 흰 글자, 검은 그림자인 새 컴포넌트.New component with empty text, font default, size 16, center alignment, white text and a black shadow.
짝Counterpart: vanilla.js ShadowText.constructor
글로 생성.
Constructor with text.
- text&str
- 그릴 한 줄 글.Single-line text to draw.
- font_sizef64
- 처음 글자 크기. (픽셀)Initial font size. (pixels)
반환ReturnsShadowText 그 글과 크기로 만든 새 컴포넌트. (나머지는 new 와 같다)New component with that text and size. (the rest is the same as new)
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");출력.
- 글이 비면 그리지 않는다. 굽는 배율, 글, 꼴, 색, 그림자, 최소 비율, 노드 너비, 글 방향, 올린 글꼴 수가 지난번과 다르면 다시 굽는다.
- 글꼴 모양을 받지 못해 굽지 못하면 그림자 없이 Graphic 의 글로 그린다. (밑줄, 취소선은 선으로 긋는다)
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
- 그릴 노드 트랜스폼. (content_size 를 읽는다)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)
글 설정.
Sets the text.
- text&str
- 그릴 한 줄 글. (빈 글이면 그리지 않는다)Single-line text to draw. (does not draw if empty)
짝Counterpart: vanilla.js ShadowText.setText
글 반환.
Returns the text.
반환Returns&str 그리는 글. (정하지 않았으면 빈 글)The text being drawn. (empty string if not set)
짝Counterpart: vanilla.js ShadowText.getText
글꼴 이름 설정. (엔진에 올린 글꼴 이름, 처음은 default. 원본 setFontFace)
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.
글꼴 이름 반환. (원본 getFontFace, 원본은 FontFace 객체라 반사 저장이 적지 않아 이것도 저장에서 뺀다)
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.
처음 글자 크기 반환. (원본 getFontSize)
Returns the initial font size. (the original getFontSize)
반환Returnsf64 처음 글자 크기. (픽셀, 넘쳐 줄인 크기가 아니다)Initial font size. (pixels, not the size shrunk for overflow)
짝Counterpart: vanilla.js ShadowText.getFontSize
가로 정렬 설정.
Sets the horizontal alignment.
- text_alignTextAlign
- 노드 안 가로 정렬. (Left, Center, Right, Start / End 는 글 방향으로 푼다, 처음 Center)Horizontal alignment within the node. (Left, Center, Right, Start / End are resolved by the text direction, initially Center)
짝Counterpart: vanilla.js ShadowText.setTextAlign
가로 정렬 반환.
Returns the horizontal alignment.
반환ReturnsTextAlign 가로 정렬.Horizontal alignment.
짝Counterpart: vanilla.js ShadowText.getTextAlign
글 방향 설정. (Ltr, Rtl, Auto. 원본 setTextDirection("ltr" / "rtl"), Inherit 는 Ltr 로 본다)
Sets the text direction. (Ltr, Rtl, Auto. The original setTextDirection("ltr" / "rtl"), Inherit is treated as Ltr)
- text_directionTextDirection
- 글 방향. (Auto 는 글의 첫 강한 글자로 정한다, 처음 Ltr)Text direction. (Auto decides by the first strong character of the text, initially Ltr)
짝Counterpart: vanilla.js ShadowText.setTextDirection
글 방향 반환.
Returns the text direction.
반환ReturnsTextDirection 설정한 글 방향. (Auto, Inherit 를 푼 값이 아니다)The set text direction. (not the value with Auto or Inherit resolved)
짝Counterpart: vanilla.js ShadowText.getTextDirection
굵게 여부.
Whether bold.
반환Returnsbool 굵게 그리면 true.true if drawn bold.
짝Counterpart: vanilla.js ShadowText.isBold
pub fn set_text_style(&mut self, font_size: f64, is_bold: bool, text_color: impl Into<Color>)#글의 꼴 설정. (처음 크기, 굵기, 색)
Sets the text style. (initial size, weight, color)
- font_sizef64
- 처음 글자 크기. (픽셀, 넘치면 이 크기에서 줄인다)Initial font size. (pixels, shrinks from this size if it overflows)
- is_boldbool
- 굵게 그릴지.Whether to draw bold.
- text_colorimpl Into<Color>
- 글자 색. (Color 또는 "#rrggbb" 같은 문자열, 알파가 글자 불투명도)Text color. (a Color or a string such as "#rrggbb", alpha is the text opacity)
짝Counterpart: vanilla.js ShadowText.setTextStyle
기울임 설정.
Sets italic.
- is_italicbool
- 기울여 그릴지. (처음 false)Whether to draw italic. (initially false)
짝Counterpart: vanilla.js ShadowText.setItalic
기울임 여부.
Whether italic.
반환Returnsbool 기울여 그리면 true.true if drawn italic.
짝Counterpart: vanilla.js ShadowText.isItalic
밑줄 설정.
Sets underline.
- is_underlinebool
- 밑줄을 그을지. (처음 false)Whether to draw an underline. (initially false)
짝Counterpart: vanilla.js ShadowText.setUnderline
밑줄 여부.
Whether underlined.
반환Returnsbool 밑줄을 그으면 true.true if an underline is drawn.
짝Counterpart: vanilla.js ShadowText.isUnderline
취소선 설정.
Sets strikethrough.
- is_strikethroughbool
- 취소선을 그을지. (처음 false)Whether to draw a strikethrough. (initially false)
짝Counterpart: vanilla.js ShadowText.setStrikethrough
취소선 여부.
Whether strikethrough.
반환Returnsbool 취소선을 그으면 true.true if a strikethrough is drawn.
짝Counterpart: vanilla.js ShadowText.isStrikethrough
pub fn set_shadow(&mut self, shadow_color: impl Into<Color>, shadow_opacity: f64, shadow_offset: f64, shadow_blur: f64)#그림자 설정. (색, 불투명도, 아래로 내려가는 만큼, 번지는 만큼, 원본 setShadow(색, 불투명도, 내려가는 만큼, 번지는 만큼) 과 같다)
Shadow settings. (color, opacity, downward offset, blur amount; same as the original setShadow(color, opacity, downward offset, blur amount))
- shadow_colorimpl Into<Color>
- 그림자 색. (Color 또는 "#rrggbb" 같은 문자열, 색의 알파는 읽지 않는다, 처음 검정)Shadow color. (a Color or a string like "#rrggbb"; the alpha of the color is not read; black at first)
- shadow_opacityf64
- 그림자 불투명도. (0 ~ 1, 처음 1)Shadow opacity. (0 to 1, 1 at first)
- shadow_offsetf64
- 그림자가 아래로 내려가는 만큼. (노드 좌표, 처음 0)How far the shadow drops down. (node coordinates, initially 0)
- shadow_blurf64
- 그림자가 번지는 만큼. (노드 좌표, 0 보다 작으면 0, 처음 0)How far the shadow blurs. (node coordinates, 0 if less than 0, initially 0)
짝Counterpart: vanilla.js ShadowText.setShadow
넘칠 때 줄이는 가장 작은 비율 설정. (1 이면 줄이지 않는다)
Sets the minimum ratio to shrink to on overflow. (no shrinking if 1)
- fit_minimum_ratiof64
- 처음 글자 크기에 대한 가장 작은 비율. (0.01 ~ 1 로 맞춘다, 처음 0.6)Smallest ratio to the initial font size. (fitted to 0.01 ~ 1, initially 0.6)
짝Counterpart: vanilla.js ShadowText.setFitMinimumRatio
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);넘칠 때 줄이는 가장 작은 비율 반환.
Returns the minimum ratio to shrink to on overflow.
반환Returnsf64 처음 글자 크기에 대한 가장 작은 비율. (0.01 ~ 1)Smallest ratio to the initial font size. (0.01 ~ 1)
짝Counterpart: vanilla.js ShadowText.getFitMinimumRatio
구운 그림 반환. (아직 굽지 않았거나 그림자 없이 엔진의 글로 그렸으면 None, 원본 getBakeCanvas)
Returns the baked image. (None if not baked yet or if drawn with the engine's text without a shadow, the original getBakeCanvas)
반환ReturnsOption<&Rc<Image>> 마지막으로 구운 그림. 아직 굽지 않았거나 굽지 못해 엔진의 글로 그렸으면 None.The last baked image. None if not baked yet or if it could not bake and drew with the engine's text.
함수Functions
덮인 정도(0 ~ 255)를 상자 흐림 세 번으로 번지게 하기. (반지름은 픽셀, 0 이하면 그대로)
- 가로, 세로 상자 흐림을 세 번 거듭해 가우시안에 가깝게 한다. 상자 반지름은 표준편차를 radius / 2 로 보고 셈하며 1 이상이다.
- 그림 밖은 0 으로 쳐서 가장자리 쪽은 옅어진다.
- coverage 가 width x height 보다 짧으면 패닉한다. (radius 가 0 이하이거나 width, height 가 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]
- 덮인 정도. (0 ~ 255, 한 줄 width 개씩 height 줄)Coverage. (0 ~ 255, height rows of width values each)
- widthi32
- 너비. (픽셀)Width. (pixels)
- heighti32
- 높이. (픽셀)Height. (pixels)
- radiusf64
- 흐림 반지름. (픽셀, 0 이하면 흐리지 않는다)Blur radius. (pixels, no blur if 0 or less)
반환ReturnsVec<u8> 번진 덮인 정도. (coverage 와 같은 길이, 0 ~ 255)Blurred coverage. (same length as coverage, 0 ~ 255)
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>#그림자 위에 모양을 얹은 RGBA 그림 짓기. (모양의 덮인 정도와 색, 그림자의 덮인 정도와 색과 아래로 내림, 곧은 알파)
- 그림자는 shape 를 흐려 shadow_offset_y 만큼 내려 깔고, 그 위에 모양을 알파로 얹는다.
- shape_pixels 를 주면 모양의 색을 픽셀마다 쓴다. (그림, None 이면 shape_color 한 빛)
- 내린 그림자가 그림 밖으로 나간 부분은 버린다. 길이가 모자라면(shape_coverage 가 width x height, shape_pixels 가 width x height x 4 보다 짧으면) 패닉한다.
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]
- 모양의 덮인 정도. (0 ~ 255, width x height) 그림자는 이것을 흐려 짓고, shape_pixels 가 None 이면 모양의 알파도 이것이다.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_pixelsOption<&[u8]>
- 모양의 곧은 알파 RGBA 픽셀. (width x height x 4, None 이면 shape_color 한 빛)Straight alpha RGBA pixels of the shape. (width x height x 4, a single shape_color if None)
- widthi32
- 너비. (픽셀)Width. (pixels)
- heighti32
- 높이. (픽셀)Height. (pixels)
- shape_colorColor
- shape_pixels 가 None 일 때 모양 색. (알파를 덮인 정도에 곱한다)Shape color when shape_pixels is None. (its alpha is multiplied by the coverage)
- shadow_colorColor
- 그림자 색. (알파가 불투명도)Shadow color. (alpha is the opacity)
- shadow_blurf64
- 그림자 흐림 반지름. (픽셀, blur_coverage 의 radius, 0 이하면 흐리지 않는다)Shadow blur radius. (pixels, the radius of blur_coverage, no blur if 0 or less)
- shadow_offset_yi32
- 그림자를 아래로 내리는 만큼. (픽셀, 음수면 위로)How far to drop the shadow down. (pixels, up if negative)
반환ReturnsRc<Image> 지은 새 그림. (width x height)The new built image. (width x height)
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);