misc/dynamicfont
다이나믹 폰트. (원본 misc/dynamicfont.js 의 DynamicFont, GlyphInfo)
- 정한 글자들을 한 글꼴, 한 크기로 글자 아틀라스(그림 한 장)에 모아 굽고, 글을 그릴 때 아틀라스에서 글리프를 잘라 그린다. 글자마다 같은 그림을 쓰므로 Graphic 이 한 번의 묶음으로 그린다. (문자열마다 텍스처를 굽는 Text 와 달리 묶어 그리기가 끊기지 않는다) 점수, 피해 숫자, 시계처럼 글자가 자주 바뀌고 같은 글자를 많이 그리는 곳에 쓴다.
- 아틀라스는 가로 1024 픽셀 안에 줄 단위로 놓고, 글리프 사이에 2 픽셀 여백을 둔다. (원본과 같음)
- 글자는 글자 묶음(받침, 결합 부호, 이모지 묶음) 단위로 센다. 아틀라스에 없는 글자는 글자 크기의 반만큼 비우고 넘어간다. (원본과 같음)
- 글자는 흰색으로, 색 글리프(이모지)는 제 색으로 굽는다. (원본은 흰색으로 fillText 해 브라우저가 이모지를 제 색으로 그린다)
- 원본과 다른 것: 원본 setFontAsset(FontAsset) 은 set_font_family(올린 글꼴 이름) 다. (FontAsset.get_family 를 넘긴다) 원본 getAtlasCanvas 는 get_atlas_image (
Rc<Image>) 다. 데스크탑은 글리프를 글자 캐시의 글꼴 얼굴(모양 짓기, 가짜 굵게와 기울임 포함)로 굽고, 글자를 덮는 얼굴이 하나도 없으면 그 글자는 굽지 못한다. (나머지 글자는 굽는다) 웹은 원본처럼 브라우저 Canvas2D 로 굽는다. build_atlas 와 draw_text 는 Graphic 을 받는다. (글꼴은 Graphic 의 글자 캐시에 있다)
Dynamic font. (DynamicFont, GlyphInfo of the original misc/dynamicfont.js)
- Bakes the chosen characters in one font and one size into a glyph atlas (one image), and draws text by cutting glyphs out of the atlas. Every character uses the same image, so Graphic draws it in one batch. (unlike Text, which bakes a texture per string, batching is not broken) Used where characters change often and the same characters are drawn many times, like scores, damage numbers and clocks.
- The atlas lays glyphs out in rows within 1024 pixels wide, with a 2 pixel gap between glyphs. (same as the original)
- Characters are counted in grapheme clusters (Hangul final consonants, combining marks, emoji sequences). Characters not in the atlas leave a gap of half the font size and are skipped. (same as the original)
- Characters are baked in white, and color glyphs (emoji) in their own colors. (the original calls fillText in white and the browser draws emoji in their own colors)
- Differences from the original:
The original setFontAsset(FontAsset) is set_font_family(registered font name). (pass FontAsset.get_family)
The original getAtlasCanvas is get_atlas_image (`Rc<Image>`). Desktop bakes glyphs with the font faces of the text cache (shaping, synthetic bold and italic included),
and a character not covered by any face cannot be baked. (the other characters are baked) The web bakes with the browser Canvas2D like the original.
build_atlas and draw_text take Graphic. (fonts are in Graphic's text cache)GlyphInfostruct traits Clone, Debug, PartialEq
글리프 정보. (원본 GlyphInfo)
- DynamicFont 가 아틀라스를 구울 때 글자마다 하나씩 만든다. get_glyph 로 읽는다.
Glyph info. (original GlyphInfo)
- DynamicFont makes one per character when baking the atlas. Read with get_glyph.
use vanilla::{GlyphInfo, Rect, Vector2};
let glyph_info = GlyphInfo {
character: String::from("7"),
uv_rect: Rect::create(2.0, 2.0, 18.0, 24.0),
advance: 20.0,
bearing_x: -1.0,
bearing_y: 23.0,
};
let pen_position = Vector2::create(100.0, 50.0);
let draw_left = pen_position.x - glyph_info.bearing_x;
let draw_top = pen_position.y - glyph_info.bearing_y;
assert_eq!((draw_left, draw_top), (101.0, 27.0));
assert_eq!(pen_position.x + glyph_info.advance, 120.0);아틀라스 안의 자리와 크기. (픽셀)
Position and size in the atlas. (pixels)
반환ReturnsRect
짝Counterpart: vanilla.js GlyphInfo.uvRect
가로로 나아가는 양. (전진폭 + 자간, 픽셀)
Horizontal advance. (advance width + letter spacing, pixels)
반환Returnsf64
짝Counterpart: vanilla.js GlyphInfo.advance
펜 자리에서 글리프 왼쪽 픽셀까지 거리. (왼쪽이 양수)
Distance from the pen position to the glyph's left pixel. (left is positive)
반환Returnsf64
짝Counterpart: vanilla.js GlyphInfo.bearingX
기준선에서 글리프 위쪽 픽셀까지 거리. (위가 양수)
Distance from the baseline to the glyph's top pixel. (up is positive)
반환Returnsf64
짝Counterpart: vanilla.js GlyphInfo.bearingY
DynamicFontstruct traits Default
다이나믹 폰트.
- 쓸 글자를 build_atlas 로 한 번 굽고, 매 프레임 draw_text 로 그린다. 둘 다 Graphic 이 있어야 한다.
Dynamic font.
- Bake the characters to use once with build_atlas, and draw every frame with draw_text. Both need a Graphic.
use vanilla::{DynamicFont, Graphic, Vector2};
fn draw_score(dynamic_font: &mut DynamicFont, graphic: &mut Graphic, score: u32) {
dynamic_font.build_atlas(graphic, "0123456789");
let score_text = score.to_string();
let text_width = dynamic_font.measure_text(&score_text);
dynamic_font.draw_text(&score_text, Vector2::create(640.0 - text_width * 0.5, 80.0), graphic);
}
let mut dynamic_font = DynamicFont::new();
dynamic_font.set_font_size(48.0);
dynamic_font.set_letter_spacing(2.0);생성. (글자 크기 32, 자간 0, 글꼴은 기본 글꼴. 원본과 같음)
Constructor. (font size 32, letter spacing 0, the default font. Same as the original)
반환ReturnsDynamicFont 아틀라스가 없는 새 다이나믹 폰트. (글꼴 이름 "default")A new dynamic font with no atlas. (font name "default")
짝Counterpart: vanilla.js DynamicFont.constructor
글꼴 이름 설정. (원본 setFontAsset, FontAsset 이면 get_family 를 넘긴다)
- 이미 구운 아틀라스는 바꾸지 않는다. 바꾼 뒤 rebuild_atlas 를 부른다.
Sets the font name. (original setFontAsset, pass get_family for a FontAsset)
- Does not change an already baked atlas. Call rebuild_atlas after changing it.
- font_family&str
- Graphic 의 글자 캐시에 올린 글꼴 이름. ("default" 는 기본 글꼴)Name of a font loaded into Graphic's glyph cache. ("default" is the default font)
글꼴 이름 반환.
Returns the font name.
반환Returns&str 지금 글꼴 이름.Current font name.
글자 크기 설정. (픽셀, 다음 build_atlas 부터)
Sets the font size. (pixels, from the next build_atlas)
- font_sizef64
- 글자 크기. (픽셀, 아틀라스에 없는 글자는 이것의 반만큼 비운다)Font size. (pixels, characters not in the atlas leave a gap of half of this)
짝Counterpart: vanilla.js DynamicFont.setFontSize
글자 크기 반환.
Returns the font size.
반환Returnsf64 지금 글자 크기. (픽셀)Current font size. (pixels)
짝Counterpart: vanilla.js DynamicFont.getFontSize
자간 설정. (픽셀, 다음 build_atlas 부터)
Sets the letter spacing. (pixels, from the next build_atlas)
- letter_spacingf64
- 글자마다 전진폭에 더할 양. (픽셀)Amount added to each character's advance width. (pixels)
짝Counterpart: vanilla.js DynamicFont.setLetterSpacing
자간 반환.
Returns the letter spacing.
반환Returnsf64 지금 자간. (픽셀)Current letter spacing. (pixels)
짝Counterpart: vanilla.js DynamicFont.getLetterSpacing
아틀라스 그림 반환. (아직 굽지 않았으면 None, 원본 getAtlasCanvas)
Returns the atlas image. (None if not baked yet, original getAtlasCanvas)
반환ReturnsOption<&Rc<Image>> 가로 1024 픽셀의 아틀라스 그림. 아직 굽지 않았거나 clear_atlas 뒤면 None.The atlas image, 1024 pixels wide. None if not baked yet or after clear_atlas.
글리프 정보 반환. (아틀라스에 없으면 None)
Returns glyph info. (None if not in the atlas)
- character&str
- 글자 묶음 하나.One grapheme cluster.
반환ReturnsOption<&GlyphInfo> 그 글자의 글리프 정보. 아틀라스에 없으면 None.Glyph info of that character. None if not in the atlas.
짝Counterpart: vanilla.js DynamicFont.getGlyph
아틀라스 굽기. (charset 의 글자 가운데 아직 없는 글자를 더하고, 아틀라스를 모든 글자로 다시 굽는다)
- 든 글자를 모두 다시 놓는다. (원본도 같다, 새 글자만 새 캔버스에 놓아 앞서 구운 글자가 어긋나던 것은 원본에서도 고쳤다)
- 데스크탑에서 글자를 덮는 얼굴이 하나도 없으면 그 글자는 건너뛰고, 하나도 굽지 못하면 그대로 둔다. (웹은 브라우저 Canvas2D 로 굽는다)
Bakes the atlas. (adds characters of charset not yet present, and rebakes the atlas with all characters)
- Lays out all held characters again. (the original does the same, the misalignment of earlier baked characters caused by placing only new characters on a new canvas was also fixed in the original)
- On desktop a character not covered by any face is skipped, and if nothing can be baked the atlas is left as is. (the web bakes with the browser Canvas2D)
- graphic&mut Graphic
- 글자 캐시를 쓸 그래픽.Graphic whose glyph cache to use.
- charset&str
- 구울 글자들. (글자 묶음 단위로 나누고, 이미 든 글자는 건너뛴다)Characters to bake. (split into grapheme clusters, characters already present are skipped)
짝Counterpart: vanilla.js DynamicFont.buildAtlas
든 글자를 모두 다시 굽기. (글꼴, 크기, 자간을 바꾼 뒤 부른다)
- 굽지 못하는 글자는 건너뛰고 나머지를 굽는다. (건너뛴 글자는 아틀라스에 없는 글자처럼 그린다)
- 하나도 굽지 못하면 아틀라스를 그대로 둔다.
Rebakes all held characters. (call after changing the font, size or letter spacing)
- Characters that cannot be baked are skipped and the rest are baked. (skipped characters are drawn like characters not in the atlas)
- If nothing can be baked, the atlas is left as is.
- graphic&mut Graphic
- 글자 캐시를 쓸 그래픽.Graphic whose glyph cache to use.
짝Counterpart: vanilla.js DynamicFont.rebuildAtlas
글 그리기. (position 은 첫 글자의 펜 자리, y 는 기준선. 색은 Graphic 의 그림 틴트를 따른다, 원본 drawText)
- 아틀라스가 없으면 아무것도 그리지 않는다.
Draws text. (position is the pen position of the first character, y is the baseline. The color follows Graphic's image tint, original drawText)
- Draws nothing if there is no atlas.
- text&str
- 그릴 글. (아틀라스에 없는 글자는 글자 크기의 반만큼 비운다)Text to draw. (characters not in the atlas leave a gap of half the font size)
- positionVector2
- 첫 글자의 펜 자리. (x 는 펜 자리, y 는 기준선, 지금 좌표계의 픽셀)Pen position of the first character. (x is the pen position, y is the baseline, pixels in the current coordinate system)
- graphic&mut Graphic
- 그릴 그래픽.Graphic to draw with.
짝Counterpart: vanilla.js DynamicFont.drawText
use vanilla::{Color, DynamicFont, Graphic, Vector2};
fn draw_damage(dynamic_font: &DynamicFont, graphic: &mut Graphic, damage: i32) {
graphic.set_image_tint_color(Some(Color::create(1.0, 0.85, 0.2, 1.0)));
dynamic_font.draw_text(&damage.to_string(), Vector2::create(300.0, 200.0), graphic);
graphic.set_image_tint_color(None);
}글 너비 재기. (아틀라스의 전진폭 합, 없는 글자는 글자 크기의 반)
Measures the text width. (sum of the advance widths in the atlas, half the font size for missing characters)
- text&str
- 잴 글.Text to measure.
반환Returnsf64 글 너비. (픽셀, 자간 포함)Text width. (pixels, including letter spacing)
짝Counterpart: vanilla.js DynamicFont.measureText
use vanilla::DynamicFont;
let mut dynamic_font = DynamicFont::new();
dynamic_font.set_font_size(40.0);
assert_eq!(dynamic_font.measure_text("123"), 60.0);
assert!(dynamic_font.get_atlas_image().is_none());아틀라스 비우기. (원본 clearAtlas)
Clears the atlas. (original clearAtlas)
짝Counterpart: vanilla.js DynamicFont.clearAtlas