@@ -109,12 +109,13 @@ class Font {
109109 *
110110 * @param {string } text - The text.
111111 * @param {number } [size=100] - The text size.
112+ * @param {string } direction - Char direction: ltr(left to right), rtl(right to left) & tb(top bottom).
112113 * @return {Array<Shape> } An array of shapes representing the text.
113114 */
114- generateShapes ( text , size = 100 ) {
115+ generateShapes ( text , size = 100 , direction = 'ltr' ) {
115116
116117 const shapes = [ ] ;
117- const paths = createPaths ( text , size , this . data ) ;
118+ const paths = createPaths ( text , size , this . data , direction ) ;
118119
119120 for ( let p = 0 , pl = paths . length ; p < pl ; p ++ ) {
120121
@@ -128,7 +129,7 @@ class Font {
128129
129130}
130131
131- function createPaths ( text , size , data ) {
132+ function createPaths ( text , size , data , direction ) {
132133
133134 const chars = Array . from ( text ) ;
134135 const scale = size / data . resolution ;
@@ -138,6 +139,12 @@ function createPaths( text, size, data ) {
138139
139140 let offsetX = 0 , offsetY = 0 ;
140141
142+ if ( direction == 'rtl' || direction == 'tb' ) {
143+
144+ chars . reverse ( ) ;
145+
146+ }
147+
141148 for ( let i = 0 ; i < chars . length ; i ++ ) {
142149
143150 const char = chars [ i ] ;
@@ -150,7 +157,18 @@ function createPaths( text, size, data ) {
150157 } else {
151158
152159 const ret = createPath ( char , scale , offsetX , offsetY , data ) ;
153- offsetX += ret . offsetX ;
160+
161+ if ( direction == 'tb' ) {
162+
163+ offsetX = 0 ;
164+ offsetY += data . ascender * scale ;
165+
166+ } else {
167+
168+ offsetX += ret . offsetX ;
169+
170+ }
171+
154172 paths . push ( ret . path ) ;
155173
156174 }
0 commit comments