No Description

Settings.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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;
  18. /**
  19. * PHPWord settings class
  20. *
  21. * @since 0.8.0
  22. */
  23. class Settings
  24. {
  25. /**
  26. * Zip libraries
  27. *
  28. * @const string
  29. */
  30. const ZIPARCHIVE = 'ZipArchive';
  31. const PCLZIP = 'PclZip';
  32. const OLD_LIB = 'PhpOffice\\PhpWord\\Shared\\ZipArchive'; // @deprecated 0.11
  33. /**
  34. * PDF rendering libraries
  35. *
  36. * @const string
  37. */
  38. const PDF_RENDERER_DOMPDF = 'DomPDF';
  39. const PDF_RENDERER_TCPDF = 'TCPDF';
  40. const PDF_RENDERER_MPDF = 'MPDF';
  41. /**
  42. * Measurement units multiplication factor
  43. *
  44. * Applied to:
  45. * - Section: margins, header/footer height, gutter, column spacing
  46. * - Tab: position
  47. * - Indentation: left, right, firstLine, hanging
  48. * - Spacing: before, after
  49. *
  50. * @const string
  51. */
  52. const UNIT_TWIP = 'twip'; // = 1/20 point
  53. const UNIT_CM = 'cm';
  54. const UNIT_MM = 'mm';
  55. const UNIT_INCH = 'inch';
  56. const UNIT_POINT = 'point'; // = 1/72 inch
  57. const UNIT_PICA = 'pica'; // = 1/6 inch = 12 points
  58. /**
  59. * Default font settings
  60. *
  61. * OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
  62. * use, and the conversion will be conducted during XML writing.
  63. */
  64. const DEFAULT_FONT_NAME = 'Arial';
  65. const DEFAULT_FONT_SIZE = 10;
  66. const DEFAULT_FONT_COLOR = '000000';
  67. const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
  68. /**
  69. * Compatibility option for XMLWriter
  70. *
  71. * @var bool
  72. */
  73. private static $xmlWriterCompatibility = true;
  74. /**
  75. * Name of the class used for Zip file management
  76. *
  77. * @var string
  78. */
  79. private static $zipClass = self::ZIPARCHIVE;
  80. /**
  81. * Name of the external Library used for rendering PDF files
  82. *
  83. * @var string
  84. */
  85. private static $pdfRendererName = null;
  86. /**
  87. * Directory Path to the external Library used for rendering PDF files
  88. *
  89. * @var string
  90. */
  91. private static $pdfRendererPath = null;
  92. /**
  93. * Measurement unit
  94. *
  95. * @var int|float
  96. */
  97. private static $measurementUnit = self::UNIT_TWIP;
  98. /**
  99. * Default font name
  100. *
  101. * @var string
  102. */
  103. private static $defaultFontName = self::DEFAULT_FONT_NAME;
  104. /**
  105. * Default font size
  106. * @var int
  107. */
  108. private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
  109. /**
  110. * The user defined temporary directory.
  111. *
  112. * @var string
  113. */
  114. private static $tempDir = '';
  115. /**
  116. * Return the compatibility option used by the XMLWriter
  117. *
  118. * @return bool Compatibility
  119. */
  120. public static function hasCompatibility()
  121. {
  122. return self::$xmlWriterCompatibility;
  123. }
  124. /**
  125. * Set the compatibility option used by the XMLWriter
  126. *
  127. * This sets the setIndent and setIndentString for better compatibility
  128. *
  129. * @param bool $compatibility
  130. * @return bool
  131. */
  132. public static function setCompatibility($compatibility)
  133. {
  134. $compatibility = (bool)$compatibility;
  135. self::$xmlWriterCompatibility = $compatibility;
  136. return true;
  137. }
  138. /**
  139. * Get zip handler class
  140. *
  141. * @return string
  142. */
  143. public static function getZipClass()
  144. {
  145. return self::$zipClass;
  146. }
  147. /**
  148. * Set zip handler class
  149. *
  150. * @param string $zipClass
  151. * @return bool
  152. */
  153. public static function setZipClass($zipClass)
  154. {
  155. if (in_array($zipClass, array(self::PCLZIP, self::ZIPARCHIVE, self::OLD_LIB))) {
  156. self::$zipClass = $zipClass;
  157. return true;
  158. }
  159. return false;
  160. }
  161. /**
  162. * Set details of the external library for rendering PDF files
  163. *
  164. * @param string $libraryName
  165. * @param string $libraryBaseDir
  166. * @return bool Success or failure
  167. */
  168. public static function setPdfRenderer($libraryName, $libraryBaseDir)
  169. {
  170. if (!self::setPdfRendererName($libraryName)) {
  171. return false;
  172. }
  173. return self::setPdfRendererPath($libraryBaseDir);
  174. }
  175. /**
  176. * Return the PDF Rendering Library.
  177. *
  178. * @return string
  179. */
  180. public static function getPdfRendererName()
  181. {
  182. return self::$pdfRendererName;
  183. }
  184. /**
  185. * Identify the external library to use for rendering PDF files
  186. *
  187. * @param string $libraryName
  188. * @return bool
  189. */
  190. public static function setPdfRendererName($libraryName)
  191. {
  192. $pdfRenderers = array(self::PDF_RENDERER_DOMPDF, self::PDF_RENDERER_TCPDF, self::PDF_RENDERER_MPDF);
  193. if (!in_array($libraryName, $pdfRenderers)) {
  194. return false;
  195. }
  196. self::$pdfRendererName = $libraryName;
  197. return true;
  198. }
  199. /**
  200. * Return the directory path to the PDF Rendering Library.
  201. *
  202. * @return string
  203. */
  204. public static function getPdfRendererPath()
  205. {
  206. return self::$pdfRendererPath;
  207. }
  208. /**
  209. * Location of external library to use for rendering PDF files
  210. *
  211. * @param string $libraryBaseDir Directory path to the library's base folder
  212. * @return bool Success or failure
  213. */
  214. public static function setPdfRendererPath($libraryBaseDir)
  215. {
  216. if (false === file_exists($libraryBaseDir) || false === is_readable($libraryBaseDir)) {
  217. return false;
  218. }
  219. self::$pdfRendererPath = $libraryBaseDir;
  220. return true;
  221. }
  222. /**
  223. * Get measurement unit
  224. *
  225. * @return string
  226. */
  227. public static function getMeasurementUnit()
  228. {
  229. return self::$measurementUnit;
  230. }
  231. /**
  232. * Set measurement unit
  233. *
  234. * @param string $value
  235. * @return bool
  236. */
  237. public static function setMeasurementUnit($value)
  238. {
  239. $units = array(self::UNIT_TWIP, self::UNIT_CM, self::UNIT_MM, self::UNIT_INCH,
  240. self::UNIT_POINT, self::UNIT_PICA);
  241. if (!in_array($value, $units)) {
  242. return false;
  243. }
  244. self::$measurementUnit = $value;
  245. return true;
  246. }
  247. /**
  248. * Sets the user defined path to temporary directory.
  249. *
  250. * @since 0.12.0
  251. *
  252. * @param string $tempDir The user defined path to temporary directory.
  253. * @return void
  254. */
  255. public static function setTempDir($tempDir)
  256. {
  257. self::$tempDir = $tempDir;
  258. }
  259. /**
  260. * Returns path to temporary directory.
  261. *
  262. * @since 0.12.0
  263. *
  264. * @return string
  265. */
  266. public static function getTempDir()
  267. {
  268. $tempDir = sys_get_temp_dir();
  269. if (!empty(self::$tempDir)) {
  270. $tempDir = self::$tempDir;
  271. }
  272. return $tempDir;
  273. }
  274. /**
  275. * Get default font name
  276. *
  277. * @return string
  278. */
  279. public static function getDefaultFontName()
  280. {
  281. return self::$defaultFontName;
  282. }
  283. /**
  284. * Set default font name
  285. *
  286. * @param string $value
  287. * @return bool
  288. */
  289. public static function setDefaultFontName($value)
  290. {
  291. if (is_string($value) && trim($value) !== '') {
  292. self::$defaultFontName = $value;
  293. return true;
  294. }
  295. return false;
  296. }
  297. /**
  298. * Get default font size
  299. *
  300. * @return integer
  301. */
  302. public static function getDefaultFontSize()
  303. {
  304. return self::$defaultFontSize;
  305. }
  306. /**
  307. * Set default font size
  308. *
  309. * @param int $value
  310. * @return bool
  311. */
  312. public static function setDefaultFontSize($value)
  313. {
  314. $value = intval($value);
  315. if ($value > 0) {
  316. self::$defaultFontSize = $value;
  317. return true;
  318. }
  319. return false;
  320. }
  321. /**
  322. * Load setting from phpword.yml or phpword.yml.dist
  323. *
  324. * @param string $filename
  325. * @return array
  326. */
  327. public static function loadConfig($filename = null)
  328. {
  329. // Get config file
  330. $configFile = null;
  331. $configPath = __DIR__ . '/../../';
  332. if ($filename !== null) {
  333. $files = array($filename);
  334. } else {
  335. $files = array("{$configPath}phpword.ini", "{$configPath}phpword.ini.dist");
  336. }
  337. foreach ($files as $file) {
  338. if (file_exists($file)) {
  339. $configFile = realpath($file);
  340. break;
  341. }
  342. }
  343. // Parse config file
  344. $config = array();
  345. if ($configFile !== null) {
  346. $config = @parse_ini_file($configFile);
  347. if ($config === false) {
  348. return $config;
  349. }
  350. }
  351. // Set config value
  352. foreach ($config as $key => $value) {
  353. $method = "set{$key}";
  354. if (method_exists(__CLASS__, $method)) {
  355. self::$method($value);
  356. }
  357. }
  358. return $config;
  359. }
  360. /**
  361. * Return the compatibility option used by the XMLWriter
  362. *
  363. * @deprecated 0.10.0
  364. * @codeCoverageIgnore
  365. */
  366. public static function getCompatibility()
  367. {
  368. return self::hasCompatibility();
  369. }
  370. }