示例#1
0
        /// <summary>
        /// Creates a new VectorFont with the File specified at path and the specified size in pixels.
        /// It is assumed that the font is either Monospaced or only single chars are used.
        /// </summary>
        /// <param name="path">path to the font-file</param>
        /// <param name="size">size in pixles</param>
        public VectorFont(string path, uint size)
        {
            lib = new Library();

            face = new Face(lib, path);

            face.SetPixelSizes(0, size);

            this.size = face.Size;
        }
示例#2
0
        public FontDef(Face face, int size)
        {
            const int atlasSize = 1024;
            atlas = new RenderTarget2D(GraphicsManager.device, atlasSize, atlasSize, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
            fontFace = face;
            pxSize = size;

            face.SetPixelSizes((uint)size, (uint)size);
            face.LoadGlyph(face.GetCharIndex('A'), loadFlags, loadTarget);
            lineSize = (int)Math.Ceiling((float)face.Glyph.Metrics.Height * 2f);

            const string defaultCharset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ!@#$%^&*()`-=~_+[]{}\\|;',./:\"<>? ";
            foreach (char c in defaultCharset) AddGlyph(c);
        }
        public FontFaceRenderMap(SharpFont.Face face, float size)
        {
            if (face == null)
            {
                throw new System.ArgumentNullException(nameof(face));
            }

            Face = face;
            Face.SetPixelSizes((uint)Util.Math.Round(size), (uint)Util.Math.Round(size));

            NominalWidth  = Face.Size.Metrics.NominalWidth;
            NominalHeight = Face.Size.Metrics.NominalHeight;

            Load();
        }
示例#4
0
        public TextGenerator(string path, string fontName, uint fontSize = 12)
        {
            if (_textPs == null)
            {
                LoadProgramShader();
            }

            FontName = fontName;
            FontSize = fontSize;

            SharpFont.Library fontLib  = new SharpFont.Library();
            SharpFont.Face    fontFace = new SharpFont.Face(fontLib, path);
            fontFace.SetPixelSizes(0, FontSize);

            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);

            for (char c = (char)0x20; c <= (char)0x7E; c++)
            {
                fontFace.LoadChar(c, LoadFlags.Render, LoadTarget.Normal);

                Texture texture = Texture.CreateTexture(
                    fontFace.Glyph.Bitmap.Width,
                    fontFace.Glyph.Bitmap.Rows,
                    PixelInternalFormat.R8,
                    PixelFormat.Red,
                    fontFace.Glyph.Bitmap.Buffer);

                Character character = new Character(
                    texture.ID,
                    new Vector2(fontFace.Glyph.Bitmap.Width, fontFace.Glyph.Bitmap.Rows),
                    new Vector2(fontFace.Glyph.BitmapLeft, fontFace.Glyph.BitmapTop),
                    (int)fontFace.Glyph.Advance.X);

                _characters.Add(c, character);
            }

            fontFace.Dispose();
            fontLib.Dispose();

            vao = VAO.Create();
            vao.LinkPS(_textPs);

            vbo     = VBO.Create <float>(vertices.Length, 4, "vertex");
            indices = new IndicesCollection();
            indices.Add(4, vertices.Length);
            vao.AddVBuff(vbo);
            vao.AddIndicesCollection(indices);
        }
示例#5
0
        public SpriteFont(string name, int size)
        {
            this.size = size;

            face = library.NewFace(name, 0);
            face.SetPixelSizes((uint)size, (uint)size);

            glyphs = new Cache<Pair<char, Color>, GlyphInfo>(CreateGlyph,
                     Pair<char,Color>.EqualityComparer);

            // setup a 1-channel SheetBuilder for our private use
            if (builder == null) builder = new SheetBuilder(TextureChannel.Alpha);

            PrecacheColor(Color.White);
            PrecacheColor(Color.Red);
        }
示例#6
0
        public SpriteFont(string name, int size)
        {
            this.size = size;

            face = library.NewFace(name, 0);
            face.SetPixelSizes((uint)size, (uint)size);

            glyphs = new Cache<Pair<char, Color>, GlyphInfo>(CreateGlyph,
                     Pair<char,Color>.EqualityComparer);

            // setup a SheetBuilder for our private use
            // TODO: SheetBuilder state is leaked between mod switches
            if (builder == null)
                builder = new SheetBuilder(SheetType.BGRA);

            PrecacheColor(Color.White);
            PrecacheColor(Color.Red);
        }
示例#7
0
        private bool SetCurrentSize(int characterSize)
        {
            // FT_Set_Pixel_Sizes is an expensive function, so we must call it
            // only when necessary to avoid killing performances
            ushort currentSize = _face.Size.Metrics.NominalWidth;

            if (currentSize != characterSize)
            {
                try
                {
                    _face.SetPixelSizes(0, (uint)characterSize);
                }
                catch (SharpFont.FreeTypeException ex)
                {
                    SharpFont.Error result = ex.Error;
                    if (result == SharpFont.Error.InvalidPixelSize)
                    {
                        // In the case of bitmap fonts, resizing can
                        // fail if the requested size is not available
                        if (!_face.IsScalable)
                        {
                            string exceptionMessage;
                            exceptionMessage  = "Failed to set bitmap font size to " + characterSize + Environment.NewLine;
                            exceptionMessage += "Available sizes are: ";
                            for (int i = 0; i < _face.FixedSizesCount; ++i)
                            {
                                exceptionMessage += _face.AvailableSizes[i].Height.ToString() + " ";
                            }
                            exceptionMessage += Environment.NewLine;

                            Logger.Warning(exceptionMessage);
                        }
                    }

                    return(result == SharpFont.Error.Ok);
                }

                return(true);
            }
            else
            {
                return(true);
            }
        }
