chinese.rb.patch

Jun NAITOH, 2011-03-21 04:12

Download (3.14 KB)

View differences:

vendor/plugins/rfpdf/lib/rfpdf/chinese.rb 2011-03-19 01:44:43.000000000 -0400
147 147
  		MBMultiCell(w,h,txt,border,align,fill)
148 148
  	else
149 149
  		super(w,h,txt,border,align,fill)
150
		end
150
  	end  
151 151
  end
152 152

  
153 153
  def MBMultiCell(w,h,txt,border=0,align='L',fill=0)
154
  	#Multi-byte version of MultiCell()
154
  	#Multi-byte version of MultiCell() for UTF-8
155
  	#Output text with automatic or explicit line breaks
155 156
  	cw=@CurrentFont['cw']
156 157
  	if(w==0)
157 158
  		w=@w-@rMargin-@x
158
		end
159
  	end  
159 160
  	wmax=(w-2*@cMargin)*1000/@FontSize
160 161
  	s=txt.gsub("\r",'')
161 162
  	nb=s.length
162 163
  	if(nb>0 and s[nb-1]=="\n")
163 164
  		nb-=1
164
		end
165
  	end  
165 166
  	b=0
166 167
  	if(border)
167 168
  		if(border==1)
......
172 173
  			b2=''
173 174
  			if(border.to_s.index('L'))
174 175
  				b2+='L'
175
				end
176
  			end
176 177
  			if(border.to_s.index('R'))
177 178
  				b2+='R'
178
				end
179
  			end
179 180
  			b=border.to_s.index('T') ? b2+'T' : b2
180 181
  		end
181 182
  	end
182 183
  	sep=-1
183 184
  	i=0
184 185
  	j=0
185
  	l=0
186
  	l=0    # Automatic line break counter
186 187
  	nl=1
187 188
  	while(i<nb)
188
  		#Get next character
189
  		c=s[i]
190
  		#Check if ASCII or MB
191
  		ascii=(c<128)
192
  		if(c.chr=="\n")
193
  			#Explicit line break
189
  		case s[i]
190
  		when 0x00 .. 0x09, 0x0b .. 0x7f	# UTF-8 1byte ASCII
191
  			ascii=true
192
	  		n=1
193
  			l+=cw[s[i].chr] || 0
194
  			if(s[i].chr==' ')
195
  				sep=i
196
  			end
197
  		when 0x0a	# Explicit line break "\n"
194 198
  			Cell(w,h,s[j,i-j],b,2,align,fill)
195 199
  			i+=1
196 200
  			sep=-1
......
199 203
  			nl+=1
200 204
  			if(border and nl==2)
201 205
  				b=b2
202
				end
206
  			end
203 207
  			next
204
  		end
205
  		if(!ascii)
208
  		when 0xc0 .. 0xdf	# UTF-8 2byte
209
  			n=2
210
  			l+=500
206 211
  			sep=i
207
  			ls=l
208
  		elsif(c==' ')
212
  		when 0xe0 .. 0xee	# UTF-8 3byte
213
  			n=3
214
  			l+=1000		# Full-width character
209 215
  			sep=i
210
  			ls=l
216
  		when 0xef		# UTF-8 3byte
217
  			n=3
218
  			if((s[i+1]==0xbd and (s[i+2]>=0xa1 and s[i+2]<=0xbf)) or (s[i+1]==0xbe and(s[i+2]>=0x80 and s[i+2]<=0x9f)))
219
  				l+=500	# Half-width katakana (UTF-8: EFBDA1 - EFBDBF, EFBE80 - EFBE9F)
220
  			else
221
  				l+=1000	# Full-width character
222
  			end
223
  			sep=i
224
  		when 0xf0 .. 0xf7	# UTF-8 4byte
225
  			n=4
226
  			l+=1000
227
  			sep=i
228
  		when 0xf8 .. 0xfb	# UTF-8 5byte
229
  			n=5
230
  			l+=1000
231
  			sep=i
232
  		when 0xfc .. 0xfd	# UTF-8 6byte
233
  			n=6
234
  			l+=1000
235
  			sep=i
236
  		else
237
	  		i+=1
238
  			next
211 239
  		end
212
  		l+=ascii ? (cw[c.chr] || 0) : 1100
240

  
213 241
  		if(l>wmax)
214 242
  			#Automatic line break
215 243
  			if(sep==-1 or i==j)
216 244
  				if(i==j)
217
  					i+=ascii ? 1 : 3
218
					end
245
  					i+=n
246
  				end
219 247
  				Cell(w,h,s[j,i-j],b,2,align,fill)
220 248
  			else
221 249
  				Cell(w,h,s[j,sep-j],b,2,align,fill)
......
224 252
  			sep=-1
225 253
  			j=i
226 254
  			l=0
227
#  			nl+=1
255
  			nl+=1
228 256
  			if(border and nl==2)
229 257
  				b=b2
230 258
  			end
231 259
  		else
232
  			i+=ascii ? 1 : 3
260
  			i+=n
261
  			if(!ascii)
262
  				sep=i
263
  			end
233 264
  		end
234 265
  	end
235 266
  	#Last chunk
236 267
  	if(border and not border.to_s.index('B').nil?)
237 268
  		b+='B'
238
		end
269
  	end  
239 270
  	Cell(w,h,s[j,i-j],b,2,align,fill)
240 271
  	@x=@lMargin
241 272
  end