Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/brunocasado/PHPWord into …
Browse files Browse the repository at this point in the history
…'develop'
  • Loading branch information
ivanlanin committed Apr 13, 2014
2 parents 9bb5655 + ad58e72 commit 89f4288
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 109 deletions.
15 changes: 13 additions & 2 deletions samples/Sample_13_Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@
$section->addText('Local image without any styles:');
$section->addImage('resources/_mars.jpg');
$section->addTextBreak(2);
//

$section->addText('Local image with styles:');
$section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'align' => 'center'));
$section->addTextBreak(2);

// Remote image
$source = 'http://php.net/images/logos/php-med-trans-light.gif';
$section->addText("Remote image from: {$source}");
$section->addImage($source);
// End code

//Wrapping style
$text = str_repeat('Hello World! ', 15);
$wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');
foreach ($wrappingStyles as $wrappingStyle) {
$section->addTextBreak(5);
$section->addText('Wrapping style ' . $wrappingStyle);
$section->addImage('resources/_earth.jpg', array('marginTop' => -1, 'marginLeft' => 1,
'width' => 80, 'height' => 80, 'wrappingStyle' => $wrappingStyle));
$section->addText($text);
}

// Save file
$name = basename(__FILE__, '.php');
Expand Down
34 changes: 18 additions & 16 deletions src/PhpWord/Writer/Word2007/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,25 +427,22 @@ protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = fa
$marginTop = $style->getMarginTop();
$marginLeft = $style->getMarginLeft();
$wrappingStyle = $style->getWrappingStyle();
$w10wrapType = null;

if (!$withoutP) {
$xmlWriter->startElement('w:p');

if (!is_null($align)) {
$xmlWriter->startElement('w:pPr');
$xmlWriter->startElement('w:jc');
$xmlWriter->writeAttribute('w:val', $align);
$xmlWriter->endElement();
$xmlWriter->endElement();
$xmlWriter->endElement(); // w:jc
$xmlWriter->endElement(); // w:pPr
}
}
$xmlWriter->startElement('w:r');

$xmlWriter->startElement('w:pict');

$xmlWriter->startElement('v:shape');
$xmlWriter->writeAttribute('type', '#_x0000_t75');

$imgStyle = '';
if (null !== $width) {
$imgStyle .= 'width:' . $width . 'px;';
Expand All @@ -459,33 +456,38 @@ protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = fa
if (null !== $marginLeft) {
$imgStyle .= 'margin-left:' . $marginLeft . 'in;';
}

switch ($wrappingStyle) {
case ImageStyle::WRAPPING_STYLE_BEHIND:
$imgStyle .= 'position:absolute;z-index:-251658752;';
break;
case ImageStyle::WRAPPING_STYLE_INFRONT:
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
break;
case ImageStyle::WRAPPING_STYLE_SQUARE:
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
$w10wrapType = 'square';
break;
case ImageStyle::WRAPPING_STYLE_TIGHT:
$imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute';
break;
case ImageStyle::WRAPPING_STYLE_INFRONT:
$imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
$w10wrapType = 'tight';
break;
}

$xmlWriter->writeAttribute('style', $imgStyle);

$xmlWriter->startElement('v:imagedata');
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$xmlWriter->writeAttribute('o:title', '');
$xmlWriter->endElement();
$xmlWriter->endElement();
$xmlWriter->endElement(); // v:imagedata

$xmlWriter->endElement();
if (!is_null($w10wrapType)) {
$xmlWriter->startElement('w10:wrap');
$xmlWriter->writeAttribute('type', $w10wrapType);
$xmlWriter->endElement(); // w10:wrap
}

$xmlWriter->endElement();
$xmlWriter->endElement(); // v:shape
$xmlWriter->endElement(); // w:pict
$xmlWriter->endElement(); // w:r

if (!$withoutP) {
$xmlWriter->endElement(); // w:p
Expand Down
171 changes: 80 additions & 91 deletions tests/PhpWord/Tests/Writer/Word2007/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function tearDown()
}

/**
* covers ::_writeText
* Test write text element
*/
public function testWriteText()
{
Expand All @@ -49,7 +49,7 @@ public function testWriteText()
}

/**
* covers ::_writeTextRun
* Test write textrun element
*/
public function testWriteTextRun()
{
Expand All @@ -74,7 +74,7 @@ public function testWriteTextRun()
}

/**
* covers ::_writeLink
* Test write link element
*/
public function testWriteLink()
{
Expand All @@ -97,7 +97,7 @@ public function testWriteLink()
}

/**
* covers ::_writePreserveText
* Test write preserve text element
*/
public function testWritePreserveText()
{
Expand All @@ -121,7 +121,7 @@ public function testWritePreserveText()
}

/**
* covers ::_writeTextBreak
* Test write text break
*/
public function testWriteTextBreak()
{
Expand All @@ -146,30 +146,93 @@ public function testWriteTextBreak()
}

/**
* covers ::_writeParagraphStyle
* covers ::_writeImage
*/
public function testWriteParagraphStyleAlign()
public function testWriteImage()
{
$phpWord = new PhpWord();
$styles = array('align' => 'left', 'width' => 40, 'height' => 40, 'marginTop' => -1, 'marginLeft' => -1);
$wraps = array('inline', 'behind', 'infront', 'square', 'tight');
$section = $phpWord->addSection();
foreach ($wraps as $wrap) {
$styles['wrappingStyle'] = $wrap;
$section->addImage(__DIR__ . "/../../_files/images/earth.jpg", $styles);
}

$doc = TestHelperDOCX::getDocument($phpWord);

// behind
$element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape');
$style = $element->getAttribute('style');
$this->assertRegExp('/z\-index:\-[0-9]*/', $style);

// square
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap');
$this->assertEquals('square', $element->getAttribute('type'));
}

/**
* covers ::_writeWatermark
*/
public function testWriteWatermark()
{
$imageSrc = __DIR__ . "/../../_files/images/earth.jpg";

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$header = $section->addHeader();
$header->addWatermark($imageSrc);
$doc = TestHelperDOCX::getDocument($phpWord);

$element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference");
$this->assertStringStartsWith("rId", $element->getAttribute('r:id'));
}

/**
* covers ::_writeTitle
*/
public function testWriteTitle()
{
$phpWord = new PhpWord();
$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
$phpWord->addSection()->addTitle('Test', 1);
$doc = TestHelperDOCX::getDocument($phpWord);

$section->addText('This is my text', null, array('align' => 'right'));
$element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
$this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
$element = "/w:document/w:body/w:p/w:r/w:fldChar";
$this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType'));
}

/**
* covers ::_writeCheckbox
*/
public function testWriteCheckbox()
{
$rStyle = 'rStyle';
$pStyle = 'pStyle';

$phpWord = new PhpWord();
$phpWord->addFontStyle($rStyle, array('bold' => true));
$phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
$section = $phpWord->addSection();
$section->addCheckbox('Check1', 'Test', $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:jc');

$this->assertEquals('right', $element->getAttribute('w:val'));
$element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name';
$this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val'));
}

/**
* covers ::_writeParagraphStyle
*/
public function testWriteParagraphStylePagination()
public function testWriteParagraphStyle()
{
// Create the doc
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$attributes = array(
'align' => 'right',
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
Expand All @@ -184,10 +247,13 @@ public function testWriteParagraphStylePagination()
$i = 0;
foreach ($attributes as $key => $value) {
$i++;
$path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$key}";
$nodeName = ($key == 'align') ? 'jc' : $key;
$path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$nodeName}";
if ($key != 'align') {
$value = $value ? 1 : 0;
}
$element = $doc->getElement($path);
$expected = $value ? 1 : 0;
$this->assertEquals($expected, $element->getAttribute('w:val'));
$this->assertEquals($value, $element->getAttribute('w:val'));
}
}

Expand Down Expand Up @@ -316,81 +382,4 @@ public function testWriteCellStyleCellGridSpan()

$this->assertEquals(5, $element->getAttribute('w:val'));
}

