protected override void OnInitGLProgram(object sender, EventArgs args)
        {
            int max = Math.Max(this.Width, this.Height);

            canvas2d = PixelFarm.Drawing.GLES2.GLES2Platform.CreateCanvasGL2d(max, max);
            painter  = new GLCanvasPainter(canvas2d, max, max);

            //---------------------
            string fontfilename = "d:\\WImageTest\\a_total.xml";
            var    atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder();

            fontAtlas = atlasBuilder.LoadFontInfo(fontfilename);

            totalImg = new System.Drawing.Bitmap("d:\\WImageTest\\a_total.png");

            var bmpdata = totalImg.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, totalImg.PixelFormat);
            var buffer  = new int[totalImg.Width * totalImg.Height];

            System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, buffer, 0, buffer.Length);
            totalImg.UnlockBits(bmpdata);
            var glyph = new Typography.Rendering.GlyphImage(totalImg.Width, totalImg.Height);

            glyph.SetImageBuffer(buffer, false);
            fontAtlas.TotalGlyph = glyph;



            //---------------------
        }
        public void ChangeFont(RequestFont font)
        {
            //from request font
            //we resolve it to actual font

            this.font = font;
            this._glyphLayout.ScriptLang = font.GetOpenFontScriptLang();

            SimpleFontAtlas foundFontAtlas;
            ActualFont      fontImp = ActiveFontAtlasService.GetTextureFontAtlasOrCreateNew(_fontLoader, font, out foundFontAtlas);

            if (foundFontAtlas != this.simpleFontAtlas)
            {
                //change to another font atlas
                _glBmp = null;
                this.simpleFontAtlas = foundFontAtlas;
            }

            _typeface = (Typography.OpenFont.Typeface)fontImp.FontFace.GetInternalTypeface();
            float srcTextureScale = _typeface.CalculateToPixelScaleFromPointSize(simpleFontAtlas.OriginalFontSizePts);
            //scale at request
            float targetTextureScale = _typeface.CalculateToPixelScaleFromPointSize(font.SizeInPoints);

            _finalTextureScale = targetTextureScale / srcTextureScale;
        }
Beispiel #3
0
        public override void Init()
        {
            //steps : detail ...
            //1. create a text service (or get it from a singleton class)

            _textServices = new LayoutFarm.OpenFontTextService();

            //2. create manager
            _bmpFontMx = new BitmapFontManager <MemBitmap>(
                TextureKind.StencilLcdEffect,
                _textServices,
                atlas =>
            {
                GlyphImage totalGlyphImg = atlas.TotalGlyph;
                return(MemBitmap.CreateFromCopy(totalGlyphImg.Width, totalGlyphImg.Height, totalGlyphImg.GetImageBuffer()));
            }
                );


            //3.
            _font      = new RequestFont("tahoma", 10);
            _fontAtlas = _bmpFontMx.GetFontAtlas(_font, out _fontBmp);


            //----------
        }
        public SimpleFontAtlas CreateSimpleFontAtlas()
        {
            SimpleFontAtlas simpleFontAtlas = new SimpleFontAtlas();

            simpleFontAtlas.TextureKind         = this.TextureKind;
            simpleFontAtlas.OriginalFontSizePts = this.FontSizeInPoints;
            foreach (CacheGlyph cacheGlyph in glyphs.Values)
            {
                //convert char to hex
                string               unicode   = ("0x" + ((int)cacheGlyph.character).ToString("X"));//code point
                Rectangle            area      = cacheGlyph.area;
                TextureFontGlyphData glyphData = new TextureFontGlyphData();
                area.Y += area.Height;//***

                //set font matrix to glyph font data
                glyphData.Rect           = Rectangle.FromLTRB(area.X, area.Top, area.Right, area.Bottom);
                glyphData.AdvanceY       = 0;// cacheGlyph.glyphMatrix.advanceY;
                glyphData.ImgWidth       = cacheGlyph.img.Width;
                glyphData.TextureXOffset = cacheGlyph.img.TextureOffsetX;
                glyphData.TextureYOffset = cacheGlyph.img.TextureOffsetY;

                simpleFontAtlas.AddGlyph(cacheGlyph.codePoint, glyphData);
            }

            return(simpleFontAtlas);
        }
