Method: PDF_Chinese#MBWrite

Defined in:
lib/chinese.rb

#MBWrite(h, txt, link) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/chinese.rb', line 225

def MBWrite(h,txt,link)
    # Multi-byte version of Write()
    cw=@CurrentFont['cw']
    w=@w-@rMargin-@x
    wmax=(w-2*@cMargin)*1000/@FontSize
    s=txt.sub("\r", ' ')
    nb=s.length
    sep=-1
    i=0
    j=0
    l=0 
    nl=1
    while i<nb
        # Get next character
        c=s[i]
        # Check if ASCII or MB
        ascii = false
        if c < 128
            ascii = true
        end
        if c.chr=="\n"
            #Explicit line break
            Cell(w,h,s[j..i-j],0,2,'',0,link)
            i+=1
            sep=-1
            j=i
            l=0
            if nl==1
                @x=@lMargin
                w=@w-@rMargin-@x
                wmax=(w-2*@cMargin)*1000/@FontSize
            end
            nl+=1
            next
        end
        sep=i if not ascii or c.chr==' '
        if ascii
            l += cw[c.chr]
        else
            l += 1000
        end
        if l>wmax
            # Automatic line break
            if sep==-1 or i==j
                if @x>@lMargin
                    # Move to next line
                    @x=@lMargin
                    @y+=h
                    w=@w-@rMargin-@x
                    wmax=(w-2*@cMargin)*1000/@FontSize
                    i+=1
                    nl+=1
                    next
                end
                i+=ascii ? 1 : 2 if i==j
                Cell(w,h,s[j..i-j],0,2,'',0,link)
            else
                Cell(w,h,s[j..sep-j],0,2,'',0,link)
                i=(s[sep]==' ') ? sep+1 : sep
            end
            sep=-1
            j=i
            l=0
            if nl==1
                @x=@lMargin
                w=@w-@rMargin-@x
                wmax=(w-2*@cMargin)*1000/@FontSize
            end
            nl+=1
        else
            i+=ascii ? 1 : 2
        end
    end

    #Last chunk
    if i!=j
        Cell(l/1000*@FontSize,h,s[j..i-j],0,0,'',0,link)
    end
end