Skip to content

Font-style addition: bgColor #145

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

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Classes/PHPWord/Section/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public function __construct($insideOf, $pCount, $style = null)
*
* @param int $height
*/
public function addRow($height = null, $style = null)
public function addRow($height = null, $style = null, $hRules = null)
{
$row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style);
$row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style, $hRules);
$this->_rows[] = $row;
return $row;
}
Expand Down
20 changes: 19 additions & 1 deletion Classes/PHPWord/Section/Table/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class PHPWord_Section_Table_Row
* @var int
*/
private $_height = null;

/**
* Row heightRules
*
* @var array
*/
private $_heightRules = array();

/**
* Row style
Expand Down Expand Up @@ -75,11 +82,12 @@ class PHPWord_Section_Table_Row
* @param int $height
* @param mixed $style
*/
public function __construct($insideOf, $pCount, $height = null, $style = null)
public function __construct($insideOf, $pCount, $height = null, $style = null, $hRules = null)
{
$this->_insideOf = $insideOf;
$this->_pCount = $pCount;
$this->_height = $height;
$this->_heightRules = $hRules;
$this->_style = new PHPWord_Style_Row();

if (!is_null($style)) {
Expand Down Expand Up @@ -138,4 +146,14 @@ public function getHeight()
{
return $this->_height;
}

/**
* Get all row height rules
*
* @return array
*/
public function getHeightRules()
{
return $this->_heightRules;
}
}
30 changes: 29 additions & 1 deletion Classes/PHPWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,13 @@ class PHPWord_Style_Font
* @var string
*/
private $_fgColor = null;


/**
* Background color
*
* @var string
*/
private $_bgColor = null;
/**
* Text line height
*
Expand Down Expand Up @@ -466,6 +472,28 @@ public function setFgColor($pValue = null)
$this->_fgColor = $pValue;
return $this;
}

/**
* Get background color
*
* @return string
*/
public function getBgColor()
{
return $this->_bgColor;
}

/**
* Set background color
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setBgColor($pValue = null)
{
$this->_bgColor = $pValue;
return $this;
}

/**
* Get style type
Expand Down
3 changes: 2 additions & 1 deletion Classes/PHPWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function save($pFilename = null)
// If $pFilename is php://output or php://stdout, make it a temporary file...
$originalFilename = $pFilename;
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam('./', 'phppttmp');
$pFilename = @tempnam(sys_get_temp_dir(), 'phpword_');// temp files should go to system temp directory (if a user cancels a download, the file stays)
if ($pFilename == '') {
$pFilename = $originalFilename;
}
Expand Down Expand Up @@ -193,6 +193,7 @@ public function save($pFilename = null)

// If a temporary file was used, copy it to the correct file stream
if ($originalFilename != $pFilename) {
header('Content-Length: '.filesize($pFilename));// if php://output, we want to know the total file size when downloading
if (copy($pFilename, $originalFilename) === false) {
throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
}
Expand Down
17 changes: 17 additions & 0 deletions Classes/PHPWord/Writer/Word2007/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWor

$objWriter->startElement('w:t');
$objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text
$strText = str_replace(" ", " ", $strText);// line above only preserves spaces before text, this one keeps spaces after text
$objWriter->writeRaw($strText);
$objWriter->endElement();

Expand Down Expand Up @@ -395,6 +396,7 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
$color = $style->getColor();
$size = $style->getSize();
$fgColor = $style->getFgColor();
$bgColor = $style->getBgColor();
$strikethrough = $style->getStrikethrough();
$underline = $style->getUnderline();
$superscript = $style->getSuperScript();
Expand Down Expand Up @@ -464,6 +466,15 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
$objWriter->writeAttribute('w:val', $fgColor);
$objWriter->endElement();
}

// Background-Color
if (!is_null($bgColor)) {
$objWriter->startElement('w:shd');
$objWriter->writeAttribute('w:val', "clear");
$objWriter->writeAttribute('w:color', "auto");
$objWriter->writeAttribute('w:fill', $bgColor);
$objWriter->endElement();
}

// Superscript/subscript
if ($superscript || $subscript) {
Expand Down Expand Up @@ -557,6 +568,7 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
for ($i = 0; $i < $_cRows; $i++) {
$row = $_rows[$i];
$height = $row->getHeight();
$heightRules = $row->getHeightRules();
$rowStyle = $row->getStyle();
$tblHeader = $rowStyle->getTblHeader();
$cantSplit = $rowStyle->getCantSplit();
Expand All @@ -567,6 +579,11 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
$objWriter->startElement('w:trPr');
if (!is_null($height)) {
$objWriter->startElement('w:trHeight');
if(!is_null($heightRules)) {
$objWriter->startAttribute('w:hRule');
$objWriter->text($heightRules);
$objWriter->endAttribute();
}
$objWriter->writeAttribute('w:val', $height);
$objWriter->endElement();
}
Expand Down
88 changes: 41 additions & 47 deletions Classes/PHPWord/_staticDocParts/settings.xml
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main">
<w:zoom w:percent="100" />
<w:embedSystemFonts />
<w:defaultTabStop w:val="708" />
<w:hyphenationZone w:val="425" />
<w:doNotHyphenateCaps />
<w:characterSpacingControl w:val="doNotCompress" />
<w:doNotValidateAgainstSchema />
<w:doNotDemarcateInvalidXml />
<w:compat>
<w:useNormalStyleForList />
<w:doNotUseIndentAsNumberingTabStop />
<w:useAltKinsokuLineBreakRules />
<w:allowSpaceOfSameStyleInTable />
<w:doNotSuppressIndentation />
<w:doNotAutofitConstrainedTables />
<w:autofitToFirstFixedWidthCell />
<w:underlineTabInNumList />
<w:displayHangulFixedWidth />
<w:splitPgBreakAndParaMark />
<w:doNotVertAlignCellWithSp />
<w:doNotBreakConstrainedForcedTable />
<w:doNotVertAlignInTxbx />
<w:useAnsiKerningPairs />
<w:cachedColBalance />
</w:compat>
<m:mathPr>
<m:mathFont m:val="Cambria Math" />
<m:brkBin m:val="before" />
<m:brkBinSub m:val="--" />
<m:smallFrac m:val="off" />
<m:dispDef />
<m:lMargin m:val="0" />
<m:rMargin m:val="0" />
<m:defJc m:val="centerGroup" />
<m:wrapIndent m:val="1440" />
<m:intLim m:val="subSup" />
<m:naryLim m:val="undOvr" />
</m:mathPr>
<w:uiCompat97To2003 />
<w:themeFontLang w:val="de-DE" />
<w:clrSchemeMapping w:bg1="light1" w:t1="dark1" w:bg2="light2" w:t2="dark2" w:accent1="accent1" w:accent2="accent2" w:accent3="accent3" w:accent4="accent4" w:accent5="accent5" w:accent6="accent6" w:hyperlink="hyperlink" w:followedHyperlink="followedHyperlink" />
<w:doNotIncludeSubdocsInStats />
<w:doNotAutoCompressPictures />
<w:decimalSymbol w:val="," />
<w:listSeparator w:val=";" />
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:settings xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" mc:Ignorable="w14">
<w:zoom w:percent="100" />
<w:proofState w:grammar="clean" />
<w:defaultTabStop w:val="708" />
<w:hyphenationZone w:val="425" />
<w:characterSpacingControl w:val="doNotCompress" />
<w:compat>
<w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="14" />
<w:compatSetting w:name="overrideTableStyleFontSizeAndJustification" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
<w:compatSetting w:name="enableOpenTypeFeatures" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
<w:compatSetting w:name="doNotFlipMirrorIndents" w:uri="http://schemas.microsoft.com/office/word" w:val="1" />
</w:compat>
<w:rsids>
<w:rsidRoot w:val="00A82EB6" />
<w:rsid w:val="00726202" />
<w:rsid w:val="00A82EB6" />
</w:rsids>
<m:mathPr>
<m:mathFont m:val="Cambria Math" />
<m:brkBin m:val="before" />
<m:brkBinSub m:val="--" />
<m:smallFrac m:val="0" />
<m:dispDef />
<m:lMargin m:val="0" />
<m:rMargin m:val="0" />
<m:defJc m:val="centerGroup" />
<m:wrapIndent m:val="1440" />
<m:intLim m:val="subSup" />
<m:naryLim m:val="undOvr" />
</m:mathPr>
<w:themeFontLang w:val="fr-CA" />
<w:clrSchemeMapping w:bg1="light1" w:t1="dark1" w:bg2="light2" w:t2="dark2" w:accent1="accent1" w:accent2="accent2" w:accent3="accent3" w:accent4="accent4" w:accent5="accent5" w:accent6="accent6" w:hyperlink="hyperlink" w:followedHyperlink="followedHyperlink" />
<w:shapeDefaults>
<o:shapedefaults v:ext="edit" spidmax="1026" />
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout>
</w:shapeDefaults>
<w:decimalSymbol w:val="," />
<w:listSeparator w:val=";" />
</w:settings>
31 changes: 31 additions & 0 deletions samples/Sample_20_BGColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
// Init
error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once '../Classes/PHPWord.php';

// New Word Document
echo date('H:i:s') , ' Create new PHPWord object' , EOL;
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();

$section->addText("This is some text highlighted using fgColor (limited to 15 colors)", array("fgColor" => PHPWord_Style_Font::FGCOLOR_YELLOW));
$section->addText("This one uses bgColor and is using hex value (0xfbbb10)", array("bgColor" => "fbbb10"));
$section->addText("Compatible with font colors", array("color"=>"0000ff", "bgColor" => "fbbb10"));
$section->addText(" Can highlight space before and after words ", array("color"=>"0000ff", "bgColor" => "fbbb10"));


// Save file
$name = basename(__FILE__, '.php');
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}


// Done
echo date('H:i:s'), " Done writing file(s)", EOL;
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
44 changes: 44 additions & 0 deletions samples/Sample_21_TableRowRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
// Init
error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once '../Classes/PHPWord.php';

// New Word Document
echo date('H:i:s') , ' Create new PHPWord object' , EOL;
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();

$section->addText("By default, a table row adds a textbreak after its content (notice the red border), even if the row height is <= height of the content");

$table1 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0));
$table1->addRow(3750);
$cell1 = $table1->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "ff0000"));
$cell1->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center"));

$section->addTextBreak();
$section->addText("But if we set the row rule \"exact\", we get rid of the textbreak!");

$table2 = $section->addTable(array("cellMargin"=> 0, "cellMarginRight"=> 0, "cellMarginBottom"=> 0, "cellMarginLeft"=> 0));
$table2->addRow(3750, null, "exact");
$cell2 = $table2->addCell(null, array("valign" => "top", "borderSize" => 30, "borderColor" => "00ff00"));
$cell2->addImage("./resources/_earth.jpg", array("width" => 250, "height" => 250, "align" => "center"));

$section->addTextBreak();
$section->addText("In this example, image is 250px height. Rows are calculated in twips, and 1px = 15twips.");
$section->addText("So: $"."table2->addRow(3750, null, 'exact');");

// Save file
$name = basename(__FILE__, '.php');
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}


// Done
echo date('H:i:s'), " Done writing file(s)", EOL;
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;