Font.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel5_Font
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel5
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel5_Font
  35. {
  36. /**
  37. * Color index
  38. *
  39. * @var int
  40. */
  41. private $_colorIndex;
  42. /**
  43. * Font
  44. *
  45. * @var PHPExcel_Style_Font
  46. */
  47. private $_font;
  48. /**
  49. * Constructor
  50. *
  51. * @param PHPExcel_Style_Font $font
  52. */
  53. public function __construct(PHPExcel_Style_Font $font = null)
  54. {
  55. $this->_colorIndex = 0x7FFF;
  56. $this->_font = $font;
  57. }
  58. /**
  59. * Set the color index
  60. *
  61. * @param int $colorIndex
  62. */
  63. public function setColorIndex($colorIndex)
  64. {
  65. $this->_colorIndex = $colorIndex;
  66. }
  67. /**
  68. * Get font record data
  69. *
  70. * @return string
  71. */
  72. public function writeFont()
  73. {
  74. $font_outline = 0;
  75. $font_shadow = 0;
  76. $icv = $this->_colorIndex; // Index to color palette
  77. if ($this->_font->getSuperScript()) {
  78. $sss = 1;
  79. } else if ($this->_font->getSubScript()) {
  80. $sss = 2;
  81. } else {
  82. $sss = 0;
  83. }
  84. $bFamily = 0; // Font family
  85. $bCharSet = PHPExcel_Shared_Font::getCharsetFromFontName($this->_font->getName()); // Character set
  86. $record = 0x31; // Record identifier
  87. $reserved = 0x00; // Reserved
  88. $grbit = 0x00; // Font attributes
  89. if ($this->_font->getItalic()) {
  90. $grbit |= 0x02;
  91. }
  92. if ($this->_font->getStrikethrough()) {
  93. $grbit |= 0x08;
  94. }
  95. if ($font_outline) {
  96. $grbit |= 0x10;
  97. }
  98. if ($font_shadow) {
  99. $grbit |= 0x20;
  100. }
  101. $data = pack("vvvvvCCCC",
  102. $this->_font->getSize() * 20, // Fontsize (in twips)
  103. $grbit,
  104. $icv, // Colour
  105. self::_mapBold($this->_font->getBold()), // Font weight
  106. $sss, // Superscript/Subscript
  107. self::_mapUnderline($this->_font->getUnderline()),
  108. $bFamily,
  109. $bCharSet,
  110. $reserved
  111. );
  112. $data .= PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($this->_font->getName());
  113. $length = strlen($data);
  114. $header = pack("vv", $record, $length);
  115. return($header . $data);
  116. }
  117. /**
  118. * Map to BIFF5-BIFF8 codes for bold
  119. *
  120. * @param boolean $bold
  121. * @return int
  122. */
  123. private static function _mapBold($bold) {
  124. if ($bold) {
  125. return 0x2BC; // 700 = Bold font weight
  126. }
  127. return 0x190; // 400 = Normal font weight
  128. }
  129. /**
  130. * Map of BIFF2-BIFF8 codes for underline styles
  131. * @static array of int
  132. *
  133. */
  134. private static $_mapUnderline = array( PHPExcel_Style_Font::UNDERLINE_NONE => 0x00,
  135. PHPExcel_Style_Font::UNDERLINE_SINGLE => 0x01,
  136. PHPExcel_Style_Font::UNDERLINE_DOUBLE => 0x02,
  137. PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING => 0x21,
  138. PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING => 0x22,
  139. );
  140. /**
  141. * Map underline
  142. *
  143. * @param string
  144. * @return int
  145. */
  146. private static function _mapUnderline($underline) {
  147. if (isset(self::$_mapUnderline[$underline]))
  148. return self::$_mapUnderline[$underline];
  149. return 0x00;
  150. }
  151. }