@@ -4,7 +4,7 @@ import ol from 'openlayers';
4
4
export var olExtends = function ( targetMap ) {
5
5
6
6
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
8
8
return new ol . proj . Projection ( {
9
9
code : '' ,
10
10
units : ol . proj . Units . TILE_PIXELS
@@ -15,7 +15,7 @@ export var olExtends = function (targetMap) {
15
15
return this . getCoordinateAt ( 0.5 ) ;
16
16
} ;
17
17
}
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
19
19
var fillStyle = state . fillStyle ;
20
20
var fillInstruction = [ ol . render . canvas . Instruction . SET_FILL_STYLE , fillStyle ] ;
21
21
if ( typeof fillStyle !== 'string' ) {
@@ -86,63 +86,97 @@ export var olExtends = function (targetMap) {
86
86
return feature ;
87
87
} ;
88
88
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 = [ ] ;
91
92
92
93
// 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 ) ;
97
97
98
- const numChars = text . length ;
98
+ var numChars = text . length ;
99
99
100
- let x1 = flatCoordinates [ offset ] ;
101
- let y1 = flatCoordinates [ offset + 1 ] ;
100
+ var x1 = flatCoordinates [ offset ] ;
101
+ var y1 = flatCoordinates [ offset + 1 ] ;
102
102
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 ) ) ;
107
107
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 ) {
112
125
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 ;
115
129
chunk = reverse ? char + chunk : chunk + char ;
116
- const charLength = measure ( chunk ) - chunkLength ;
130
+ var charLength = measure ( chunk ) - chunkLength ;
117
131
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 ) ) {
120
134
x1 = x2 ;
121
135
y1 = y2 ;
122
136
offset += stride ;
123
137
x2 = flatCoordinates [ offset ] ;
124
138
y2 = flatCoordinates [ offset + 1 ] ;
125
- segmentM += segmentLength ;
126
- segmentLength = Math . sqrt ( Math . pow ( x2 - x1 , 2 ) + Math . pow ( y2 - y1 , 2 ) ) ;
127
139
}
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
+
130
166
if ( reverse ) {
131
167
angle += angle > 0 ? - Math . PI : Math . PI ;
132
168
}
133
169
if ( ischinese && isRotateUp ) {
134
170
angle += angle > 0 ? - Math . PI / 2 : Math . PI / 2 ;
135
171
}
136
172
if ( previousAngle !== undefined ) {
137
- let delta = angle - previousAngle ;
173
+ var delta = angle - previousAngle ;
138
174
delta += ( delta > Math . PI ) ? - 2 * Math . PI : ( delta < - Math . PI ) ? 2 * Math . PI : 0 ;
139
175
if ( ischinese === previousLang ? Math . abs ( delta ) > maxAngle : Math . abs ( delta ) > ( maxAngle + Math . PI / 2 ) ) {
140
176
return null ;
141
177
}
142
178
}
143
- const interpolate = segmentPos / segmentLength ;
144
- const x = ol . math . lerp ( x1 , x2 , interpolate ) ;
145
- const y = ol . math . lerp ( y1 , y2 , interpolate ) ;
179
+
146
180
if ( previousAngle == angle && ! isRotateUp ) {
147
181
if ( reverse ) {
148
182
data [ 0 ] = x ;
@@ -162,10 +196,12 @@ export var olExtends = function (targetMap) {
162
196
previousAngle = angle ;
163
197
previousLang = ischinese ;
164
198
}
199
+ x0 = x3 ;
200
+ y0 = y3 ;
165
201
startM += charLength ;
166
202
}
167
203
return result ;
168
- }
204
+ } ;
169
205
ol . layer . VectorTile . prototype . setFastRender = function ( fastRender ) {
170
206
return this . fastRender = fastRender ;
171
207
} ;
@@ -257,4 +293,4 @@ export var olExtends = function (targetMap) {
257
293
ol . renderer . canvas . TileLayer . prototype . postCompose . apply ( this , arguments ) ;
258
294
} ;
259
295
}
260
- window . olExtends = olExtends ;
296
+ window . olExtends = olExtends ;
0 commit comments