genericfontnames.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Functions to list locale-specific font list and generic name.
  16. * Generic name used for a font family would be locale dependent. For example,
  17. * for 'zh'(Chinese) users, the name for Serif family would be in Chinese.
  18. * Further documentation at: http://go/genericfontnames.
  19. */
  20. goog.provide('goog.locale.genericFontNames');
  21. /**
  22. * This object maps (resourceName, localeName) to a resourceObj.
  23. * @type {Object}
  24. * @private
  25. */
  26. goog.locale.genericFontNames.data_ = {};
  27. /**
  28. * Normalizes the given locale id to standard form. eg: zh_Hant_TW.
  29. * Many a times, input locale would be like: zh-tw, zh-hant-tw.
  30. * @param {string} locale The locale id to be normalized.
  31. * @return {string} Normalized locale id.
  32. * @private
  33. */
  34. goog.locale.genericFontNames.normalize_ = function(locale) {
  35. locale = locale.replace(/-/g, '_');
  36. locale =
  37. locale.replace(/_[a-z]{2}$/, function(str) { return str.toUpperCase(); });
  38. locale = locale.replace(/[a-z]{4}/, function(str) {
  39. return str.substring(0, 1).toUpperCase() + str.substring(1);
  40. });
  41. return locale;
  42. };
  43. /**
  44. * Gets the list of fonts and their generic names for the given locale.
  45. * @param {string} locale The locale for which font lists and font family names
  46. * to be produced. The expected locale id is as described in
  47. * http://wiki/Main/IIISynonyms in all lowercase for easy matching.
  48. * Smallest possible id is expected.
  49. * Examples: 'zh', 'zh-tw', 'iw' instead of 'zh-CN', 'zh-Hant-TW', 'he'.
  50. * @return {Array<Object>} List of objects with generic name as 'caption' and
  51. * corresponding font name lists as 'value' property.
  52. */
  53. goog.locale.genericFontNames.getList = function(locale) {
  54. locale = goog.locale.genericFontNames.normalize_(locale);
  55. if (locale in goog.locale.genericFontNames.data_) {
  56. return goog.locale.genericFontNames.data_[locale];
  57. }
  58. return [];
  59. };