Merge pull request #118 from jhfangying/develop

suport for eastAsia fontstyle
This commit is contained in:
Progi1984 2014-03-13 14:06:35 +01:00
commit 70d3a1a759
5 changed files with 123 additions and 92 deletions

1
.gitignore vendored
View File

@ -19,3 +19,4 @@ vendor
*.rtf
*.txt
*.xml
nbproject

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) {
@ -184,8 +189,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);
@ -205,8 +209,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);
@ -218,8 +221,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getName()
{
public function getName() {
return $this->_name;
}
@ -229,8 +231,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;
}
@ -238,13 +239,13 @@ class PHPWord_Style_Font
return $this;
}
/**
* Get font size
*
* @return int|float
*/
public function getSize()
{
public function getSize() {
return $this->_size;
}
@ -254,8 +255,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;
}
@ -268,8 +268,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getBold()
{
public function getBold() {
return $this->_bold;
}
@ -279,8 +278,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;
}
@ -293,8 +291,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getItalic()
{
public function getItalic() {
return $this->_italic;
}
@ -304,8 +301,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;
}
@ -318,8 +314,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getSuperScript()
{
public function getSuperScript() {
return $this->_superScript;
}
@ -329,8 +324,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;
}
@ -344,8 +338,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getSubScript()
{
public function getSubScript() {
return $this->_subScript;
}
@ -355,8 +348,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;
}
@ -370,8 +362,7 @@ class PHPWord_Style_Font
*
* @return string
*/
public function getUnderline()
{
public function getUnderline() {
return $this->_underline;
}
@ -381,8 +372,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;
}
@ -395,8 +385,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getStrikethrough()
{
public function getStrikethrough() {
return $this->_strikethrough;
}
@ -406,8 +395,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;
}
@ -420,8 +408,7 @@ class PHPWord_Style_Font
*
* @return string
*/
public function getColor()
{
public function getColor() {
return $this->_color;
}
@ -431,8 +418,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;
}
@ -445,8 +431,7 @@ class PHPWord_Style_Font
*
* @return bool
*/
public function getFgColor()
{
public function getFgColor() {
return $this->_fgColor;
}
@ -456,8 +441,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;
}
@ -467,8 +451,7 @@ class PHPWord_Style_Font
*
* @return string
*/
public function getStyleType()
{
public function getStyleType() {
return $this->_type;
}
@ -477,12 +460,10 @@ class PHPWord_Style_Font
*
* @return PHPWord_Style_Paragraph
*/
public function getParagraphStyle()
{
public function getParagraphStyle() {
return $this->_paragraphStyle;
}
/**
* Set the line height
*
@ -490,8 +471,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));
}
@ -508,8 +488,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;
}
}

48
Classes/PHPWord/Writer/Word2007/Base.php Executable file → Normal file
View File

@ -1,4 +1,5 @@
<?php
/**
* PHPWord
*
@ -28,11 +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 {
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;
@ -81,8 +80,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
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();
@ -132,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();
@ -222,8 +221,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
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)) {
@ -278,8 +276,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
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();
@ -370,8 +367,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement(); // p
}
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();
@ -382,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');
@ -390,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');
@ -451,13 +454,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
{
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) {
$objWriter->writeElement('w:p', null);
}
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);
@ -565,8 +566,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
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;
@ -610,8 +610,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
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();
@ -717,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();
@ -794,8 +792,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
}
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image)
{
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) {
$rId = $image->getRelationId();
$style = $image->getStyle();
@ -838,8 +835,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
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();

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;