Beispiel #5
0
        protected override void OnReadyForInitGLShaderProgram()
        {
            //---------------------
            var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder();

            using (System.IO.FileStream fs = new System.IO.FileStream(RootDemoPath.Path + @"\a_total.xml", System.IO.FileMode.Open))
            {
                _fontAtlas = atlasBuilder.LoadFontAtlasInfo(fs)[0];
            }

            PixelFarm.CpuBlit.MemBitmap actualImg = null;
            using (System.IO.FileStream fs = new System.IO.FileStream(RootDemoPath.Path + @"\a_total.png", System.IO.FileMode.Open))
            {
                actualImg = PixelFarm.CpuBlit.MemBitmap.LoadBitmap(fs);
            }
            _totalBmp = actualImg;
            //var bmpdata = totalImg.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, totalImg.PixelFormat);
            //var buffer = new int[totalImg.Width * totalImg.Height];
            //System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, buffer, 0, buffer.Length);
            //totalImg.UnlockBits(bmpdata);
            var glyph = new Typography.Rendering.GlyphImage(_totalBmp.Width, _totalBmp.Height);

            glyph.SetImageBuffer(PixelFarm.CpuBlit.MemBitmap.CopyImgBuffer(actualImg), false);
            _fontAtlas.TotalGlyph = glyph;
        }
