コード例 #1
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);
        }
コード例 #2
0
 private bool TryGlyph(uint c, out GlyphEntry glyphEntry)
 {
     return(spriteReferences.TryGetValue(c, out glyphEntry) || CacheGlyph(c, out glyphEntry));
 }