@@ -506,7 +506,11 @@ Font.prototype.removeUnifontPlaceholders = function() {
506
506
} ) ;
507
507
} ;
508
508
509
- // Outputs as JavaScript for a custom font
509
+ /* Outputs as JavaScript for a custom font.
510
+ options = {
511
+ compressed : bool
512
+ }
513
+ */
510
514
Font . prototype . getJS = function ( options ) {
511
515
// options.compressed
512
516
options = options || { } ;
@@ -613,7 +617,7 @@ Font.prototype.getHeaderFile = function() {
613
617
return header ;
614
618
}
615
619
616
- // Output as a PBF file
620
+ // Output as a PBF file, returns as a Uint8Array
617
621
Font . prototype . getPBF = function ( ) {
618
622
// https://github.com/pebble-dev/wiki/wiki/Firmware-Font-Format
619
623
// setup to ensure we're not writing entire glyphs
@@ -809,19 +813,70 @@ JsVar *jswrap_graphics_setFont${options.name}(JsVar *parent) {
809
813
` ) ;
810
814
} ;
811
815
816
+ // Renders the given text to a on object { width, height, bpp:32, data : Uint32Array }
817
+ Font . prototype . renderString = function ( text ) {
818
+ // work out width
819
+ var width = 0 ;
820
+ for ( var i = 0 ; i < text . length ; i ++ ) {
821
+ var ch = text . charCodeAt ( i ) ;
822
+ if ( ! this . glyphs [ ch ] ) continue ;
823
+ width += this . glyphs [ ch ] . advance ;
824
+ }
825
+ // allocate array
826
+ var bmp = new Uint32Array ( width * this . height ) ;
827
+ // render
828
+ const bpp = this . bpp ;
829
+ var ox = 0 ;
830
+ for ( var i = 0 ; i < text . length ; i ++ ) {
831
+ var ch = text . charCodeAt ( i ) ;
832
+ if ( ! this . glyphs [ ch ] ) continue ;
833
+ var g = this . glyphs [ ch ] ;
834
+ for ( var y = g . yStart ; y <= g . yEnd ; y ++ ) {
835
+ for ( var x = g . xStart ; x <= g . yEnd ; x ++ ) {
836
+ var c = g . getPixel ( x , y ) << 8 - bpp ;
837
+ c |= c >> bpp ;
838
+ c |= c >> ( bpp * 2 ) ;
839
+ c |= c >> ( bpp * 4 ) ;
840
+ c = 255 - c ;
841
+ var px = x + ox ;
842
+ if ( ( px >= 0 ) && ( px < width ) && ( y >= 0 ) && ( y < this . height ) )
843
+ bmp [ px + ( y * width ) ] = 0xFF000000 | ( c << 16 ) | ( c << 8 ) | c ;
844
+ }
845
+ }
846
+ ox += this . glyphs [ ch ] . advance ;
847
+ }
848
+ // return
849
+ return { width : width , height : this . height , bpp : 32 , data : bmp } ;
850
+ } ;
851
+
852
+ /* Outputs an object containing suggested sets of characters, with the following fields:
853
+ {
854
+ id : {
855
+ id : string,
856
+ range : [ {min:int,max:int}, ... ],
857
+ text : string // test string
858
+ charCount : // number of characters in range
859
+ }
860
+ }
861
+ */
812
862
function getRanges ( ) {
813
863
// https://arxiv.org/pdf/1801.07779.pdf#page=5 is handy to see what covers most writing
814
- return { // https://www.unicode.org/charts/
815
- "ASCII" : { range : [ { min : 32 , max : 127 } ] } ,
816
- "ASCII Capitals" : { range : [ { min : 32 , max : 93 } ] } ,
817
- "Numeric" : { range : [ { min : 46 , max : 58 } ] } ,
818
- "ISO8859-1" : { range : [ { min : 32 , max : 255 } ] } ,
819
- "Extended" : { range : [ { min : 32 , max : 1103 } ] } , // 150 languages + Cyrillic
820
- "All" : { range : [ { min : 32 , max : 0xFFFF } ] } ,
821
- "Chinese" : { range : [ { min : 32 , max : 255 } , { min : 0x4E00 , max : 0x9FAF } ] } ,
822
- "Korean" : { range : [ { min : 32 , max : 255 } , { min : 0x1100 , max : 0x11FF } , { min : 0x3130 , max : 0x318F } , { min : 0xA960 , max : 0xA97F } , { min : 0xAC00 , max : 0xD7FF } ] } ,
823
- "Japanese" : { range : [ { min : 32 , max : 255 } , { min : 0x3000 , max : 0x30FF } , { min : 0x4E00 , max : 0x9FAF } , { min : 0xFF00 , max : 0xFFEF } ] } ,
864
+ var ranges = { // https://www.unicode.org/charts/
865
+ "ASCII" : { range : [ { min : 32 , max : 127 } ] , text : "This is a test" } ,
866
+ "ASCII Capitals" : { range : [ { min : 32 , max : 93 } ] , text : "THIS IS A TEST" } ,
867
+ "Numeric" : { range : [ { min : 46 , max : 58 } ] , text : "0.123456789:/" } ,
868
+ "ISO8859-1" : { range : [ { min : 32 , max : 255 } ] , text : "Thís îs ã tést" } ,
869
+ "Extended" : { range : [ { min : 32 , max : 1103 } ] , text : "Thís îs ã tést" } , // 150 languages + Cyrillic
870
+ "All" : { range : [ { min : 32 , max : 0xFFFF } ] , text : "이것 îs ã 测试" } ,
871
+ "Chinese" : { range : [ { min : 32 , max : 255 } , { min : 0x4E00 , max : 0x9FAF } ] , text : "这是一个测试" } ,
872
+ "Korean" : { range : [ { min : 32 , max : 255 } , { min : 0x1100 , max : 0x11FF } , { min : 0x3130 , max : 0x318F } , { min : 0xA960 , max : 0xA97F } , { min : 0xAC00 , max : 0xD7FF } ] , text : "이것은 테스트입니다" } ,
873
+ "Japanese" : { range : [ { min : 32 , max : 255 } , { min : 0x3000 , max : 0x30FF } , { min : 0x4E00 , max : 0x9FAF } , { min : 0xFF00 , max : 0xFFEF } ] , text : "これはテストです" } ,
824
874
} ;
875
+ for ( var id in ranges ) {
876
+ ranges [ id ] . id = id ;
877
+ ranges [ id ] . charCount = ranges [ id ] . range . reduce ( ( a , r ) => a + r . max + 1 - r . min , 0 ) ;
878
+ }
879
+ return ranges ;
825
880
}
826
881
827
882
0 commit comments