Beispiel #1
0
        bool SplitFreeNode(BinPackRect freeNode, BinPackRect usedNode)
        {
            // test if the rects even intersect
            var insideX = usedNode.X <freeNode.Right && usedNode.Right> freeNode.X;
            var insideY = usedNode.Y <freeNode.Bottom && usedNode.Bottom> freeNode.Y;

            if (!insideX || !insideY)
            {
                return(false);
            }

            if (insideX)
            {
                // new node at the top side of the used node
                if (usedNode.Y > freeNode.Y && usedNode.Y < freeNode.Bottom)
                {
                    var newNode = freeNode;
                    newNode.Height = usedNode.Y - newNode.Y;
                    freeList.Add(newNode);
                }

                // new node at the bottom side of the used node
                if (usedNode.Bottom < freeNode.Bottom)
                {
                    var newNode = freeNode;
                    newNode.Y      = usedNode.Bottom;
                    newNode.Height = freeNode.Bottom - usedNode.Bottom;
                    freeList.Add(newNode);
                }
            }

            if (insideY)
            {
                // new node at the left side of the used node
                if (usedNode.X > freeNode.X && usedNode.X < freeNode.Right)
                {
                    var newNode = freeNode;
                    newNode.Width = usedNode.X - newNode.X;
                    freeList.Add(newNode);
                }

                // new node at the right side of the used node
                if (usedNode.Right < freeNode.Right)
                {
                    var newNode = freeNode;
                    newNode.X     = usedNode.Right;
                    newNode.Width = freeNode.Right - usedNode.Right;
                    freeList.Add(newNode);
                }
            }

            return(true);
        }
        public GlyphImage BuildSingleImage()
        {
            //1. add to list
            var glyphList = new List <CacheGlyph>(glyphs.Count);

            foreach (CacheGlyph glyphImg in glyphs.Values)
            {
                //sort data
                glyphList.Add(glyphImg);
            }


            int totalMaxLim  = MaxAtlasWidth;
            int maxRowHeight = 0;
            int currentY     = 0;
            int currentX     = 0;


            if (CompactGlyphSpace)
            {
                //2. sort by glyph width
                glyphList.Sort((a, b) =>
                {
                    return(a.img.Width.CompareTo(b.img.Width));
                });
                //3. layout
                for (int i = glyphList.Count - 1; i >= 0; --i)
                {
                    CacheGlyph g = glyphList[i];
                    if (g.img.Height > maxRowHeight)
                    {
                        maxRowHeight = g.img.Height;
                    }
                    if (currentX + g.img.Width > totalMaxLim)
                    {
                        //start new row
                        currentY += maxRowHeight;
                        currentX  = 0;
                    }
                    //-------------------
                    g.area    = new Rectangle(currentX, currentY, g.img.Width, g.img.Height);
                    currentX += g.img.Width;
                }
            }
            else
            {    //3. layout
                int glyphCount = glyphList.Count;
                for (int i = 0; i < glyphCount; ++i)
                {
                    CacheGlyph g = glyphList[i];
                    if (g.img.Height > maxRowHeight)
                    {
                        maxRowHeight = g.img.Height;
                    }
                    if (currentX + g.img.Width > totalMaxLim)
                    {
                        //start new row
                        currentY += maxRowHeight;
                        currentX  = 0;
                    }
                    //-------------------
                    g.area    = new Rectangle(currentX, currentY, g.img.Width, g.img.Height);
                    currentX += g.img.Width;
                }
            }

            currentY += maxRowHeight;
            int imgH = currentY;
            // -------------------------------
            //compact image location
            // TODO: review performance here again***

            int totalImgWidth = totalMaxLim;

            if (CompactGlyphSpace)
            {
                totalImgWidth = 0;//reset
                BinPacker binPacker = new BinPacker(totalMaxLim, currentY);
                for (int i = glyphList.Count - 1; i >= 0; --i)
                {
                    CacheGlyph  g       = glyphList[i];
                    BinPackRect newRect = binPacker.Insert(g.img.Width, g.img.Height);
                    g.area = new Rectangle(newRect.X, newRect.Y,
                                           g.img.Width, g.img.Height);


                    //recalculate proper max midth again, after arrange and compact space
                    if (newRect.Right > totalImgWidth)
                    {
                        totalImgWidth = newRect.Right;
                    }
                }
            }


            // -------------------------------
            //4. create array that can hold data
            int[] totalBuffer = new int[totalImgWidth * imgH];
            if (CompactGlyphSpace)
            {
                for (int i = glyphList.Count - 1; i >= 0; --i)
                {
                    CacheGlyph g = glyphList[i];
                    //copy data to totalBuffer
                    GlyphImage img = g.img;
                    CopyToDest(img.GetImageBuffer(), img.Width, img.Height, totalBuffer, g.area.Left, g.area.Top, totalImgWidth);
                }
            }
            else
            {
                int glyphCount = glyphList.Count;
                for (int i = 0; i < glyphCount; ++i)
                {
                    CacheGlyph g = glyphList[i];
                    //copy data to totalBuffer
                    GlyphImage img = g.img;
                    CopyToDest(img.GetImageBuffer(), img.Width, img.Height, totalBuffer, g.area.Left, g.area.Top, totalImgWidth);
                }
            }

            GlyphImage glyphImage = new GlyphImage(totalImgWidth, imgH);

            glyphImage.SetImageBuffer(totalBuffer, true);
            latestGenGlyphImage = glyphImage;
            return(glyphImage);
        }
        public GlyphImage BuildSingleImage()
        {
            //1. add to list
            var glyphList = new List <CacheGlyph>(glyphs.Count);

            foreach (CacheGlyph glyphImg in glyphs.Values)
            {
                //sort data
                glyphList.Add(glyphImg);
            }
            //2. sort
            glyphList.Sort((a, b) =>
            {
                return(a.img.Width.CompareTo(b.img.Width));
            });
            //3. layout

            int totalMaxLim  = 800;
            int maxRowHeight = 0;
            int currentY     = 0;
            int currentX     = 0;

            for (int i = glyphList.Count - 1; i >= 0; --i)
            {
                CacheGlyph g = glyphList[i];
                if (g.img.Height > maxRowHeight)
                {
                    maxRowHeight = g.img.Height;
                }
                if (currentX + g.img.Width > totalMaxLim)
                {
                    //start new row
                    currentY += maxRowHeight;
                    currentX  = 0;
                }
                //-------------------
                g.area    = new Rectangle(currentX, currentY, g.img.Width, g.img.Height);
                currentX += g.img.Width;
            }
            currentY += maxRowHeight;
            int imgH = currentY;
            //-------------------------------
            //compact image location
            //TODO: review performance here again***
            BinPacker binPacker = new BinPacker(totalMaxLim, currentY);

            for (int i = glyphList.Count - 1; i >= 0; --i)
            {
                CacheGlyph  g       = glyphList[i];
                BinPackRect newRect = binPacker.Insert(g.img.Width, g.img.Height);
                g.area = new Rectangle(newRect.X, newRect.Y,
                                       g.img.Width, g.img.Height);
            }
            //-------------------------------

            //4. create array that can hold data
            int[] totalBuffer = new int[totalMaxLim * imgH];
            for (int i = glyphList.Count - 1; i >= 0; --i)
            {
                CacheGlyph g = glyphList[i];
                //copy data to totalBuffer
                GlyphImage img = g.img;
                CopyToDest(img.GetImageBuffer(), img.Width, img.Height, totalBuffer, g.area.Left, g.area.Top, totalMaxLim);
            }
            //------------------

            GlyphImage glyphImage = new GlyphImage(totalMaxLim, imgH);

            glyphImage.SetImageBuffer(totalBuffer, true);
            latestGenGlyphImage = glyphImage;
            return(glyphImage);
        }
