fontfamily.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @fileoverview
  3. * 설정에서 지정된 여러 글꼴들을 선택할 수 있는 메뉴를 포함하는 tool인 '글꼴' Icon을 위한 source로,
  4. * 필요한 configuration과 Class Trex.Tool.FontFamily을/를 포함
  5. *
  6. *
  7. */
  8. TrexMessage.addMsg({
  9. '@fontfamily.gulim': '굴림',
  10. '@fontfamily.batang': '바탕',
  11. '@fontfamily.dotum': '돋움',
  12. '@fontfamily.gungsuh': '궁서'
  13. });
  14. TrexConfig.addTool(
  15. "fontfamily",
  16. {
  17. sync: _TRUE,
  18. status: _TRUE,
  19. useFavorite: _TRUE,
  20. options: [
  21. { label: TXMSG('@fontfamily.gulim')+' (<span class="tx-txt">가나다라</span>)', title: TXMSG('@fontfamily.gulim'), data: 'Gulim,굴림,AppleGothic,sans-serif', klass: 'tx-gulim' },
  22. { label: TXMSG('@fontfamily.batang')+' (<span class="tx-txt">가나다라</span>)', title: TXMSG('@fontfamily.batang'), data: 'Batang,바탕,serif', klass: 'tx-batang' },
  23. { label: TXMSG('@fontfamily.dotum')+' (<span class="tx-txt">가나다라</span>)', title: TXMSG('@fontfamily.dotum'), data: 'Dotum,돋움,sans-serif', klass: 'tx-dotum' },
  24. { label: TXMSG('@fontfamily.gungsuh')+' (<span class="tx-txt">가나다라</span>)', title: TXMSG('@fontfamily.gungsuh'), data: 'Gungsuh,궁서,serif', klass: 'tx-gungseo' },
  25. { label: 'Arial (<span class="tx-txt">abcde</span>)', title: 'Arial', data: 'Arial,sans-serif', klass: 'tx-arial' },
  26. { label: 'Verdana (<span class="tx-txt">abcde</span>)', title: 'Verdana', data: 'Verdana,sans-serif', klass: 'tx-verdana' },
  27. { label: 'Courier New (<span class="tx-txt">abcde</span>)', title: 'Courier New', data: 'Courier New,monspace', klass: 'tx-courier-new' }
  28. ]
  29. }
  30. );
  31. /* legacy fontfamily *
  32. { label: ' 굴림 (<span class="tx-txt">가나다라</span>)', title: '굴림', data: 'Gulim,굴림,AppleGothic,sans-serif', klass: 'tx-gulim' },
  33. { label: ' 바탕 (<span class="tx-txt">가나다라</span>)', title: '바탕', data: 'Batang,바탕', klass: 'tx-batang' },
  34. { label: ' 돋움 (<span class="tx-txt">가나다라</span>)', title: '돋움', data: 'Dotum,돋움', klass: 'tx-dotum' },
  35. { label: ' 궁서 (<span class="tx-txt">가나다라</span>)', title: '궁서', data: 'Gungsuh,궁서', klass: 'tx-gungseo' },
  36. { label: ' Arial (<span class="tx-txt">abcde</span>)', title: 'Arial', data: 'Arial', klass: 'tx-arial' },
  37. { label: ' Verdana (<span class="tx-txt">abcde</span>)', title: 'Verdana', data: 'Verdana', klass: 'tx-verdana' },
  38. { label: ' Arial Black (<span class="tx-txt">abcde</span>)', title: 'Arial Black', data: 'Arial Black', klass: 'tx-arial-black' },
  39. { label: ' Book Antiqua (<span class="tx-txt">abcde</span>)', title: 'Book Antiqua', data: 'Book Antiqua', klass: 'tx-book-antiqua' },
  40. { label: ' Comic Sans MS (<span class="tx-txt">abcde</span>)', title: 'Comic Sans MS', data: 'Comic Sans MS', klass: 'tx-comic-sans-ms' },
  41. { label: ' Courier New (<span class="tx-txt">abcde</span>)', title: 'Courier New', data: 'Courier New', klass: 'tx-courier-new' },
  42. { label: ' Georgia (<span class="tx-txt">abcde</span>)', title: 'Georgia', data: 'Georgia', klass: 'tx-georgia' },
  43. { label: ' Helvetica (<span class="tx-txt">abcde</span>)', title: 'Helvetica', data: 'Helvetica', klass: 'tx-helvetica' },
  44. { label: ' Impact (<span class="tx-txt">abcde</span>)', title: 'Impact', data: 'Impact', klass: 'tx-impact' },
  45. { label: ' Symbol (<span class="tx-txt">abcde</span>)', title: 'Symbol', data: 'Symbol', klass: 'tx-symbol' },
  46. { label: ' Tahoma (<span class="tx-txt">abcde</span>)', title: 'Tahoma', data: 'Tahoma', klass: 'tx-tahoma' },
  47. { label: ' Terminal (<span class="tx-txt">abcde</span>)', title: 'Terminal', data: 'Terminal', klass: 'tx-terminal' },
  48. { label: ' Times New Roman (<span class="tx-txt">abcde</span>)', title: 'Times New R..', data: 'Times New Roman', klass: 'tx-times-new-roman' },
  49. { label: ' Trebuchet MS (<span class="tx-txt">abcde</span>)', title: 'Trebuchet MS', data: 'Trebuchet MS', klass: 'tx-trebuchet-ms' },
  50. { label: ' Webdings (<span class="tx-txt">abcde</span>)', title: 'Webdings', data: 'Webdings', klass: 'tx-webdings' },
  51. { label: ' Wingdings (<span class="tx-txt">abcde</span>)', title: 'Wingdings', data: 'Wingdings', klass: 'tx-wingdings' }
  52. */
  53. Trex.Tool.FontFamily = Trex.Class.create({
  54. $const: {
  55. __Identity: 'fontfamily'
  56. },
  57. $extend: Trex.Tool,
  58. $mixins: [Trex.I.CookieBaker, Trex.I.FontTool, Trex.I.MenuFontTool, Trex.I.WrappingSpanFontTool],
  59. beforeOnInitialized: function(config) {
  60. function findAvailableFonts(config) {
  61. self.usedWebFonts = (($tx.msie && config.webfont && config.webfont.use) ? config.webfont.options : []);
  62. self.usedFonts = config.options.concat(self.usedWebFonts);
  63. }
  64. function setUseFavoriteFont(config) {
  65. if (config.useFavorite && self.usedWebFonts.length > 0) {
  66. self.useFavorite = _TRUE;
  67. self.initCookie('txFontFamilyFavorite');
  68. } else {
  69. self.useFavorite = _FALSE;
  70. }
  71. }
  72. var self = this;
  73. self.focusLoosed = _FALSE;
  74. findAvailableFonts(config);
  75. setUseFavoriteFont(config);
  76. self.createFontFamilyMap(self.usedFonts);
  77. },
  78. createFontFamilyMap: function(usedFonts) {
  79. var self = this, fontFamilyMap = {};
  80. this.fontFamilyMap = fontFamilyMap;
  81. usedFonts.each(function (option) {
  82. var fontNames, title, i, key;
  83. fontNames = option.data.split(",");
  84. title = option.title;
  85. for (i = 0; i < fontNames.length; i += 1) {
  86. key = self.preprocessFontFamily(fontNames[i]);
  87. fontFamilyMap[key] = title;
  88. }
  89. if (!fontFamilyMap[title.toLowerCase()]) {
  90. fontFamilyMap[title.toLowerCase()] = title;
  91. }
  92. });
  93. },
  94. createButton: function() {
  95. var button = new Trex.Button.Select(this.buttonCfg);
  96. this.button = button;
  97. button.setValue(this.getDefaultProperty());
  98. button.setText(this.getTextByValue(this.getDefaultProperty()));
  99. return button;
  100. },
  101. createMenu: function() {
  102. var self = this;
  103. var menu = new Trex.Menu.Select(TrexConfig.merge(self.menuCfg, {
  104. options: self.usedFonts
  105. }));
  106. this.menu = menu;
  107. //overwrite generateListItem
  108. menu.generateListItem = function (option) {
  109. var result = [], i, item, labalBackup;
  110. for(i = 0; i < option.length; i += 1) {
  111. item = option[i];
  112. labalBackup = item.label;
  113. item.label = item.label.replace(/<span class="tx\-txt">/, '<span class="tx-txt" style="font-family:' + item.data + ';">');
  114. result.push(Trex.MarkupTemplate.get("menu.select.item").evaluate(item));
  115. item.label = labalBackup;
  116. }
  117. return result.join("");
  118. };
  119. if (self.usedWebFonts.length > 0) {
  120. $tx.addClassName(menu.elMenu, "tx-fontfamily-webfont-menu");
  121. var elDummyForFocus = tx.input({'type': 'text', 'className': 'tx-dummyfocus'});
  122. $tom.append(menu.elMenu, elDummyForFocus);
  123. $tx.observe(menu.elMenu, 'mousedown', function(ev) {
  124. if (ev.offsetX < self.menu.elMenu.clientWidth) { //not scrollbar
  125. return;
  126. }
  127. elDummyForFocus.style.top = ev.offsetY.toPx();
  128. if (!self.focusLoosed) {
  129. elDummyForFocus.focus();
  130. elDummyForFocus.blur();
  131. self.menu.elMenu.focus();
  132. self.focusLoosed = _TRUE;
  133. }
  134. });
  135. }
  136. return menu;
  137. },
  138. menuInitHandler: function() {
  139. var self = this;
  140. var menu = self.menu;
  141. self.focusLoosed = _FALSE;
  142. if(!self.useFavorite) {
  143. return [];
  144. }
  145. menu.elMenu.scrollTop = 0;
  146. var elGroup = $tom.collect(menu.elMenu, "ul.tx-menu-favlist");
  147. if(elGroup) {
  148. $tom.remove(elGroup);
  149. }
  150. var favorite = self.extractOptions(self.usedFonts, self.readCookie());
  151. elGroup = menu.generateList(favorite);
  152. $tom.insertFirst(menu.elMenu, elGroup);
  153. $tx.addClassName(elGroup, 'tx-menu-favlist');
  154. return favorite;
  155. },
  156. onBeforeHandler: function(data) {
  157. this.canvas.includeWebfontCss("font-family: " + data);
  158. },
  159. onAfterHandler: function(data) {
  160. var self = this;
  161. if (self.useFavorite) {
  162. self.writeCookie(self.mergeValues(self.readCookie(), data));
  163. }
  164. },
  165. getDefaultProperty: function() {
  166. return this.canvas.getStyleConfig().fontFamily;
  167. },
  168. getRelatedCssPropertyNames: function() {
  169. return ["font", this.getCssPropertyName()];
  170. },
  171. getCssPropertyName: function() {
  172. return "fontFamily";
  173. },
  174. getQueryCommandName: function() {
  175. return "fontname";
  176. },
  177. getFontTagAttribute: function() {
  178. return "face";
  179. },
  180. preprocessFontFamily: function (name) {
  181. //브라우저에 따라 qoute 등으로 감싸주는 경우가 있음.
  182. //폰트 이름중에 _9 나 9 로 끝나는게 있어 문제가 있었다고 주석이 있었음.
  183. return name.toLowerCase().replace(/'|"/g, "").replace(/_?9$/, "");
  184. },
  185. getTextByValue: function(value) {
  186. if (value.include(",")) {
  187. value = value.split(",")[0];
  188. }
  189. value = this.preprocessFontFamily(value);
  190. return this.fontFamilyMap[value];
  191. }
  192. });