Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Do Not Install #2567

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,6 @@ parameters:
count: 1
path: src/PhpWord/Shared/Html.php

-
message: "#^Cannot call method setBorderSize\\(\\) on PhpOffice\\\\PhpWord\\\\Style\\\\Table\\|string\\.$#"
count: 1
path: src/PhpWord/Shared/Html.php

-
message: "#^Cannot call method setStyleName\\(\\) on PhpOffice\\\\PhpWord\\\\Style\\\\Table\\|string\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<exclude>
<directory suffix=".php">./src/PhpWord/Shared/PCLZip</directory>
</exclude>
<report>
<!--report>
<clover outputFile="./build/logs/clover.xml"/>
<html outputDirectory="./build/coverage"/>
</report>
</report-->
</coverage>
<php>
<ini name="error_reporting" value="E_ALL"/>
Expand Down
35 changes: 35 additions & 0 deletions samples/Sample_45_RTLTitles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

include_once 'Sample_Header.php';

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\Html as SharedHtml;

// Suggested by issue #2427.
echo date('H:i:s'), ' Create new PhpWord object', EOL;
$phpWord = new PhpWord();
Settings::setDefaultRtl(true);
$phpWord->setDefaultFontName('DejaVu Sans'); // for good rendition of PDF
$rendererName = Settings::PDF_RENDERER_MPDF;
$rendererLibraryPath = $vendorDirPath . '/mpdf/mpdf';
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);

// Define styles for headers
$phpWord->addTitleStyle(1, ['bold' => true, 'name' => 'Arial', 'size' => 16], []);
//var_dump($x);
$phpWord->addTitleStyle(2, ['bold' => true, 'name' => 'Arial', 'size' => 14], []);
$phpWord->addTitleStyle(3, ['bold' => true, 'name' => 'Arial', 'size' => 12], []);
$phpWord->addTitleStyle(4, ['bold' => true, 'name' => 'Arial', 'size' => 10], []);

// New section
$section = $phpWord->addSection();
$htmlContent = '<h1>مرحبا 1</h1><h2>تجربة 2</h2><h3>تجربة تجربة</h3><h4 dir="rtl">هناك hello هنا 4</h4><p>مرحبا here كلمة انجليزي.</p>';
SharedHtml::addHtml($section, $htmlContent, false, false);

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
Settings::setDefaultRtl(false);
5 changes: 3 additions & 2 deletions src/PhpWord/PhpWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use BadMethodCallException;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Style\Font;

/**
* PHPWord main class.
Expand Down Expand Up @@ -283,9 +284,9 @@ public function setDefaultFontSize($fontSize): void
*
* @return \PhpOffice\PhpWord\Style\Paragraph
*/
public function setDefaultParagraphStyle($styles)
public function setDefaultParagraphStyle($styles, ?Font $fontStyles = null)
{
return Style::setDefaultParagraphStyle($styles);
return Style::setDefaultParagraphStyle($styles, $fontStyles);
}

