Bug 1003620 - Add QR encoder library. r=paul

Imports the QR Code Generator library[1].

The original author is Kazuhiko Arase <arase@d-project.com>.

A copy of the library's MIT license is included.

[1]: https://github.com/kazuhikoarase/qrcode-generator

--HG--
rename : toolkit/devtools/moz.build => toolkit/devtools/qrcode/encoder/moz.build
rename : toolkit/devtools/moz.build => toolkit/devtools/qrcode/moz.build
This commit is contained in:
J. Ryan Stinnett 2014-05-01 13:28:00 +02:00
parent beac47a846
commit e609163c15
11 changed files with 1849 additions and 1 deletions

View File

@ -39,3 +39,4 @@
[include:js/xpconnect/tests/unit/xpcshell.ini]
[include:js/jsd/test/xpcshell.ini]
[include:security/manager/ssl/tests/unit/xpcshell.ini]
[include:toolkit/devtools/qrcode/tests/unit/xpcshell.ini]

View File

@ -9,6 +9,7 @@
[include:dom/wappush/tests/xpcshell.ini]
[include:toolkit/devtools/apps/tests/unit/xpcshell.ini]
[include:toolkit/devtools/debugger/tests/unit/xpcshell.ini]
[include:toolkit/devtools/qrcode/tests/unit/xpcshell.ini]
[include:toolkit/devtools/sourcemap/tests/unit/xpcshell.ini]
[include:toolkit/mozapps/downloads/tests/unit/xpcshell.ini]
[include:toolkit/mozapps/update/tests/unit_aus_update/xpcshell.ini]

View File

@ -113,6 +113,7 @@
<li><a href="about:license#pbkdf2-sha256">pbkdf2_sha256 License</a></li>
<li><a href="about:license#praton">praton License</a></li>
<li><a href="about:license#qcms">qcms License</a></li>
<li><a href="about:license#qrcode-generator">QR Code Generator License</a></li>
<li><a href="about:license#xdg">Red Hat xdg_user_dir_lookup License</a></li>
<li><a href="about:license#hunspell-ru">Russian Spellchecking Dictionary License</a></li>
<li><a href="about:license#sctp">SCTP Licenses</a></li>
@ -3574,6 +3575,35 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</pre>
<hr>
<h1><a id="qrcode-generator"></a>QR Code Generator License</h1>
<p>This license applies to certain files in the directory
<span class="path">toolkit/devtools/qrcode/encoder/</span>.</p>
<pre>
Copyright (c) 2009 Kazuhiko Arase
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
</pre>
<hr>
<h1><a id="xdg"></a>Red Hat xdg_user_dir_lookup License</h1>

View File

@ -13,7 +13,8 @@ PARALLEL_DIRS += [
'apps',
'styleinspector',
'acorn',
'pretty-fast'
'pretty-fast',
'qrcode'
]
MOCHITEST_CHROME_MANIFESTS += ['tests/mochitest/chrome.ini']

View File

@ -0,0 +1,19 @@
Copyright (c) 2009 Kazuhiko Arase
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,11 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
JS_MODULES_PATH = 'modules/devtools/qrcode/encoder'
EXTRA_JS_MODULES += [
'index.js',
]

View File

@ -0,0 +1,17 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
PARALLEL_DIRS += [
'encoder'
]
JS_MODULES_PATH = 'modules/devtools/qrcode'
EXTRA_JS_MODULES += [
'qrcode.js',
]
XPCSHELL_TESTS_MANIFESTS += ['tests/unit/xpcshell.ini']

View File

@ -0,0 +1,62 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
let { Encoder, QRRSBlock, QRErrorCorrectLevel } = require("./encoder/index");
/**
* There are many "versions" of QR codes, which describes how many dots appear
* in the resulting image, thus limiting the amount of data that can be
* represented.
*
* The encoder used here allows for versions 1 - 10 (more dots for larger
* versions).
*
* It expects you to pick a version large enough to contain your message. Here
* we search for the mimimum version based on the message length.
* @param string message
* Text to encode
* @param string quality
* Quality level: L, M, Q, H
* @return integer
*/
exports.findMinimumVersion = function(message, quality) {
let msgLength = message.length;
let qualityLevel = QRErrorCorrectLevel[quality];
for (let version = 1; version <= 10; version++) {
let rsBlocks = QRRSBlock.getRSBlocks(version, qualityLevel);
let maxLength = rsBlocks.reduce((prev, block) => {
return prev + block.dataCount;
}, 0);
// Remove two bytes to fit header info
maxLength -= 2;
if (msgLength <= maxLength) {
return version;
}
}
throw new Error("Message too large");
};
/**
* Simple wrapper around the underlying encoder's API.
* @param string message
* Text to encode
* @param string quality (optional)
Quality level: L, M, Q, H
* @param integer version (optional)
* QR code "version" large enough to contain the message
* @return object with the following fields:
* * src: an image encoded a data URI
* * height: image height
* * width: image width
*/
exports.encodeToDataURI = function(message, quality, version) {
quality = quality || "H";
version = version || exports.findMinimumVersion(message, quality);
let encoder = new Encoder(version, quality);
encoder.addData(message);
encoder.make();
return encoder.createImgData();
};

View File

@ -0,0 +1,28 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
/**
* Test encoding a simple message.
*/
const { utils: Cu } = Components;
const { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
const { require } = devtools;
const QR = require("devtools/toolkit/qrcode/qrcode");
function run_test() {
let imgData = QR.encodeToDataURI("HELLO", "L");
do_check_eq(imgData.src,
"data:image/gif;base64,R0lGODdhOgA6AIAAAAAAAP///ywAAAAAOgA6AAAC" +
"/4yPqcvtD6OctNqLs968+w+G4gKU5nkaKKquLuW+QVy2tAkDTj3rfQts8CRDko" +
"+HPPoYRUgy9YsyldDm44mLWhHYZM6W7WaDqyCRGkZDySxpRGw2sqvLt1q5w/fo" +
"XyE6vnUQOJUHBlinMGh046V1F5PDqNcoqcgBOWKBKbK2N+aY+Ih49VkmqMcl2l" +
"dkhZUK1umE6jZXJ2ZJaujZaRqH4bpb2uZrJxvIt4Ebe9qoYYrJOsw8apz2bCut" +
"m9kqDcw52uuImyr5Oh1KXH1jrn2anuunywtODU/o2c6teceW39ZcLFg/fNMo1b" +
"t3jVw2dwTPwJq1KYG3gAklCgu37yGxeScYKyiCc+7DR34hPVQiuQ7UhJMagyEb" +
"lymmzJk0a9q8iTOnzp0NCgAAOw==");
do_check_eq(imgData.width, 58);
do_check_eq(imgData.height, 58);
}

View File

@ -0,0 +1,5 @@
[DEFAULT]
head =
tail =
[test_encode.js]