@@ -4,7 +4,7 @@ import ol from 'openlayers';
44export var olExtends = function ( targetMap ) {
55
66 window . targetMapCache = targetMap ;
7- ol . format . MVT . prototype . readProjection = function ( source ) {
7+ ol . format . MVT . prototype . readProjection = function ( source ) { // eslint-disable-line no-unused-vars
88 return new ol . proj . Projection ( {
99 code : '' ,
1010 units : ol . proj . Units . TILE_PIXELS
@@ -15,7 +15,7 @@ export var olExtends = function (targetMap) {
1515 return this . getCoordinateAt ( 0.5 ) ;
1616 } ;
1717 }
18- ol . render . canvas . Replay . prototype . applyFill = function ( state , geometry ) {
18+ ol . render . canvas . Replay . prototype . applyFill = function ( state , geometry ) { // eslint-disable-line no-unused-vars
1919 var fillStyle = state . fillStyle ;
2020 var fillInstruction = [ ol . render . canvas . Instruction . SET_FILL_STYLE , fillStyle ] ;
2121 if ( typeof fillStyle !== 'string' ) {
@@ -86,63 +86,97 @@ export var olExtends = function (targetMap) {
8686 return feature ;
8787 } ;
8888
89- ol . geom . flat . textpath . lineString = function ( flatCoordinates , offset , end , stride , text , measure , startM , maxAngle ) {
90- const result = [ ] ;
89+ ol . geom . flat . textpath . lineString = function (
90+ flatCoordinates , offset , end , stride , text , measure , startM , maxAngle ) {
91+ var result = [ ] ;
9192
9293 // Keep text upright
93- //const reverse = flatCoordinates[offset] > flatCoordinates[end - stride];
94- const anglereverse = Math . atan2 ( flatCoordinates [ end - stride + 1 ] - flatCoordinates [ offset + 1 ] , flatCoordinates [ end - stride ] - flatCoordinates [ offset ] ) ;
95- const reverse = anglereverse < - 0.785 || anglereverse > 2.356 ? true : false ; //0.785//2.356
96- const isRotateUp = ( anglereverse < - 0.785 && anglereverse > - 2.356 ) || ( anglereverse > 0.785 && anglereverse < 2.356 ) ;
94+ var anglereverse = Math . atan2 ( flatCoordinates [ end - stride + 1 ] - flatCoordinates [ offset + 1 ] , flatCoordinates [ end - stride ] - flatCoordinates [ offset ] ) ;
95+ var reverse = anglereverse < - 0.785 || anglereverse > 2.356 ; //0.785//2.356
96+ var isRotateUp = ( anglereverse < - 0.785 && anglereverse > - 2.356 ) || ( anglereverse > 0.785 && anglereverse < 2.356 ) ;
9797
98- const numChars = text . length ;
98+ var numChars = text . length ;
9999
100- let x1 = flatCoordinates [ offset ] ;
101- let y1 = flatCoordinates [ offset + 1 ] ;
100+ var x1 = flatCoordinates [ offset ] ;
101+ var y1 = flatCoordinates [ offset + 1 ] ;
102102 offset += stride ;
103- let x2 = flatCoordinates [ offset ] ;
104- let y2 = flatCoordinates [ offset + 1 ] ;
105- let segmentM = 0 ;
106- let segmentLength = Math . sqrt ( Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) ) ;
103+ var x2 = flatCoordinates [ offset ] ;
104+ var y2 = flatCoordinates [ offset + 1 ] ;
105+ var segmentM = 0 ;
106+ var segmentLength = Math . sqrt ( Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) ) ;
107107
108- let chunk = '' ;
109- let chunkLength = 0 ;
110- let data , index , previousAngle , previousLang ;
111- for ( let i = 0 ; i < numChars ; ++ i ) {
108+ while ( offset < end - stride && segmentM + segmentLength < startM ) {
109+ x1 = x2 ;
110+ y1 = y2 ;
111+ offset += stride ;
112+ x2 = flatCoordinates [ offset ] ;
113+ y2 = flatCoordinates [ offset + 1 ] ;
114+ segmentM += segmentLength ;
115+ segmentLength = Math . sqrt ( Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) ) ;
116+ }
117+ var interpolate = ( startM - segmentM ) / segmentLength ;
118+ var x0 = ol . math . lerp ( x1 , x2 , interpolate ) ; //起始点
119+ var y0 = ol . math . lerp ( y1 , y2 , interpolate ) ; //起始点
120+
121+ var chunk = '' ;
122+ var chunkLength = 0 ;
123+ var data , index , previousAngle , previousLang ;
124+ for ( var i = 0 ; i < numChars ; ++ i ) {
112125 index = reverse ? numChars - i - 1 : i ;
113- const char = text . charAt ( index ) ;
114- const ischinese = char . charCodeAt ( 0 ) >= 19968 && char . charCodeAt ( 0 ) <= 40907 ? true : false ;
126+ var char = text . charAt ( index ) ;
127+ var charcode = char . charCodeAt ( 0 ) ;
128+ var ischinese = charcode >= 19968 && charcode <= 40907 ;
115129 chunk = reverse ? char + chunk : chunk + char ;
116- const charLength = measure ( chunk ) - chunkLength ;
130+ var charLength = measure ( chunk ) - chunkLength ;
117131 chunkLength += charLength ;
118- const charM = startM + charLength / 2 ;
119- while ( offset < end - stride && segmentM + segmentLength < charM ) {
132+ //var charM = startM + charLength / 2;
133+ while ( offset < end - stride && Math . sqrt ( Math . pow ( x2 - x0 , 2 ) + Math . pow ( y2 - y0 , 2 ) ) < ( charLength / 2 ) ) {
120134 x1 = x2 ;
121135 y1 = y2 ;
122136 offset += stride ;
123137 x2 = flatCoordinates [ offset ] ;
124138 y2 = flatCoordinates [ offset + 1 ] ;
125- segmentM += segmentLength ;
126- segmentLength = Math . sqrt ( Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) ) ;
127139 }
128- const segmentPos = charM - segmentM ;
129- let angle = Math . atan2 ( y2 - y1 , x2 - x1 ) ;
140+ var a = Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) ;
141+ var b = 2 * ( x2 - x1 ) * ( x1 - x0 ) + 2 * ( y2 - y1 ) * ( y1 - y0 ) ;
142+ var c = Math . pow ( x1 - x0 , 2 ) + Math . pow ( y1 - y0 , 2 ) - Math . pow ( charLength / 2 , 2 ) ;
143+ var scale1 = ( - b + Math . sqrt ( b * b - 4 * a * c ) ) / ( 2 * a ) ;
144+ var scale2 = ( - b - Math . sqrt ( b * b - 4 * a * c ) ) / ( 2 * a ) ;
145+ interpolate = scale1 < 0 || scale1 > 1 ? scale2 : scale2 < 0 || scale2 > 1 ? scale1 : scale1 < scale2 ? scale2 : scale1 ;
146+ var x = ol . math . lerp ( x1 , x2 , interpolate ) ;
147+ var y = ol . math . lerp ( y1 , y2 , interpolate ) ;
148+
149+ while ( offset < end - stride && Math . sqrt ( Math . pow ( x2 - x , 2 ) + Math . pow ( y2 - y , 2 ) ) < ( charLength / 2 ) ) {
150+ x1 = x2 ;
151+ y1 = y2 ;
152+ offset += stride ;
153+ x2 = flatCoordinates [ offset ] ;
154+ y2 = flatCoordinates [ offset + 1 ] ;
155+ }
156+ a = Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) ;
157+ b = 2 * ( x2 - x1 ) * ( x1 - x ) + 2 * ( y2 - y1 ) * ( y1 - y ) ;
158+ c = Math . pow ( x1 - x , 2 ) + Math . pow ( y1 - y , 2 ) - Math . pow ( charLength / 2 , 2 ) ;
159+ scale1 = ( - b + Math . sqrt ( b * b - 4 * a * c ) ) / ( 2 * a ) ;
160+ scale2 = ( - b - Math . sqrt ( b * b - 4 * a * c ) ) / ( 2 * a ) ;
161+ interpolate = scale1 < 0 || scale1 > 1 ? scale2 : scale2 < 0 || scale2 > 1 ? scale1 : scale1 < scale2 ? scale2 : scale1 ;
162+ var x3 = ol . math . lerp ( x1 , x2 , interpolate ) ;
163+ var y3 = ol . math . lerp ( y1 , y2 , interpolate ) ;
164+ var angle = Math . atan2 ( y3 - y0 , x3 - x0 ) ;
165+
130166 if ( reverse ) {
131167 angle += angle > 0 ? - Math . PI : Math . PI ;
132168 }
133169 if ( ischinese && isRotateUp ) {
134170 angle += angle > 0 ? - Math . PI / 2 : Math . PI / 2 ;
135171 }
136172 if ( previousAngle !== undefined ) {
137- let delta = angle - previousAngle ;
173+ var delta = angle - previousAngle ;
138174 delta += ( delta > Math . PI ) ? - 2 * Math . PI : ( delta < - Math . PI ) ? 2 * Math . PI : 0 ;
139175 if ( ischinese === previousLang ? Math . abs ( delta ) > maxAngle : Math . abs ( delta ) > ( maxAngle + Math . PI / 2 ) ) {
140176 return null ;
141177 }
142178 }
143- const interpolate = segmentPos / segmentLength ;
144- const x = ol . math . lerp ( x1 , x2 , interpolate ) ;
145- const y = ol . math . lerp ( y1 , y2 , interpolate ) ;
179+
146180 if ( previousAngle == angle && ! isRotateUp ) {
147181 if ( reverse ) {
148182 data [ 0 ] = x ;
@@ -162,10 +196,12 @@ export var olExtends = function (targetMap) {
162196 previousAngle = angle ;
163197 previousLang = ischinese ;
164198 }
199+ x0 = x3 ;
200+ y0 = y3 ;
165201 startM += charLength ;
166202 }
167203 return result ;
168- }
204+ } ;
169205 ol . layer . VectorTile . prototype . setFastRender = function ( fastRender ) {
170206 return this . fastRender = fastRender ;
171207 } ;
@@ -257,4 +293,4 @@ export var olExtends = function (targetMap) {
257293 ol . renderer . canvas . TileLayer . prototype . postCompose . apply ( this , arguments ) ;
258294 } ;
259295}
260- window . olExtends = olExtends ;
296+ window . olExtends = olExtends ;
0 commit comments