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.
6 * @fileoverview Rule stores for the basic components of math expressions:
7 * Unicode symbols and functions.
9 * The idea of these stores is to provide a more efficient data structure to
10 * look up rules in the background page than the usual flat array of rules
11 * implemented by other stores.
15 goog
.provide('cvox.MathCompoundStore');
16 goog
.provide('cvox.MathSimpleStore');
18 goog
.require('cvox.MathStore');
19 goog
.require('cvox.SpeechRule');
22 * A base store for simple Math objects.
24 * @extends {cvox.MathStore}
26 cvox
.MathSimpleStore = function() {
29 goog
.inherits(cvox
.MathSimpleStore
, cvox
.MathStore
);
33 * Turns a domain mapping from its JSON representation containing simple strings
34 * only into a list of speech rules.
35 * @param {string} name Name for the rules.
36 * @param {string} str String for precondition and constraints.
37 * @param {Object<Object<string>>} mapping Simple string
40 cvox
.MathSimpleStore
.prototype.defineRulesFromMappings = function(
42 for (var domain
in mapping
) {
43 for (var style
in mapping
[domain
]) {
44 var content
= mapping
[domain
][style
];
45 var cstr
= 'self::text() = "' + str
+ '"';
46 var rule
= this.defineRule(
47 name
, domain
+ '.' + style
, '[t] "' + content
+ '"',
48 'self::text()', cstr
);
55 * A compound store for simple Math objects.
58 cvox
.MathCompoundStore = function() {
60 * A set of efficient substores.
61 * @type {Object<cvox.MathStore>}
66 goog
.addSingletonGetter(cvox
.MathCompoundStore
);
70 * Function creates a rule store in the compound store for a particular string,
71 * and populates it with a set of rules.
72 * @param {string} name Name of the rule.
73 * @param {string} str String used as key to refer to the rule store
74 * precondition and constr
75 * @param {Object} mappings JSON representation of mappings from styles and
76 * domains to strings, from which the speech rules will be computed.
78 cvox
.MathCompoundStore
.prototype.defineRules = function(name
, str
, mappings
) {
79 var store
= new cvox
.MathSimpleStore();
80 store
.defineRulesFromMappings(name
, str
, mappings
);
81 this.subStores_
[str
] = store
;
86 * Makes a speech rule for Unicode characters from its JSON representation.
87 * @param {Object} json JSON object of the speech rules.
89 cvox
.MathCompoundStore
.prototype.addSymbolRules = function(json
) {
90 var key
= cvox
.MathSimpleStore
.parseUnicode_(json
['key']);
91 this.defineRules(json
['key'], key
, json
['mappings']);
96 * Makes a speech rule for Unicode characters from its JSON representation.
97 * @param {Object} json JSON object of the speech rules.
99 cvox
.MathCompoundStore
.prototype.addFunctionRules = function(json
) {
100 var names
= json
['names'];
101 var mappings
= json
['mappings'];
102 for (var j
= 0, name
; name
= names
[j
]; j
++) {
103 this.defineRules(name
, name
, mappings
);
109 * Retrieves a rule for the given node if one exists.
110 * @param {Node} node A node.
111 * @param {!cvox.SpeechRule.DynamicCstr} dynamic Additional dynamic
112 * constraints. These are matched against properties of a rule.
113 * @return {cvox.SpeechRule} The speech rule if it exists.
115 cvox
.MathCompoundStore
.prototype.lookupRule = function(node
, dynamic
) {
116 var store
= this.subStores_
[node
.textContent
];
118 return store
.lookupRule(node
, dynamic
);
124 * Looks up a rule for a given string and executes its actions.
125 * @param {string} text The text to be translated.
126 * @param {!cvox.SpeechRule.DynamicCstr} dynamic Additional dynamic
127 * constraints. These are matched against properties of a rule.
128 * @return {!string} The string resulting from the action of speech rule.
130 cvox
.MathCompoundStore
.prototype.lookupString = function(text
, dynamic
) {
131 var textNode
= document
.createTextNode(text
);
132 var rule
= this.lookupRule(textNode
, dynamic
);
136 return rule
.action
.components
137 .map(function(comp
) {
138 return comp
.content
.slice(1, -1);})
144 * Get a set of all dynamic constraint values.
145 * @return {!Object<cvox.SpeechRule.DynamicCstrAttrib, Array<string>>} The
146 * object with all annotations.
148 cvox
.MathCompoundStore
.prototype.getDynamicConstraintValues = function() {
150 for (var store
in this.subStores_
) {
151 var cstr
= this.subStores_
[store
].getDynamicConstraintValues();
152 for (var key
in cstr
) {
153 var set = newCstr
[key
];
155 newCstr
[key
] = cvox
.MathUtil
.union(set, cstr
[key
]);
157 newCstr
[key
] = cstr
[key
];
166 * Parses a string with a hex representatino of a unicode code point into the
167 * corresponding unicode character.
168 * @param {string} number The code point to be parsed.
169 * @return {string} The unicode character.
172 cvox
.MathSimpleStore
.parseUnicode_ = function(number
) {
173 var keyValue
= parseInt(number
, 16);
174 if (keyValue
< 0x10000) {
175 return String
.fromCharCode(keyValue
);
178 var hiSurrogate
= (keyValue
>> 10) + 0xD800;
179 var lowSurrogate
= (keyValue
& 0x3FF) + 0xDC00;
180 return String
.fromCharCode(hiSurrogate
, lowSurrogate
);