TextRun.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /**
  28. * PHPWord_Section_TextRun
  29. *
  30. * @category PHPWord
  31. * @package PHPWord_Section
  32. * @copyright Copyright (c) 2011 PHPWord
  33. */
  34. class PHPWord_Section_TextRun {
  35. /**
  36. * Paragraph style
  37. *
  38. * @var PHPWord_Style_Font
  39. */
  40. private $_styleParagraph;
  41. /**
  42. * Text collection
  43. *
  44. * @var array
  45. */
  46. private $_elementCollection;
  47. /**
  48. * Create a new TextRun Element
  49. */
  50. public function __construct($styleParagraph = null) {
  51. $this->_elementCollection = array();
  52. // Set paragraph style
  53. if(is_array($styleParagraph)) {
  54. $this->_styleParagraph = new PHPWord_Style_Paragraph();
  55. foreach($styleParagraph as $key => $value) {
  56. if(substr($key, 0, 1) != '_') {
  57. $key = '_'.$key;
  58. }
  59. $this->_styleParagraph->setStyleValue($key, $value);
  60. }
  61. } else {
  62. $this->_styleParagraph = $styleParagraph;
  63. }
  64. }
  65. /**
  66. * Add a Text Element
  67. *
  68. * @var string $text
  69. * @var mixed $styleFont
  70. * @return PHPWord_Section_Text
  71. */
  72. public function addText($text = null, $styleFont = null) {
  73. //$givenText = utf8_encode($text);
  74. //$givenText = iconv('gbk', 'utf-8', $givenText);
  75. //$givenText = iconv('GB2312', 'utf-8//IGNORE', $givenText);
  76. $givenText = PHPWord_Media::GBKToUTF8($givenText);
  77. $text = new PHPWord_Section_Text($givenText, $styleFont);
  78. $this->_elementCollection[] = $text;
  79. return $text;
  80. }
  81. /**
  82. * Add a Link Element
  83. *
  84. * @param string $linkSrc
  85. * @param string $linkName
  86. * @param mixed $styleFont
  87. * @return PHPWord_Section_Link
  88. */
  89. public function addLink($linkSrc, $linkName = null, $styleFont = null) {
  90. //$linkSrc = utf8_encode($linkSrc);
  91. //$linkSrc = iconv('gbk', 'utf-8', $linkSrc);
  92. //$linkSrc = iconv('GB2312', 'utf-8//IGNORE', $linkSrc);
  93. $linkSrc = PHPWord_Media::GBKToUTF8($linkSrc);
  94. if(!is_null($linkName)) {
  95. //$linkName = utf8_encode($linkName);
  96. //$linkName = iconv('gbk', 'utf-8', $linkName);
  97. //$linkName = iconv('GB2312', 'utf-8//IGNORE', $linkName);
  98. $linkName = PHPWord_Media::GBKToUTF8($linkName);
  99. }
  100. $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont);
  101. $rID = PHPWord_Media::addSectionLinkElement($linkSrc);
  102. $link->setRelationId($rID);
  103. $this->_elementCollection[] = $link;
  104. return $link;
  105. }
  106. /**
  107. * Get TextRun content
  108. *
  109. * @return string
  110. */
  111. public function getElements() {
  112. return $this->_elementCollection;
  113. }
  114. /**
  115. * Get Paragraph style
  116. *
  117. * @return PHPWord_Style_Paragraph
  118. */
  119. public function getParagraphStyle() {
  120. return $this->_styleParagraph;
  121. }
  122. }
  123. ?>