MoonBit port of cosmic-text (text shaping, layout, and editor primitives) built on moon_swash.
Dependencies
moon add Milky2018/moon_cosmicimport Milky2018/moon_cosmic
fn main {
// Provide font bytes from your host app (file I/O is app-specific).
let font_bytes : Bytes = /* ... */
let font_system = FontSystem::new().load_font_data(font_bytes)
let metrics = Metrics::new(14.0F, 20.0F)
let attrs = Attrs::new().family(Family::Name("Inter"))
let attrs_list = AttrsList::new(attrs)
let buffer = Buffer::new(metrics)
.set_wrap(Wrap::WordOrGlyph)
.set_text("Hello world", attrs_list, Shaping::Advanced)
.set_size(Some(200.0F), None)
.layout_all_with_font_system(font_system)
for run in buffer.layout_runs() {
// `run.glyphs`: Array[LayoutGlyph]
// `run.line_w`: line width in pixels
ignore(run)
}
}import Milky2018/moon_cosmic
fn draw_demo(font_bytes : Bytes) -> Unit {
let font_system = FontSystem::new().load_font_data(font_bytes)
let cache = SwashCache::new()
let buffer = Buffer::new(Metrics::new(14.0F, 20.0F))
.set_text("Hi!", AttrsList::new(Attrs::new()), Shaping::Advanced)
.set_size(Some(200.0F), None)
let (_buffer, _cache) = buffer.draw(
font_system,
cache,
Color::rgb(0, 0, 0),
fn(_x, _y, _w, _h, _color) { () },
)
}moon check # Lint/type-check
moon test # Run tests
moon fmt # Format
moon info # Update .mbti
moon info && moon fmtpython3 scripts/gen_test_font_mbt.py
moon fmt
moon testpub(open) trait Renderer {
rectangle(Self, x : Int, y : Int, w : UInt, h : UInt, color : Color) -> Unit
glyph(Self, physical_glyph : PhysicalGlyph, color : Color) -> Unit
}pub(all) enum Action {
Motion(Motion)
Escape
Insert(Char)
Enter
Backspace
Delete
Indent
Unindent
Click(Int, Int)
DoubleClick(Int, Int)
TripleClick(Int, Int)
Drag(Int, Int)
Scroll(Float)
}pub(all) enum Affinity {
Before
After
}pub struct Attrs {
metadata : Int
family : Family
weight : Weight
stretch : Stretch
style : Style
metrics_opt : CacheMetrics?
}pub struct BidiParagraphs {
text : String
pos : Int
ascii_fast : Bool
}fn Buffer::draw(self : Buffer, font_system : FontSystem, swash_cache : SwashCache, text_color : Color, f : (Int, Int, UInt, UInt, Color) -> Unit) -> (Buffer, SwashCache)fn Buffer::draw_with_font_system(self : Buffer, font_system : FontSystem, swash_cache : SwashCache, text_color : Color, f : (Int, Int, UInt, UInt, Color) -> Unit) -> (Buffer, SwashCache)pub struct BufferLine {
text : String
ending : LineEnding
attrs_list : AttrsList
align : Align?
shape_opt : Cached[ShapeLine]
layout_opt : Cached[Array[LayoutLine]]
shaping : Shaping
metadata : Int?
}fn BufferLine::layout(self : BufferLine, font_size : Float, width_opt : Float?, wrap : Wrap, match_mono_width : Float?, tab_width : Int, hinting : Hinting) -> BufferLinefn BufferLine::layout_with_font_system(self : BufferLine, font_system : FontSystem, font_size : Float, width_opt : Float?, wrap : Wrap, match_mono_width : Float?, tab_width : Int, hinting : Hinting) -> BufferLinefn BufferLine::new(text : String, ending : LineEnding, attrs_list : AttrsList, shaping : Shaping) -> BufferLinefn BufferLine::set_text(self : BufferLine, text : String, ending : LineEnding, attrs_list : AttrsList) -> (BufferLine, Bool)fn BufferLine::shape_with_font_system(self : BufferLine, font_system : FontSystem, tab_width : Int) -> BufferLinepub struct CacheKey {
font_id : Int
glyph_id : Int
font_size_bits : UInt
x_bin : SubpixelBin
y_bin : SubpixelBin
font_weight : Int
flags : UInt
}pub struct CacheMetrics {
font_size_bits : UInt
line_height_bits : UInt
}impl Eq for CacheMetricsimpl Hash for CacheMetricsimpl Show for CacheMetricspub(all) enum Cached[T] {
Empty
Unused(T)
Used(T)
}pub struct Color {
value : UInt
}pub(all) enum Family {
Name(String)
Serif
SansSerif
Cursive
Fantasy
Monospace
}pub struct FontEntry {
id : Int
family_name : String
postscript_name : String
font : FontRef
charmap_proxy : CharmapProxy
attributes : Attributes
is_monospace : Bool
not_emoji : Bool
}pub struct FontSystem {
locale : String
fonts : Array[FontEntry]
font_matches_cache : HashMap[FontMatchAttrs, Array[Int]]
codepoint_support_cache : HashMap[Int, FontCachedCodepointSupportInfo]
shape_run_cache : ShapeRunCache
// private fields
}fn FontSystem::count_supported_codepoints(self : FontSystem, font_id : Int, codepoints : Array[UInt]) -> Intfn FontSystem::font_matches_for_script(self : FontSystem, attrs : Attrs, script : Script) -> Array[Int]fn FontSystem::font_matches_for_scripts(self : FontSystem, attrs : Attrs, scripts : Array[Script]) -> Array[Int]pub struct LayoutCursor {
line : Int
layout : Int
glyph : Int
}impl Eq for LayoutCursorpub struct LayoutGlyph {
start : Int
end : Int
font_size : Float
font_id : Int
font_weight : Int
glyph_id : Int
x : Float
y : Float
w : Float
level : Int
line_height_opt : Float?
x_offset : Float
y_offset : Float
color_opt : Color?
metadata : Int
cache_key_flags : UInt
}fn LayoutGlyph::new(start : Int, end : Int, font_size : Float, font_id : Int, font_weight : Int, glyph_id : Int, x : Float, y : Float, w : Float, metadata : Int) -> LayoutGlyphfn LayoutGlyph::physical(self : LayoutGlyph, offset : (Float, Float), scale : Float) -> PhysicalGlyphpub struct LayoutLine {
start : Int
end : Int
w : Float
max_ascent : Float
max_descent : Float
line_height_opt : Float?
glyphs : Array[LayoutGlyph]
}pub struct LayoutRun {
line_i : Int
line_y : Float
line_top : Float
line_height : Float
text : String
rtl : Bool
glyphs : Array[LayoutGlyph]
line_w : Float
}pub struct LegacyRenderer {
font_system : FontSystem
cache : SwashCache
callback : (Int, Int, UInt, UInt, Color) -> Unit
}impl Renderer for LegacyRendererfn LegacyRenderer::new(font_system : FontSystem, cache : SwashCache, callback : (Int, Int, UInt, UInt, Color) -> Unit) -> LegacyRendererpub(all) enum LineBreak {
NoWrap
AnyCharacter
WordBoundary
}pub(all) enum LineEnding {
Lf
CrLf
Cr
LfCr
None
}pub struct LineIter {
string : String
start : Int
end : Int
}pub struct Metrics {
font_size : Float
line_height : Float
}pub(all) enum Motion {
LayoutCursor(LayoutCursor)
Previous
Next
Left
Right
Up
Down
Home
SoftHome
End
ParagraphStart
ParagraphEnd
PageUp
PageDown
Vertical(Int)
PreviousWord
NextWord
LeftWord
RightWord
BufferStart
BufferEnd
GotoLine(Int)
}pub struct Scroll {
line : Int
vertical : Float
horizontal : Float
}pub struct ShapeGlyph {
start : Int
end : Int
x_advance : Float
y_advance : Float
x_offset : Float
y_offset : Float
ascent : Float
descent : Float
font_id : Int
font_weight : Int
glyph_id : Int
metadata : Int
level : Int
metrics_opt : Metrics?
}fn ShapeGlyph::new(start : Int, end : Int, x_advance : Float, y_advance : Float, x_offset : Float, y_offset : Float, font_id : Int, glyph_id : Int, metadata : Int) -> ShapeGlyphfn ShapeGlyph::with_ascent_descent(self : ShapeGlyph, ascent : Float, descent : Float) -> ShapeGlyphimpl Show for ShapeRunCachefn ShapeRunCache::insert(self : ShapeRunCache, key : ShapeRunKey, glyphs : Array[ShapeGlyph]) -> Unitpub(all) enum Shaping {
Basic
Advanced
}pub enum SubpixelBin {
Zero
One
Two
Three
}impl Eq for SubpixelBinimpl Hash for SubpixelBinimpl Show for SubpixelBinfn SwashCache::get_image(self : SwashCache, font_system : FontSystem, cache_key : CacheKey) -> Image?fn SwashCache::get_image_uncached(self : SwashCache, font_system : FontSystem, cache_key : CacheKey) -> Image?fn SwashCache::get_outline_commands(self : SwashCache, font_system : FontSystem, cache_key : CacheKey) -> (Array[Vector], Array[Verb])?fn SwashCache::get_outline_commands_uncached(self : SwashCache, font_system : FontSystem, cache_key : CacheKey) -> (Array[Vector], Array[Verb])?fn SwashCache::with_pixels(self : SwashCache, font_system : FontSystem, cache_key : CacheKey, base : Color, f : (Int, Int, Color) -> Unit) -> Unitpub struct Weight {
value : Int
}fn apply_align(text : String, lines : Array[LayoutLine], width : Float, align : Align) -> Array[LayoutLine]fn apply_align_with_rtl(text : String, lines : Array[LayoutLine], width : Float, align : Align, rtl : Bool) -> Array[LayoutLine]fn cosmic_text_version() -> Stringfn layout_ascii(text : String, glyph_w : Float, width_opt : Float?, wrap : Wrap) -> Array[LayoutLine]fn layout_from_shape(text : String, shape : ShapeLine, font_size : Float, cell_w : Float, width_opt : Float?, wrap : Wrap) -> Array[LayoutLine]MoonBit port of cosmic-text (text shaping, layout, and editor primitives) built on moon_swash.
Dependencies