Beispiel #6
0
        public SimpleFontAtlas CreateSimpleFontAtlas()
        {
            SimpleFontAtlas simpleFontAtlas = new SimpleFontAtlas();

            simpleFontAtlas.TextureKind         = this.TextureKind;
            simpleFontAtlas.OriginalFontSizePts = this.FontSizeInPoints;
            foreach (CacheGlyph cacheGlyph in _glyphs.Values)
            {
                Rectangle           area      = cacheGlyph.area;
                TextureGlyphMapData glyphData = new TextureGlyphMapData();

                glyphData.Width  = cacheGlyph.img.Width;
                glyphData.Left   = area.X;
                glyphData.Top    = area.Top;
                glyphData.Height = area.Height;

                glyphData.TextureXOffset = cacheGlyph.img.TextureOffsetX;
                glyphData.TextureYOffset = cacheGlyph.img.TextureOffsetY;


                simpleFontAtlas.AddGlyph(cacheGlyph.glyphIndex, glyphData);
            }

            return(simpleFontAtlas);
        }
        //read font info from xml document
        public SimpleFontAtlas LoadFontInfo(string filename)
        {
            SimpleFontAtlas simpleFontAtlas = new SimpleFontAtlas();

            simpleFontAtlas.TextureKind = this.TextureKind;
            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(filename);
            //read
            int total_W = 0;
            int total_H = 0;

            {
                foreach (XmlElement xmlelem in xmldoc.GetElementsByTagName("total_img"))
                {
                    simpleFontAtlas.Width  = total_W = int.Parse(xmlelem.GetAttribute("w"));
                    simpleFontAtlas.Height = total_H = int.Parse(xmlelem.GetAttribute("h"));
                    //only 1...

                    break;
                }
            }
            foreach (XmlElement glyphElem in xmldoc.GetElementsByTagName("glyph"))
            {
                //read
                string    unicodeHex = glyphElem.GetAttribute("uc");
                int       codepoint  = int.Parse(glyphElem.GetAttribute("c"));
                char      c          = (char)int.Parse(unicodeHex.Substring(2), System.Globalization.NumberStyles.HexNumber);
                Rectangle area       = ParseRect(glyphElem.GetAttribute("ltwh"));
                var       glyphData  = new TextureFontGlyphData();
                area.Y += area.Height;//***
                //glyphData.Rect = Rectangle.c((short)area.X, (short)area.Bottom, (short)area.Right, (short)area.Top);
                glyphData.Rect = Rectangle.FromLTRB(area.X, area.Top, area.Right, area.Bottom);
                float[] borderXY = ParseFloatArray(glyphElem.GetAttribute("borderXY"));
                float[] matrix   = ParseFloatArray(glyphElem.GetAttribute("mat"));

                glyphData.BorderX = borderXY[0];
                glyphData.BorderY = borderXY[1];

                glyphData.AdvanceX  = matrix[0];
                glyphData.AdvanceY  = matrix[1];
                glyphData.BBoxXMin  = matrix[2];
                glyphData.BBoxXMax  = matrix[3];
                glyphData.BBoxYMin  = matrix[4];
                glyphData.BBoxYMax  = matrix[5];
                glyphData.ImgWidth  = matrix[6];
                glyphData.ImgHeight = matrix[7];
                glyphData.HAdvance  = matrix[8];
                glyphData.HBearingX = matrix[9];
                glyphData.HBearingY = matrix[10];
                glyphData.VAdvance  = matrix[11];
                glyphData.VBearingX = matrix[12];
                glyphData.VBearingY = matrix[13];
                //---------------
                simpleFontAtlas.AddGlyph(codepoint, glyphData);
            }
            return(simpleFontAtlas);
        }
        public static ActualFont GetTextureFontAtlasOrCreateNew(
            IFontLoader fontLoader,
            RequestFont font,
            out SimpleFontAtlas fontAtlas)
        {
            //check if we have created this font
            var key = new FontTextureKey();

            key.fontName    = font.Name;
            key.scriptLang  = font.ScriptLang.shortname;
            key.sizeInPoint = font.SizeInPoints;
            key.fontStyle   = font.Style;
            //------------------------
            TextureAtlasCache found;
            FontFace          ff = null;

            if (!s_cachedFontAtlas.TryGetValue(key, out found))
            {
                //if not, then create the new one
                string fontfile = fontLoader.GetFont(font.Name, font.Style.ConvToInstalledFontStyle()).FontPath;
                //ptimize here
                //TODO: review
                TextureFontCreationParams creationParams = new TextureFontCreationParams();
                creationParams.originalFontSizeInPoint = font.SizeInPoints;
                creationParams.scriptLang     = font.ScriptLang;
                creationParams.writeDirection = WriteDirection.LTR;//default
                //TODO: review here, langBits can be created with scriptLang ?
                creationParams.langBits = new Typography.OpenFont.Tables.UnicodeLangBits[]
                {
                    Typography.OpenFont.Tables.UnicodeLangBits.BasicLatin, //0-127
                    Typography.OpenFont.Tables.UnicodeLangBits.Thai        //eg. Thai, for test with complex script, you can change to your own
                };
                //
                creationParams.textureKind = Typography.Rendering.TextureKind.AggSubPixel;
                if (font.SizeInPoints >= 4 && font.SizeInPoints <= 14)
                {
                    //creationParams.hintTechnique = Typography.Rendering.HintTechnique.TrueTypeInstruction;
                    //creationParams.hintTechnique = Typography.Rendering.HintTechnique.TrueTypeInstruction_VerticalOnly;
                    creationParams.hintTechnique = Typography.Rendering.HintTechnique.CustomAutoFit;
                }
                //
                ff = TextureFontLoader.LoadFont(fontfile, creationParams, out fontAtlas);


                //cache it
                var textureAtlasCache = new TextureAtlasCache();
                textureAtlasCache.fontFace = ff;
                textureAtlasCache.atlas    = fontAtlas;
                s_cachedFontAtlas.Add(key, textureAtlasCache);
                return(ff.GetFontAtPointSize(font.SizeInPoints));
            }
            fontAtlas = found.atlas;
            ff        = found.fontFace;
            return(ff.GetFontAtPointSize(font.SizeInPoints));
        }
 public void ChangeFont(RequestFont font)
 {
     if (this.font == font)
     {
         return;
     }
     //font has been changed,
     //resolve for the new one
     //check if we have this texture-font atlas in our MySimpleGLBitmapFontManager
     //if not-> request the MySimpleGLBitmapFontManager to create a newone
     _fontAtlas = _myGLBitmapFontMx.GetFontAtlas(font, out _glBmp);
     this.font  = font;
 }
        public void ChangeFont(RequestFont font)
        {
            if (_font == font)
            {
                return;
            }
            //font has been changed,
            //resolve for the new one
            //check if we have this texture-font atlas in our MySimpleGLBitmapFontManager
            //if not-> request the MySimpleGLBitmapFontManager to create a newone
            _fontAtlas = _myGLBitmapFontMx.GetFontAtlas(font, out _glBmp);
            _font      = font;

            Typeface typeface = _textServices.ResolveTypeface(font);

            _px_scale = typeface.CalculateScaleToPixelFromPointSize(font.SizeInPoints);
        }
