Font.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_Excel5
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel5_Font
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel5
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel5_Font
  35. {
  36. /**
  37. * BIFF version
  38. *
  39. * @var int
  40. */
  41. private $_BIFFVersion;
  42. /**
  43. * Color index
  44. *
  45. * @var int
  46. */
  47. private $_colorIndex;
  48. /**
  49. * Font
  50. *
  51. * @var PHPExcel_Style_Font
  52. */
  53. private $_font;
  54. /**
  55. * Constructor
  56. *
  57. * @param PHPExcel_Style_Font $font
  58. */
  59. public function __construct(PHPExcel_Style_Font $font = null)
  60. {
  61. $this->_BIFFVersion = 0x0600;
  62. $this->_colorIndex = 0x7FFF;
  63. $this->_font = $font;
  64. }
  65. /**
  66. * Set the color index
  67. *
  68. * @param int $colorIndex
  69. */
  70. public function setColorIndex($colorIndex)
  71. {
  72. $this->_colorIndex = $colorIndex;
  73. }
  74. /**
  75. * Get font record data
  76. *
  77. * @return string
  78. */
  79. public function writeFont()
  80. {
  81. $font_outline = 0;
  82. $font_shadow = 0;
  83. $icv = $this->_colorIndex; // Index to color palette
  84. if ($this->_font->getSuperScript()) {
  85. $sss = 1;
  86. } else if ($this->_font->getSubScript()) {
  87. $sss = 2;
  88. } else {
  89. $sss = 0;
  90. }
  91. $bFamily = 0; // Font family
  92. $bCharSet = PHPExcel_Shared_Font::getCharsetFromFontName($this->_font->getName()); // Character set
  93. $record = 0x31; // Record identifier
  94. $reserved = 0x00; // Reserved
  95. $grbit = 0x00; // Font attributes
  96. if ($this->_font->getItalic()) {
  97. $grbit |= 0x02;
  98. }
  99. if ($this->_font->getStrikethrough()) {
  100. $grbit |= 0x08;
  101. }
  102. if ($font_outline) {
  103. $grbit |= 0x10;
  104. }
  105. if ($font_shadow) {
  106. $grbit |= 0x20;
  107. }
  108. if ($this->_BIFFVersion == 0x0500) {
  109. $data = pack("vvvvvCCCCC",
  110. $this->_font->getSize() * 20,
  111. $grbit,
  112. $icv,
  113. $this->_mapBold($this->_font->getBold()),
  114. $sss,
  115. $this->_mapUnderline($this->_font->getUnderline()),
  116. $bFamily,
  117. $bCharSet,
  118. $reserved,
  119. strlen($this->_font->getName())
  120. );
  121. $data .= $this->_font->getName();
  122. } elseif ($this->_BIFFVersion == 0x0600) {
  123. $data = pack("vvvvvCCCC",
  124. $this->_font->getSize() * 20,
  125. $grbit,
  126. $icv,
  127. $this->_mapBold($this->_font->getBold()),
  128. $sss,
  129. $this->_mapUnderline($this->_font->getUnderline()),
  130. $bFamily,
  131. $bCharSet,
  132. $reserved
  133. );
  134. $data .= PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($this->_font->getName());
  135. }
  136. $length = strlen($data);
  137. $header = pack("vv", $record, $length);
  138. return($header . $data);
  139. }
  140. /**
  141. * Set BIFF version
  142. *
  143. * @param int $BIFFVersion
  144. */
  145. public function setBIFFVersion($BIFFVersion)
  146. {
  147. $this->_BIFFVersion = $BIFFVersion;
  148. }
  149. /**
  150. * Map to BIFF5-BIFF8 codes for bold
  151. *
  152. * @param boolean $bold
  153. * @return int
  154. */
  155. private function _mapBold($bold) {
  156. if ($bold) {
  157. return 0x2BC;
  158. }
  159. return 0x190;
  160. }
  161. /**
  162. * Map underline
  163. *
  164. * @param string
  165. * @return int
  166. */
  167. private function _mapUnderline($underline) {
  168. switch ($underline) {
  169. case PHPExcel_Style_Font::UNDERLINE_NONE: return 0x00;
  170. case PHPExcel_Style_Font::UNDERLINE_SINGLE: return 0x01;
  171. case PHPExcel_Style_Font::UNDERLINE_DOUBLE: return 0x02;
  172. case PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING: return 0x21;
  173. case PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING: return 0x22;
  174. default: return 0x00;
  175. }
  176. }
  177. }