Skip to content
This repository
tree: b9da8ebedd
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 57 lines (42 sloc) 1.551 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
PDFFont = require '../font'

module.exports =
    initFonts: ->
        # Lookup table for embedded fonts
        @_fontFamilies = {}
        @_fontCount = 0
        
        # Font state
        @_fontSize = 12
        @_font = null
        
        @_registeredFonts = {}
        
        # Set the default font
        @font 'Helvetica'
        
    font: (filename, family, size) ->
        if typeof family is 'number'
            size = family
            family = null
        
        if @_registeredFonts[filename]
            {filename, family} = @_registeredFonts[filename]
        
        @fontSize size if size?
        family ?= filename
        
        if @_fontFamilies[family]
            @_font = @_fontFamilies[family]
            return this
            
        id = 'F' + (++@_fontCount)
        @_font = new PDFFont(this, filename, family, id)
        @_fontFamilies[family] = @_font
        
        return this
        
    fontSize: (@_fontSize) ->
        return this
        
    widthOfString: (string) ->
        @_font.widthOfString string, @_fontSize
        
    currentLineHeight: (includeGap = false) ->
        @_font.lineHeight @_fontSize, includeGap
        
    registerFont: (name, path, family) ->
        @_registeredFonts[name] =
            filename: path
            family: family
            
    embedFonts: (fn) ->
        fonts = (font for family, font of @_fontFamilies)
        do proceed = =>
            return fn() if fonts.length is 0
            fonts.shift().embed(proceed)
Something went wrong with that request. Please try again.