Beispiel #11
0
        private void button4_Click(object sender, EventArgs e)
        {
            string          fontfilename = "d:\\WImageTest\\a_total.xml";
            var             atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder();
            SimpleFontAtlas fontAtlas    = atlasBuilder.LoadFontInfo(fontfilename);

            using (Bitmap totalImg = new Bitmap("d:\\WImageTest\\a_total.png"))
            {
                var bmpdata = totalImg.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, totalImg.PixelFormat);
                var buffer  = new int[totalImg.Width * totalImg.Height];
                System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, buffer, 0, buffer.Length);
                totalImg.UnlockBits(bmpdata);
                var glyImage = new GlyphImage(totalImg.Width, totalImg.Height);
                glyImage.SetImageBuffer(buffer, false);
                fontAtlas.TotalGlyph = glyImage;
            }
        }
        //read font info from xml document
        public SimpleFontAtlas LoadAtlasInfo(System.IO.Stream inputStream)
        {
            SimpleFontAtlas simpleFontAtlas = new SimpleFontAtlas();

            simpleFontAtlas.TextureKind = this.TextureKind;
            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(inputStream);
            //read
            int total_W = 0;
            int total_H = 0;

            {
                foreach (XmlElement xmlelem in xmldoc.GetElementsByTagName("total_img"))
                {
                    simpleFontAtlas.Width  = total_W = int.Parse(xmlelem.GetAttribute("w"));
                    simpleFontAtlas.Height = total_H = int.Parse(xmlelem.GetAttribute("h"));
                    //only 1...

                    break;
                }
            }
            foreach (XmlElement glyphElem in xmldoc.GetElementsByTagName("glyph"))
            {
                //read
                string unicodeHex = glyphElem.GetAttribute("uc");
                int    glyphIndex = int.Parse(glyphElem.GetAttribute("c"));
                //TODO: this should be codepoint
                char      c    = (char)int.Parse(unicodeHex.Substring(2), System.Globalization.NumberStyles.HexNumber);
                Rectangle area = ParseRect(glyphElem.GetAttribute("ltwh"));
                area.Y += area.Height;//***
                var glyphData = new TextureFontGlyphData();
                glyphData.Rect = Rectangle.FromLTRB(area.X, area.Top, area.Right, area.Bottom);
                float[] borderAndTransform = ParseFloatArray(glyphElem.GetAttribute("tx"));
                glyphData.ImgWidth       = borderAndTransform[0];
                glyphData.BorderX        = borderAndTransform[1];
                glyphData.BorderY        = borderAndTransform[2];
                glyphData.TextureXOffset = borderAndTransform[3];
                glyphData.TextureYOffset = borderAndTransform[4];

                //---------------
                simpleFontAtlas.AddGlyph(glyphIndex, glyphData);
            }
            return(simpleFontAtlas);
        }
