示例#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 AppendText(char[] text, int startIndex, int count, TextFormat format) {
     fixed(char *ptr = text)
     AppendText(ptr + startIndex, count, format);
 }
示例#3
0
        public void AppendText(char *text, int count, TextFormat format)
        {
            // look up the cache entry for the given font and size
            CachedFace cachedFace;
            var        font = format.Font;
            var        size = FontFace.ComputePixelSize(format.Size, Dpi);
            var        key  = new CacheKey(font.Id, size);

            if (!cache.TryGetValue(key, out cachedFace))
            {
                cache.Add(key, cachedFace = new CachedFace(font, size));
            }

            // process each character in the string
            var   nextBreak = BreakCategory.None;
            var   previous  = new CodePoint();
            char *end       = text + count;

            while (text != end)
            {
                // handle surrogate pairs properly
                CodePoint codePoint;
                char      c = *text++;
                if (char.IsSurrogate(c) && text != end)
                {
                    codePoint = new CodePoint(c, *text++);
                }
                else
                {
                    codePoint = c;
                }

                // ignore linefeeds directly after a carriage return
                if (c == '\n' && (char)previous == '\r')
                {
                    continue;
                }

                // get the glyph data
                CachedGlyph glyph;
                if (!cachedFace.Glyphs.TryGetValue(codePoint, out glyph) && !char.IsControl(c))
                {
                    var data   = font.GetGlyph(codePoint, size);
                    var width  = data.RenderWidth;
                    var height = data.RenderHeight;
                    if (width > atlas.Width || height > atlas.Height)
                    {
                        throw new InvalidOperationException("Glyph is larger than the size of the provided atlas.");
                    }

                    var rect = new Rect();
                    if (width > 0 && height > 0)
                    {
                        // render the glyph
                        var memSize = width * height;
                        var mem     = memoryBuffer;
                        if (mem == null)
                        {
                            memoryBuffer = mem = new MemoryBuffer(memSize);
                        }

                        mem.Clear(memSize);
                        data.RenderTo(new Surface {
                            Bits   = mem.Pointer,
                            Width  = width,
                            Height = height,
                            Pitch  = width
                        });

                        // save the rasterized glyph in the user's atlas
                        rect = packer.Insert(width, height);
                        if (rect.Height == 0)
                        {
                            // didn't fit in the atlas... start a new sheet
                            currentPage++;
                            packer.Clear(atlas.Width, atlas.Height);
                            rect = packer.Insert(width, height);
                            if (rect.Height == 0)
                            {
                                throw new InvalidOperationException("Failed to insert glyph into fresh page.");
                            }
                        }
                        atlas.Insert(currentPage, rect.X, rect.Y, rect.Width, rect.Height, mem.Pointer);
                    }

                    glyph = new CachedGlyph(rect, data.HorizontalMetrics.Bearing, data.HorizontalMetrics.Advance);
                    cachedFace.Glyphs.Add(codePoint, glyph);
                }

                // check for a kerning offset
                var kerning = font.GetKerning(previous, codePoint, size);
                previous = codePoint;

                // figure out whether this character can serve as a line break point
                // TODO: more robust character class handling
                var breakCategory = BreakCategory.None;
                if (char.IsWhiteSpace(c))
                {
                    if (c == '\r' || c == '\n')
                    {
                        breakCategory = BreakCategory.Mandatory;
                    }
                    else
                    {
                        breakCategory = BreakCategory.Opportunity;
                    }
                }

                // the previous character might make us think that this one should be a break opportunity
                if (nextBreak > breakCategory)
                {
                    breakCategory = nextBreak;
                }
                if (c == '-')
                {
                    nextBreak = BreakCategory.Opportunity;
                }

                // alright, we have all the right glyph data cached and loaded
                // append relevant info to our buffer; we'll do the actual layout later
                buffer.Add(new BufferEntry {
                    GlyphData = glyph,
                    Kerning   = kerning,
                    Break     = breakCategory
                });
            }
        }
示例#4
0
 public void AppendText(string text, TextFormat format) => AppendText(text, 0, text.Length, format);