/**
* covers ::_writeImage
*/
public function testWriteImagePosition()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addImage(
__DIR__ . "/../../_files/images/earth.jpg",
array(
'marginTop' => -1,
'marginLeft' => -1,
'wrappingStyle' => 'behind'
)
);

$doc = TestHelperDOCX::getDocument($phpWord);
$element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape');

$style = $element->getAttribute('style');

$this->assertRegExp('/z\-index:\-[0-9]*/', $style);
$this->assertRegExp('/position:absolute;/', $style);
}

/**
* covers ::_writeWatermark
*/
public function testWriteWatermark()
{
$imageSrc = __DIR__ . "/../../_files/images/earth.jpg";

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$header = $section->addHeader();
$header->addWatermark($imageSrc);
$doc = TestHelperDOCX::getDocument($phpWord);

$element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference");
$this->assertStringStartsWith("rId", $element->getAttribute('r:id'));
}

/**
* covers ::_writeTitle
*/
public function testWriteTitle()
{
$phpWord = new PhpWord();
$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
$phpWord->addSection()->addTitle('Test', 1);
$doc = TestHelperDOCX::getDocument($phpWord);

$element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
$this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
$element = "/w:document/w:body/w:p/w:r/w:fldChar";
$this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType'));
}

/**
* covers ::_writeCheckbox
*/
public function testWriteCheckbox()
{
$rStyle = 'rStyle';
$pStyle = 'pStyle';

$phpWord = new PhpWord();
$phpWord->addFontStyle($rStyle, array('bold' => true));
$phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
$section = $phpWord->addSection();
$section->addCheckbox('Check1', 'Test', $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($phpWord);

$element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name';
$this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val'));
}
}

4 comments on commit 89f4288

@ivanlanin
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can only use @covers for public methods. I used covers without @ just as a note.

@ivanlanin
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the _writeImage. I've read it somewhere that @covers can only be assigned to public methods of the class being tested. So we can't use @covers ::_writeImage because the phpUnit will ignore the directive. I've just tested it and it turned out that this is not true. My bad :) I'll fix the unit tests docblocks.

@ivanlanin
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've implemented the reference in AbstractStyleTest.

@ivanlanin
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree :) I'll try to refactor the codes or the tests.

Please sign in to comment.