Method: FPDF#AddFont

Defined in:
lib/fpdf.rb

#AddFont(family, style = '', file = '') ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/fpdf.rb', line 412

def AddFont(family, style='', file='')
     # Add a TrueType or Type1 font
     family = family.downcase
     family = 'helvetica' if family == 'arial'

     style = style.upcase
     style = 'BI' if style == 'IB'

    fontkey = family + style

    if @fonts.has_key?(fontkey)
         self.Error("Font already added: #{family} #{style}")
    end

    file = family.gsub(' ', '') + style.downcase + '.rb' if file == ''

    if self.class.const_defined? 'FPDF_FONTPATH'
        if FPDF_FONTPATH[-1,1] == '/'
            file = FPDF_FONTPATH + file
        else
            file = FPDF_FONTPATH + '/' + file
        end
    end
    
    # Changed from "require file" to fix bug reported by Hans Allis.
    load file

    if FontDef.desc.nil?
       self.Error("Could not include font definition file #{file}")
    end

    i = @fonts.length + 1

    @fonts[fontkey] = {'i'   => i,
                      'type' => FontDef.type,
                      'name' => FontDef.name,
                      'desc' => FontDef.desc,
                        'up' => FontDef.up,
                        'ut' => FontDef.ut,
                        'cw' => FontDef.cw,
                       'enc' => FontDef.enc,
                      'file' => FontDef.file
                   }

    if FontDef.diff
        # Search existing encodings
        idx = @diffs.index(FontDef.diff)
        
        unless idx
            @diffs.push(FontDef.diff)
            @fonts[fontkey]['diff'] = @diffs.length
        else
            @fonts[fontkey]['diff'] = idx + 1
        end
    end

    if FontDef.file
         if FontDef.type == 'TrueType'
             @FontFiles[FontDef.file] = {'length1' => FontDef.originalsize}
         else
             @FontFiles[FontDef.file] = {'length1' => FontDef.size1, 'length2' => FontDef.size2}
        end
    end

    return self
end