示例#1
0
文件: Font.cs 项目: xxami/Pulsus
        public Font(string path, int pointSize, FontStyle style = FontStyle.Normal, bool unicode = true)
        {
            cacheFonts     = SettingsManager.instance.engine.cacheFonts;
            this.pointSize = pointSize;
            this.style     = style;
            this.unicode   = unicode;

            if (SDL_ttf.TTF_WasInit() == 0)
            {
                if (SDL_ttf.TTF_Init() != 0)
                {
                    throw new ApplicationException("Failed to initialize SDL_ttf: " + SDL.SDL_GetError());
                }
            }

            handle = SDL_ttf.TTF_OpenFont(path, pointSize);
            if (handle == IntPtr.Zero)
            {
                throw new ApplicationException("Failed to load font: " + SDL.SDL_GetError());
            }

            SDL_ttf.TTF_SetFontStyle(handle, (int)style);

            familyName = SDL_ttf.TTF_FontFaceFamilyName(handle);

            ushort characterCount = unicode ? ushort.MaxValue : (ushort)256;

            bool generateTextures = true;

            if (cacheFonts)
            {
                if (LoadGlyphDataCached() && LoadTextureDataCached())
                {
                    Log.Info("Font: Using cached font data");
                    generateTextures = false;
                }
                else
                {
                    glyphs = null;
                    foreach (Texture2D texture in textures)
                    {
                        texture.Dispose();
                    }
                    textures.Clear();
                }
            }

            if (generateTextures)
            {
                GenerateGlyphData(characterCount);
                GenerateTextures(characterCount);

                if (cacheFonts)
                {
                    CacheGlyphData();
                }
            }

            loadedFonts++;
        }
        private bool CollectFontInfo(Font ifont)
        {
            IntPtr font = IntPtr.Zero;

            if (UseRWops && CreateRWopsCacheOnFontRegister)
            {
                font = SDL_ttf.TTF_OpenFontIndexRW(ifont.RWops, 1, 16, ifont.index);
            }
            else
            {
                font = SDL_ttf.TTF_OpenFontIndex(ifont.filename, 16, ifont.index);
            }

            if (font.ShowSDLError("FontManager.CollectFontInfo(): Failed to load font!"))
            {
                return(false);
            }
            else
            {
                int    fontfaces           = (int)SDL_ttf.TTF_FontFaces(font);
                string fontface_familyname = SDL_ttf.TTF_FontFaceFamilyName(font);
                string fontface_stylename  = SDL_ttf.TTF_FontFaceStyleName(font);
                bool   fontface_mono       = (SDL_ttf.TTF_FontFaceIsFixedWidth(font) > 0);
                int    font_style          = SDL_ttf.TTF_GetFontStyle(font);

                ifont.fontFamilyId = GetFontFamilyId(fontface_familyname, true);
                ifont.fontStyle    = font_style;
                ifont.mono         = fontface_mono;

                /*
                 * Console.WriteLine("  Hash:{0}", _fonts[font_id].hash);
                 * Console.WriteLine("  TTF_FontFaces: {0}", fontfaces);
                 * Console.WriteLine("  TTF_FontFaceIsFixedWidth: {0}", fontface_mono);
                 *
                 * Console.WriteLine("  TTF_FontFaceFamilyName: {0}", fontface_familyname);
                 * Console.WriteLine("  TTF_FontFaceStyleName: {0}", fontface_stylename);
                 * Console.WriteLine("  TTF_GetFontStyle: {0}", font_style);
                 */

                if (ifont.index == 0 && fontfaces > 1)
                {
                    for (int index = 1; index < fontfaces; index++)
                    {
                        //Console.WriteLine("         index: {0}", index);
                        Font sub_ifont = ifont.Clone();
                        sub_ifont.index = index;
                        if (CollectFontInfo(sub_ifont))
                        {
                            _fonts.Add(sub_ifont);
                        }
                    }
                }
                SDL_ttf.TTF_CloseFont(font);
                return(true);
            }
        }
示例#3
0
 public string FontFaceFamilyName()
 {
     return(SDL_ttf.TTF_FontFaceFamilyName(myPtr));
 }