Font.php 3.6 KB

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