public void AddGlyph(char glyph) { if (!GlyphItems.ContainsKey(glyph)) { GlyphItem item = new GlyphItem(glyph); GlyphItems.Add(glyph, item); dirty = true; textureDirty = true; version++; allGlyphs = allGlyphs + new string(glyph, 1); } else { GlyphItems[glyph].AddRef(); } }
public void AddGlyph(char glyph) { if (!GlyphItems.ContainsKey(glyph)) { GlyphItem item = new GlyphItem(glyph); GlyphItems.Add(glyph, item); dirty = true; textureDirty = true; version++; allGlyphs = allGlyphs + new string(glyph,1); } else { GlyphItems[glyph].AddRef(); } }
public void PrepareBatch() { if (glyphCache == null) { glyphCache = GlyphCache.GetCache(Height); } // Add All Glyphs foreach (Text3d t3d in Items) { foreach (char c in t3d.Text) { glyphCache.AddGlyph(c); } } // Calculate Metrics TextObject.Text = ""; TextObject.FontSize = (float)Height * .50f; System.Drawing.Font font = TextObject.Font; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; Bitmap bmp = new Bitmap(20, 20); Graphics g = Graphics.FromImage(bmp); // Create Index Buffers List <PositionColoredTextured> verts = new List <PositionColoredTextured>(); foreach (Text3d t3d in Items) { float fntAdjust = font.Size / 128f; String text = t3d.Text; SizeF size = g.MeasureString(text, font); float factor = .6666f; t3d.width = size.Width * (float)t3d.scale * factor; t3d.height = size.Height * (float)t3d.scale * factor; float left = 0; int charsLeft = text.Length; int index = 0; // SetMeasurableCharacterRanges has a limit of 32 items per call; while (charsLeft > 0) { int charsNow = Math.Min(32, charsLeft); charsLeft -= charsNow; CharacterRange[] ranges = new CharacterRange[charsNow]; for (int i = 0; i < charsNow; i++) { ranges[i] = new CharacterRange(i + index, 1); } sf.SetMeasurableCharacterRanges(ranges); Region[] reg = g.MeasureCharacterRanges(text, font, new RectangleF(new PointF(0, 0), size), sf); for (int i = 0; i < (charsNow); i++) { GlyphItem item = glyphCache.GetGlyphItem(text[i + index]); RectangleF rectf = reg[i].GetBounds(g); RectangleF position = new RectangleF(rectf.Left * (float)t3d.scale * factor, rectf.Top * (float)t3d.scale * factor, rectf.Width * (float)t3d.scale * factor, rectf.Height * (float)t3d.scale * factor); position = new RectangleF(left * (float)t3d.scale * factor, 0 * (float)t3d.scale * factor, item.Extents.Width * fntAdjust * (float)t3d.scale * factor, item.Extents.Height * fntAdjust * (float)t3d.scale * factor); left += item.Extents.Width * fntAdjust; t3d.AddGlyphPoints(verts, item.Size, position, item.UVRect); } index += charsNow; } } g.Dispose(); GC.SuppressFinalize(g); bmp.Dispose(); font.Dispose(); vertCount = verts.Count; vertexBuffer = new PositionColorTexturedVertexBuffer11(vertCount, RenderContext11.PrepDevice); PositionColoredTextured[] vertBuf = (PositionColoredTextured[])vertexBuffer.Lock(0, 0); // Lock the buffer (which will return our structs) for (int i = 0; i < vertCount; i++) { vertBuf[i] = verts[i]; } vertexBuffer.Unlock(); glyphVersion = glyphCache.Version; }