Skip to content

Commit

Permalink
Correct Font Size Calculated by MsDoc Reader
Browse files Browse the repository at this point in the history
Fix PHPOffice#2526. Most of that issue has already been fixed. The one remaining problem was a deprecation message handling font size. The code used `dechex($operand / 2)`, and issued the deprecation message whenever `$operand` was odd because `dechex` is designed only for integer conversion. `$operand` is actually 2 times the point size, so it will be odd only when the point size is some integer plus half a point (no other fractions are allowed). At any rate, it seems that `dechex` should not be used here in the first place; font size is a numeric value, not a hex string.

There are many problems with MsDoc Reader at the moment. This PR is narrowly focused on the problem at hand. Its test is, at least, more detailed than the existing MsDoc Reader test, which does nothing more than confirm that read successfully creates a PhpWord object. The new test verifies that the font size is as expected, but does not validate any other aspect of the read.
  • Loading branch information
oleibman committed Dec 17, 2023
1 parent 11a7aaa commit bf9e2b0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpWord/Reader/MsDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,7 @@ private function readPrl($data, $pos, $cbNum)
break;
// sprmCHps
case 0x43:
$oStylePrl->styleFont['size'] = dechex($operand / 2);
$oStylePrl->styleFont['size'] = $operand / 2;

break;
// sprmCIss
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpWordTests/Reader/MsDocTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ public function testLoad(): void
self::assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
}

public function testLoadHalfPointFont(): void
{
$filename = __DIR__ . '/../_files/documents/word.2526.doc';
$phpWord = IOFactory::load($filename, 'MsDoc');
$sections = $phpWord->getSections();
self::assertCount(1, $sections);
$elements = $sections[0]->getElements();
self::assertArrayHasKey(0, $elements);
$element0 = $elements[0];
if (method_exists($element0, 'getFontStyle')) {
self::assertSame(19.5, $element0->getFontStyle()->getSize());
} else {
self::fail('Unexpected no font style for first element');
}
}

/**
* Test exception on not existing file.
*/
Expand Down
Binary file not shown.

0 comments on commit bf9e2b0

Please sign in to comment.