• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @fileoverview A class for loading and storing the maps for math atoms from
7 * JSON files. The class (and entries) can then be used as via the
8 * background page.
9 */
10
11
12goog.provide('cvox.MathMap');
13
14goog.require('cvox.MathCompoundStore');
15goog.require('cvox.MathUtil');
16
17
18/**
19 *
20 * @constructor
21 */
22cvox.MathMap = function() {
23
24  /**
25   * The compund store for symbol and function mappings.
26   * @type {cvox.MathCompoundStore}
27   */
28  this.store = cvox.MathCompoundStore.getInstance();
29  cvox.MathMap.parseFiles(
30      cvox.MathMap.FUNCTIONS_FILES_.map(
31          function(file) {
32            return cvox.MathMap.FUNCTIONS_PATH_ + file;
33          }))
34              .forEach(goog.bind(this.store.addFunctionRules, this.store));
35  cvox.MathMap.parseFiles(
36      cvox.MathMap.SYMBOLS_FILES_.map(
37          function(file) {
38            return cvox.MathMap.SYMBOLS_PATH_ + file;
39          }))
40              .forEach(goog.bind(this.store.addSymbolRules, this.store));
41
42  var cstrValues = this.store.getDynamicConstraintValues();
43  /**
44   * Array of domain names.
45   * @type {Array.<string>}
46   */
47  this.allDomains = cstrValues.domain;
48
49  /**
50   * Array of style names.
51   * @type {Array.<string>}
52   */
53  this.allStyles = cstrValues.style;
54};
55
56
57/**
58 * Stringifies MathMap into JSON object.
59 * @return {string} The stringified version of the mapping.
60 */
61cvox.MathMap.prototype.stringify = function() {
62  return JSON.stringify(this);
63};
64
65
66/**
67 * Path to dir containing ChromeVox JSON definitions for math speak.
68 * @type {string}
69 * @const
70 * @private
71 */
72cvox.MathMap.MATHMAP_PATH_ = 'chromevox/background/mathmaps/';
73
74
75/**
76 * Subpath to dir containing ChromeVox JSON definitions for symbols.
77 * @type {string}
78 * @const
79 * @private
80 */
81cvox.MathMap.SYMBOLS_PATH_ = cvox.MathMap.MATHMAP_PATH_ + 'symbols/';
82
83
84/**
85 * Subpath to dir containing ChromeVox JSON definitions for functions.
86 * @type {string}
87 * @const
88 * @private
89 */
90cvox.MathMap.FUNCTIONS_PATH_ = cvox.MathMap.MATHMAP_PATH_ + 'functions/';
91
92
93/**
94 * Array of JSON filenames containing symbol definitions for math speak.
95 * @type {Array.<string>}
96 * @const
97 * @private
98 */
99cvox.MathMap.SYMBOLS_FILES_ = [
100  // Greek
101  'greek-capital.json', 'greek-small.json', 'greek-scripts.json',
102  'greek-mathfonts.json', 'greek-symbols.json',
103
104  // Hebrew
105  'hebrew_letters.json',
106
107  // Latin
108  'latin-lower-double-accent.json', 'latin-lower-normal.json',
109  'latin-lower-phonetic.json', 'latin-lower-single-accent.json',
110  'latin-rest.json', 'latin-upper-double-accent.json',
111  'latin-upper-normal.json', 'latin-upper-single-accent.json',
112  'latin-mathfonts.json',
113
114  // Math Symbols
115  'math_angles.json', 'math_arrows.json', 'math_characters.json',
116  'math_delimiters.json', 'math_digits.json', 'math_geometry.json',
117  'math_harpoons.json', 'math_non_characters.json', 'math_symbols.json',
118  'math_whitespace.json', 'other_stars.json'
119];
120
121
122/**
123 * Array of JSON filenames containing symbol definitions for math speak.
124 * @type {Array.<string>}
125 * @const
126 * @private
127 */
128cvox.MathMap.FUNCTIONS_FILES_ = [
129  'algebra.json', 'elementary.json', 'hyperbolic.json', 'trigonometry.json'
130];
131
132
133/**
134 * Loads JSON for a given file name.
135 * @param {string} file A valid filename.
136 * @return {string} A string representing JSON array.
137 */
138cvox.MathMap.loadFile = function(file) {
139  try {
140    return cvox.MathMap.readJSON_(file);
141  } catch (x) {
142    console.log('Unable to load file: ' + file + ', error: ' + x);
143  }
144};
145
146
147/**
148 * Loads a list of JSON files.
149 * @param {Array.<string>} files An array of valid filenames.
150 * @return {Array.<string>} A string representing JSON array.
151 */
152cvox.MathMap.loadFiles = function(files) {
153  return files.map(cvox.MathMap.loadFile);
154};
155
156
157/**
158 * Creates an array of JSON objects from a list of files.
159 * @param {Array.<string>} files An array of filenames.
160 * @return {Array.<Object>} Array of JSON objects.
161 */
162cvox.MathMap.parseFiles = function(files) {
163  var strs = cvox.MathMap.loadFiles(files);
164
165  return [].concat.apply([], strs.map(
166      // Note: As JSON.parse does not expect the value index as the second
167      // parameter, we wrap it.
168      function(value) { return JSON.parse(value); }));
169};
170
171
172/**
173 * Takes path to a JSON file and returns a JSON object.
174 * @param {string} path Contains the path to a JSON file.
175 * @return {string} JSON.
176 * @private
177 */
178cvox.MathMap.readJSON_ = function(path) {
179  var url = chrome.extension.getURL(path);
180  if (!url) {
181    throw 'Invalid path: ' + path;
182    }
183
184  var xhr = new XMLHttpRequest();
185  xhr.open('GET', url, false);
186  xhr.send();
187  return xhr.responseText;
188};
189