From 4f2de650ae691559c84ad33574b554774b1ebdd9 Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Sat, 26 Feb 2011 09:02:50 +0800 Subject: [PATCH] Improve behavior of font size detection when editors have changing visibility - Allow on-demand checking of text layer character sizes - Disregard character measurements that result in width or height of 0 --- lib/ace/layer/text.js | 24 +++++++++++++++++------- lib/ace/virtual_renderer.js | 4 ++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/ace/layer/text.js b/lib/ace/layer/text.js index 84d2d2ec..43f24baa 100644 --- a/lib/ace/layer/text.js +++ b/lib/ace/layer/text.js @@ -50,7 +50,7 @@ var Text = function(parentEl) { this.element.className = "ace_layer ace_text-layer"; parentEl.appendChild(this.element); - this.$characterSize = this.$measureSizes(); + this.$characterSize = this.$measureSizes() || {width: 0, height: 0}; this.$pollSizeChanges(); }; @@ -75,14 +75,18 @@ var Text = function(parentEl) { return this.$characterSize.width || 1; }; + this.checkForSizeChanges = function() { + var size = this.$measureSizes(); + if (size && (this.$characterSize.width !== size.width || this.$characterSize.height !== size.height)) { + this.$characterSize = size; + this._dispatchEvent("changeCharaterSize", {data: size}); + } + }; + this.$pollSizeChanges = function() { var self = this; setInterval(function() { - var size = self.$measureSizes(); - if (self.$characterSize.width !== size.width || self.$characterSize.height !== size.height) { - self.$characterSize = size; - self._dispatchEvent("changeCharaterSize", {data: size}); - } + self.checkForSizeChanges(); }, 500); }; @@ -92,7 +96,7 @@ var Text = function(parentEl) { fontWeight : 1, fontStyle : 1, lineHeight : 1 - }, + }; this.$measureSizes = function() { var n = 1000; @@ -134,6 +138,12 @@ var Text = function(parentEl) { height: this.$measureNode.offsetHeight, width: this.$measureNode.offsetWidth / (n * 2) }; + + // Size and width can be null if the editor is not visible or + // detached from the document + if (size.width == 0 && size.height == 0) + return null; + return size; }; diff --git a/lib/ace/virtual_renderer.js b/lib/ace/virtual_renderer.js index 0027d18c..8ef081e0 100644 --- a/lib/ace/virtual_renderer.js +++ b/lib/ace/virtual_renderer.js @@ -192,6 +192,10 @@ var VirtualRenderer = function(container, theme) { this.$loop.schedule(this.CHANGE_FULL); }; + this.updateFontSize = function() { + this.$textLayer.checkForSizeChanges(); + }; + /** * Triggers resize of the editor */