Пример #1
0
        public unsafe void Append(TextAnalyzer analyzer, FontFace font, string text)
        {
            var layout = new TextLayout();
            var format = new TextFormat {
                Font = font,
                Size = 32.0f
            };

            analyzer.AppendText(text, format);
            analyzer.PerformLayout(0, 32, 1000, 1000, layout);

            var memBlock = new MemoryBlock(text.Length * 6 * PosColorTexture.Layout.Stride);
            var mem = (PosColorTexture*)memBlock.Data;
            foreach (var thing in layout.Stuff) {
                var width = thing.Width;
                var height = thing.Height;
                var region = new Vector4(thing.SourceX, thing.SourceY, width, height) / 4096;
                var origin = new Vector2(thing.DestX, thing.DestY);
                *mem++ = new PosColorTexture(origin + new Vector2(0, height), new Vector2(region.X, region.Y + region.W), unchecked((int)0xff000000));
                *mem++ = new PosColorTexture(origin + new Vector2(width, height), new Vector2(region.X + region.Z, region.Y + region.W), unchecked((int)0xff000000));
                *mem++ = new PosColorTexture(origin + new Vector2(width, 0), new Vector2(region.X + region.Z, region.Y), unchecked((int)0xff000000));
                *mem++ = new PosColorTexture(origin, new Vector2(region.X, region.Y), unchecked((int)0xff000000));
                count++;
            }

            vertexBuffer = new DynamicVertexBuffer(memBlock, PosColorTexture.Layout);
        }
Пример #2
0
        public void PerformLayout(float x, float y, float width, float height, TextLayout layout)
        {
            layout.SetCount(buffer.Count);

            var pen = new Vector2(x, y);

            for (int i = 0; i < buffer.Count; i++)
            {
                var entry = buffer[i];
                if (entry.Break == BreakCategory.Mandatory)
                {
                    pen.X  = x;
                    pen.Y += 32; // TODO: line spacing
                }

                // data can be null for control characters,
                // or for glyphs without image data
                var data = entry.GlyphData;
                if (data == null)
                {
                    continue;
                }

                pen.X += entry.Kerning;
                layout.AddGlyph(
                    (int)Math.Round(pen.X + data.Bearing.X),
                    (int)Math.Round(pen.Y - data.Bearing.Y),
                    data.Bounds.X,
                    data.Bounds.Y,
                    data.Bounds.Width,
                    data.Bounds.Height
                    );

                pen.X += (float)Math.Round(data.AdvanceWidth);
            }
        }