Mercurial > hg > soundsoftware-site
comparison vendor/plugins/rfpdf/lib/fpdf/korean.rb @ 514:7eba09d624db live
Merge
author | Chris Cannam |
---|---|
date | Thu, 14 Jul 2011 10:50:53 +0100 |
parents | 0c939c159af4 |
children | cbb26bc654de |
comparison
equal
deleted
inserted
replaced
512:b9aebdd7dd40 | 514:7eba09d624db |
---|---|
1 # Copyright (c) 2006 4ssoM LLC <www.4ssoM.com> | |
2 # 1.12 contributed by Ed Moss. | |
3 # | |
4 # The MIT License | |
5 # | |
6 # Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 # of this software and associated documentation files (the "Software"), to deal | |
8 # in the Software without restriction, including without limitation the rights | |
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 # copies of the Software, and to permit persons to whom the Software is | |
11 # furnished to do so, subject to the following conditions: | |
12 # | |
13 # The above copyright notice and this permission notice shall be included in | |
14 # all copies or substantial portions of the Software. | |
15 # | |
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 # THE SOFTWARE. | |
23 # | |
24 # This is direct port of korean.php | |
25 # | |
26 # Korean PDF support. | |
27 # | |
28 # Usage is as follows: | |
29 # | |
30 # require 'fpdf' | |
31 # require 'chinese' | |
32 # pdf = FPDF.new | |
33 # pdf.extend(PDF_Korean) | |
34 # | |
35 # This allows it to be combined with other extensions, such as the bookmark | |
36 # module. | |
37 | |
38 module PDF_Korean | |
39 | |
40 UHC_widths={' ' => 333, '!' => 416, '"' => 416, '#' => 833, '$' => 625, '%' => 916, '&' => 833, '\'' => 250, | |
41 '(' => 500, ')' => 500, '*' => 500, '+' => 833, ',' => 291, '-' => 833, '.' => 291, '/' => 375, '0' => 625, '1' => 625, | |
42 '2' => 625, '3' => 625, '4' => 625, '5' => 625, '6' => 625, '7' => 625, '8' => 625, '9' => 625, ':' => 333, ';' => 333, | |
43 '<' => 833, '=' => 833, '>' => 916, '?' => 500, '@' => 1000, 'A' => 791, 'B' => 708, 'C' => 708, 'D' => 750, 'E' => 708, | |
44 'F' => 666, 'G' => 750, 'H' => 791, 'I' => 375, 'J' => 500, 'K' => 791, 'L' => 666, 'M' => 916, 'N' => 791, 'O' => 750, | |
45 'P' => 666, 'Q' => 750, 'R' => 708, 'S' => 666, 'T' => 791, 'U' => 791, 'V' => 750, 'W' => 1000, 'X' => 708, 'Y' => 708, | |
46 'Z' => 666, '[' => 500, '\\' => 375, ']' => 500, '^' => 500, '_' => 500, '`' => 333, 'a' => 541, 'b' => 583, 'c' => 541, | |
47 'd' => 583, 'e' => 583, 'f' => 375, 'g' => 583, 'h' => 583, 'i' => 291, 'j' => 333, 'k' => 583, 'l' => 291, 'm' => 875, | |
48 'n' => 583, 'o' => 583, 'p' => 583, 'q' => 583, 'r' => 458, 's' => 541, 't' => 375, 'u' => 583, 'v' => 583, 'w' => 833, | |
49 'x' => 625, 'y' => 625, 'z' => 500, '{' => 583, '|' => 583, '}' => 583, '~' => 750} | |
50 | |
51 def AddCIDFont(family,style,name,cw,cMap,registry) | |
52 fontkey=family.downcase+style.upcase | |
53 unless @fonts[fontkey].nil? | |
54 Error("Font already added: family style") | |
55 end | |
56 i=@fonts.length+1 | |
57 name=name.gsub(' ','') | |
58 @fonts[fontkey]={'i'=>i,'type'=>'Type0','name'=>name,'up'=>-130,'ut'=>40,'cw'=>cw, | |
59 'CMap'=>cMap,'registry'=>registry} | |
60 end | |
61 | |
62 def AddCIDFonts(family,name,cw,cMap,registry) | |
63 AddCIDFont(family,'',name,cw,cMap,registry) | |
64 AddCIDFont(family,'B',name+',Bold',cw,cMap,registry) | |
65 AddCIDFont(family,'I',name+',Italic',cw,cMap,registry) | |
66 AddCIDFont(family,'BI',name+',BoldItalic',cw,cMap,registry) | |
67 end | |
68 | |
69 def AddUHCFont(family='UHC',name='HYSMyeongJoStd-Medium-Acro') | |
70 #Add UHC font with proportional Latin | |
71 cw=UHC_widths | |
72 cMap='KSCms-UHC-H' | |
73 registry={'ordering'=>'Korea1','supplement'=>1} | |
74 AddCIDFonts(family,name,cw,cMap,registry) | |
75 end | |
76 | |
77 def AddUHChwFont(family='UHC-hw',name='HYSMyeongJoStd-Medium-Acro') | |
78 #Add UHC font with half-witdh Latin | |
79 32.upto(126) do |i| | |
80 cw[i.chr]=500 | |
81 end | |
82 cMap='KSCms-UHC-HW-H' | |
83 registry={'ordering'=>'Korea1','supplement'=>1} | |
84 AddCIDFonts(family,name,cw,cMap,registry) | |
85 end | |
86 | |
87 def GetStringWidth(s) | |
88 if(@current_font['type']=='Type0') | |
89 return GetMBStringWidth(s) | |
90 else | |
91 return super(s) | |
92 end | |
93 end | |
94 | |
95 def GetMBStringWidth(s) | |
96 #Multi-byte version of GetStringWidth() | |
97 l=0 | |
98 cw=@current_font['cw'] | |
99 nb=s.length | |
100 i=0 | |
101 while(i<nb) | |
102 c = s[i].is_a?(String) ? s[i].ord : s[i] | |
103 if(c<128) | |
104 l+=cw[c.chr] if cw[c.chr] | |
105 i+=1 | |
106 else | |
107 l+=1000 | |
108 i+=2 | |
109 end | |
110 end | |
111 return l*@font_size/1000 | |
112 end | |
113 | |
114 def MultiCell(w,h,txt,border=0,align='L',fill=0,ln=1) | |
115 if(@current_font['type']=='Type0') | |
116 MBMultiCell(w,h,txt,border,align,fill) | |
117 else | |
118 super(w,h,txt,border,align,fill) | |
119 end | |
120 end | |
121 | |
122 def MBMultiCell(w,h,txt,border=0,align='L',fill=0) | |
123 #Multi-byte version of MultiCell() | |
124 cw=@current_font['cw'] | |
125 if(w==0) | |
126 w=@w-@r_margin-@x | |
127 end | |
128 wmax=(w-2*@c_margin)*1000/@font_size | |
129 s=txt.gsub("\r",'') | |
130 nb=s.length | |
131 if(nb>0 and s[nb-1]=="\n") | |
132 nb-=1 | |
133 end | |
134 b=0 | |
135 if(border) | |
136 if(border==1) | |
137 border='LTRB' | |
138 b='LRT' | |
139 b2='LR' | |
140 else | |
141 b2='' | |
142 b2='L' unless border.to_s.index('L').nil? | |
143 b2=b2+'R' unless border.to_s.index('R').nil? | |
144 b=(border.to_s.index('T')) ? (b2+'T') : b2 | |
145 end | |
146 end | |
147 sep=-1 | |
148 i=0 | |
149 j=0 | |
150 l=0 | |
151 nl=1 | |
152 while(i<nb) | |
153 #Get next character | |
154 c = s[i].is_a?(String) ? s[i].ord : s[i] | |
155 #Check if ASCII or MB | |
156 ascii=(c<128) | |
157 if(c.chr=="\n") | |
158 #Explicit line break | |
159 Cell(w,h,s[j,i-j],b,2,align,fill) | |
160 i+=1 | |
161 sep=-1 | |
162 j=i | |
163 l=0 | |
164 nl+=1 | |
165 if(border and nl==2) | |
166 b=b2 | |
167 end | |
168 next | |
169 end | |
170 if(!ascii) | |
171 sep=i | |
172 ls=l | |
173 elsif(c.chr==' ') | |
174 sep=i | |
175 ls=l | |
176 end | |
177 l+=(ascii ? cw[c.chr] : 1000) || 0 | |
178 if(l>wmax) | |
179 #Automatic line break | |
180 if(sep==-1 or i==j) | |
181 if(i==j) | |
182 i+=ascii ? 1 : 2 | |
183 end | |
184 Cell(w,h,s[j,i-j],b,2,align,fill) | |
185 else | |
186 Cell(w,h,s[j,sep-j],b,2,align,fill) | |
187 i=(s[sep].chr==' ') ? sep+1 : sep | |
188 end | |
189 sep=-1 | |
190 j=i | |
191 l=0 | |
192 nl+=1 | |
193 if(border and nl==2) | |
194 b=b2 | |
195 end | |
196 else | |
197 i+=ascii ? 1 : 2 | |
198 end | |
199 end | |
200 #Last chunk | |
201 if(border and not border.to_s.index('B').nil?) | |
202 b+='B' | |
203 end | |
204 Cell(w,h,s[j,i-j],b,2,align,fill) | |
205 @x=@l_margin | |
206 end | |
207 | |
208 def Write(h,txt,link='',fill=0) | |
209 if(@current_font['type']=='Type0') | |
210 MBWrite(h,txt,link) | |
211 else | |
212 super(h,txt,link) | |
213 end | |
214 end | |
215 | |
216 def MBWrite(h,txt,link) | |
217 #Multi-byte version of Write() | |
218 cw=@current_font['cw'] | |
219 w=@w-@r_margin-@x | |
220 wmax=(w-2*@c_margin)*1000/@font_size | |
221 s=txt.gsub("\r",'') | |
222 nb=s.length | |
223 sep=-1 | |
224 i=0 | |
225 j=0 | |
226 l=0 | |
227 nl=1 | |
228 while(i<nb) | |
229 #Get next character | |
230 c = s[i].is_a?(String) ? s[i].ord : s[i] | |
231 #Check if ASCII or MB | |
232 ascii=(c<128) | |
233 if(c.chr=="\n") | |
234 #Explicit line break | |
235 Cell(w,h,s[j,i-j],0,2,'',0,link) | |
236 i+=1 | |
237 sep=-1 | |
238 j=i | |
239 l=0 | |
240 if(nl==1) | |
241 @x=@l_margin | |
242 w=@w-@r_margin-@x | |
243 wmax=(w-2*@c_margin)*1000/@font_size | |
244 end | |
245 nl+=1 | |
246 next | |
247 end | |
248 if(!ascii or c.chr==' ') | |
249 sep=i | |
250 end | |
251 l+=(ascii ? cw[c.chr] : 1000) || 0 | |
252 if(l>wmax) | |
253 #Automatic line break | |
254 if(sep==-1 or i==j) | |
255 if(@x>@l_margin) | |
256 #Move to next line | |
257 @x=@l_margin | |
258 @y+=h | |
259 w=@w-@r_margin-@x | |
260 wmax=(w-2*@c_margin)*1000/@font_size | |
261 i+=1 | |
262 nl+=1 | |
263 next | |
264 end | |
265 if(i==j) | |
266 i+=ascii ? 1 : 2 | |
267 end | |
268 Cell(w,h,s[j,i-j],0,2,'',0,link) | |
269 else | |
270 Cell(w,h,s[j,sep-j],0,2,'',0,link) | |
271 i=(s[sep].chr==' ') ? sep+1 : sep | |
272 end | |
273 sep=-1 | |
274 j=i | |
275 l=0 | |
276 if(nl==1) | |
277 @x=@l_margin | |
278 w=@w-@r_margin-@x | |
279 wmax=(w-2*@c_margin)*1000/@font_size | |
280 end | |
281 nl+=1 | |
282 else | |
283 i+=ascii ? 1 : 2 | |
284 end | |
285 end | |
286 #Last chunk | |
287 if(i!=j) | |
288 Cell(l/1000*@font_size,h,s[j,i-j],0,0,'',0,link) | |
289 end | |
290 end | |
291 | |
292 private | |
293 | |
294 def putfonts() | |
295 nf=@n | |
296 @diffs.each do |diff| | |
297 #Encodings | |
298 newobj() | |
299 out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['+diff+']>>') | |
300 out('endobj') | |
301 end | |
302 # mqr=get_magic_quotes_runtime() | |
303 # set_magic_quotes_runtime(0) | |
304 @font_files.each_pair do |file, info| | |
305 #Font file embedding | |
306 newobj() | |
307 @font_files[file]['n']=@n | |
308 if(defined('FPDF_FONTPATH')) | |
309 file=FPDF_FONTPATH+file | |
310 end | |
311 size=filesize(file) | |
312 if(!size) | |
313 Error('Font file not found') | |
314 end | |
315 out('<</Length '+size) | |
316 if(file[-2]=='.z') | |
317 out('/Filter /FlateDecode') | |
318 end | |
319 out('/Length1 '+info['length1']) | |
320 if(not info['length2'].nil?) | |
321 out('/Length2 '+info['length2']+' /Length3 0') | |
322 end | |
323 out('>>') | |
324 f=fopen(file,'rb') | |
325 putstream(fread(f,size)) | |
326 fclose(f) | |
327 out('endobj') | |
328 end | |
329 # set_magic_quotes_runtime(mqr) | |
330 @fonts.each_pair do |k, font| | |
331 #Font objects | |
332 newobj() | |
333 @fonts[k]['n']=@n | |
334 out('<</Type /Font') | |
335 if(font['type']=='Type0') | |
336 putType0(font) | |
337 else | |
338 name=font['name'] | |
339 out('/BaseFont /'+name) | |
340 if(font['type']=='core') | |
341 #Standard font | |
342 out('/Subtype /Type1') | |
343 if(name!='Symbol' and name!='ZapfDingbats') | |
344 out('/Encoding /WinAnsiEncoding') | |
345 end | |
346 else | |
347 #Additional font | |
348 out('/Subtype /'+font['type']) | |
349 out('/FirstChar 32') | |
350 out('/LastChar 255') | |
351 out('/Widths '+(@n+1)+' 0 R') | |
352 out('/FontDescriptor '+(@n+2)+' 0 R') | |
353 if(font['enc']) | |
354 if(not font['diff'].nil?) | |
355 out('/Encoding '+(nf+font['diff'])+' 0 R') | |
356 else | |
357 out('/Encoding /WinAnsiEncoding') | |
358 end | |
359 end | |
360 end | |
361 out('>>') | |
362 out('endobj') | |
363 if(font['type']!='core') | |
364 #Widths | |
365 newobj() | |
366 cw=font['cw'] | |
367 s='[' | |
368 32.upto(255) do |i| | |
369 s+=cw[i.chr]+' ' | |
370 end | |
371 out(s+']') | |
372 out('endobj') | |
373 #Descriptor | |
374 newobj() | |
375 s='<</Type /FontDescriptor /FontName /'+name | |
376 font['desc'].each_pair do |k, v| | |
377 s+=' /'+k+' '+v | |
378 end | |
379 file=font['file'] | |
380 if(file) | |
381 s+=' /FontFile'+(font['type']=='Type1' ? '' : '2')+' '+@font_files[file]['n']+' 0 R' | |
382 end | |
383 out(s+'>>') | |
384 out('endobj') | |
385 end | |
386 end | |
387 end | |
388 end | |
389 | |
390 def putType0(font) | |
391 #Type0 | |
392 out('/Subtype /Type0') | |
393 out('/BaseFont /'+font['name']+'-'+font['CMap']) | |
394 out('/Encoding /'+font['CMap']) | |
395 out('/DescendantFonts ['+(@n+1).to_s+' 0 R]') | |
396 out('>>') | |
397 out('endobj') | |
398 #CIDFont | |
399 newobj() | |
400 out('<</Type /Font') | |
401 out('/Subtype /CIDFontType0') | |
402 out('/BaseFont /'+font['name']) | |
403 out('/CIDSystemInfo <</Registry (Adobe) /Ordering ('+font['registry']['ordering']+') /Supplement '+font['registry']['supplement'].to_s+'>>') | |
404 out('/FontDescriptor '+(@n+1).to_s+' 0 R') | |
405 if(font['CMap']=='KSCms-UHC-HW-H') | |
406 w='8094 8190 500' | |
407 else | |
408 w='1 [' | |
409 font['cw'].keys.sort.each {|key| | |
410 w+=font['cw'][key].to_s + " " | |
411 # ActionController::Base::logger.debug key.to_s | |
412 # ActionController::Base::logger.debug font['cw'][key].to_s | |
413 } | |
414 w +=']' | |
415 end | |
416 out('/W ['+w+']>>') | |
417 out('endobj') | |
418 #Font descriptor | |
419 newobj() | |
420 out('<</Type /FontDescriptor') | |
421 out('/FontName /'+font['name']) | |
422 out('/Flags 6') | |
423 out('/FontBBox [0 -200 1000 900]') | |
424 out('/ItalicAngle 0') | |
425 out('/Ascent 800') | |
426 out('/Descent -200') | |
427 out('/CapHeight 800') | |
428 out('/StemV 50') | |
429 out('>>') | |
430 out('endobj') | |
431 end | |
432 end |