示例#1
0
        private bool Place(List <SpriteEntry> sprites, int startIndex)
        {
            if (spriteMaps[mapCount] == null)
            {
                var size = Math.Max(1024, Math.Max(sprites[startIndex].Texture2D.Width, sprites[startIndex].Texture2D.Height));
                spriteMaps[mapCount] = new SpriteMap(graphicsDevice, size);
            }

            var fit = spriteMaps[mapCount].Place(sprites, startIndex, out var packed);

            if (packed == startIndex)
            {
                return(false);
            }
            if (packed >= sprites.Count - 1)
            {
                mapCount++;
                return(true);
            }
            mapCount++;
            return(Place(sprites, packed));
        }
示例#2
0
        private unsafe bool CacheGlyph(uint c, out GlyphEntry glyphEntry)
        {
            foreach (var face in faces)
            {
                if (face.TryCharIndex(c, out var glyph))
                {
                    if (face.HasColor())
                    {
                        face.LoadGlyph(glyph, LoadFlags.RENDER);//NOT IMPLEMENTED face.LoadGlyph(glyph, LoadFlags.COLOR);
                    }
                    else
                    {
                        face.LoadGlyph(glyph, LoadFlags.RENDER);
                    }
                    var glyphSlot = face.GetGlyphSlot();
                    var w         = glyphSlot.bitmap.width;
                    var h         = glyphSlot.bitmap.rows;

                    var buffer = (byte *)glyphSlot.bitmap.buffer;



                    if (glyphSlot.bitmap.pitch < 0)
                    {
                        continue;
                    }

                    var colorData = new Color[w * h];

                    var result = glyphSlot.bitmap.pixel_mode switch
                    {
                        FTPixelMode.FT_PIXEL_MODE_GRAY => FTPixelModeGrayPutColorData(colorData, w, h, buffer),
                        FTPixelMode.FT_PIXEL_MODE_BGRA => FTPixelModeColorPutColorData(colorData, w, h, buffer),
                        _ => throw new ArgumentException("Unsupported pixel mode")
                    };

                    if (result)
                    {
                        if (spriteMaps.Count > 0 && spriteMaps[spriteMaps.Count - 1].Place(colorData, (int)w, (int)h, out var idx))
                        {
                            glyphEntry = new GlyphEntry
                            {
                                sprite = new Sprite
                                {
                                    mapId     = spriteMaps.Count - 1,
                                    spriteIdx = idx
                                },
                                height     = (int)h,
                                width      = (int)w,
                                leftOffset = glyphSlot.bitmap_left,
                                topOffset  = glyphSlot.bitmap_top
                            };
                            spriteReferences.TryAdd(c, glyphEntry);
                        }
                        else
                        {
                            var newMap = new SpriteMap(graphicsDevice);
                            if (newMap.Place(colorData, (int)w, (int)h, out idx))
                            {
                                glyphEntry = new GlyphEntry
                                {
                                    sprite = new Sprite
                                    {
                                        mapId     = spriteMaps.Count,
                                        spriteIdx = idx
                                    },
                                    height     = (int)h,
                                    width      = (int)w,
                                    leftOffset = glyphSlot.bitmap_left,
                                    topOffset  = glyphSlot.bitmap_top
                                };
                                spriteReferences.TryAdd(c, glyphEntry);
                                spriteMaps.Add(newMap);
                            }
                        }
                    }
                }
            }
            glyphEntry = default;
            return(false);
        }