Beispiel #13
0
        protected override void OnReadyForInitGLShaderProgram()
        {
            //---------------------
            var atlasBuilder = new Typography.Rendering.SimpleFontAtlasBuilder();

            fontAtlas = atlasBuilder.LoadFontInfo(RootDemoPath.Path + @"\a_total.xml");


            var actualImg = DemoHelper.LoadImage(RootDemoPath.Path + @"\a_total.png");

            //var bmpdata = totalImg.LockBits(new System.Drawing.Rectangle(0, 0, totalImg.Width, totalImg.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, totalImg.PixelFormat);
            //var buffer = new int[totalImg.Width * totalImg.Height];
            //System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, buffer, 0, buffer.Length);
            //totalImg.UnlockBits(bmpdata);
            var glyph = new Typography.Rendering.GlyphImage(totalImg.Width, totalImg.Height);

            glyph.SetImageBuffer(PixelFarm.Agg.ActualImage.CopyImgBuffer(actualImg), false);
            fontAtlas.TotalGlyph = glyph;
        }
Beispiel #14
0
        public void AddSimpleFontAtlas(SimpleFontAtlas[] simpleFontAtlases, System.IO.Stream totalGlyphImgStream)
        {
            //multiple font atlas that share the same glyphImg

            GlyphImage glyphImg = ReadGlyphImages(totalGlyphImgStream);

            for (int i = 0; i < simpleFontAtlases.Length; ++i)
            {
                SimpleFontAtlas simpleFontAtlas = simpleFontAtlases[i];
                simpleFontAtlas.TotalGlyph          = glyphImg;
                simpleFontAtlas.UseSharedGlyphImage = true;
                _createdAtlases.Add(simpleFontAtlas.FontKey, simpleFontAtlas);

                if (simpleFontAtlas.TextureKind == PixelFarm.Drawing.BitmapAtlas.TextureKind.Msdf)
                {
                    //if we have msdf texture
                    //then we can use this to do autoscale
                    _msdfTextureFonts.Add(simpleFontAtlas.FontFilename, simpleFontAtlas);
                }
            }
        }
        public void ChangeFont(RequestFont font)
        {
            if (_font == font || (_font != null && _font.FontKey == font.FontKey))
            {
                //not change -> then return
                return;
            }

            //_loadedFont = _loadFonts.RegisterFont(font);
            //System.Diagnostics.Debug.WriteLine(font.Name + font.SizeInPoints);

            //LoadedFont loadFont = _loadFonts.RegisterFont(font);
            //font has been changed,
            //resolve for the new one
            //check if we have this texture-font atlas in our MySimpleGLBitmapFontManager
            //if not-> request the MySimpleGLBitmapFontManager to create a newone
            _fontAtlas = _myGLBitmapFontMx.GetFontAtlas(font, out _glBmp);
            _font      = font;
            Typeface typeface = _textServices.ResolveTypeface(font);

            _px_scale = typeface.CalculateScaleToPixelFromPointSize(font.SizeInPoints);
        }
Beispiel #16
0
        public SimpleFontAtlas CreateSimpleFontAtlas()
        {
            SimpleFontAtlas simpleFontAtlas = new SimpleFontAtlas();

            foreach (CacheGlyph g in glyphs.Values)
            {
                //convert char to hex
                string    unicode   = ("0x" + ((int)g.character).ToString("X"));//code point
                Rectangle area      = g.area;
                var       glyphData = new TextureFontGlyphData();
                area.Y += area.Height;//***

                //set font matrix to glyph font data
                glyphData.Rect     = Rectangle.FromLTRB(area.X, area.Top, area.Right, area.Bottom);
                glyphData.AdvanceY = g.glyphMatrix.advanceY;
                glyphData.ImgWidth = g.img.Width;

                simpleFontAtlas.AddGlyph(g.codePoint, glyphData);
            }


            return(simpleFontAtlas);
        }
