コード例 #1
0
        /// <summary>
        /// Return glyph advance distances along the X axis to layout text
        /// </summary>
        /// <param name="text">Text to layout</param>
        /// <returns>Advance distances for each glyph in pixel units</returns>
        public MeasurementInfo GetMeasurementInfoForString(string text)
        {
            var layout = new GlyphLayout();

            layout.Typeface = typeface;

            var measure    = layout.LayoutAndMeasureString(text.ToCharArray(), 0, text.Length, FontSizeInPoints);
            var lineHeight = typeface.CalculateLineSpacing(LineSpacingChoice.TypoMetric) * (FontSizeInPixels / typeface.UnitsPerEm);

            var glyphPositions = layout.ResultUnscaledGlyphPositions;
            var advanceWidths  = new float[glyphPositions.Count];
            var scale          = FontSizeInPixels / TotalHeight;

            for (var i = 0; i < glyphPositions.Count; i++)
            {
                glyphPositions.GetGlyph(i, out var advanceW);
                advanceWidths[i] = advanceW * (FontSizeInPixels / typeface.UnitsPerEm) * scale;
            }

            return(new MeasurementInfo
            {
                Ascender = measure.AscendingInPx,
                Descender = measure.DescendingInPx,
                AdvanceWidths = advanceWidths,
                LineHeight = lineHeight
            });
        }