Merge branch 'develop' of https://github.com/jhfangying/PHPWord into develop

This commit is contained in:
Ivan Lanin 2014-03-13 19:28:52 +07:00
commit d180ac7fdf
4 changed files with 125 additions and 134 deletions

View File

@ -44,7 +44,11 @@ class PHPWord
* Default font name (Arial)
*/
const DEFAULT_FONT_NAME = 'Arial';
/**
* Default Font Content Type(default)
* default|eastAsia|cs
*/
const DEFAULT_FONT_CONTENT_TYPE='default';
/**
* Default font size in points (10pt)
*

View File

@ -1,4 +1,5 @@
<?php
/**
* PHPWord
*
@ -24,14 +25,13 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 0.7.0
*/
use PhpOffice\PhpWord\Exceptions\InvalidStyleException;
/**
* Class PHPWord_Style_Font
*/
class PHPWord_Style_Font
{
class PHPWord_Style_Font {
const UNDERLINE_NONE = 'none';
const UNDERLINE_DASH = 'dash';
const UNDERLINE_DASHHEAVY = 'dashHeavy';
@ -50,7 +50,6 @@ class PHPWord_Style_Font
const UNDERLINE_WAVYDOUBLE = 'wavyDbl';
const UNDERLINE_WAVYHEAVY = 'wavyHeavy';
const UNDERLINE_WORDS = 'words';
const FGCOLOR_YELLOW = 'yellow';
const FGCOLOR_LIGHTGREEN = 'green';
const FGCOLOR_CYAN = 'cyan';
@ -158,14 +157,20 @@ class PHPWord_Style_Font
*/
private $lineHeight = 1.0;
/**
* Font Content Type
*
* @var string
*/
private $_hint = PHPWord::DEFAULT_FONT_CONTENT_TYPE;
/**
* New font style
*
* @param string $type Type of font
* @param array $paragraphStyle Paragraph styles definition
*/
public function __construct($type = 'text', $paragraphStyle = null)
{
public function __construct($type = 'text', $paragraphStyle = null) {
$this->_type = $type;
if ($paragraphStyle instanceof PHPWord_Style_Paragraph) {
@ -182,8 +187,7 @@ class PHPWord_Style_Font
* @param array $style
* @return $this
*/
public function setArrayStyle(array $style = array())
{
public function setArrayStyle(array $style = array()) {
foreach ($style as $key => $value) {
if ($key === 'line-height') {
$this->setLineHeight($value);
@ -203,8 +207,7 @@ class PHPWord_Style_Font
* @param string $key
* @param mixed $value
*/
public function setStyleValue($key, $value)
{
public function setStyleValue($key, $value) {
$method = 'set' . substr($key, 1);
if (method_exists($this, $method)) {
$this->$method($value);
@ -216,8 +219,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getName()
{
public function getName() {
return $this->_name;
}
@ -227,8 +229,7 @@ class PHPWord_Style_Font
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setName($pValue = PHPWord::DEFAULT_FONT_NAME)
{
public function setName($pValue = PHPWord::DEFAULT_FONT_NAME) {
if (is_null($pValue) || $pValue == '') {
$pValue = PHPWord::DEFAULT_FONT_NAME;
}
@ -236,13 +237,13 @@ class PHPWord_Style_Font
return $this;
}
/**
* Get font size
*
* @return int|float
*/
public function getSize()
{
public function getSize() {
return $this->_size;
}
@ -252,8 +253,7 @@ class PHPWord_Style_Font
* @param int|float $pValue
* @return PHPWord_Style_Font
*/
public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE)
{
public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE) {
if (!is_numeric($pValue)) {
$pValue = PHPWord::DEFAULT_FONT_SIZE;
}
@ -266,8 +266,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getBold()
{
public function getBold() {
return $this->_bold;
}
@ -277,8 +276,7 @@ class PHPWord_Style_Font
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setBold($pValue = false)
{
public function setBold($pValue = false) {
if (!is_bool($pValue)) {
$pValue = false;
}
@ -291,8 +289,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getItalic()
{
public function getItalic() {
return $this->_italic;
}
@ -302,8 +299,7 @@ class PHPWord_Style_Font
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setItalic($pValue = false)
{
public function setItalic($pValue = false) {
if (!is_bool($pValue)) {
$pValue = false;
}
@ -316,8 +312,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getSuperScript()
{
public function getSuperScript() {
return $this->_superScript;
}
@ -327,8 +322,7 @@ class PHPWord_Style_Font
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setSuperScript($pValue = false)
{
public function setSuperScript($pValue = false) {
if (!is_bool($pValue)) {
$pValue = false;
}
@ -342,8 +336,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getSubScript()
{
public function getSubScript() {
return $this->_subScript;
}
@ -353,8 +346,7 @@ class PHPWord_Style_Font
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setSubScript($pValue = false)
{
public function setSubScript($pValue = false) {
if (!is_bool($pValue)) {
$pValue = false;
}
@ -368,8 +360,7 @@ class PHPWord_Style_Font
*
* @return string
*/
public function getUnderline()
{
public function getUnderline() {
return $this->_underline;
}
@ -379,8 +370,7 @@ class PHPWord_Style_Font
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE)
{
public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE) {
if ($pValue == '') {
$pValue = PHPWord_Style_Font::UNDERLINE_NONE;
}
@ -393,8 +383,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getStrikethrough()
{
public function getStrikethrough() {
return $this->_strikethrough;
}
@ -404,8 +393,7 @@ class PHPWord_Style_Font
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setStrikethrough($pValue = false)
{
public function setStrikethrough($pValue = false) {
if (!is_bool($pValue)) {
$pValue = false;
}
@ -418,8 +406,7 @@ class PHPWord_Style_Font
*
* @return string
*/
public function getColor()
{
public function getColor() {
return $this->_color;
}
@ -429,8 +416,7 @@ class PHPWord_Style_Font
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setColor($pValue = PHPWord::DEFAULT_FONT_COLOR)
{
public function setColor($pValue = PHPWord::DEFAULT_FONT_COLOR) {
if (is_null($pValue) || $pValue == '') {
$pValue = PHPWord::DEFAULT_FONT_COLOR;
}
@ -443,8 +429,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getFgColor()
{
public function getFgColor() {
return $this->_fgColor;
}
@ -454,8 +439,7 @@ class PHPWord_Style_Font
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setFgColor($pValue = null)
{
public function setFgColor($pValue = null) {
$this->_fgColor = $pValue;
return $this;
}
@ -465,8 +449,7 @@ class PHPWord_Style_Font
*
* @return string
*/
public function getStyleType()
{
public function getStyleType() {
return $this->_type;
}
@ -475,12 +458,10 @@ class PHPWord_Style_Font
*
* @return PHPWord_Style_Paragraph
*/
public function getParagraphStyle()
{
public function getParagraphStyle() {
return $this->_paragraphStyle;
}
/**
* Set the line height
*
@ -488,8 +469,7 @@ class PHPWord_Style_Font
* @return $this
* @throws InvalidStyleException
*/
public function setLineHeight($lineHeight)
{
public function setLineHeight($lineHeight) {
if (is_string($lineHeight)) {
$lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight));
}
@ -506,8 +486,28 @@ class PHPWord_Style_Font
/**
* @return int|float
*/
public function getLineHeight()
{
public function getLineHeight() {
return $this->lineHeight;
}
/**
* Get Font Content Type
*
* @return bool
*/
public function getHint() {
return $this->_hint;
}
/**
* Set Font Content Type
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setHint($pValue = PHPWord::DEFAULT_FONT_CONTENT_TYPE) {
if (is_null($pValue) || $pValue == '') {
$pValue = PHPWord::DEFAULT_FONT_CONTENT_TYPE;
}
$this->_hint = $pValue;
return $this;
}
}

View File

@ -1,4 +1,5 @@
<?php
/**
* PHPWord
*
@ -28,14 +29,9 @@
/**
* Class PHPWord_Writer_Word2007_Base
*/
class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
{
class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
/**
* Write text
*/
protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false)
{
protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) {
$styleFont = $text->getFontStyle();
$SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
@ -84,11 +80,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
/**
* Write text run
*/
protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun)
{
protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) {
$elements = $textrun->getElements();
$styleParagraph = $textrun->getParagraphStyle();
@ -138,6 +130,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
PHPWord_Style_Paragraph $style,
$withoutPPR = false
) {
$align = $style->getAlign();
$spacing = $style->getSpacing();
$spaceBefore = $style->getSpaceBefore();
@ -151,9 +144,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$pageBreakBefore = $style->getPageBreakBefore();
if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) ||
!is_null($spaceAfter) || !is_null($indent) || !is_null($hanging) ||
!is_null($tabs) || !is_null($widowControl) || !is_null($keepNext) ||
!is_null($keepLines) || !is_null($pageBreakBefore)) {
!is_null($spaceAfter) || !is_null($indent) || !is_null($hanging) ||
!is_null($tabs) || !is_null($widowControl) || !is_null($keepNext) ||
!is_null($keepLines) || !is_null($pageBreakBefore)) {
if (!$withoutPPR) {
$objWriter->startElement('w:pPr');
}
@ -180,7 +173,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
// Spacing
if (!is_null($spaceBefore) || !is_null($spaceAfter) ||
!is_null($spacing)) {
!is_null($spacing)) {
$objWriter->startElement('w:spacing');
if (!is_null($spaceBefore)) {
$objWriter->writeAttribute('w:before', $spaceBefore);
@ -228,11 +221,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
/**
* Write table
*/
protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false)
{
protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false) {
$rID = $link->getRelationId();
$linkName = $link->getLinkName();
if (is_null($linkName)) {
@ -287,11 +276,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
/**
* Write preserve text
*/
protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun)
{
protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun) {
$styleFont = $textrun->getFontStyle();
$styleParagraph = $textrun->getParagraphStyle();
@ -382,11 +367,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement(); // p
}
/**
* Write text style
*/
protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style)
{
protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style) {
$font = $style->getName();
$bold = $style->getBold();
$italic = $style->getItalic();
@ -397,6 +378,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$underline = $style->getUnderline();
$superscript = $style->getSuperScript();
$subscript = $style->getSubScript();
$hint = $style->getHint();
$objWriter->startElement('w:rPr');
@ -405,10 +387,16 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->startElement('w:rFonts');
$objWriter->writeAttribute('w:ascii', $font);
$objWriter->writeAttribute('w:hAnsi', $font);
$objWriter->writeAttribute('w:eastAsia', $font);
$objWriter->writeAttribute('w:cs', $font);
//Font Content Type
if ($hint != PHPWord::DEFAULT_FONT_CONTENT_TYPE) {
$objWriter->writeAttribute('w:hint', $hint);
}
$objWriter->endElement();
}
// Color
if ($color != PHPWord::DEFAULT_FONT_COLOR) {
$objWriter->startElement('w:color');
@ -466,19 +454,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
/**
* Write text break
*/
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
{
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) {
$objWriter->writeElement('w:p', null);
}
/**
* Write table
*/
protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table)
{
protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) {
$_rows = $table->getRows();
$_cRows = count($_rows);
@ -520,14 +500,14 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->writeAttribute('w:val', $height);
$objWriter->endElement();
}
if ($tblHeader) {
if (!is_null($tblHeader)) {
$objWriter->startElement('w:tblHeader');
$objWriter->writeAttribute('w:val', '1');
$objWriter->writeAttribute('w:val', $tblHeader);
$objWriter->endElement();
}
if ($cantSplit) {
if (!is_null($cantSplit)) {
$objWriter->startElement('w:cantSplit');
$objWriter->writeAttribute('w:val', '1');
$objWriter->writeAttribute('w:val', $cantSplit);
$objWriter->endElement();
}
$objWriter->endElement();
@ -565,7 +545,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
} elseif ($element instanceof PHPWord_Section_ListItem) {
$this->_writeListItem($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_Image ||
$element instanceof PHPWord_Section_MemoryImage
$element instanceof PHPWord_Section_MemoryImage
) {
$this->_writeImage($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_Object) {
@ -586,11 +566,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
/**
* Write table style
*/
protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null)
{
protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null) {
$margins = $style->getCellMargin();
$mTop = (!is_null($margins[0])) ? true : false;
$mLeft = (!is_null($margins[1])) ? true : false;
@ -634,11 +610,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
/**
* Write cell style
*/
protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null)
{
protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) {
$bgColor = $style->getBgColor();
$valign = $style->getVAlign();
$textDir = $style->getTextDirection();
@ -744,8 +716,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
* @param \PHPWord_Shared_XMLWriter $objWriter
* @param \PHPWord_Section_Image $image
*/
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false)
{
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false) {
$rId = $image->getRelationId();
$style = $image->getStyle();
@ -821,11 +792,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
/**
* Write watermark
*/
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image)
{
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) {
$rId = $image->getRelationId();
$style = $image->getStyle();
@ -868,11 +835,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
/**
* Write title
*/
protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title)
{
protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title) {
$text = htmlspecialchars($title->getText());
$text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text);
$anchor = $title->getAnchor();
@ -913,9 +876,6 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
/**
* Write footnote
*/
protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote)
{
$objWriter->startElement('w:footnote');
@ -951,9 +911,6 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement(); // w:footnote
}
/**
* Write footnote reference
*/
protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false)
{
if (!$withoutP) {

View File

@ -0,0 +1,30 @@
<?php
error_reporting(E_ALL);
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
} else {
define('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();
$header = array('size' => 16, 'bold' => true);
//1.Use EastAisa FontStyle
$section->addText('中文楷体样式测试',array('name' => '楷体', 'size' => 16, 'color' => '1B2232'));
// Save File
echo date('H:i:s') , ' Write to Word2007 format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save(str_replace('.php', '.docx', __FILE__));
// Echo memory peak usage
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
// Echo done
echo date('H:i:s') , ' Done writing file' , EOL;