private Map<TexFontBuilder,String> doBuildTypefacePackage( File dir, String packageName ) throws IOException {

        if( packageName == null )
            packageName = mFamilyName;

        dir.mkdirs();

        // Only build fonts once.
        Map<TexFontBuilder,String> nameMap = new HashMap<TexFontBuilder,String>();

        StringBuilder shapeString = new StringBuilder();
        StringBuilder mapString   = new StringBuilder();
        FileGarbage garbage       = new FileGarbage();


        // Build fonts and assign internal names.
        for( Map.Entry<Type,TexFontBuilder> e : mFontMap.entrySet() ) {
            Type type         = e.getKey();
            TexFontBuilder fb = e.getValue();

            String name = nameMap.get( fb );

            if( name == null ) {
                // Font must be built.
                name = String.format( "%s_%s_%s", mFamilyName, type.mSeries, type.mShape );
                nameMap.put( fb, name );

                // Build font and add all the resulting files.
                List<File> files = fb.buildFont( name, garbage );

                for( File f : files ) {
                    if( Files.suffix( f ) == "map" ) {
                        // Map files get combined into a single map file.
                        appendMapFile( f, mapString );

                    } else {
                        // Otherwise, just copy everything.
                        File outFile = new File( dir, f.getName() );
                        NativeFiles.copy( f, outFile );
                    }
                }
            }
        }


        PrintWriter out;

        // Write out map file.
        File mapFile = new File( dir, packageName + ".map" );

        out = new PrintWriter( mapFile );
        out.print( mapString.toString() );
        out.close();


        // Write out style file.
        File styleFile = new File( dir, packageName + ".sty" );

        out = new PrintWriter( styleFile );
        out.println( "\\NeedsTeXFormat{LaTeX2e}" );
        out.format( "\\ProvidesPackage{%s}[%s Package for loading font family %s]\n",
                    packageName,
                    new SimpleDateFormat( "yyyy/MM/dd" ).format( new Date() ),
                    mFamilyName );
        
        out.println( "\\RequirePackage[T1]{fontenc}" );
        out.println( "\\RequirePackage[utf8]{inputenc}\n" );
        out.format( "\\pdfmapfile{+%s}\n", mapFile.getName() );
        out.format( "\\DeclareFontFamily{T1}{%s}{}\n", mFamilyName );

        for( Map.Entry<Type,TexFontBuilder> e : mFontMap.entrySet() ) {
            out.format( "\\DeclareFontShape{T1}{%s}{%s}{%s}{ <-> %s }{}\n",
                        mFamilyName,
                        e.getKey().mSeries,
                        e.getKey().mShape,
                        nameMap.get( e.getValue() ) );
        }

        out.println();

        if( !mDefaultSet.isEmpty() ) {
            for( TexFamily fam : mDefaultSet ) {
                out.format( "\\renewcommand*{\\%s}{%s}\n", fam.defaultVariable(), mFamilyName );
            }

            out.println();
        }

        out.println( "\\endinput" );
        out.close();

        garbage.empty();
        return nameMap;
    }