Ei kuvausta

AbstractStyle.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\Word2007\Style;
  18. use PhpOffice\PhpWord\Settings;
  19. use PhpOffice\PhpWord\Shared\XMLWriter;
  20. /**
  21. * Style writer
  22. *
  23. * @since 0.10.0
  24. */
  25. abstract class AbstractStyle
  26. {
  27. /**
  28. * XML writer
  29. *
  30. * @var \PhpOffice\PhpWord\Shared\XMLWriter
  31. */
  32. private $xmlWriter;
  33. /**
  34. * Style; set protected for a while
  35. *
  36. * @var string|\PhpOffice\PhpWord\Style\AbstractStyle
  37. */
  38. protected $style;
  39. /**
  40. * Write style
  41. */
  42. abstract public function write();
  43. /**
  44. * Create new instance.
  45. *
  46. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  47. * @param string|\PhpOffice\PhpWord\Style\AbstractStyle $style
  48. */
  49. public function __construct(XMLWriter $xmlWriter, $style = null)
  50. {
  51. $this->xmlWriter = $xmlWriter;
  52. $this->style = $style;
  53. }
  54. /**
  55. * Get XML Writer
  56. *
  57. * @return \PhpOffice\PhpWord\Shared\XMLWriter
  58. */
  59. protected function getXmlWriter()
  60. {
  61. return $this->xmlWriter;
  62. }
  63. /**
  64. * Get Style
  65. *
  66. * @return \PhpOffice\PhpWord\Style\AbstractStyle
  67. */
  68. protected function getStyle()
  69. {
  70. return $this->style;
  71. }
  72. /**
  73. * Convert twip value
  74. *
  75. * @param int|float $value
  76. * @param int $default (int|float)
  77. * @return int|float
  78. */
  79. protected function convertTwip($value, $default = 0)
  80. {
  81. $factors = array(
  82. Settings::UNIT_CM => 567,
  83. Settings::UNIT_MM => 56.7,
  84. Settings::UNIT_INCH => 1440,
  85. Settings::UNIT_POINT => 20,
  86. Settings::UNIT_PICA => 240,
  87. );
  88. $unit = Settings::getMeasurementUnit();
  89. $factor = 1;
  90. if (in_array($unit, $factors) && $value != $default) {
  91. $factor = $factors[$unit];
  92. }
  93. return $value * $factor;
  94. }
  95. /**
  96. * Write child style.
  97. *
  98. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  99. * @param string $name
  100. * @param mixed $value
  101. * @return void
  102. */
  103. protected function writeChildStyle(XMLWriter $xmlWriter, $name, $value)
  104. {
  105. if ($value !== null) {
  106. $class = "PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\" . $name;
  107. /** @var \PhpOffice\PhpWord\Writer\Word2007\Style\AbstractStyle $writer */
  108. $writer = new $class($xmlWriter, $value);
  109. $writer->write();
  110. }
  111. }
  112. /**
  113. * Assemble style array into style string
  114. *
  115. * @param array $styles
  116. * @return string
  117. */
  118. protected function assembleStyle($styles = array())
  119. {
  120. $style = '';
  121. foreach ($styles as $key => $value) {
  122. if (!is_null($value) && $value != '') {
  123. $style .= "{$key}:{$value}; ";
  124. }
  125. }
  126. return trim($style);
  127. }
  128. }