Skip to content

Commit f6e8409

Browse files
committed
SourceHighlighter now always draws on-pixel & with a minimum 1px gap between highlights.
Signed-off-by: Grant Skinner <[email protected]>
1 parent 89a4a96 commit f6e8409

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

js/SourceHighlighter.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ SOFTWARE.
3232
p.cm = null;
3333
p.canvas = null;
3434
p.fill = "#6CF";
35+
p.lineSpacing = 2;
36+
p.lastBottom = -1;
37+
p.lastRight = -1;
3538

3639
p.initialize = function(cm, canvas, fill) {
3740
this.cm = cm;
@@ -52,7 +55,7 @@ SOFTWARE.
5255
var scroll = cm.getScrollInfo();
5356
var top = cm.indexFromPos(cm.coordsChar({left:0, top:scroll.top}, "local"));
5457
var bottom = cm.indexFromPos(cm.coordsChar({left:scroll.clientWidth, top:scroll.top+scroll.clientHeight}, "local"));
55-
58+
5659
for (var i=0,l=matches.length; i<l; i++) {
5760
var match = matches[i];
5861
var active = (match == activeMatch);
@@ -66,6 +69,7 @@ SOFTWARE.
6669
if (active) { ctx.globalAlpha = 0.6; }
6770
var startRect = cm.charCoords(startPos, "local");
6871
var endRect = cm.charCoords(endPos, "local");
72+
6973
if (startRect.bottom == endRect.bottom) {
7074
this.drawHighlight(ctx, startRect.left, startRect.top, endRect.right, endRect.bottom, scroll.top);
7175
} else {
@@ -87,7 +91,17 @@ SOFTWARE.
8791
};
8892

8993
p.drawHighlight = function(ctx, left, top, right, bottom, scrollY, startCap, endCap) {
90-
var capW = 4;
94+
var capW = 5;
95+
96+
left = left+0.5|0;
97+
right = right+0.5|0;
98+
top = top+0.5|0 + this.lineSpacing;
99+
bottom = bottom+0.5|0;
100+
101+
if (top > this.lastBottom-1) { this.lastBottom = bottom; }
102+
else if (left < this.lastRight) { left = this.lastRight; }
103+
this.lastRight = right;
104+
91105
var a = ctx.globalAlpha;
92106
if (startCap) {
93107
ctx.globalAlpha = a*0.5;
@@ -105,6 +119,7 @@ SOFTWARE.
105119

106120
p.clear = function() {
107121
this.canvas.width = this.canvas.width;
122+
this.lastBottom = -1;
108123
};
109124

110125
window.SourceHighlighter = SourceHighlighter;

0 commit comments

Comments
 (0)