-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Feature: Ruby (phonetic guide) text - Word2007 Read/Write, HTML Read/Write, RTF write, ODT basic write #2727
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
Merged
Merged
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
7c2b5cd
Allow reading ruby text in from Word2007 docs
Deadpikle e513c94
Rename ruby language to language Id
Deadpikle 5bede93
Add basic ruby Word2007 writer
Deadpikle 68ecb1a
Update changelog
Deadpikle 1650bce
Add ruby element docs
Deadpikle 4d60f38
Run PHP CS Fixer
Deadpikle 8d4af09
Fix PHPStan errors
Deadpikle 6552d71
Run PHP CS Fixer again
Deadpikle 588d808
Fix ruby in titles when reading
Deadpikle 8bc7ceb
Add/tweak tests for reading/writing Title with ruby
Deadpikle 4cecbec
Use elementExists to try and fix PHP7.1
Deadpikle cc6b6ec
Review: add missing type hints; refine types
Deadpikle 91a49bf
Add setters to Ruby class
Deadpikle 89a00be
Add basic sample for ruby text
Deadpikle 74f8941
Add base constructor to RubyProperties
Deadpikle 106c8d6
Add basic ruby tests
Deadpikle b563e79
Run PHP CS Fixer
Deadpikle 3c363ce
Update sample to handle properties better
Deadpikle 4366ec0
Add Ruby HTML output including tests
Deadpikle 7decf92
Update changelog
Deadpikle 05ff3f5
Update PhpStand baseline
Deadpikle ef28187
Forgot to run php-cs-fixer again
Deadpikle 09a38f6
Update 1.4.0.md
Deadpikle 376754e
Fix TextRun::getText not working with Ruby element
Deadpikle 46143dd
Add test for TextRun ruby element
Deadpikle 0423961
Add RTF ruby output
Deadpikle 66232da
Add ruby HTML reading
Deadpikle 94ee043
Tweak param calls in testRubyWriting
Deadpikle 1e55972
Update changelog
Deadpikle cf51a65
Update sample 46 to be less confusing with ruby output
Deadpikle c115fa8
Move ODText/Element/Text::replaceTabs method
Deadpikle fb98e25
Add very basic ODT Ruby output
Deadpikle 3281b3b
Run PHP CS Fixer
Deadpikle a4368df
Update 1.4.0.md
Deadpikle 00c4b2d
Try to fix unexpected EOF in HtmlTest
Deadpikle 68e1dbb
Merge branch 'master' of https://github.com/PHPOffice/PHPWord into fe…
Deadpikle 04aefba
Add missing tests for RubyProperties
Deadpikle ec18c81
Merge branch 'master' of https://github.com/PHPOffice/PHPWord into fe…
Deadpikle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Ruby | ||
|
||
Ruby (phonetic guide) text can be added by using the ``addRuby`` method. Ruby elements require a ``RubyProperties`` object, a ``TextRun`` for the base text, and a ``TextRun`` for the actual ruby (phonetic guide) text. | ||
|
||
Here is one example for a complete ruby element setup: | ||
|
||
``` php | ||
<?php | ||
$phpWord = new PhpWord(); | ||
|
||
$section = $phpWord->addSection(); | ||
$properties = new RubyProperties(); | ||
$properties->setAlignment(RubyProperties::ALIGNMENT_RIGHT_VERTICAL); | ||
$properties->setFontFaceSize(10); | ||
$properties->setFontPointsAboveBaseText(4); | ||
$properties->setFontSizeForBaseText(18); | ||
$properties->setLanguageId('ja-JP'); | ||
|
||
$baseTextRun = new TextRun(null); | ||
$baseTextRun->addText('私'); | ||
$rubyTextRun = new TextRun(null); | ||
$rubyTextRun->addText('わたし'); | ||
|
||
$section->addRuby($baseTextRun, $rubyTextRun, $properties); | ||
``` | ||
|
||
- ``$baseTextRun``. ``TextRun`` to be used for the base text. | ||
- ``$rubyTextRun``. ``TextRun`` to be used for the ruby text. | ||
- ``$properties``. ``RubyProperties`` properties object for the ruby text. | ||
|
||
A title with a phonetic guide is a little more complex, but still possible. Make sure you add the appropraite title style to your document. | ||
|
||
```php | ||
$phpWord = new PhpWord(); | ||
$fontStyle = new Font(); | ||
$fontStyle->setAllCaps(true); | ||
$fontStyle->setBold(true); | ||
$fontStyle->setSize(24); | ||
$phpWord->addTitleStyle(1, ['name' => 'Arial', 'size' => 24, 'bold' => true, 'color' => '990000']); | ||
|
||
$section = $phpWord->addSection(); | ||
$properties = new RubyProperties(); | ||
$properties->setAlignment(RubyProperties::ALIGNMENT_RIGHT_VERTICAL); | ||
$properties->setFontFaceSize(10); | ||
$properties->setFontPointsAboveBaseText(4); | ||
$properties->setFontSizeForBaseText(18); | ||
$properties->setLanguageId('ja-JP'); | ||
|
||
$baseTextRun = new TextRun(null); | ||
$baseTextRun->addText('私'); | ||
$rubyTextRun = new TextRun(null); | ||
$rubyTextRun->addText('わたし'); | ||
|
||
$textRun = new TextRun(); | ||
$textRun->addRuby($baseTextRun, $rubyTextRun, $properties); | ||
$section->addTitle($textRun, 1); | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
use PhpOffice\PhpWord\ComplexType\RubyProperties; | ||
use PhpOffice\PhpWord\Element\TextRun; | ||
|
||
include_once 'Sample_Header.php'; | ||
|
||
// New Word Document | ||
echo date('H:i:s'), ' Create sample for Ruby (Phonetic Guide) use', EOL; | ||
$phpWord = new PhpOffice\PhpWord\PhpWord(); | ||
|
||
// Section for demonstrating ruby (phonetic guide) features | ||
$section = $phpWord->addSection(); | ||
|
||
$section->addText('Here is some normal text with no ruby, also known as "phonetic guide", text.'); | ||
|
||
$properties = new RubyProperties(); | ||
$properties->setAlignment(RubyProperties::ALIGNMENT_CENTER); | ||
$properties->setFontFaceSize(10); | ||
$properties->setFontPointsAboveBaseText(20); | ||
$properties->setFontSizeForBaseText(18); | ||
$properties->setLanguageId('en-US'); | ||
|
||
$textRun = $section->addTextRun(); | ||
$textRun->addText('Here is a demonstration of ruby text for '); | ||
$baseTextRun = new TextRun(null); | ||
$baseTextRun->addText('this'); | ||
$rubyTextRun = new TextRun(null); | ||
$rubyTextRun->addText('ruby-text'); | ||
$textRun->addRuby($baseTextRun, $rubyTextRun, $properties); | ||
$textRun->addText(' word.'); | ||
|
||
$textRun = $section->addTextRun(); | ||
$properties = new RubyProperties(); | ||
$properties->setAlignment(RubyProperties::ALIGNMENT_CENTER); | ||
$properties->setFontFaceSize(10); | ||
$properties->setFontPointsAboveBaseText(20); | ||
$properties->setFontSizeForBaseText(18); | ||
$properties->setLanguageId('ja-JP'); | ||
$textRun->addText('Here is a demonstration of ruby text for Japanese text: '); | ||
$baseTextRun = new TextRun(null); | ||
$baseTextRun->addText('私'); | ||
$rubyTextRun = new TextRun(null); | ||
$rubyTextRun->addText('わたし'); | ||
$textRun->addRuby($baseTextRun, $rubyTextRun, $properties); | ||
|
||
$section->addText('You can also have ruby text for titles:'); | ||
|
||
$phpWord->addTitleStyle(1, ['name' => 'Arial', 'size' => 24, 'bold' => true, 'color' => '000099']); | ||
|
||
$properties = new RubyProperties(); | ||
$properties->setAlignment(RubyProperties::ALIGNMENT_CENTER); | ||
$properties->setFontFaceSize(10); | ||
$properties->setFontPointsAboveBaseText(50); | ||
$properties->setFontSizeForBaseText(18); | ||
$properties->setLanguageId('ja-JP'); | ||
|
||
$baseTextRun = new TextRun(null); | ||
$baseTextRun->addText('私'); | ||
$rubyTextRun = new TextRun(null); | ||
$rubyTextRun->addText('わたし'); | ||
$textRun = new TextRun(); | ||
$textRun->addRuby($baseTextRun, $rubyTextRun, $properties); | ||
$section->addTitle($textRun, 1); | ||
|
||
// Save file | ||
echo write($phpWord, basename(__FILE__, '.php'), $writers); | ||
if (!CLI) { | ||
include_once 'Sample_Footer.php'; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of PHPWord - A pure PHP library for reading and writing | ||
* word processing documents. | ||
* | ||
* PHPWord is free software distributed under the terms of the GNU Lesser | ||
* General Public License version 3 as published by the Free Software Foundation. | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. For the full list of | ||
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. | ||
* | ||
* @see https://github.com/PHPOffice/PHPWord | ||
* | ||
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 | ||
*/ | ||
|
||
namespace PhpOffice\PhpWord\ComplexType; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Ruby properties. | ||
* | ||
* @see https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.rubyproperties?view=openxml-3.0.1 | ||
*/ | ||
class RubyProperties | ||
{ | ||
const ALIGNMENT_CENTER = 'center'; | ||
const ALIGNMENT_DISTRIBUTE_LETTER = 'distributeLetter'; | ||
const ALIGNMENT_DISTRIBUTE_SPACE = 'distributeSpace'; | ||
const ALIGNMENT_LEFT = 'left'; | ||
const ALIGNMENT_RIGHT = 'right'; | ||
const ALIGNMENT_RIGHT_VERTICAL = 'rightVertical'; | ||
|
||
/** | ||
* Ruby alignment (w:rubyAlign). | ||
* | ||
* @var string | ||
*/ | ||
private $alignment; | ||
|
||
/** | ||
* Ruby font face size (w:hps). | ||
* | ||
* @var float | ||
*/ | ||
private $fontFaceSize; | ||
|
||
/** | ||
* Ruby font points above base text (w:hpsRaise). | ||
* | ||
* @var float | ||
*/ | ||
private $fontPointsAboveText; | ||
|
||
/** | ||
* Ruby font size for base text (w:hpsBaseText). | ||
* | ||
* @var float | ||
*/ | ||
private $baseTextFontSize; | ||
|
||
/** | ||
* Ruby type/language id (w:lid). | ||
* | ||
* @var string | ||
*/ | ||
private $languageId; | ||
|
||
/** | ||
* Create a new RubyProperties object. | ||
*/ | ||
public function __construct() | ||
{ | ||
// these defaults came from opening a new Word doc, adding some ruby text to some | ||
// Japanese text, and copying out the defaults. | ||
$this->alignment = self::ALIGNMENT_DISTRIBUTE_SPACE; | ||
$this->fontFaceSize = 12; | ||
$this->fontPointsAboveText = 22; | ||
$this->languageId = 'ja-JP'; | ||
$this->baseTextFontSize = 24; | ||
} | ||
|
||
/** | ||
* Get the ruby alignment. | ||
*/ | ||
public function getAlignment(): string | ||
{ | ||
return $this->alignment; | ||
} | ||
|
||
/** | ||
* Set the Ruby Alignment (center, distributeLetter, distributeSpace, left, right, rightVertical). | ||
*/ | ||
public function setAlignment(string $alignment): self | ||
{ | ||
$alignmentTypes = [ | ||
self::ALIGNMENT_CENTER, | ||
self::ALIGNMENT_DISTRIBUTE_LETTER, | ||
self::ALIGNMENT_DISTRIBUTE_SPACE, | ||
self::ALIGNMENT_LEFT, | ||
self::ALIGNMENT_RIGHT, | ||
self::ALIGNMENT_RIGHT_VERTICAL, | ||
]; | ||
|
||
if (in_array($alignment, $alignmentTypes)) { | ||
$this->alignment = $alignment; | ||
} else { | ||
throw new InvalidArgumentException('Invalid value, alignments of ' . implode(', ', $alignmentTypes) . ' possible'); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the ruby font face size. | ||
*/ | ||
public function getFontFaceSize(): float | ||
{ | ||
return $this->fontFaceSize; | ||
} | ||
|
||
/** | ||
* Set the ruby font face size. | ||
*/ | ||
public function setFontFaceSize(float $size): self | ||
{ | ||
$this->fontFaceSize = $size; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the ruby font points above base text. | ||
*/ | ||
public function getFontPointsAboveBaseText(): float | ||
{ | ||
return $this->fontPointsAboveText; | ||
} | ||
|
||
/** | ||
* Set the ruby font points above base text. | ||
*/ | ||
public function setFontPointsAboveBaseText(float $size): self | ||
{ | ||
$this->fontPointsAboveText = $size; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the ruby font size for base text. | ||
*/ | ||
public function getFontSizeForBaseText(): float | ||
{ | ||
return $this->baseTextFontSize; | ||
} | ||
|
||
/** | ||
* Set the ruby font size for base text. | ||
*/ | ||
public function setFontSizeForBaseText(float $size): self | ||
{ | ||
$this->baseTextFontSize = $size; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the ruby language id. | ||
*/ | ||
public function getLanguageId(): string | ||
{ | ||
return $this->languageId; | ||
} | ||
|
||
/** | ||
* Set the ruby language id. | ||
*/ | ||
public function setLanguageId(string $langId): self | ||
{ | ||
$this->languageId = $langId; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.