/**
Expand Down
65 changes: 38 additions & 27 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,35 +747,46 @@ protected function readTableStyle(XMLReader $xmlReader, DOMElement $domNode)
$borders = array_merge($margins, ['insideH', 'insideV']);

if ($xmlReader->elementExists('w:tblPr', $domNode)) {
$tblStyleName = '';
if ($xmlReader->elementExists('w:tblPr/w:tblStyle', $domNode)) {
$style = $xmlReader->getAttribute('w:val', $domNode, 'w:tblPr/w:tblStyle');
} else {
$styleNode = $xmlReader->getElement('w:tblPr', $domNode);
$styleDefs = [];
foreach ($margins as $side) {
$ucfSide = ucfirst($side);
$styleDefs["cellMargin$ucfSide"] = [self::READ_VALUE, "w:tblCellMar/w:$side", 'w:w'];
}
foreach ($borders as $side) {
$ucfSide = ucfirst($side);
$styleDefs["border{$ucfSide}Size"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:sz'];
$styleDefs["border{$ucfSide}Color"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:color'];
$styleDefs["border{$ucfSide}Style"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:val'];
}
$styleDefs['layout'] = [self::READ_VALUE, 'w:tblLayout', 'w:type'];
$styleDefs['bidiVisual'] = [self::READ_TRUE, 'w:bidiVisual'];
$styleDefs['cellSpacing'] = [self::READ_VALUE, 'w:tblCellSpacing', 'w:w'];
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);

$tablePositionNode = $xmlReader->getElement('w:tblpPr', $styleNode);
if ($tablePositionNode !== null) {
$style['position'] = $this->readTablePosition($xmlReader, $tablePositionNode);
}
$tblStyleName = $xmlReader->getAttribute('w:val', $domNode, 'w:tblPr/w:tblStyle');
}
$styleNode = $xmlReader->getElement('w:tblPr', $domNode);
$styleDefs = [];

$indentNode = $xmlReader->getElement('w:tblInd', $styleNode);
if ($indentNode !== null) {
$style['indent'] = $this->readTableIndent($xmlReader, $indentNode);
}
foreach ($margins as $side) {
$ucfSide = ucfirst($side);
$styleDefs["cellMargin$ucfSide"] = [self::READ_VALUE, "w:tblCellMar/w:$side", 'w:w'];
}
foreach ($borders as $side) {
$ucfSide = ucfirst($side);
$styleDefs["border{$ucfSide}Size"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:sz'];
$styleDefs["border{$ucfSide}Color"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:color'];
$styleDefs["border{$ucfSide}Style"] = [self::READ_VALUE, "w:tblBorders/w:$side", 'w:val'];
}
$styleDefs['layout'] = [self::READ_VALUE, 'w:tblLayout', 'w:type'];
$styleDefs['bidiVisual'] = [self::READ_TRUE, 'w:bidiVisual'];
$styleDefs['cellSpacing'] = [self::READ_VALUE, 'w:tblCellSpacing', 'w:w'];
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);

$tablePositionNode = $xmlReader->getElement('w:tblpPr', $styleNode);
if ($tablePositionNode !== null) {
$style['position'] = $this->readTablePosition($xmlReader, $tablePositionNode);
}

$indentNode = $xmlReader->getElement('w:tblInd', $styleNode);
if ($indentNode !== null) {
$style['indent'] = $this->readTableIndent($xmlReader, $indentNode);
}
if ($xmlReader->elementExists('w:basedOn', $domNode)) {
$style['basedOn'] = $xmlReader->getAttribute('w:val', $domNode, 'w:basedOn');
}
if ($tblStyleName !== '') {
$style['tblStyle'] = $tblStyleName;
}
// this may be unneeded
if ($xmlReader->elementExists('w:name', $domNode)) {
$style['styleName'] = $xmlReader->getAttribute('w:val', $domNode, 'w:name');
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/PhpWord/Reader/Word2007/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public function read(PhpWord $phpWord): void
foreach ($nodes as $node) {
$type = $xmlReader->getAttribute('w:type', $node);
$name = $xmlReader->getAttribute('w:val', $node, 'w:name');
$styleId = $xmlReader->getAttribute('w:styleId', $node);
if (null === $name) {
$name = $xmlReader->getAttribute('w:styleId', $node);
$name = $styleId;
}
$headingMatches = [];
preg_match('/Heading\s*(\d)/i', $name, $headingMatches);
Expand Down Expand Up @@ -98,7 +99,8 @@ public function read(PhpWord $phpWord): void
case 'table':
$tStyle = $this->readTableStyle($xmlReader, $node);
if (!empty($tStyle)) {
$phpWord->addTableStyle($name, $tStyle);
$newTable = $phpWord->addTableStyle($styleId, $tStyle);
$newTable->setStyleName($name);
}

break;
Expand Down
5 changes: 5 additions & 0 deletions src/PhpWord/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace PhpOffice\PhpWord;

use PhpOffice\PhpWord\SimpleType\TextDirection;

/**
* PHPWord settings class.
*
Expand Down Expand Up @@ -397,6 +399,9 @@ public static function setDefaultFontSize($value): bool
public static function setDefaultRtl(?bool $defaultRtl): void
{
self::$defaultRtl = $defaultRtl;
if ($defaultRtl === true && Style::getStyle('Normal') === null) {
Style::setDefaultParagraphStyle(['bidi' => true, 'textDirection' => TextDirection::RLTB], ['rtl' => true]);
}
}

public static function isDefaultRtl(): ?bool
Expand Down
Loading
Loading