r92219 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92218‎ | r92219 | r92220 >
Date:00:04, 15 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Re-introduced final measurements and reduced array lookups.
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.TextFlow.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js
@@ -224,14 +224,14 @@
225225 rs.charFit.end = rs.lineEnd = this.boundaries[rs.wordOffset];
226226 }
227227 }
228 - this.appendLine( rs.charOffset, rs.charFit.end );
 228+ this.appendLine( rs.charOffset, rs.charFit.end, rs.wordOffset, true );
229229 // Move on to another line
230230 rs.charOffset = rs.charFit.end;
231231 } while ( rs.charOffset < rs.lineEnd );
232232 } else {
233233 rs.wordOffset = rs.wordFit.end;
234234 rs.lineEnd = this.boundaries[rs.wordOffset];
235 - this.appendLine( rs.lineStart, rs.lineEnd );
 235+ this.appendLine( rs.lineStart, rs.lineEnd, rs.wordOffset, false );
236236 }
237237 rs.lineStart = rs.lineEnd;
238238 }
@@ -317,7 +317,7 @@
318318 * @param start {Integer} Beginning of text range for line
319319 * @param end {Integer} Ending of text range for line
320320 */
321 -TextFlow.prototype.appendLine = function( start, end ) {
 321+TextFlow.prototype.appendLine = function( start, end, word, fractional ) {
322322 $line = this.$.find( '.editSurface-line[line-index=' + this.lines.length + ']' );
323323 if ( !$line.length ) {
324324 $line = $( '<div class="editSurface-line" line-index="' + this.lines.length + '"></div>' )
@@ -330,8 +330,11 @@
331331 'start': start,
332332 'end': end,
333333 'width': $line.outerWidth(),
334 - 'height': $line.outerHeight()
 334+ 'height': $line.outerHeight(),
 335+ 'word': word,
 336+ 'fractional': fractional
335337 });
 338+ //console.log( start, end, this.lines[this.lines.length - 1].text, this.lines[this.lines.length - 1].fractional );
336339 };
337340
338341 /**
@@ -358,20 +361,22 @@
359362 */
360363 TextFlow.prototype.fitWords = function( start, end, ruler, width ) {
361364 var offset = start,
 365+ charOffset = this.boundaries[offset],
362366 middle,
363367 lineWidth,
364368 cacheKey;
365369 do {
366370 // Place "middle" directly in the center of "start" and "end"
367371 middle = Math.ceil( ( start + end ) / 2 );
 372+ charMiddle = this.boundaries[middle];
368373
369 - cacheKey = this.boundaries[offset] + ':' + this.boundaries[middle];
370 -
 374+ // Measure and cache width of substring
 375+ cacheKey = charOffset + ':' + charMiddle;
371376 // Prepare the line for measurement using pre-escaped HTML
372 - ruler.innerHTML = this.content.render( this.boundaries[offset], this.boundaries[middle] );
 377+ ruler.innerHTML = this.content.render( charOffset, charMiddle );
373378 // Test for over/under using width of the rendered line
374379 this.widthCache[cacheKey] = lineWidth = ruler.clientWidth;
375 -
 380+
376381 // Test for over/under using width of the rendered line
377382 if ( lineWidth > width ) {
378383 // Detect impossible fit (the first word won't fit by itself)
@@ -386,6 +391,13 @@
387392 start = middle;
388393 }
389394 } while ( start < end );
 395+ // Check if we ended by moving end to the left of middle
 396+ if ( end === middle - 1 ) {
 397+ // A final measurement is required
 398+ var charStart = this.boundaries[start];
 399+ ruler.innerHTML = this.content.render( charOffset, charStart );
 400+ lineWidth = this.widthCache[charOffset + ':' + charStart] = ruler.clientWidth;
 401+ }
390402 return { 'end': start, 'width': lineWidth };
391403 };
392404
@@ -411,8 +423,8 @@
412424 // Place "middle" directly in the center of "start" and "end"
413425 middle = Math.ceil( ( start + end ) / 2 );
414426
 427+ // Measure and cache width of substring
415428 cacheKey = offset + ':' + middle;
416 -
417429 if ( cacheKey in this.widthCache ) {
418430 lineWidth = this.widthCache[cacheKey];
419431 } else {
@@ -421,7 +433,7 @@
422434 // Test for over/under using width of the rendered line
423435 this.widthCache[cacheKey] = lineWidth = ruler.clientWidth;
424436 }
425 -
 437+
426438 if ( lineWidth > width ) {
427439 // Detect impossible fit (the first character won't fit by itself)
428440 if (middle - offset === 1) {
@@ -435,6 +447,18 @@
436448 start = middle;
437449 }
438450 } while ( start < end );
 451+ // Check if we ended by moving end to the left of middle
 452+ if ( end === middle - 1 ) {
 453+ // Try for cache hit
 454+ cacheKey = offset + ':' + start;
 455+ if ( cacheKey in this.widthCache ) {
 456+ lineWidth = this.widthCache[cacheKey];
 457+ } else {
 458+ // A final measurement is required
 459+ ruler.innerHTML = this.content.render( offset, start );
 460+ lineWidth = this.widthCache[cacheKey] = ruler.clientWidth;
 461+ }
 462+ }
439463 return { 'end': start, 'width': lineWidth };
440464 };
441465

Status & tagging log