Beispiel #1
0
        private static void InitLibrary()
        {
            Monitor.Enter(flag);
            try
            {
                if (libraryinitialized)
                {
                    return;
                }
                CheckFreeType(FT.FT_Init_FreeType(out FreeTypeLib));
                libraryinitialized = true;

                Strings npaths = GetFontDirectories();
                foreach (string ndir in npaths)
                {
                    if (Directory.Exists(ndir))
                    {
                        string[] nfiles = StreamUtil.GetFiles(ndir, "*.TTF|*.ttf|*.pf*", SearchOption.TopDirectoryOnly);
                        foreach (string nfile in nfiles)
                        {
                            IntPtr     iface = (IntPtr)0;
                            FT_FaceRec aface = new FT_FaceRec();
                            CheckFreeType(FT.FT_New_Face(FreeTypeLib, nfile, 0, out iface));
                            try
                            {
                                aface = (FT_FaceRec)Marshal.PtrToStructure(iface, typeof(FT_FaceRec));
                                if ((aface.face_flags & (int)FT_Face_Flags.FT_FACE_FLAG_SCALABLE) != 0)
                                {
                                    LogFontFt aobj = new LogFontFt();
                                    aobj.ftlibrary = FreeTypeLib;
                                    aobj.fullinfo  = false;
                                    // Fill font properties
                                    aobj.type1 = ((int)FT_Face_Flags.FT_FACE_FLAG_SFNT & aface.face_flags) == 0;
                                    if (aobj.type1)
                                    {
                                        aobj.convfactor = 1;
                                        //       aobj.convfactor:=1000/aface.units_per_EM;
                                        aobj.widthmult = aobj.convfactor;
                                    }
                                    else
                                    {
                                        aobj.convfactor = 1;
                                        //       aobj.convfactor:=1000/aface.units_per_EM;
                                        aobj.widthmult = aobj.convfactor;
                                    }
                                    aobj.filename = nfile;
                                    string family_name = Marshal.PtrToStringAnsi(aface.family_name);
                                    aobj.postcriptname = family_name.Replace(" ", "");
                                    aobj.familyname    = family_name;
                                    aobj.keyname       = family_name + "____";
                                    aobj.fixedpitch    = (aface.face_flags & (int)FT_Face_Flags.FT_FACE_FLAG_FIXED_WIDTH) != 0;
                                    aobj.havekerning   = (aface.face_flags & (int)FT_Face_Flags.FT_FACE_FLAG_KERNING) != 0;
                                    int nleft   = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.bbox.xMin));
                                    int nright  = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.bbox.xMax));
                                    int ntop    = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.bbox.yMax));
                                    int nbottom = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.bbox.yMin));
                                    aobj.BBox      = new Rectangle(nleft, ntop, nright - nleft, nbottom - ntop);
                                    aobj.ascent    = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.ascender));
                                    aobj.descent   = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.descender));
                                    aobj.leading   = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.height) - (aobj.ascent - aobj.descent));
                                    aobj.MaxWidth  = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.max_advance_width));
                                    aobj.Capheight = System.Convert.ToInt32(Math.Round(aobj.convfactor * aface.ascender));
                                    string style_name = Marshal.PtrToStringAnsi(aface.style_name);
                                    aobj.stylename = style_name;
                                    aobj.bold      = (aface.style_flags & (int)FT_Style_Flags.FT_STYLE_FLAG_BOLD) != 0;
                                    aobj.italic    = (aface.style_flags & (int)FT_Style_Flags.FT_STYLE_FLAG_ITALIC) != 0;
                                    if (aobj.bold)
                                    {
                                        aobj.keyname = aobj.keyname + "B1";
                                    }
                                    else
                                    {
                                        aobj.keyname = aobj.keyname + "B0";
                                    }

                                    if (aobj.italic)
                                    {
                                        aobj.keyname = aobj.keyname + "I1";
                                    }
                                    else
                                    {
                                        aobj.keyname = aobj.keyname + "I0";
                                    }
                                    // Default font configuration, LUXI SANS is default
                                    if ((!aobj.italic) && (!aobj.bold))
                                    {
                                        if (defaultfont == null)
                                        {
                                            defaultfont = aobj;
                                        }
                                        else
                                        {
                                            if (aobj.familyname.ToUpper() == "LUXI SANS")
                                            {
                                                defaultfont = aobj;
                                            }
                                        }
                                    }
                                    else
                                    if ((!aobj.italic) && (aobj.bold))
                                    {
                                        if (defaultfontb == null)
                                        {
                                            defaultfontb = aobj;
                                        }
                                        else
                                        {
                                            if (aobj.familyname.ToUpper() == "LUXI SANS")
                                            {
                                                defaultfontb = aobj;
                                            }
                                        }
                                    }
                                    else
                                    if ((aobj.italic) && (!aobj.bold))
                                    {
                                        if (defaultfontit == null)
                                        {
                                            defaultfontit = aobj;
                                        }
                                        else
                                        {
                                            if (aobj.familyname.ToUpper() == "LUXI SANS")
                                            {
                                                defaultfontit = aobj;
                                            }
                                        }
                                    }
                                    else
                                    if ((aobj.italic) && (aobj.bold))
                                    {
                                        if (defaultfontbit == null)
                                        {
                                            defaultfontbit = aobj;
                                        }
                                        else
                                        {
                                            if (aobj.familyname.ToUpper() == "LUXI SANS")
                                            {
                                                defaultfontbit = aobj;
                                            }
                                        }
                                    }

                                    aobj.keyname = aobj.keyname.ToUpper();
                                    if (fontlist.IndexOfKey(aobj.keyname) < 0)
                                    {
                                        fontlist.Add(aobj.keyname.ToUpper(), aobj);
                                    }
                                }
                                int nindex = fontfiles.IndexOfKey(nfile);
                                if (nindex < 0)
                                {
                                    fontfiles.Add(nfile, nfile);
                                }
                            }
                            finally
                            {
                                CheckFreeType(FT.FT_Done_Face(iface));
                            }
                        }
                    }
                }
            }
            finally
            {
                Monitor.Exit(flag);
            }
        }