misc/dynamicfont
다이나믹 폰트.
Dynamic font.
GlyphInfoextends Object
글리프 정보.
Glyph info.
import { DynamicFont, GlyphInfo } from "./build/vanilla.js";
const dynamicFont = new DynamicFont();
dynamicFont.setFontSize(32);
dynamicFont.buildAtlas("AB");
const glyphInfo = dynamicFont.getGlyph("A");
console.log(glyphInfo instanceof GlyphInfo, glyphInfo.char, glyphInfo.advance, glyphInfo.uvRect.size.x, glyphInfo.bearingY);생성.
Constructor.
아틀라스 내 UV 좌표 및 크기.
UV coordinates and size within the atlas.
반환ReturnsRect
짝Counterpart: vanilla-engine GlyphInfo.uv_rect
수평 이동량 (advance width + 자간, px).
Horizontal advance (advance width + letter spacing, px).
반환Returnsnumber
짝Counterpart: vanilla-engine GlyphInfo.advance
커서에서 글리프 좌측 픽셀까지 거리.
Distance from the cursor to the glyph's left pixel.
반환Returnsnumber
짝Counterpart: vanilla-engine GlyphInfo.bearing_x
베이스라인에서 글리프 상단 픽셀까지 거리.
Distance from the baseline to the glyph's top pixel.
반환Returnsnumber
짝Counterpart: vanilla-engine GlyphInfo.bearing_y
DynamicFontextends Object
다이나믹 폰트.
- 정한 글자들을 한 글꼴, 한 크기로 글자 아틀라스(캔버스 한 장)에 모아 굽고, 글을 그릴 때 아틀라스에서 글리프를 잘라 그린다. 글자마다 같은 그림을 쓰므로 묶어 그리기가 끊기지 않는다. (문자열마다 텍스처를 굽는 Text 와 다르다) 점수, 피해 숫자, 시계처럼 글자가 자주 바뀌고 같은 글자를 많이 그리는 곳에 쓴다.
- 글자는 글자 묶음(받침, 결합 부호, 이모지 묶음) 단위로 센다. 아틀라스에 없는 글자는 글자 크기의 반만큼 비우고 넘어간다.
- buildAtlas 로 글자를 더하면 든 글자를 모두 새 캔버스에 다시 놓는다. (앞서 구운 글자의 자리가 새 캔버스와 어긋나지 않게)
Dynamic font.
- Bakes the chosen characters in one font and one size into a glyph atlas (a single canvas), and when drawing text it cuts the glyphs out of the atlas. Every character uses the same image, so sprite batching is not broken. (unlike Text, which bakes a texture per string) Used where characters change often and the same characters are drawn many times, such as scores, damage numbers and clocks.
- Characters are counted by grapheme cluster (final consonants, combining marks, emoji sequences). Characters missing from the atlas are skipped, leaving a gap of half the font size.
- Adding characters with buildAtlas places all contained characters again on a new canvas. (so the positions of previously baked characters do not mismatch the new canvas)
import { Engine, EngineConfiguration, Scene, DynamicFont, FontAsset, Vector2 } from "./build/vanilla.js";
const fontAsset = new FontAsset();
await fontAsset.loadFont("DOSGothic", "examples/assets/DOSGothic.woff");
const dynamicFont = new DynamicFont();
dynamicFont.setFontAsset(fontAsset);
dynamicFont.setFontSize(48);
dynamicFont.setLetterSpacing(2);
dynamicFont.buildAtlas("0123456789:");
console.log(dynamicFont.measureText("12:34"));
class ClockScene extends Scene {
draw(graphic) {
super.draw(graphic);
const position = Vector2.create(100, 200);
dynamicFont.drawText("12:34", position, graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
const engine = new Engine(engineConfiguration);
engine.run(new ClockScene());폰트 애셋 설정.
Sets the font asset.
- fontAssetFontAsset
- 구울 때 쓸 폰트 애셋. (null 이면 시스템 글꼴, 바꾼 뒤에는 rebuildAtlas 로 다시 굽는다)Font asset used for baking. (system font if null, call rebuildAtlas after changing it)
폰트 크기 설정.
Sets the font size.
- fontSizenumber
- 글자 크기. (픽셀, 처음은 32)Font size. (pixels, 32 at first)
짝Counterpart: vanilla-engine DynamicFont.set_font_size
폰트 크기 반환.
Returns the font size.
반환Returnsnumber 글자 크기. (픽셀)Font size. (pixels)
짝Counterpart: vanilla-engine DynamicFont.get_font_size
자간 설정.
Sets the letter spacing.
- letterSpacingnumber
- 글자마다 더할 자간. (픽셀, 처음은 0)Letter spacing added to each character. (pixels, 0 at first)
짝Counterpart: vanilla-engine DynamicFont.set_letter_spacing
자간 반환.
Returns the letter spacing.
반환Returnsnumber 더하는 자간. (픽셀)Added letter spacing. (pixels)
짝Counterpart: vanilla-engine DynamicFont.get_letter_spacing
아틀라스 캔버스 반환.
Returns the atlas canvas.
반환ReturnsHTMLCanvasElement 글자 아틀라스 캔버스. 아직 굽지 않았으면 null.The glyph atlas canvas. null if not baked yet.
글리프 정보 반환.
Returns the glyph info.
- charstring
- 글자 하나. (글자 묶음 단위)One character. (a grapheme cluster)
반환ReturnsGlyphInfo | null 그 글자의 글리프 정보. 아틀라스에 없으면 null.Glyph info for that character. null if not in the atlas.
짝Counterpart: vanilla-engine DynamicFont.get_glyph
아틀라스 빌드. (charset 의 글자 가운데 아직 없는 글자를 더하고, 아틀라스를 든 글자 모두로 다시 굽는다)
Builds the atlas. (adds the characters in charset that are not yet present, and rebakes the atlas with all contained characters)
- charsetstring
- 아틀라스에 넣을 글자들. (이미 든 글자는 건너뛴다)Characters to put in the atlas. (characters already in it are skipped)
짝Counterpart: vanilla-engine DynamicFont.build_atlas
든 글자를 모두 다시 굽기. (글꼴, 크기, 자간을 바꾼 뒤 부른다)
Rebakes all contained characters. (call after changing the font, size or letter spacing)
짝Counterpart: vanilla-engine DynamicFont.rebuild_atlas
텍스트 렌더링 (아틀라스에서 글리프 샘플링).
Text rendering (samples glyphs from the atlas).
- textstring
- 그릴 글.Text to draw.
- positionVector2
- 베이스라인 기준 좌상단 위치.Top-left position relative to the baseline.
- graphicGraphic
- 그릴 그래픽.Graphic to draw with.
짝Counterpart: vanilla-engine DynamicFont.draw_text
글 너비 재기. (아틀라스의 전진폭 합, 없는 글자는 글자 크기의 반)
Measures the text width. (sum of the advance widths in the atlas, half the font size for missing characters)
- textstring
- 잴 글.Text to measure.
반환Returnsnumber 글 너비. (픽셀)Text width. (pixels)
짝Counterpart: vanilla-engine DynamicFont.measure_text
글리프 맵 초기화.
Initializes the glyph map.
짝Counterpart: vanilla-engine DynamicFont.clear_atlas