From fa940d70d4eecf0e0663961d0a48248ebd8ccfc9 Mon Sep 17 00:00:00 2001 From: raqmorita Date: Fri, 20 Oct 2017 14:07:36 +0200 Subject: [PATCH 1/3] Fix encoding UTF-8 --- src/Wesnick/FdfUtility/FdfWriter.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Wesnick/FdfUtility/FdfWriter.php b/src/Wesnick/FdfUtility/FdfWriter.php index 9c7c7ed..a9233bf 100644 --- a/src/Wesnick/FdfUtility/FdfWriter.php +++ b/src/Wesnick/FdfUtility/FdfWriter.php @@ -62,8 +62,28 @@ public function addField(PdfField $field) public static function escapePdfString($string) { $escaped = ''; - - foreach (str_split($string) as $char) { + + $uniord = function ($u) { + $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8'); + $k1 = ord(substr($k, 0, 1)); + $k2 = ord(substr($k, 1, 1)); + return $k2 * 256 + $k1; + }; + + $str_split_unicode = function ($str, $l = 0) { + if ($l > 0) { + $ret = array(); + $len = mb_strlen($str, "UTF-8"); + for ($i = 0; $i < $len; $i += $l) { + $ret[] = mb_substr($str, $i, $l, "UTF-8"); + } + return $ret; + } + return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY); + }; + + $chars = $str_split_unicode($string); + foreach ($chars as $char) { switch ($ordinal = ord($char)) { // If open paren, close paren, or backslash, escape character case in_array($ordinal, [0x28, 0x29, 0x5c], true): @@ -72,7 +92,7 @@ public static function escapePdfString($string) break; case $ordinal < 32: case $ordinal > 126: - $escaped .= sprintf('\\%03o', $ordinal); + $escaped .= sprintf('\\%03o', $uniord($char)); break; default: $escaped .= $char; From 8400aaa9d6b71fea67c28c2ee61fb8a8be666664 Mon Sep 17 00:00:00 2001 From: raqmorita Date: Fri, 20 Oct 2017 14:38:52 +0200 Subject: [PATCH 2/3] fix psr --- src/Wesnick/FdfUtility/FdfWriter.php | 57 +++++++++++++++++----------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/src/Wesnick/FdfUtility/FdfWriter.php b/src/Wesnick/FdfUtility/FdfWriter.php index a9233bf..2cb2801 100644 --- a/src/Wesnick/FdfUtility/FdfWriter.php +++ b/src/Wesnick/FdfUtility/FdfWriter.php @@ -54,6 +54,38 @@ public function addField(PdfField $field) $fields = $field; } + /** + * @param string $u + * + * @return int + */ + public static function uniord($u) + { + $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8'); + $k1 = ord(substr($k, 0, 1)); + $k2 = ord(substr($k, 1, 1)); + return $k2 * 256 + $k1; + } + + /** + * @param string $str + * @param int $l + * + * @return array + */ + public static function str_split_unicode($str, $l = 0) + { + if ($l > 0) { + $ret = []; + $len = mb_strlen($str, 'UTF-8'); + for ($i = 0; $i < $len; $i += $l) { + $ret[] = mb_substr($str, $i, $l, 'UTF-8'); + } + return $ret; + } + return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY); + } + /** * @param $string * @@ -62,27 +94,8 @@ public function addField(PdfField $field) public static function escapePdfString($string) { $escaped = ''; - - $uniord = function ($u) { - $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8'); - $k1 = ord(substr($k, 0, 1)); - $k2 = ord(substr($k, 1, 1)); - return $k2 * 256 + $k1; - }; - - $str_split_unicode = function ($str, $l = 0) { - if ($l > 0) { - $ret = array(); - $len = mb_strlen($str, "UTF-8"); - for ($i = 0; $i < $len; $i += $l) { - $ret[] = mb_substr($str, $i, $l, "UTF-8"); - } - return $ret; - } - return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY); - }; - - $chars = $str_split_unicode($string); + + $chars = self::str_split_unicode($string); foreach ($chars as $char) { switch ($ordinal = ord($char)) { // If open paren, close paren, or backslash, escape character @@ -92,7 +105,7 @@ public static function escapePdfString($string) break; case $ordinal < 32: case $ordinal > 126: - $escaped .= sprintf('\\%03o', $uniord($char)); + $escaped .= sprintf('\\%03o', self::uniord($char)); break; default: $escaped .= $char; From cc1f27275e689954fe6382327253c7805b8ded57 Mon Sep 17 00:00:00 2001 From: raqmorita Date: Mon, 23 Oct 2017 11:03:53 +0200 Subject: [PATCH 3/3] fix test --- tests/FdfUtility/Fields/TextFieldTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/FdfUtility/Fields/TextFieldTest.php b/tests/FdfUtility/Fields/TextFieldTest.php index fae2a6d..947d66f 100644 --- a/tests/FdfUtility/Fields/TextFieldTest.php +++ b/tests/FdfUtility/Fields/TextFieldTest.php @@ -84,6 +84,6 @@ public function testDefaultValueIsRespected() $this->assertSame('(value)', $field->getEscapedValue(), 'Default Value is ignored if value not null'); $field->setValue(''); - $this->assertSame('(\)', $field->getEscapedValue(), 'Default Value is ignored if value is empty'); + $this->assertSame('()', $field->getEscapedValue(), 'Default Value is ignored if value is empty'); } }