示例#8
0
        public SpriteFont(string name, int size, SheetBuilder builder)
        {
            if (builder.Type != SheetType.BGRA)
                throw new ArgumentException("The sheet builder must create BGRA sheets.", "builder");

            this.size = size;
            this.builder = builder;

            face = new Face(Library, name);
            face.SetPixelSizes((uint)size, (uint)size);

            glyphs = new Cache<Pair<char, Color>, GlyphInfo>(CreateGlyph, Pair<char, Color>.EqualityComparer);

            Func<char, float> characterWidth = character => glyphs[Pair.New(character, Color.White)].Advance;
            lineWidth = line => line.Sum(characterWidth);

            PrecacheColor(Color.White, name);
            PrecacheColor(Color.Red, name);
        }
示例#9
0
        private void InitFont(float scaleFactor)
        {
            try {
                System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();

                ScaleFactor   = scaleFactor;
                YOffsetScaled = YOffset * scaleFactor;

                // Reset everything
                Clear();

                Font = new Face(FontManager.Library, FilePath);

                // Go on

                float size = Size.Scale(ScaleFactor);

                Fixed26Dot6 sz = new Fixed26Dot6(size / 64);
                Font.SetCharSize(sz, sz, 72, 72);

                int pixelSize = (size * 1.3334).Ceil();
                Font.SetPixelSizes((uint)pixelSize, (uint)pixelSize);

                GlyphCount = Font.GlyphCount;
                int glyphCount = GlyphCount;
                Monospace = Font.FaceFlags.HasFlag(FaceFlags.FixedWidth);

                string tmpName = Font.GetPostscriptName();
                if (!String.IsNullOrEmpty(tmpName))
                {
                    Name = tmpName;
                }


                // We support 4 different glyph loading strategies:
                //
                // (1) All: all glyphs loaded at once on start
                // (2) Filtered: all filtered glyphs loaded at once on start
                // (3) OnDemand: no glyphs loaded at start, all glyphs on demand


                if (OnDemand)
                {
                    // Startegy (3)
                    GlyphCount = 0;
                }
                else if (Filter > GlyphFilterFlags.OnDemand)
                {
                    // Startegy (2)
                    // If we have a Filter set, let's count the number of valid glyphs
                    // to minimize graphics memory.
                    uint glyphindex;
                    uint cc    = Font.GetFirstChar(out glyphindex);
                    int  count = 0;
                    while (glyphindex > 0)
                    {
                        char c = (char)cc;
                        if (Filter.IsValid(c))
                        {
                            count++;
                        }
                        cc = Font.GetNextChar(cc, out glyphindex);
                    }
                    GlyphCount = count;
                }
                else
                {
                    // Strategy (1), loading the entire font
                }

                m_Textures = new int[Math.Max(32, GlyphCount)];
                CharMap    = new ThreadSafeDictionary <char, GlyphInfo>(Math.Max(31, GlyphCount));

                if (!OnDemand)
                {
                    // Strategy (1) + (2): Load all or filtered glyphs
                    m_ListBase = GL.GenLists(GlyphCount);
                    GL.GenTextures(GlyphCount, m_Textures);

                    uint glyphindex;
                    uint cc = Font.GetFirstChar(out glyphindex);
                    while (glyphindex > 0)
                    {
                        char c = (char)cc;
                        if (!CharMap.ContainsKey(c) && Filter.IsValid(c))
                        {
                            try {
                                CharMap.Add(c, CompileCharacter(Font, glyphindex, c));
                            } catch (Exception ex) {
                                ex.LogWarning();
                            }
                        }
                        cc = Font.GetNextChar(cc, out glyphindex);
                    }
                    CharMap.TryGetValue(SpecialCharacters.Ellipsis, out m_EllipsisGlyphIndex);
                }
                else
                {
                    try {
                        GetGlyphIndex(SpecialCharacters.Ellipsis, out m_EllipsisGlyphIndex);
                    } catch (Exception ex) {
                        ex.LogError();
                    }
                }

                //if (Height <= 1)
                //Height = pixelSize.NextPowerOf2();
                //Height = pixelSize * 1.33335f;

                Height = pixelSize;

                float fscale = Height / Font.Height * 1.33334f;
                //float fscale = Height / Font.Height * 0.776f;

                Ascender  = Font.Ascender * fscale;
                Descender = Font.Descender * fscale;
                //HalfHeight = Height / 2;

                Height     = (Ascender).Ceil();
                HalfHeight = (int)(Height / 2);

                //LineHeight = Height * 1.42f * LineSpacing;
                LineHeight = (int)((Height * 1.42f * LineSpacing) + 0.5f);

                //TextBoxHeight = ((Height * 2f) + (ScaleFactor * 2f)).Ceil();
                //TextBoxHeight = (int)(Height * 1.85f + 0.5f);
                TextBoxHeight = (int)(Height * 1.85f + 2);
                CaptionHeight = (int)(Height * 1.55 + 2);

                YOffsetScaled = (YOffset * ScaleFactor) - HalfHeight;

                if (OnDemand)
                {
                    Count.LogInformation("Font {0} ({1}), {2}/{3} glyphs pre-loaded in {4} ms, more glyphs are loaded on demand.", Name, Size, Count, glyphCount, sw.ElapsedMilliseconds);
                }
                else
                {
                    Count.LogInformation("Font {0} ({1}), {2}/{3} glyphs loaded in {4} ms.", Name, Size, Count, glyphCount, sw.ElapsedMilliseconds);
                }
            } catch (Exception ex) {
                ex.LogError();
            } finally {
                if (!OnDemand && Font != null)
                {
                    Font.Dispose();
                    Font = null;
                }
            }
        }