chinese.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. require('fpdf.php');
  3. class PDF_Chinese extends FPDF
  4. {
  5. private $Big5_widths = array(' '=>250,'!'=>250,'"'=>408,'#'=>668,'$'=>490,'%'=>875,'&'=>698,'\''=>250,
  6. '('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>500,'1'=>500,
  7. '2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>250,';'=>250,
  8. '<'=>667,'='=>667,'>'=>667,'?'=>396,'@'=>921,'A'=>677,'B'=>615,'C'=>719,'D'=>760,'E'=>625,
  9. 'F'=>552,'G'=>771,'H'=>802,'I'=>354,'J'=>354,'K'=>781,'L'=>604,'M'=>927,'N'=>750,'O'=>823,
  10. 'P'=>563,'Q'=>823,'R'=>729,'S'=>542,'T'=>698,'U'=>771,'V'=>729,'W'=>948,'X'=>771,'Y'=>677,
  11. 'Z'=>635,'['=>344,'\\'=>520,']'=>344,'^'=>469,'_'=>500,'`'=>250,'a'=>469,'b'=>521,'c'=>427,
  12. 'd'=>521,'e'=>438,'f'=>271,'g'=>469,'h'=>531,'i'=>250,'j'=>250,'k'=>458,'l'=>240,'m'=>802,
  13. 'n'=>531,'o'=>500,'p'=>521,'q'=>521,'r'=>365,'s'=>333,'t'=>292,'u'=>521,'v'=>458,'w'=>677,
  14. 'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'}'=>480,'~'=>667);
  15. private $GB_widths = array(' '=>207,'!'=>270,'"'=>342,'#'=>467,'$'=>462,'%'=>797,'&'=>710,'\''=>239,
  16. '('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,
  17. '2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,';'=>238,
  18. '<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,
  19. 'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,
  20. 'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,
  21. 'Z'=>607,'['=>374,'\\'=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,
  22. 'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,
  23. 'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,
  24. 'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'}'=>370,'~'=>605);
  25. function AddCIDFont($family, $style, $name, $cw, $CMap, $registry)
  26. {
  27. $fontkey = strtolower($family).strtoupper($style);
  28. if(isset($this->fonts[$fontkey]))
  29. $this->Error("Font already added: $family $style");
  30. $i = count($this->fonts)+1;
  31. $name = str_replace(' ','',$name);
  32. $this->fonts[$fontkey] = array('i'=>$i, 'type'=>'Type0', 'name'=>$name, 'up'=>-130, 'ut'=>40, 'cw'=>$cw, 'CMap'=>$CMap, 'registry'=>$registry);
  33. }
  34. function AddCIDFonts($family, $name, $cw, $CMap, $registry)
  35. {
  36. $this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
  37. $this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
  38. $this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
  39. $this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
  40. }
  41. function AddBig5Font($family='Big5', $name='MSungStd-Light-Acro')
  42. {
  43. // Add Big5 font with proportional Latin
  44. $cw = $this->Big5_widths;
  45. $CMap = 'ETenms-B5-H';
  46. $registry = array('ordering'=>'CNS1', 'supplement'=>0);
  47. $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
  48. }
  49. function AddBig5hwFont($family='Big5-hw', $name='MSungStd-Light-Acro')
  50. {
  51. // Add Big5 font with half-witdh Latin
  52. for($i=32;$i<=126;$i++)
  53. $cw[chr($i)] = 500;
  54. $CMap = 'ETen-B5-H';
  55. $registry = array('ordering'=>'CNS1', 'supplement'=>0);
  56. $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
  57. }
  58. function AddGBFont($family='GB', $name='STSongStd-Light-Acro')
  59. {
  60. // Add GB font with proportional Latin
  61. $cw = $this->GB_widths;
  62. $CMap = 'GBKp-EUC-H';
  63. $registry = array('ordering'=>'GB1', 'supplement'=>2);
  64. $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
  65. }
  66. function AddGBhwFont($family='GB-hw', $name='STSongStd-Light-Acro')
  67. {
  68. // Add GB font with half-width Latin
  69. for($i=32;$i<=126;$i++)
  70. $cw[chr($i)] = 500;
  71. $CMap = 'GBK-EUC-H';
  72. $registry = array('ordering'=>'GB1', 'supplement'=>2);
  73. $this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
  74. }
  75. function GetStringWidth($s)
  76. {
  77. if($this->CurrentFont['type']=='Type0')
  78. return $this->GetMBStringWidth($s);
  79. else
  80. return parent::GetStringWidth($s);
  81. }
  82. function GetMBStringWidth($s)
  83. {
  84. // Multi-byte version of GetStringWidth()
  85. $l = 0;
  86. $cw = &$this->CurrentFont['cw'];
  87. $nb = strlen($s);
  88. $i = 0;
  89. while($i<$nb)
  90. {
  91. $c = $s[$i];
  92. if(ord($c)<128)
  93. {
  94. $l += $cw[$c];
  95. $i++;
  96. }
  97. else
  98. {
  99. $l += 1000;
  100. $i += 2;
  101. }
  102. }
  103. return $l*$this->FontSize/1000;
  104. }
  105. function MultiCell($w, $h, $txt, $border=0, $align='L', $fill=0)
  106. {
  107. if($this->CurrentFont['type']=='Type0')
  108. $this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
  109. else
  110. parent::MultiCell($w,$h,$txt,$border,$align,$fill);
  111. }
  112. function MBMultiCell($w, $h, $txt, $border=0, $align='L', $fill=0)
  113. {
  114. // Multi-byte version of MultiCell()
  115. $cw = &$this->CurrentFont['cw'];
  116. if($w==0)
  117. $w = $this->w-$this->rMargin-$this->x;
  118. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  119. $s = str_replace("\r",'',$txt);
  120. $nb = strlen($s);
  121. if($nb>0 && $s[$nb-1]=="\n")
  122. $nb--;
  123. $b = 0;
  124. if($border)
  125. {
  126. if($border==1)
  127. {
  128. $border = 'LTRB';
  129. $b = 'LRT';
  130. $b2 = 'LR';
  131. }
  132. else
  133. {
  134. $b2 = '';
  135. if(is_int(strpos($border,'L')))
  136. $b2 .= 'L';
  137. if(is_int(strpos($border,'R')))
  138. $b2 .= 'R';
  139. $b = is_int(strpos($border,'T')) ? $b2.'T' : $b2;
  140. }
  141. }
  142. $sep = -1;
  143. $i = 0;
  144. $j = 0;
  145. $l = 0;
  146. $nl = 1;
  147. while($i<$nb)
  148. {
  149. // Get next character
  150. $c = $s[$i];
  151. // Check if ASCII or MB
  152. $ascii = (ord($c)<128);
  153. if($c=="\n")
  154. {
  155. // Explicit line break
  156. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  157. $i++;
  158. $sep = -1;
  159. $j = $i;
  160. $l = 0;
  161. $nl++;
  162. if($border && $nl==2)
  163. $b = $b2;
  164. continue;
  165. }
  166. if(!$ascii)
  167. {
  168. $sep = $i;
  169. $ls = $l;
  170. }
  171. elseif($c==' ')
  172. {
  173. $sep = $i;
  174. $ls = $l;
  175. }
  176. $l += $ascii ? $cw[$c] : 1000;
  177. if($l>$wmax)
  178. {
  179. // Automatic line break
  180. if($sep==-1 || $i==$j)
  181. {
  182. if($i==$j)
  183. $i += $ascii ? 1 : 2;
  184. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  185. }
  186. else
  187. {
  188. $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
  189. $i = ($s[$sep]==' ') ? $sep+1 : $sep;
  190. }
  191. $sep = -1;
  192. $j = $i;
  193. $l = 0;
  194. $nl++;
  195. if($border && $nl==2)
  196. $b = $b2;
  197. }
  198. else
  199. $i += $ascii ? 1 : 2;
  200. }
  201. // Last chunk
  202. if($border && is_int(strpos($border,'B')))
  203. $b .= 'B';
  204. $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
  205. $this->x = $this->lMargin;
  206. }
  207. function Write($h, $txt, $link='')
  208. {
  209. if($this->CurrentFont['type']=='Type0')
  210. $this->MBWrite($h,$txt,$link);
  211. else
  212. parent::Write($h,$txt,$link);
  213. }
  214. function MBWrite($h, $txt, $link)
  215. {
  216. // Multi-byte version of Write()
  217. $cw = &$this->CurrentFont['cw'];
  218. $w = $this->w-$this->rMargin-$this->x;
  219. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  220. $s = str_replace("\r",'',$txt);
  221. $nb = strlen($s);
  222. $sep = -1;
  223. $i = 0;
  224. $j = 0;
  225. $l = 0;
  226. $nl = 1;
  227. while($i<$nb)
  228. {
  229. // Get next character
  230. $c = $s[$i];
  231. // Check if ASCII or MB
  232. $ascii = (ord($c)<128);
  233. if($c=="\n")
  234. {
  235. // Explicit line break
  236. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  237. $i++;
  238. $sep = -1;
  239. $j = $i;
  240. $l = 0;
  241. if($nl==1)
  242. {
  243. $this->x = $this->lMargin;
  244. $w = $this->w-$this->rMargin-$this->x;
  245. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  246. }
  247. $nl++;
  248. continue;
  249. }
  250. if(!$ascii || $c==' ')
  251. $sep = $i;
  252. $l += $ascii ? $cw[$c] : 1000;
  253. if($l>$wmax)
  254. {
  255. // Automatic line break
  256. if($sep==-1 || $i==$j)
  257. {
  258. if($this->x>$this->lMargin)
  259. {
  260. // Move to next line
  261. $this->x = $this->lMargin;
  262. $this->y += $h;
  263. $w = $this->w-$this->rMargin-$this->x;
  264. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  265. $i++;
  266. $nl++;
  267. continue;
  268. }
  269. if($i==$j)
  270. $i += $ascii ? 1 : 2;
  271. $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
  272. }
  273. else
  274. {
  275. $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
  276. $i = ($s[$sep]==' ') ? $sep+1 : $sep;
  277. }
  278. $sep = -1;
  279. $j = $i;
  280. $l = 0;
  281. if($nl==1)
  282. {
  283. $this->x = $this->lMargin;
  284. $w = $this->w-$this->rMargin-$this->x;
  285. $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
  286. }
  287. $nl++;
  288. }
  289. else
  290. $i += $ascii ? 1 : 2;
  291. }
  292. // Last chunk
  293. if($i!=$j)
  294. $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
  295. }
  296. function _putType0($font)
  297. {
  298. // Type0
  299. $this->_newobj();
  300. $this->_out('<</Type /Font');
  301. $this->_out('/Subtype /Type0');
  302. $this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
  303. $this->_out('/Encoding /'.$font['CMap']);
  304. $this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
  305. $this->_out('>>');
  306. $this->_out('endobj');
  307. // CIDFont
  308. $this->_newobj();
  309. $this->_out('<</Type /Font');
  310. $this->_out('/Subtype /CIDFontType0');
  311. $this->_out('/BaseFont /'.$font['name']);
  312. $this->_out('/CIDSystemInfo <</Registry '.$this->_textstring('Adobe').' /Ordering '.$this->_textstring($font['registry']['ordering']).' /Supplement '.$font['registry']['supplement'].'>>');
  313. $this->_out('/FontDescriptor '.($this->n+1).' 0 R');
  314. if($font['CMap']=='ETen-B5-H')
  315. $W = '13648 13742 500';
  316. elseif($font['CMap']=='GBK-EUC-H')
  317. $W = '814 907 500 7716 [500]';
  318. else
  319. $W = '1 ['.implode(' ',$font['cw']).']';
  320. $this->_out('/W ['.$W.']>>');
  321. $this->_out('endobj');
  322. // Font descriptor
  323. $this->_newobj();
  324. $this->_out('<</Type /FontDescriptor');
  325. $this->_out('/FontName /'.$font['name']);
  326. $this->_out('/Flags 6');
  327. $this->_out('/FontBBox [0 -200 1000 900]');
  328. $this->_out('/ItalicAngle 0');
  329. $this->_out('/Ascent 800');
  330. $this->_out('/Descent -200');
  331. $this->_out('/CapHeight 800');
  332. $this->_out('/StemV 50');
  333. $this->_out('>>');
  334. $this->_out('endobj');
  335. }
  336. }
  337. ?>