Repository navigation
Expand file tree
/
Copy pathcalibrator.js
More file actions
140 lines (116 loc) · 3.65 KB
/
Copy pathcalibrator.js
File metadata and controls
140 lines (116 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
var Promise = require('bluebird'),
fs = require('fs'),
path = require('path'),
_ = require('lodash'),
GeminiError = require('./errors/gemini-error'),
looksSame = require('looks-same'),
clientScriptCalibrate = fs.readFileSync(path.join(__dirname, 'browser', 'client-scripts', 'gemini.calibrate.min.js'), 'utf8'),
DIRECTION = {FORWARD: 'forward', REVERSE: 'reverse'};
/**
* @constructor
*/
function Calibrator() {
this._cache = {};
}
/**
* @param {Browser} browser
* @returns {Promise.<CalibrationResult>}
*/
Calibrator.prototype.calibrate = function(browser) {
var _this = this;
if (this._cache[browser.id]) {
return Promise.resolve(this._cache[browser.id]);
}
return browser.open('about:blank', {resetZoom: false})
.then(function() {
return browser.evalScript(clientScriptCalibrate);
})
.then(function(features) {
return [features, browser.captureViewportImage()];
})
.spread(function(features, image) {
var innerWidth = features.innerWidth,
hasPixelRatio = !!(features.pixelRatio && features.pixelRatio > 1.0),
imageFeatures = _this._analyzeImage(image, {calculateColorLength: hasPixelRatio});
if (!imageFeatures) {
return Promise.reject(new GeminiError(
'Could not calibrate. This could be due to calibration page has failed to open properly'
));
}
_.extend(features, {
top: imageFeatures.viewportStart.y,
left: imageFeatures.viewportStart.x,
usePixelRatio: hasPixelRatio && imageFeatures.colorLength > innerWidth
});
_this._cache[browser.id] = features;
return features;
});
};
Calibrator.prototype._analyzeImage = function(image, params) {
var imageHeight = image.getSize().height;
for (var y = 0; y < imageHeight; y++) {
var result = analyzeRow(y, image, params);
if (result) {
return result;
}
}
return null;
};
function analyzeRow(row, image, params) {
params = params || {};
var markerStart = findMarkerInRow(row, image, DIRECTION.FORWARD);
if (markerStart === -1) {
return null;
}
if (!params.calculateColorLength) {
return {viewportStart: {x: markerStart, y: row}};
}
var markerEnd = findMarkerInRow(row, image, DIRECTION.REVERSE),
colorLength = markerEnd - markerStart + 1;
return {viewportStart: {x: markerStart, y: row}, colorLength: colorLength};
}
function findMarkerInRow(row, image, searchDirection) {
var imageWidth = image.getSize().width,
searchColor = {R: 148, G: 250, B: 0};
if (searchDirection === DIRECTION.REVERSE) {
return searchReverse_();
} else {
return searchForward_();
}
function searchForward_() {
for (var x = 0; x < imageWidth; x++) {
if (compare_(x)) {
return x;
}
}
return -1;
}
function searchReverse_() {
for (var x = imageWidth - 1; x >= 0; x--) {
if (compare_(x)) {
return x;
}
}
return -1;
}
function compare_(x) {
var color = pickRGB(image.getRGBA(x, row));
return looksSame.colors(color, searchColor);
}
}
function pickRGB(rgba) {
return {
R: rgba.r,
G: rgba.g,
B: rgba.b
};
}
/**
* @typedef {Object} CalibrationResult
* @property {Number} top
* @property {Number} left
* @property {Number} right
* @property {Number} bottom
*/
module.exports = Calibrator;