Beispiel #17
0
        private void cmdTestFontAtlas_Click(object sender, EventArgs e)
        {
            //read string from txtSampleChars
            //please make sure all are unique. (TODO: check it)
            //then create a font atlas from the sample chars

            char[] sampleChars = txtSampleChars.Text.ToCharArray();
            if (sampleChars.Length == 0)
            {
                return;
            }
            //

            GlyphImage             totalGlyphsImg = null;
            SimpleFontAtlasBuilder atlasBuilder   = null;
            var glyphTextureGen = new GlyphTextureBitmapGenerator();
            //
            Typeface typeface         = _basicOptions.Typeface;
            float    fontSizeInPoints = _basicOptions.FontSizeInPoints;

            //
            glyphTextureGen.CreateTextureFontFromInputChars(
                typeface,
                fontSizeInPoints,
                TextureKind.StencilLcdEffect,
                sampleChars,
                (glyphIndex, glyphImage, outputAtlasBuilder) =>
            {
                if (outputAtlasBuilder != null)
                {
                    //finish
                    atlasBuilder = outputAtlasBuilder;
                }
            }
                );

            atlasBuilder.SpaceCompactOption = SimpleFontAtlasBuilder.CompactOption.ArrangeByHeight;
            totalGlyphsImg = atlasBuilder.BuildSingleImage();
            string fontTextureImg = "d:\\WImageTest\\test_glyph_atlas.png";


            //create atlas
            SimpleFontAtlas fontAtlas = atlasBuilder.CreateSimpleFontAtlas();

            fontAtlas.TotalGlyph = totalGlyphsImg;
            //
            using (MemBitmap memBmp = MemBitmap.CreateFromCopy(totalGlyphsImg.Width, totalGlyphsImg.Height, totalGlyphsImg.GetImageBuffer()))
                using (System.Drawing.Bitmap bmp = new Bitmap(memBmp.Width, memBmp.Height))
                {
                    var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, memBmp.Width, memBmp.Height),
                                               System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    var tmpMem = MemBitmap.GetBufferPtr(memBmp);
                    unsafe
                    {
                        PixelFarm.CpuBlit.NativeMemMx.MemCopy((byte *)bmpdata.Scan0,
                                                              (byte *)tmpMem.Ptr,
                                                              tmpMem.LengthInBytes);
                    }
                    bmp.UnlockBits(bmpdata);
                    bmp.Save(fontTextureImg);
                }


#if DEBUG
            //save glyph image for debug
            //PixelFarm.Agg.ActualImage.SaveImgBufferToPngFile(
            //    totalGlyphsImg.GetImageBuffer(),
            //    totalGlyphsImg.Width * 4,
            //    totalGlyphsImg.Width, totalGlyphsImg.Height,
            //    "d:\\WImageTest\\total_" + reqFont.Name + "_" + reqFont.SizeInPoints + ".png");
            ////save image to cache
            //SaveImgBufferToFile(totalGlyphsImg, fontTextureImg);
#endif

            //cache the atlas
            //_createdAtlases.Add(fontKey, fontAtlas);
            ////
            ////calculate some commonly used values
            //fontAtlas.SetTextureScaleInfo(
            //    resolvedTypeface.CalculateScaleToPixelFromPointSize(fontAtlas.OriginalFontSizePts),
            //    resolvedTypeface.CalculateScaleToPixelFromPointSize(reqFont.SizeInPoints));
            ////TODO: review here, use scaled or unscaled values
            //fontAtlas.SetCommonFontMetricValues(
            //    resolvedTypeface.Ascender,
            //    resolvedTypeface.Descender,
            //    resolvedTypeface.LineGap,
            //    resolvedTypeface.CalculateRecommendLineSpacing());

            ///
        }