webfontloader.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function() {
  2. /**
  3. * 웹폰트를 로딩하기 위한 클래스로 WysiwygPanel 내부에서만 사용된다.
  4. * @private
  5. * @class
  6. */
  7. Trex.WebfontLoader = Trex.Class.create({
  8. initialize: function(doc, config) {
  9. this.doc = doc;
  10. this.styleCnt = 0;
  11. this.defWebfont = config.styles.fontFamily;
  12. this.useWebfont = (config.webfont && config.webfont.use);
  13. this.webfontCfg = config.webfont || [];
  14. this.elStyleSheet = this.getStyleSheet();
  15. },
  16. load: function(content) {
  17. if (!$tx.msie) {
  18. return;
  19. }
  20. if (!content) {
  21. return;
  22. }
  23. if (!this.useWebfont) {
  24. return;
  25. }
  26. var _matchs = [];
  27. content += " // font-family:" + this.defWebfont;
  28. content.replace(/font-family\s*:\s*(\w*)/gi, function(full, name) {
  29. _matchs.push(name);
  30. return full;
  31. });
  32. if (_matchs.length == 0) {
  33. return;
  34. }
  35. var _loader = this;
  36. setTimeout(function() {
  37. var _matchedSource = _matchs.uniq().join("||");
  38. _loader.webfontCfg.options.each(function(item) {
  39. if (item.url && _matchedSource.indexOf(item.data) > -1) {
  40. _loader.imports(item);
  41. }
  42. });
  43. }, 10);
  44. },
  45. getUsed: function() {
  46. if (!$tx.msie) {
  47. return [];
  48. }
  49. var _result = [];
  50. if (!this.useWebfont) {
  51. return _result;
  52. }
  53. this.webfontCfg.options.each(function(item) {
  54. if (!item.url) {
  55. _result.push(item.data);
  56. }
  57. });
  58. return _result;
  59. },
  60. getStyleSheet: function() {
  61. return this.doc.styleSheets[this.styleCnt++];
  62. },
  63. imports: function(item) {
  64. try {
  65. this.elStyleSheet.addImport(item.url, 2);
  66. } catch(e) {
  67. this.elStyleSheet = this.getStyleSheet();
  68. this.elStyleSheet.addImport(item.url, 2);
  69. }
  70. item.url = _NULL;
  71. }
  72. });
  73. })();