From 05b25cfd53e9864521add2e59f970b409eab7a04 Mon Sep 17 00:00:00 2001 From: hanenoshino Date: Wed, 29 Aug 2012 18:04:26 +0800 Subject: [PATCH] Use Unicode on PDFObject.s for CJK metadata As a Chinese user, I want Use Unicode to store PDF metadata, So that I can read a Chinese Title&Author... Not so familiar with coffeescript re-organize may be required --- lib/object.coffee | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/object.coffee b/lib/object.coffee index e2fed0c..b29c380 100644 --- a/lib/object.coffee +++ b/lib/object.coffee @@ -42,6 +42,20 @@ class PDFObject '' + object @s: (string) -> + + # Convert Big-endian UCS-2 to Little-endian to support most PDFRreaders + swapBytes = (buff) -> + l = buff.length + if l & 0x01 + throw new Error("Buffer length must be even") + else + for i in [0...l - 1] by 2 + a = buff[i] + buff[i] = buff[i+1] + buff[i+1] = a + buff + string = swapBytes(new Buffer('\ufeff' + string, 'ucs-2')).toString('binary') + string = string.replace(/\\/g, '\\\\\\\\') .replace(/\(/g, '\\(') .replace(/\)/g, '\\)')