Beispiel #4
0
        public GlyphImage BuildSingleImage()
        {
            //1. add to list
            var glyphList = new List <CacheGlyph>(_glyphs.Count);

            foreach (CacheGlyph glyphImg in _glyphs.Values)
            {
                //sort data
                glyphList.Add(glyphImg);
            }

            int totalMaxLim  = MaxAtlasWidth;
            int maxRowHeight = 0;
            int currentY     = 0;
            int currentX     = 0;

            switch (this.SpaceCompactOption)
            {
            default:
                throw new System.NotSupportedException();

            case CompactOption.BinPack:
            {
                //2. sort by glyph width
                glyphList.Sort((a, b) =>
                    {
                        return(a.img.Width.CompareTo(b.img.Width));
                    });
                //3. layout
                for (int i = glyphList.Count - 1; i >= 0; --i)
                {
                    CacheGlyph g = glyphList[i];
                    if (g.img.Height > maxRowHeight)
                    {
                        maxRowHeight = g.img.Height;
                    }
                    if (currentX + g.img.Width > totalMaxLim)
                    {
                        //start new row
                        currentY += maxRowHeight;
                        currentX  = 0;
                    }
                    //-------------------
                    g.area    = new Rectangle(currentX, currentY, g.img.Width, g.img.Height);
                    currentX += g.img.Width;
                }
            }
            break;

            case CompactOption.ArrangeByHeight:
            {
                //2. sort by height
                glyphList.Sort((a, b) =>
                    {
                        return(a.img.Height.CompareTo(b.img.Height));
                    });
                //3. layout
                int glyphCount = glyphList.Count;
                for (int i = 0; i < glyphCount; ++i)
                {
                    CacheGlyph g = glyphList[i];
                    if (g.img.Height > maxRowHeight)
                    {
                        maxRowHeight = g.img.Height;
                    }
                    if (currentX + g.img.Width > totalMaxLim)
                    {
                        //start new row
                        currentY    += maxRowHeight;
                        currentX     = 0;
                        maxRowHeight = g.img.Height;        //reset, after start new row
                    }
                    //-------------------
                    g.area    = new Rectangle(currentX, currentY, g.img.Width, g.img.Height);
                    currentX += g.img.Width;
                }
            }
            break;

            case CompactOption.None:
            {
                //3. layout
                int glyphCount = glyphList.Count;
                for (int i = 0; i < glyphCount; ++i)
                {
                    CacheGlyph g = glyphList[i];
                    if (g.img.Height > maxRowHeight)
                    {
                        maxRowHeight = g.img.Height;
                    }
                    if (currentX + g.img.Width > totalMaxLim)
                    {
                        //start new row
                        currentY    += maxRowHeight;
                        currentX     = 0;
                        maxRowHeight = g.img.Height;        //reset, after start new row
                    }
                    //-------------------
                    g.area    = new Rectangle(currentX, currentY, g.img.Width, g.img.Height);
                    currentX += g.img.Width;
                }
            }
            break;
            }

            currentY += maxRowHeight;
            int imgH = currentY;
            // -------------------------------
            //compact image location
            // TODO: review performance here again***

            int totalImgWidth = totalMaxLim;

            if (SpaceCompactOption == CompactOption.BinPack) //again here?
            {
                totalImgWidth = 0;                           //reset
                //use bin packer
                BinPacker binPacker = new BinPacker(totalMaxLim, currentY);
                for (int i = glyphList.Count - 1; i >= 0; --i)
                {
                    CacheGlyph  g       = glyphList[i];
                    BinPackRect newRect = binPacker.Insert(g.img.Width, g.img.Height);
                    g.area = new Rectangle(newRect.X, newRect.Y,
                                           g.img.Width, g.img.Height);


                    //recalculate proper max midth again, after arrange and compact space
                    if (newRect.Right > totalImgWidth)
                    {
                        totalImgWidth = newRect.Right;
                    }
                }
            }
            // -------------------------------
            //4. create array that can hold data
            int[] totalBuffer = new int[totalImgWidth * imgH];
            if (SpaceCompactOption == CompactOption.BinPack) //again here?
            {
                for (int i = glyphList.Count - 1; i >= 0; --i)
                {
                    CacheGlyph g = glyphList[i];
                    //copy data to totalBuffer
                    GlyphImage img = g.img;
                    CopyToDest(img.GetImageBuffer(), img.Width, img.Height, totalBuffer, g.area.Left, g.area.Top, totalImgWidth);
                }
            }
            else
            {
                int glyphCount = glyphList.Count;
                for (int i = 0; i < glyphCount; ++i)
                {
                    CacheGlyph g = glyphList[i];
                    //copy data to totalBuffer
                    GlyphImage img = g.img;
                    CopyToDest(img.GetImageBuffer(), img.Width, img.Height, totalBuffer, g.area.Left, g.area.Top, totalImgWidth);
                }
            }

            //new total glyph img
            GlyphImage glyphImage = new GlyphImage(totalImgWidth, imgH);

            //flip vertical Y
            {
                int[] totalBufferFlipY = new int[totalBuffer.Length];
                int   srcRowIndex      = imgH - 1;
                int   strideInBytes    = totalImgWidth * 4;
                for (int i = 0; i < imgH; ++i)
                {
                    //copy each row from src to dst
                    System.Buffer.BlockCopy(totalBuffer, strideInBytes * srcRowIndex, totalBufferFlipY, strideInBytes * i, strideInBytes);
                    srcRowIndex--;
                }
                totalBuffer = totalBufferFlipY;
            }
            glyphImage.SetImageBuffer(totalBuffer, true);
            _latestGenGlyphImage = glyphImage;
            return(glyphImage);
        }
