@@ -400,7 +400,54 @@ pub struct HMtxTable {
400400}
401401
402402#[ derive( Debug ) ]
403- pub struct GlyphTable ( pub Vec < ( GlyphDescription , Glyph ) > ) ;
403+ pub struct GlyphTable {
404+ pub glyphs : Vec < Glyph > ,
405+ }
406+
407+ #[ derive( Debug ) ]
408+ pub struct Glyph {
409+ pub description : GlyphDescription ,
410+ pub data : GlyphData ,
411+ }
412+
413+ impl Glyph {
414+ pub fn is_simple ( & self ) -> bool {
415+ matches ! ( self . data, GlyphData :: Simple ( _) )
416+ }
417+
418+ pub fn draw_to_canvas ( & self , key : usize ) -> String {
419+ let GlyphData :: Simple ( simple_glyph) = & self . data else {
420+ todo ! ( "how does compound glyphs look on canvas?" ) ;
421+ } ;
422+
423+ let mut out = String :: new ( ) ;
424+ out += & format ! ( "ctx{key}.translate(0, newCanvas{key}.height);\n " ) ;
425+ out += & format ! ( "ctx{key}.scale(1, -1);\n " ) ;
426+
427+ out += & format ! ( "ctx{key}.beginPath()\n " ) ;
428+
429+ let mut start_index = 0 ;
430+
431+ for end_index in & simple_glyph. end_points_of_contours {
432+ let end_index = * end_index as usize ;
433+
434+ let ( x_start, y_start) = simple_glyph. coordinates [ start_index] ;
435+ out += & format ! ( "ctx{key}.moveTo({}, {});\n " , x_start, y_start) ;
436+ for i in start_index + 1 ..=end_index {
437+ let ( x, y) = simple_glyph. coordinates [ i] ;
438+ out += & format ! ( "ctx{key}.lineTo({}, {});\n " , x, y) ;
439+ }
440+
441+ out += & format ! ( "ctx{key}.closePath();\n " ) ;
442+
443+ start_index = end_index + 1 ;
444+ }
445+
446+ out += & format ! ( "ctx{key}.stroke();\n " ) ;
447+
448+ out
449+ }
450+ }
404451
405452#[ derive( Debug ) ]
406453pub struct GlyphDescription {
@@ -419,27 +466,18 @@ impl GlyphDescription {
419466 pub fn height ( & self ) -> usize {
420467 ( self . y_max - self . y_min ) as usize
421468 }
422- }
423469
424- impl GlyphDescription {
425470 pub ( crate ) const fn is_simple ( & self ) -> bool {
426471 self . number_of_contours >= 0
427472 }
428473}
429474
430475#[ derive( Debug ) ]
431- pub enum Glyph {
476+ pub enum GlyphData {
432477 Simple ( SimpleGlyph ) ,
433478 Compound ( CompoundGlyph ) ,
434479}
435480
436- pub trait DrawCanvas {
437- const CANVAS_WIDTH : usize = 500 ;
438- const CANVAS_HEIGHT : usize = 500 ;
439-
440- fn to_canvas ( & self ) -> String ;
441- }
442-
443481#[ derive( Debug ) ]
444482pub struct SimpleGlyph {
445483 pub end_points_of_contours : Vec < u16 > ,
@@ -449,37 +487,6 @@ pub struct SimpleGlyph {
449487 pub coordinates : Vec < ( i16 , i16 ) > ,
450488}
451489
452- impl DrawCanvas for SimpleGlyph {
453- fn to_canvas ( & self ) -> String {
454- let mut out = String :: new ( ) ;
455-
456- out += "ctx.translate(0, canvas.height);\n " ;
457- out += "ctx.scale(1, -1);\n " ;
458-
459- out += "ctx.beginPath()\n " ;
460-
461- for & ( x, y) in & self . coordinates {
462- out += & format ! ( "ctx.moveTo({}, {});\n " , x, y) ;
463- out += & format ! ( "ctx.arc({}, {}, 3, 0, 2 * Math.PI);\n \n " , x, y) ;
464- }
465-
466- out += "ctx.fill();\n " ;
467-
468- out
469- }
470- }
471-
472- #[ derive( Debug ) ]
473- pub struct CompoundGlyph {
474- pub components : Vec < ComponentGlyph > ,
475- }
476-
477- impl DrawCanvas for ComponentGlyph {
478- fn to_canvas ( & self ) -> String {
479- todo ! ( )
480- }
481- }
482-
483490#[ derive( Debug , Clone , Copy ) ]
484491pub struct SimpleGlyphFlag ( pub u8 ) ;
485492
@@ -510,9 +517,8 @@ impl SimpleGlyphFlag {
510517}
511518
512519#[ derive( Debug ) ]
513- pub enum ComponentGlyphArgument {
514- Point ( u16 ) ,
515- Coord ( i16 ) ,
520+ pub struct CompoundGlyph {
521+ pub components : Vec < ComponentGlyph > ,
516522}
517523
518524#[ derive( Debug ) ]
@@ -524,6 +530,12 @@ pub struct ComponentGlyph {
524530 pub transformation : ComponentGlyphTransformation ,
525531}
526532
533+ #[ derive( Debug ) ]
534+ pub enum ComponentGlyphArgument {
535+ Point ( u16 ) ,
536+ Coord ( i16 ) ,
537+ }
538+
527539#[ derive( Debug , Clone , Copy ) ]
528540pub struct ComponentGlyphFlag ( pub u16 ) ;
529541
0 commit comments