summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authordpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-22 02:53:39 +0000
committerdpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-22 02:53:39 +0000
commit68e6e7b280889fcca1c216640e84c2c610ae6372 (patch)
tree98cd900b0c0515040b0225ea00759bd615be04f6 /chrome
parent768ec65e5192f4e28cdf219e4a7fd70104260a0c (diff)
downloadchromium_src-68e6e7b280889fcca1c216640e84c2c610ae6372.zip
chromium_src-68e6e7b280889fcca1c216640e84c2c610ae6372.tar.gz
chromium_src-68e6e7b280889fcca1c216640e84c2c610ae6372.tar.bz2
Print Preview: Adding support for localized margin units and format.
BUG=100416 TEST=The decimal point (dot vs comma) is displayed according to the current locale. Also the units used for the margins (mm vs inches) respect the locale. Try LANGUAGE=el_GR ./out/Release/chrome vs LANGUAGE=en_US ./out/Release/chrome. Review URL: http://codereview.chromium.org/8345025 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106846 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/resources/print_preview/margin_settings.js59
-rw-r--r--chrome/browser/resources/print_preview/margin_textbox.js38
-rw-r--r--chrome/browser/resources/print_preview/margin_utils.js102
-rw-r--r--chrome/browser/resources/print_preview/print_preview.js1
-rw-r--r--chrome/browser/resources/print_preview/print_preview_utils.js23
-rw-r--r--chrome/browser/resources/print_preview/print_preview_utils_test.html12
-rw-r--r--chrome/browser/ui/webui/print_preview_handler.cc27
-rw-r--r--chrome/browser/ui/webui/print_preview_handler.h4
8 files changed, 172 insertions, 94 deletions
diff --git a/chrome/browser/resources/print_preview/margin_settings.js b/chrome/browser/resources/print_preview/margin_settings.js
index ea2ec12..819d79f 100644
--- a/chrome/browser/resources/print_preview/margin_settings.js
+++ b/chrome/browser/resources/print_preview/margin_settings.js
@@ -72,37 +72,17 @@ cr.define('print_preview', function() {
/**
* Rounds |this| based on the precision used when displaying the margins in
- * inches. This is done by converting from points to inches and back to
- * points.
+ * the user's prefered units. This is done by converting from points to
+ * those units (mm/inches) and back to points.
*/
- roundToInches: function() {
+ roundToLocaleUnits: function() {
var indicesAsArray = this.indicesAsArray_();
for (var i = 0; i < indicesAsArray.length; i++) {
this[indicesAsArray[i]] =
- print_preview.convertPointsToInchesTextAndBack(
+ print_preview.convertPointsToLocaleUnitsAndBack(
this[indicesAsArray[i]]);
}
},
-
- /**
- * Converts |this| to inches and returns the result in a new Margins object.
- * |this| is not affected. It assumes that |this| is currently expressed in
- * points.
- * @param {number} The number of decimal points to keep.
- * @return {Margins} The equivalent of |this| in inches.
- */
- toInches: function(precision) {
- return new Margins(
- Margins.roundToPrecision(convertPointsToInches(
- this[MarginSettings.LEFT_GROUP]), precision),
- Margins.roundToPrecision(convertPointsToInches(
- this[MarginSettings.TOP_GROUP]), precision),
- Margins.roundToPrecision(convertPointsToInches(
- this[MarginSettings.RIGHT_GROUP]), precision),
- Margins.roundToPrecision(convertPointsToInches(
- this[MarginSettings.BOTTOM_GROUP]), precision)
- );
- }
};
/**
@@ -186,6 +166,24 @@ cr.define('print_preview', function() {
// Group name corresponding to the bottom margin.
MarginSettings.BOTTOM_GROUP = 'bottom';
+ /**
+ * Extracts the number formatting and measurement system for the current
+ * locale.
+ * @param {string} numberFormat Is the formatted version of a sample number,
+ * sent from the backend.
+ * @oaram {number} measurementSystem 0 for SI (aka metric system), 1 for the
+ * system used in the US. Note: Mathces UMeasurementSystem enum in
+ * third_party/icu/public/i18n/unicode/ulocdata.h.
+ */
+ MarginSettings.setNumberFormatAndMeasurementSystem = function(
+ numberFormat, measurementSystem) {
+ var regex = /^(\d+)(\.|\,)(\d+)(\.|\,)(\d+)$/;
+ var matches = numberFormat.match(regex);
+ MarginSettings.thousandsPoint = matches[2];
+ MarginSettings.decimalPoint = matches[4];
+ MarginSettings.useMetricSystem = measurementSystem == 0;
+ };
+
cr.addSingletonGetter(MarginSettings);
MarginSettings.prototype = {
@@ -256,10 +254,8 @@ cr.define('print_preview', function() {
requestPreviewIfNeeded_: function() {
if (!this.areMarginSettingsValid())
return;
- if (this.customMargins_.toInches(2).isEqual(
- this.previousCustomMargins_.toInches(2))) {
+ if (this.customMargins_.isEqual(this.previousCustomMargins_))
return;
- }
this.previousCustomMargins_.copy(this.customMargins_);
setDefaultValuesAndRegeneratePreview(false);
},
@@ -362,7 +358,7 @@ cr.define('print_preview', function() {
for (var i = 0; i < marginValueLimits.length; i++) {
marginValueLimits[i] = Math.max(marginValueLimits[i], 0);
- marginValueLimits[i] = print_preview.convertPointsToInchesTextAndBack(
+ marginValueLimits[i] = print_preview.convertPointsToLocaleUnitsAndBack(
marginValueLimits[i]);
}
return marginValueLimits;
@@ -401,7 +397,8 @@ cr.define('print_preview', function() {
*/
onMarginTextValueMayHaveChanged_: function(event) {
var marginBox = event.target;
- var marginBoxValue = convertInchesToPoints(marginBox.margin);
+ var marginBoxValue =
+ print_preview.convertLocaleUnitsToPoints(marginBox.margin);
this.customMargins_[marginBox.marginGroup] = marginBoxValue;
this.requestPreviewIfNeeded_();
},
@@ -518,7 +515,7 @@ cr.define('print_preview', function() {
if (this.lastSelectedOption_ == MarginSettings.MARGINS_VALUE_DEFAULT) {
this.customMargins_ = this.currentDefaultPageLayout.margins_;
- this.customMargins_.roundToInches();
+ this.customMargins_.roundToLocaleUnits();
}
this.previousCustomMargins_.copy(this.customMargins_);
@@ -609,5 +606,7 @@ cr.define('print_preview', function() {
return {
MarginSettings: MarginSettings,
PageLayout: PageLayout,
+ setNumberFormatAndMeasurementSystem:
+ MarginSettings.setNumberFormatAndMeasurementSystem,
};
});
diff --git a/chrome/browser/resources/print_preview/margin_textbox.js b/chrome/browser/resources/print_preview/margin_textbox.js
index 733d46b..3f3e80d 100644
--- a/chrome/browser/resources/print_preview/margin_textbox.js
+++ b/chrome/browser/resources/print_preview/margin_textbox.js
@@ -44,6 +44,16 @@ cr.define('print_preview', function() {
},
/**
+ * Sets the contents of the textbox.
+ * @param {number} newValueInPoints The value to be displayed in points.
+ * @private
+ */
+ setValue_: function(newValueInPoints) {
+ this.value =
+ print_preview.convertPointsToLocaleUnitsText(newValueInPoints);
+ },
+
+ /**
* Updates the state of |this|.
* @param {number} value The margin value in points.
* @param {number} valueLimit The upper allowed value for the margin.
@@ -52,10 +62,8 @@ cr.define('print_preview', function() {
*/
update: function(value, valueLimit, keepDisplayedValue) {
this.lastValidValueInPoints = value;
- if (!keepDisplayedValue) {
- this.value = print_preview.convertPointsToInchesText(
- this.lastValidValueInPoints);
- }
+ if (!keepDisplayedValue)
+ this.setValue_(this.lastValidValueInPoints);
this.valueLimit = valueLimit;
this.validate();
@@ -69,14 +77,12 @@ cr.define('print_preview', function() {
updateWhileDragging: function(dragDeltaInPoints) {
var validity = this.validateDelta(dragDeltaInPoints);
- if (validity == print_preview.marginValidationStates.WITHIN_RANGE) {
- this.value = print_preview.convertPointsToInchesText(
- this.lastValidValueInPoints + dragDeltaInPoints);
- } else if (validity == print_preview.marginValidationStates.TOO_SMALL) {
- this.value = print_preview.convertPointsToInchesText(0);
- } else if (validity == print_preview.marginValidationStates.TOO_BIG) {
- this.value = print_preview.convertPointsToInchesText(this.valueLimit);
- }
+ if (validity == print_preview.marginValidationStates.WITHIN_RANGE)
+ this.setValue_(this.lastValidValueInPoints + dragDeltaInPoints);
+ else if (validity == print_preview.marginValidationStates.TOO_SMALL)
+ this.setValue_(0);
+ else if (validity == print_preview.marginValidationStates.TOO_BIG)
+ this.setValue_(this.valueLimit);
this.validate();
this.updateColor();
@@ -99,8 +105,6 @@ cr.define('print_preview', function() {
this.isValid =
print_preview.validateMarginText(this.value, this.valueLimit) ==
print_preview.marginValidationStates.WITHIN_RANGE;
- if (this.isValid)
- this.value = print_preview.convertInchesToInchesText(this.margin);
},
/**
@@ -139,8 +143,7 @@ cr.define('print_preview', function() {
clearTimeout(this.timerId_);
this.validate();
if (!this.isValid) {
- this.value = print_preview.convertPointsToInchesText(
- this.lastValidValueInPoints);
+ this.setValue_(this.lastValidValueInPoints);
this.validate();
}
@@ -170,8 +173,7 @@ cr.define('print_preview', function() {
*/
onKeyUp_: function(e) {
if (e.keyCode == MarginTextbox.ESCAPE_KEYCODE) {
- this.value = print_preview.convertPointsToInchesText(
- this.lastValidValueInPoints);
+ this.setValue_(this.lastValidValueInPoints);
this.validate();
this.updateColor();
cr.dispatchSimpleEvent(document, 'updateSummary');
diff --git a/chrome/browser/resources/print_preview/margin_utils.js b/chrome/browser/resources/print_preview/margin_utils.js
index e217857..78dba89 100644
--- a/chrome/browser/resources/print_preview/margin_utils.js
+++ b/chrome/browser/resources/print_preview/margin_utils.js
@@ -17,19 +17,34 @@ cr.define('print_preview', function() {
* does not represent a valid number.
*/
function extractMarginValue(text) {
- // Remove whitespace anywhere in the string.
- text.replace(/\s*/g, '');
+ // Removing whitespace anywhere in the string.
+ text = text.replace(/\s*/g, '');
if (text.length == 0)
return -1;
- // Remove the inch(") symbol at end of string if present.
- if (text.charAt(text.length - 1) == '\"')
- text = text.slice(0, text.length - 1);
- var regex = /^\d*(\.\d+)?$/
- if (regex.test(text))
+ var validationRegex = getValidationRegExp();
+ if (validationRegex.test(text)) {
+ // Replacing decimal point with the dot symbol in order to use
+ // parseFloat() properly.
+ var replacementRegex = new RegExp('\\' +
+ print_preview.MarginSettings.decimalPoint + '{1}');
+ text = text.replace(replacementRegex, '.');
return parseFloat(text);
+ }
return -1;
}
+ /**
+ * @return {RegExp} A regular expression for validating the input of the user.
+ * It takes into account the user's locale.
+ */
+ function getValidationRegExp() {
+ var regex = new RegExp('(^\\d+)(\\' +
+ print_preview.MarginSettings.thousandsPoint + '\\d{3})*(\\' +
+ print_preview.MarginSettings.decimalPoint + '\\d+)*' +
+ (!print_preview.MarginSettings.useMetricSystem ? '"?' : '(mm)?') + '$');
+ return regex;
+ }
+
var marginValidationStates = {
TOO_SMALL: 0,
WITHIN_RANGE: 1,
@@ -51,7 +66,7 @@ cr.define('print_preview', function() {
}
/**
- * @param {sting} text The text to check (in inches).
+ * @param {sting} text The text to check in user's locale units.
* @param {number} limit The upper bound of the valid margin range (in
* points).
* @return {number} An appropriate value from enum |marginValidationStates|.
@@ -60,40 +75,62 @@ cr.define('print_preview', function() {
var value = extractMarginValue(text);
if (value == -1)
return marginValidationStates.NOT_A_NUMBER;
- value = convertInchesToPoints(value);
+ value = print_preview.convertLocaleUnitsToPoints(value);
return validateMarginValue(value, limit);
}
/**
- * @param {number} toConvert The value to convert in points
- * @return {string} The equivalent text in inches.
+ * @param {number} value The value to convert in points.
+ * @return {number} The corresponding value after converting to user's locale
+ * units.
*/
- function convertPointsToInchesText(toConvert) {
- var inInches = convertPointsToInches(toConvert);
- return convertInchesToInchesText(inInches);
+ function convertPointsToLocaleUnits(value) {
+ return print_preview.MarginSettings.useMetricSystem ?
+ convertPointsToMillimeters(value) : convertPointsToInches(value);
}
/**
- * @param {number} toConvert The value to convert in inches.
- * @return {string} The equivalent text in inches with precision of two
- * digits.
+ * @param {number} value The value to convert in user's locale units.
+ * @return {number} The corresponding value after converting to points.
*/
- function convertInchesToInchesText(toConvert) {
- return toConvert.toFixed(2) + '"';
+ function convertLocaleUnitsToPoints(value) {
+ return print_preview.MarginSettings.useMetricSystem ?
+ convertMillimetersToPoints(value) : convertInchesToPoints(value);
}
/**
- * Converts |value| to inches text (keeping 2 decimal digits) and then back to
- * points. Note: Because of the precision the return value might be different
- * than |value|.
+ * Converts |value| to user's locale units and then back to points. Note:
+ * Because of the precision the return value might be different than |value|.
* @param {number} value The value in points to convert.
- * @return {number} The value in points after converting to inches with a
- * certain precision and back.
+ * @return {number} The value in points after converting to user's locale
+ * units with a certain precision and back.
+ */
+ function convertPointsToLocaleUnitsAndBack(value) {
+ var inLocaleUnits =
+ convertPointsToLocaleUnits(value).toFixed(getDesiredPrecision());
+ return convertLocaleUnitsToPoints(inLocaleUnits);
+ }
+
+ /**
+ * @return {number} The number of decimal digits to keep based on the
+ * measurement system used.
+ */
+ function getDesiredPrecision() {
+ return print_preview.MarginSettings.useMetricSystem ? 0 : 2;
+ }
+
+ /**
+ * @param {number} toConvert The value to convert in points.
+ * @return {string} The equivalent text in user locale units with precision
+ * of two digits.
*/
- function convertPointsToInchesTextAndBack(value) {
- var text = convertPointsToInchesText(value);
- var inches = extractMarginValue(text);
- return convertInchesToPoints(inches);
+ function convertPointsToLocaleUnitsText(value) {
+ var inLocaleUnits =
+ convertPointsToLocaleUnits(value).toFixed(getDesiredPrecision());
+ var inLocaleUnitsText = inLocaleUnits.toString(10).replace(
+ /\./g, print_preview.MarginSettings.decimalPoint);
+ return !print_preview.MarginSettings.useMetricSystem ?
+ inLocaleUnitsText + '"' : inLocaleUnitsText + 'mm';
}
/**
@@ -137,13 +174,14 @@ cr.define('print_preview', function() {
};
return {
- convertInchesToInchesText: convertInchesToInchesText,
- convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack,
- convertPointsToInchesText: convertPointsToInchesText,
+ convertPointsToLocaleUnitsAndBack:
+ convertPointsToLocaleUnitsAndBack,
+ convertPointsToLocaleUnitsText: convertPointsToLocaleUnitsText,
+ convertLocaleUnitsToPoints: convertLocaleUnitsToPoints,
extractMarginValue: extractMarginValue,
marginValidationStates: marginValidationStates,
Rect: Rect,
validateMarginText: validateMarginText,
- validateMarginValue: validateMarginValue
+ validateMarginValue: validateMarginValue,
};
});
diff --git a/chrome/browser/resources/print_preview/print_preview.js b/chrome/browser/resources/print_preview/print_preview.js
index 3b25f8f..135257e 100644
--- a/chrome/browser/resources/print_preview/print_preview.js
+++ b/chrome/browser/resources/print_preview/print_preview.js
@@ -142,6 +142,7 @@ function onLoad() {
previewArea.showLoadingAnimation();
chrome.send('getInitiatorTabTitle');
chrome.send('getDefaultPrinter');
+ chrome.send('getNumberFormatAndMeasurementSystem');
}
/**
diff --git a/chrome/browser/resources/print_preview/print_preview_utils.js b/chrome/browser/resources/print_preview/print_preview_utils.js
index 90ecd16..e16c445 100644
--- a/chrome/browser/resources/print_preview/print_preview_utils.js
+++ b/chrome/browser/resources/print_preview/print_preview_utils.js
@@ -190,7 +190,6 @@ function getPageSrcURL(id, pageNumber) {
return 'chrome://print/' + id + '/' + pageNumber + '/print.pdf';
}
-
/**
* Returns a random integer within the specified range, |endPointA| and
* |endPointB| are included.
@@ -205,7 +204,9 @@ function randomInteger(endPointA, endPointB) {
}
// Number of points per inch.
-POINTS_PER_INCH = 72;
+var POINTS_PER_INCH = 72;
+// Number of points per millimeter.
+var POINTS_PER_MILLIMETER = 2.83464567;
/**
* Converts |value| from inches to points.
@@ -224,3 +225,21 @@ function convertInchesToPoints(value) {
function convertPointsToInches(value) {
return value / POINTS_PER_INCH;
}
+
+/**
+ * Converts |value| from millimeters to points.
+ * @param {number} value The number in millimeters.
+ * @return {number} |value| in points.
+ */
+function convertMillimetersToPoints(value) {
+ return value * POINTS_PER_MILLIMETER;
+}
+
+/**
+ * Converts |value| from points to millimeters.
+ * @param {number} value The number in points.
+ * @return {number} |value| in millimeters.
+ */
+function convertPointsToMillimeters(value) {
+ return value / POINTS_PER_MILLIMETER;
+}
diff --git a/chrome/browser/resources/print_preview/print_preview_utils_test.html b/chrome/browser/resources/print_preview/print_preview_utils_test.html
index c633954..58994ed 100644
--- a/chrome/browser/resources/print_preview/print_preview_utils_test.html
+++ b/chrome/browser/resources/print_preview/print_preview_utils_test.html
@@ -105,18 +105,6 @@ function testPageSetToPageRanges() {
assertEquals(pageRanges[2].to, 11);
}
-function testConvertInchesToPoints() {
- assertEquals(convertInchesToPoints(1), 72);
- assertEquals(convertInchesToPoints(2), 144);
- assertEquals(convertInchesToPoints(0.45), 32.4);
-}
-
-function testConvertPointsToInches() {
- assertEquals(convertPointsToInches(72), 1);
- assertEquals(convertPointsToInches(144), 2);
- assertEquals(convertPointsToInches(32.4), 0.45);
-}
-
</script>
</body>
</html>
diff --git a/chrome/browser/ui/webui/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview_handler.cc
index 361f43e..0223677 100644
--- a/chrome/browser/ui/webui/print_preview_handler.cc
+++ b/chrome/browser/ui/webui/print_preview_handler.cc
@@ -16,6 +16,7 @@
#include "base/json/json_reader.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram.h"
+#include "base/i18n/number_formatting.h"
#include "base/path_service.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
@@ -47,6 +48,7 @@
#include "printing/metafile_impl.h"
#include "printing/page_range.h"
#include "printing/print_settings.h"
+#include "unicode/ulocdata.h"
#if !defined(OS_CHROMEOS)
#include "base/command_line.h"
@@ -258,6 +260,10 @@ void PrintPreviewHandler::RegisterMessages() {
web_ui_->RegisterMessageCallback("getInitiatorTabTitle",
base::Bind(&PrintPreviewHandler::HandleGetInitiatorTabTitle,
base::Unretained(this)));
+ web_ui_->RegisterMessageCallback("getNumberFormatAndMeasurementSystem",
+ base::Bind(
+ &PrintPreviewHandler::HandleGetNumberFormatAndMeasurementSystem,
+ base::Unretained(this)));
}
TabContentsWrapper* PrintPreviewHandler::preview_tab_wrapper() const {
@@ -601,6 +607,27 @@ void PrintPreviewHandler::HandleGetInitiatorTabTitle(
web_ui_->CallJavascriptFunction("setInitiatorTabTitle", tab_title);
}
+void PrintPreviewHandler::HandleGetNumberFormatAndMeasurementSystem(
+ const ListValue* /*args*/) {
+
+ // Getting the measurement system based on the locale.
+ UErrorCode errorCode = U_ZERO_ERROR;
+ const char* locale = g_browser_process->GetApplicationLocale().c_str();
+ UMeasurementSystem measurement_system =
+ ulocdata_getMeasurementSystem(locale, &errorCode);
+ if (errorCode > U_ZERO_ERROR || measurement_system == UMS_LIMIT)
+ measurement_system = UMS_SI;
+
+ // Getting the number formatting based on the locale.
+ StringValue number_format(base::FormatDouble(123456.78, 2));
+ base::FundamentalValue system(measurement_system);
+
+ web_ui_->CallJavascriptFunction(
+ "print_preview.setNumberFormatAndMeasurementSystem",
+ number_format,
+ system);
+}
+
void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() {
TabContentsWrapper* initiator_tab = GetInitiatorTab();
if (initiator_tab) {
diff --git a/chrome/browser/ui/webui/print_preview_handler.h b/chrome/browser/ui/webui/print_preview_handler.h
index f7491dd..86b7abb 100644
--- a/chrome/browser/ui/webui/print_preview_handler.h
+++ b/chrome/browser/ui/webui/print_preview_handler.h
@@ -136,6 +136,10 @@ class PrintPreviewHandler : public WebUIMessageHandler,
// |args| is unused.
void HandleGetInitiatorTabTitle(const base::ListValue* args);
+ // Asks the browser for the number formatting and measurement system according
+ // to the current locale.
+ void HandleGetNumberFormatAndMeasurementSystem(const base::ListValue* args);
+
// Sends the printer capabilities to the Web UI. |settings_info| contains
// printer capabilities information.
void SendPrinterCapabilities(const base::DictionaryValue& settings_info);