Beispiel #5
0
        public BinPackRect Insert(int width, int height)
        {
            var bestNode     = new BinPackRect();
            var bestShortFit = int.MaxValue;
            var bestLongFit  = int.MaxValue;

            var count = freeList.Count;

            for (int i = 0; i < count; i++)
            {
                // try to place the rect
                var rect = freeList[i];
                if (rect.Width < width || rect.Height < height)
                {
                    continue;
                }

                var leftoverX = Math.Abs(rect.Width - width);
                var leftoverY = Math.Abs(rect.Height - height);
                var shortFit  = Math.Min(leftoverX, leftoverY);
                var longFit   = Math.Max(leftoverX, leftoverY);

                if (shortFit < bestShortFit || (shortFit == bestShortFit && longFit < bestLongFit))
                {
                    bestNode     = new BinPackRect(rect.X, rect.Y, width, height);
                    bestShortFit = shortFit;
                    bestLongFit  = longFit;
                }
            }

            if (bestNode.Height == 0)
            {
                return(bestNode);
            }

            // split out free areas into smaller ones
            for (int i = 0; i < count; i++)
            {
                if (SplitFreeNode(freeList[i], bestNode))
                {
                    freeList.RemoveAt(i);
                    i--;
                    count--;
                }
            }

            // prune the freelist
            for (int i = 0; i < freeList.Count; i++)
            {
                for (int j = i + 1; j < freeList.Count; j++)
                {
                    var idata = freeList[i];
                    var jdata = freeList[j];
                    if (jdata.Contains(idata))
                    {
                        freeList.RemoveAt(i);
                        i--;
                        break;
                    }

                    if (idata.Contains(jdata))
                    {
                        freeList.RemoveAt(j);
                        j--;
                    }
                }
            }

            return(bestNode);
        }
Beispiel #6
0
 public bool Contains(BinPackRect rect)
 {
     return(rect.X >= X && rect.Y >= Y &&
            rect.Right <= Right && rect.Bottom <= Bottom);
 }