Skip to content

Commit 086edaf

Browse files
committed
Add direction featrue for fontLoader
1 parent 071bb8f commit 086edaf

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

examples/jsm/geometries/TextGeometry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TextGeometry extends ExtrudeGeometry {
4343

4444
} else {
4545

46-
const shapes = font.generateShapes( text, parameters.size );
46+
const shapes = font.generateShapes( text, parameters.size, parameters.direction );
4747

4848
// defaults
4949

examples/jsm/loaders/FontLoader.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)