public void TryMeasureCharacterBounds() { string text = "a b\nc"; var expectedGlyphMetrics = new GlyphMetric[] { new GlyphMetric('a', new FontRectangle(10, 0, 10, 10), false), new GlyphMetric(' ', new FontRectangle(40, 0, 30, 10), false), new GlyphMetric('b', new FontRectangle(70, 0, 10, 10), false), new GlyphMetric('\n', new FontRectangle(100, 0, 0, 10), true), new GlyphMetric('c', new FontRectangle(10, 30, 10, 10), false), }; Font font = CreateFont(text); int scaleFactor = 72 * font.EmSize; // 72 * emSize means 1 point = 1px Assert.True(TextMeasurer.TryMeasureCharacterBounds(text.AsSpan(), new RendererOptions(font, 72 * font.EmSize), out GlyphMetric[] glyphMetrics)); Assert.Equal(text.Length, glyphMetrics.Length); for (int i = 0; i < glyphMetrics.Length; i++) { GlyphMetric expected = expectedGlyphMetrics[i]; GlyphMetric actual = glyphMetrics[i]; Assert.Equal(expected.Character, actual.Character); Assert.Equal(expected.IsControlCharacter, actual.IsControlCharacter); // 4 dp as there is minor offset difference in the float values Assert.Equal(expected.Bounds.X, actual.Bounds.X, 4); Assert.Equal(expected.Bounds.Y, actual.Bounds.Y, 4); Assert.Equal(expected.Bounds.Height, actual.Bounds.Height, 4); Assert.Equal(expected.Bounds.Width, actual.Bounds.Width, 4); } }
public void TryMeasureCharacterBounds() { string text = "a b\nc"; GlyphMetric[] expectedGlyphMetrics = new GlyphMetric[] { new GlyphMetric('a', new RectangleF(10, 10, 10, 10), false), new GlyphMetric(' ', new RectangleF(40, 10, 30, 10), false), new GlyphMetric('b', new RectangleF(70, 10, 10, 10), false), new GlyphMetric('\n', new RectangleF(100, 10, 0, 10), true), new GlyphMetric('c', new RectangleF(10, 40, 10, 10), false), }; Font font = CreateFont(text); int scaleFactor = 72 * font.EmSize; // 72 * emSize means 1 point = 1px IReadOnlyList <GlyphMetric> glyphMetrics; Assert.True(TextMeasurer.TryMeasureCharacterBounds(text, new RendererOptions(font, 72 * font.EmSize), out glyphMetrics)); Assert.Equal(text.Length, glyphMetrics.Count); int i = 0; foreach (GlyphMetric glyphMetric in glyphMetrics) { Assert.Equal(expectedGlyphMetrics[i++], glyphMetric); } }
public void PrintJapaneseLetter(UChar letter, float length) { var deltaY = new GlyphMetric(letter, length).VerticalOffset; DrawString(letter.ToString(), _x, _y - deltaY, _graphics, _fontCache); _y += length; }
public void PrintJapaneseLetter(UChar letter, float length) { var deltaY = new GlyphMetric(letter, length).VerticalOffset; DrawString(letter.ToString(), _x, _y - deltaY, _graphics, _fontCache); _y += length; }