Font.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
  3. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  4. class Font
  5. {
  6. /**
  7. * Color index.
  8. *
  9. * @var int
  10. */
  11. private $colorIndex;
  12. /**
  13. * Font.
  14. *
  15. * @var \PhpOffice\PhpSpreadsheet\Style\Font
  16. */
  17. private $font;
  18. /**
  19. * Constructor.
  20. *
  21. * @param \PhpOffice\PhpSpreadsheet\Style\Font $font
  22. */
  23. public function __construct(\PhpOffice\PhpSpreadsheet\Style\Font $font)
  24. {
  25. $this->colorIndex = 0x7FFF;
  26. $this->font = $font;
  27. }
  28. /**
  29. * Set the color index.
  30. *
  31. * @param int $colorIndex
  32. */
  33. public function setColorIndex($colorIndex)
  34. {
  35. $this->colorIndex = $colorIndex;
  36. }
  37. /**
  38. * Get font record data.
  39. *
  40. * @return string
  41. */
  42. public function writeFont()
  43. {
  44. $font_outline = 0;
  45. $font_shadow = 0;
  46. $icv = $this->colorIndex; // Index to color palette
  47. if ($this->font->getSuperscript()) {
  48. $sss = 1;
  49. } elseif ($this->font->getSubscript()) {
  50. $sss = 2;
  51. } else {
  52. $sss = 0;
  53. }
  54. $bFamily = 0; // Font family
  55. $bCharSet = \PhpOffice\PhpSpreadsheet\Shared\Font::getCharsetFromFontName($this->font->getName()); // Character set
  56. $record = 0x31; // Record identifier
  57. $reserved = 0x00; // Reserved
  58. $grbit = 0x00; // Font attributes
  59. if ($this->font->getItalic()) {
  60. $grbit |= 0x02;
  61. }
  62. if ($this->font->getStrikethrough()) {
  63. $grbit |= 0x08;
  64. }
  65. if ($font_outline) {
  66. $grbit |= 0x10;
  67. }
  68. if ($font_shadow) {
  69. $grbit |= 0x20;
  70. }
  71. $data = pack(
  72. 'vvvvvCCCC',
  73. // Fontsize (in twips)
  74. $this->font->getSize() * 20,
  75. $grbit,
  76. // Colour
  77. $icv,
  78. // Font weight
  79. self::mapBold($this->font->getBold()),
  80. // Superscript/Subscript
  81. $sss,
  82. self::mapUnderline($this->font->getUnderline()),
  83. $bFamily,
  84. $bCharSet,
  85. $reserved
  86. );
  87. $data .= StringHelper::UTF8toBIFF8UnicodeShort($this->font->getName());
  88. $length = strlen($data);
  89. $header = pack('vv', $record, $length);
  90. return $header . $data;
  91. }
  92. /**
  93. * Map to BIFF5-BIFF8 codes for bold.
  94. *
  95. * @param bool $bold
  96. *
  97. * @return int
  98. */
  99. private static function mapBold($bold)
  100. {
  101. if ($bold) {
  102. return 0x2BC; // 700 = Bold font weight
  103. }
  104. return 0x190; // 400 = Normal font weight
  105. }
  106. /**
  107. * Map of BIFF2-BIFF8 codes for underline styles.
  108. *
  109. * @var array of int
  110. */
  111. private static $mapUnderline = [
  112. \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE => 0x00,
  113. \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE => 0x01,
  114. \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE => 0x02,
  115. \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLEACCOUNTING => 0x21,
  116. \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLEACCOUNTING => 0x22,
  117. ];
  118. /**
  119. * Map underline.
  120. *
  121. * @param string $underline
  122. *
  123. * @return int
  124. */
  125. private static function mapUnderline($underline)
  126. {
  127. if (isset(self::$mapUnderline[$underline])) {
  128. return self::$mapUnderline[$underline];
  129. }
  130. return 0x00;
  131. }
  132. }