PHPWord/src/PhpWord/Settings.php

484 lines
11 KiB
PHP
Raw Normal View History

2014-03-13 00:17:48 -06:00
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
2014-03-13 00:17:48 -06:00
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2014-03-13 00:17:48 -06:00
*/
namespace PhpOffice\PhpWord;
/**
2022-09-16 11:45:45 +02:00
* PHPWord settings class.
*
* @since 0.8.0
*/
class Settings
{
/**
2022-09-16 11:45:45 +02:00
* Zip libraries.
2014-05-02 01:15:28 +07:00
*
* @const string
*/
const ZIPARCHIVE = 'ZipArchive';
const PCLZIP = 'PclZip';
2022-09-16 14:45:25 +02:00
const OLD_LIB = \PhpOffice\PhpWord\Shared\ZipArchive::class; // @deprecated 0.11
/**
2022-09-16 11:45:45 +02:00
* PDF rendering libraries.
2014-05-02 01:15:28 +07:00
*
* @const string
*/
2014-04-13 23:17:39 +07:00
const PDF_RENDERER_DOMPDF = 'DomPDF';
const PDF_RENDERER_TCPDF = 'TCPDF';
const PDF_RENDERER_MPDF = 'MPDF';
2014-04-13 23:17:39 +07:00
/**
2022-09-16 11:45:45 +02:00
* Measurement units multiplication factor.
*
* Applied to:
* - Section: margins, header/footer height, gutter, column spacing
* - Tab: position
* - Indentation: left, right, firstLine, hanging
* - Spacing: before, after
*
* @const string
*/
const UNIT_TWIP = 'twip'; // = 1/20 point
const UNIT_CM = 'cm';
const UNIT_MM = 'mm';
const UNIT_INCH = 'inch';
const UNIT_POINT = 'point'; // = 1/72 inch
const UNIT_PICA = 'pica'; // = 1/6 inch = 12 points
/**
2022-09-16 11:45:45 +02:00
* Default font settings.
*
* OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
* use, and the conversion will be conducted during XML writing.
*/
const DEFAULT_FONT_NAME = 'Arial';
const DEFAULT_FONT_SIZE = 10;
const DEFAULT_FONT_COLOR = '000000';
const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
const DEFAULT_PAPER = 'A4';
2014-03-13 00:17:48 -06:00
/**
2022-09-16 11:45:45 +02:00
* Compatibility option for XMLWriter.
2014-03-13 00:17:48 -06:00
*
* @var bool
2014-03-13 00:17:48 -06:00
*/
private static $xmlWriterCompatibility = true;
2014-03-13 00:17:48 -06:00
/**
2022-09-16 11:45:45 +02:00
* Name of the class used for Zip file management.
*
* @var string
*/
private static $zipClass = self::ZIPARCHIVE;
2014-04-13 23:17:39 +07:00
/**
2022-09-16 11:45:45 +02:00
* Name of the external Library used for rendering PDF files.
2014-04-13 23:17:39 +07:00
*
* @var string
*/
2022-09-16 11:45:45 +02:00
private static $pdfRendererName;
2014-04-13 23:17:39 +07:00
/**
2022-09-16 11:45:45 +02:00
* Directory Path to the external Library used for rendering PDF files.
2014-04-13 23:17:39 +07:00
*
* @var string
*/
2022-09-16 11:45:45 +02:00
private static $pdfRendererPath;
2014-04-13 23:17:39 +07:00
/**
2022-09-16 11:45:45 +02:00
* Measurement unit.
*
2022-09-16 11:45:45 +02:00
* @var float|int
*/
private static $measurementUnit = self::UNIT_TWIP;
/**
2022-09-16 11:45:45 +02:00
* Default font name.
*
* @var string
*/
private static $defaultFontName = self::DEFAULT_FONT_NAME;
/**
2022-09-16 11:45:45 +02:00
* Default font size.
*
* @var int
*/
private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
/**
2022-09-16 11:45:45 +02:00
* Default paper.
*
* @var string
*/
private static $defaultPaper = self::DEFAULT_PAPER;
/**
* The user defined temporary directory.
*
* @var string
*/
private static $tempDir = '';
2016-06-04 20:06:37 +04:00
/**
* Enables built-in output escaping mechanism.
* Default value is `false` for backward compatibility with versions below 0.13.0.
*
* @var bool
*/
private static $outputEscapingEnabled = false;
2014-05-04 15:13:31 +07:00
/**
2022-09-16 11:45:45 +02:00
* Return the compatibility option used by the XMLWriter.
2014-05-04 15:13:31 +07:00
*
* @return bool Compatibility
*/
public static function hasCompatibility()
{
return self::$xmlWriterCompatibility;
}
2014-03-13 00:17:48 -06:00
/**
2022-09-16 11:45:45 +02:00
* Set the compatibility option used by the XMLWriter.
2014-03-13 00:17:48 -06:00
*
* This sets the setIndent and setIndentString for better compatibility
*
* @param bool $compatibility
2022-09-16 11:45:45 +02:00
*
2014-05-16 16:00:40 +07:00
* @return bool
2014-03-13 00:17:48 -06:00
*/
public static function setCompatibility($compatibility)
{
$compatibility = (bool) $compatibility;
2014-05-16 14:44:56 +07:00
self::$xmlWriterCompatibility = $compatibility;
2014-05-16 14:44:56 +07:00
return true;
}
2014-03-13 00:17:48 -06:00
/**
2022-09-16 11:45:45 +02:00
* Get zip handler class.
2014-03-13 00:17:48 -06:00
*
2014-05-04 15:13:31 +07:00
* @return string
2014-03-13 00:17:48 -06:00
*/
2014-05-04 15:13:31 +07:00
public static function getZipClass()
{
2014-05-04 15:13:31 +07:00
return self::$zipClass;
}
/**
2022-09-16 11:45:45 +02:00
* Set zip handler class.
*
* @param string $zipClass
2022-09-16 11:45:45 +02:00
*
* @return bool
*/
public static function setZipClass($zipClass)
{
2022-09-16 11:45:45 +02:00
if (in_array($zipClass, [self::PCLZIP, self::ZIPARCHIVE, self::OLD_LIB])) {
self::$zipClass = $zipClass;
return true;
}
return false;
2014-04-13 23:17:39 +07:00
}
2014-04-13 23:17:39 +07:00
/**
2022-09-16 11:45:45 +02:00
* Set details of the external library for rendering PDF files.
2014-04-13 23:17:39 +07:00
*
* @param string $libraryName
* @param string $libraryBaseDir
2022-09-16 11:45:45 +02:00
*
* @return bool Success or failure
2014-04-13 23:17:39 +07:00
*/
public static function setPdfRenderer($libraryName, $libraryBaseDir)
{
if (!self::setPdfRendererName($libraryName)) {
return false;
}
return self::setPdfRendererPath($libraryBaseDir);
}
/**
* Return the PDF Rendering Library.
*
* @return string
2014-04-13 23:17:39 +07:00
*/
public static function getPdfRendererName()
{
return self::$pdfRendererName;
}
/**
2022-09-16 11:45:45 +02:00
* Identify the external library to use for rendering PDF files.
2014-04-13 23:17:39 +07:00
*
* @param string $libraryName
2022-09-16 11:45:45 +02:00
*
* @return bool
2014-04-13 23:17:39 +07:00
*/
public static function setPdfRendererName($libraryName)
{
2022-09-16 11:45:45 +02:00
$pdfRenderers = [self::PDF_RENDERER_DOMPDF, self::PDF_RENDERER_TCPDF, self::PDF_RENDERER_MPDF];
if (!in_array($libraryName, $pdfRenderers)) {
2014-04-13 23:17:39 +07:00
return false;
}
self::$pdfRendererName = $libraryName;
return true;
}
/**
* Return the directory path to the PDF Rendering Library.
*
* @return string
2014-04-13 23:17:39 +07:00
*/
public static function getPdfRendererPath()
{
return self::$pdfRendererPath;
}
/**
2022-09-16 11:45:45 +02:00
* Location of external library to use for rendering PDF files.
2014-04-13 23:17:39 +07:00
*
* @param string $libraryBaseDir Directory path to the library's base folder
2022-09-16 11:45:45 +02:00
*
* @return bool Success or failure
2014-04-13 23:17:39 +07:00
*/
public static function setPdfRendererPath($libraryBaseDir)
{
if (!$libraryBaseDir || false === file_exists($libraryBaseDir) || false === is_readable($libraryBaseDir)) {
2014-04-13 23:17:39 +07:00
return false;
}
self::$pdfRendererPath = $libraryBaseDir;
return true;
}
/**
2022-09-16 11:45:45 +02:00
* Get measurement unit.
*
* @return string
*/
public static function getMeasurementUnit()
{
return self::$measurementUnit;
}
/**
2022-09-16 11:45:45 +02:00
* Set measurement unit.
*
* @param string $value
2022-09-16 11:45:45 +02:00
*
* @return bool
*/
public static function setMeasurementUnit($value)
{
2022-09-16 11:45:45 +02:00
$units = [self::UNIT_TWIP, self::UNIT_CM, self::UNIT_MM, self::UNIT_INCH,
self::UNIT_POINT, self::UNIT_PICA, ];
if (!in_array($value, $units)) {
return false;
}
self::$measurementUnit = $value;
return true;
}
2014-05-04 15:13:31 +07:00
/**
* Sets the user defined path to temporary directory.
*
* @since 0.12.0
*
* @param string $tempDir The user defined path to temporary directory
*/
2022-09-16 11:45:45 +02:00
public static function setTempDir($tempDir): void
{
self::$tempDir = $tempDir;
}
/**
* Returns path to temporary directory.
*
* @since 0.12.0
*
* @return string
*/
public static function getTempDir()
{
if (!empty(self::$tempDir)) {
$tempDir = self::$tempDir;
} else {
$tempDir = sys_get_temp_dir();
}
2017-11-22 09:02:16 +01:00
return $tempDir;
}
2016-06-04 20:06:37 +04:00
/**
* @since 0.13.0
*
* @return bool
2016-06-04 20:06:37 +04:00
*/
public static function isOutputEscapingEnabled()
{
return self::$outputEscapingEnabled;
}
/**
* @since 0.13.0
*
* @param bool $outputEscapingEnabled
2016-06-04 20:06:37 +04:00
*/
2022-09-16 11:45:45 +02:00
public static function setOutputEscapingEnabled($outputEscapingEnabled): void
2016-06-04 20:06:37 +04:00
{
self::$outputEscapingEnabled = $outputEscapingEnabled;
}
/**
2022-09-16 11:45:45 +02:00
* Get default font name.
*
* @return string
*/
public static function getDefaultFontName()
{
return self::$defaultFontName;
}
/**
2022-09-16 11:45:45 +02:00
* Set default font name.
*
* @param string $value
2022-09-16 11:45:45 +02:00
*
* @return bool
*/
public static function setDefaultFontName($value)
{
if (is_string($value) && trim($value) !== '') {
self::$defaultFontName = $value;
return true;
}
return false;
}
/**
2022-09-16 11:45:45 +02:00
* Get default font size.
*
* @return int
*/
public static function getDefaultFontSize()
{
return self::$defaultFontSize;
}
/**
2022-09-16 11:45:45 +02:00
* Set default font size.
*
* @param int $value
2022-09-16 11:45:45 +02:00
*
* @return bool
*/
public static function setDefaultFontSize($value)
{
$value = (int) $value;
if ($value > 0) {
self::$defaultFontSize = $value;
return true;
}
return false;
}
/**
2022-09-16 11:45:45 +02:00
* Load setting from phpword.yml or phpword.yml.dist.
*
* @param string $filename
2022-09-16 11:45:45 +02:00
*
* @return array
*/
public static function loadConfig($filename = null)
{
// Get config file
$configFile = null;
$configPath = __DIR__ . '/../../';
2014-05-16 14:44:56 +07:00
if ($filename !== null) {
2022-09-16 11:45:45 +02:00
$files = [$filename];
2014-05-16 14:44:56 +07:00
} else {
2022-09-16 11:45:45 +02:00
$files = ["{$configPath}phpword.ini", "{$configPath}phpword.ini.dist"];
2014-05-16 14:44:56 +07:00
}
foreach ($files as $file) {
if (file_exists($file)) {
$configFile = realpath($file);
2022-09-16 11:45:45 +02:00
break;
}
}
2014-05-16 14:44:56 +07:00
// Parse config file
2022-09-16 11:45:45 +02:00
$config = [];
2014-05-16 14:44:56 +07:00
if ($configFile !== null) {
2014-05-24 23:07:34 +07:00
$config = @parse_ini_file($configFile);
2014-05-16 14:44:56 +07:00
if ($config === false) {
2022-09-16 11:45:45 +02:00
return [];
2014-05-16 14:44:56 +07:00
}
}
// Set config value
2022-09-16 11:45:45 +02:00
$appliedConfig = [];
foreach ($config as $key => $value) {
$method = "set{$key}";
if (method_exists(__CLASS__, $method)) {
self::$method($value);
$appliedConfig[$key] = $value;
}
}
return $appliedConfig;
}
/**
2022-09-16 11:45:45 +02:00
* Get default paper.
*
* @return string
*/
public static function getDefaultPaper()
{
return self::$defaultPaper;
}
/**
2022-09-16 11:45:45 +02:00
* Set default paper.
*
* @param string $value
2022-09-16 11:45:45 +02:00
*
* @return bool
*/
public static function setDefaultPaper($value)
{
if (is_string($value) && trim($value) !== '') {
self::$defaultPaper = $value;
return true;
}
return false;
}
}