Source: widgets/kekule.widget.sys.js

  1. /**
  2. * @fileoverview
  3. * Some system functions of Kekule widget lib.
  4. * @author Partridge Jiang
  5. */
  6. /*
  7. * requires /lan/classes.js
  8. * requires /core/kekule.common.js
  9. * requires /widget/kekule.widget.base.js
  10. * requires /xbrowsers/kekule.x.js
  11. */
  12. (function(){
  13. "use strict";
  14. /** @ignore */
  15. var _FontDetector = function() {
  16. // a font will be compared against all the three default fonts.
  17. // and if it doesn't match all 3 then that font is not available.
  18. var baseFonts = ['monospace', 'sans-serif', 'serif'];
  19. //we use m or w because these two characters take up the maximum width.
  20. // And we use a LLi so that the same matching fonts can get separated
  21. var testString = "mmmmmmmmmmlli";
  22. //we test using 72px font size, we may use any size. I guess larger the better.
  23. var testSize = '72px';
  24. var h = document.getElementsByTagName("body")[0];
  25. // create a SPAN in the document to get the width of the text we use to test
  26. var s = document.createElement("span");
  27. s.style.fontSize = testSize;
  28. s.innerHTML = testString;
  29. var defaultWidth = {};
  30. var defaultHeight = {};
  31. for (var index in baseFonts) {
  32. //get the default width for the three base fonts
  33. s.style.fontFamily = baseFonts[index];
  34. h.appendChild(s);
  35. defaultWidth[baseFonts[index]] = s.offsetWidth; //width for the default font
  36. defaultHeight[baseFonts[index]] = s.offsetHeight; //height for the defualt font
  37. h.removeChild(s);
  38. }
  39. function detect(font) {
  40. var detected = false;
  41. for (var index in baseFonts) {
  42. s.style.fontFamily = font + ',' + baseFonts[index]; // name of the font along with the base font for fallback.
  43. h.appendChild(s);
  44. var matched = (s.offsetWidth != defaultWidth[baseFonts[index]] || s.offsetHeight != defaultHeight[baseFonts[index]]);
  45. h.removeChild(s);
  46. detected = detected || matched;
  47. }
  48. return detected;
  49. }
  50. this.detect = detect;
  51. };
  52. var _fontDetector;
  53. /**
  54. * A util class to detect if a font is available in browser.
  55. * The detection method is borrowed from http://www.lalit.org/lab/javascript-css-font-detect/.
  56. * @class
  57. */
  58. Kekule.Widget.FontDetector = {
  59. /**
  60. * Check if a font family is available in browser.
  61. * @param {String} fontFamily
  62. * @returns {Bool}
  63. */
  64. detect: function(fontFamily)
  65. {
  66. var d = _fontDetector;
  67. if (!d)
  68. {
  69. d = new _FontDetector();
  70. _fontDetector = d;
  71. }
  72. return d.detect(fontFamily);
  73. }
  74. };
  75. var _defFontList = [
  76. 'Arial, Helvetica, sans-serif',
  77. 'Georgia, Times New Roman, Times, serif',
  78. 'Courier New, Courier, monospace',
  79. 'Tahoma, Geneva, sans-serif',
  80. 'Trebuchet MS, Arial, Helvetica, sans-serif',
  81. 'Arial Black, Gadget, sans-serif',
  82. 'Palatino Linotype, Book Antiqua, Palatino, serif',
  83. 'Lucida Sans Unicode, Lucida Grande, sans-serif',
  84. 'MS Serif, New York, serif',
  85. 'Lucida Console, Monaco, monospace',
  86. 'Comic Sans MS, cursive'
  87. ];
  88. /**
  89. * A util class to list a set of fonts available for Kekule widget system.
  90. * @class
  91. */
  92. Kekule.Widget.FontEnumerator = {
  93. /**
  94. * Returns a list of available fonts in browser out of candidateFonts.
  95. * If candidateFonts is not set, a default font set will be used.
  96. * @param {Array} candidateFonts
  97. * @returns {Array}
  98. */
  99. getAvailableFontFamilies: function(candidateFonts)
  100. {
  101. if (!candidateFonts)
  102. candidateFonts = _defFontList;
  103. var result = [];
  104. for (var i = 0, l = candidateFonts.length; i < l; ++i)
  105. {
  106. var fn = candidateFonts[i];
  107. if (Kekule.Widget.FontDetector.detect(fn))
  108. result.push(fn);
  109. }
  110. return result;
  111. }
  112. };
  113. })();