在WPF中精确定位字形文本

时间:2018-05-04 12:48:11

标签: wpf text glyphrun

我正在为Windows编写化学分子编辑器。因为它必须在Word加载项中使用,所以我被限制使用WPF来渲染结构。除了一个微小的琐碎点之外,这种方法运作得很好。

Sample molecule with misaligned labels

我使用GlyphRuns渲染原子标签,它们总是略微向右移位。如果您查看屏幕截图,您会看到有一个领先的空白,特别是H2N和Hg原子标签。为什么?获得字形运行的轮廓几何体时,您将获得白色背景。

GlyphRun类的记录非常糟糕,我无法看到要修改哪些属性以精确定位我想要的文本。所以任何尝试的建议都会受到欢迎。

更新:我被要求提供样品。代码很复杂,但并非无偿,所以我将其缩减为专注于基本要素:

    public void MeasureAtCenter(Point center)
        {
            GlyphInfo = GlyphUtils.GetGlyphsAndInfo(Text, PixelsPerDip, out GlyphRun groupGlyphRun, center, _glyphTypeface, TypeSize);
            //compensate the main offset vector for any descenders
            Vector mainOffset = GlyphUtils.GetOffsetVector(groupGlyphRun, AtomShape.SymbolSize) + new Vector(0.0, -MaxBaselineOffset) + new Vector(-FirstBearing(groupGlyphRun), 0.0);



            TextRun = groupGlyphRun;
            TextMetrics = new AtomTextMetrics
            {
                BoundingBox = groupGlyphRun.GetBoundingBox(center + mainOffset),
                Geocenter = center,
                TotalBoundingBox = groupGlyphRun.GetBoundingBox(center + mainOffset),
                OffsetVector = mainOffset
            };
        }

  public static GlyphInfo GetGlyphs(string symbolText, GlyphTypeface glyphTypeFace, double size)
        {
            ushort[] glyphIndexes = new ushort[symbolText.Length];
            double[] advanceWidths = new double[symbolText.Length];
            double[] uprightBaselineOffsets = new double[symbolText.Length];
            double totalWidth = 0;

            for (int n = 0; n < symbolText.Length; n++)
            {
                ushort glyphIndex = glyphTypeFace.CharacterToGlyphMap[symbolText[n]];
                glyphIndexes[n] = glyphIndex;

                double width = glyphTypeFace.AdvanceWidths[glyphIndex] * size;
                advanceWidths[n] = width;

                double ubo = glyphTypeFace.DistancesFromHorizontalBaselineToBlackBoxBottom[glyphIndex] * size;
                uprightBaselineOffsets[n] = ubo;
                totalWidth += width;
            }
            return new GlyphInfo { AdvanceWidths = advanceWidths, Indexes = glyphIndexes, Width = totalWidth, UprightBaselineOffsets = uprightBaselineOffsets };
        }

        public static GlyphUtils.GlyphInfo GetGlyphsAndInfo(string symbolText, float pixelsPerDip, out GlyphRun hydrogenGlyphRun, Point point, GlyphTypeface glyphTypeFace, double symbolSize)
        {
            //measure the H atom first
            var glyphInfo = GlyphUtils.GetGlyphs(symbolText, glyphTypeFace, symbolSize);
            hydrogenGlyphRun = GlyphUtils.GetGlyphRun(glyphInfo, glyphTypeFace,
                symbolSize, pixelsPerDip, point);
            //work out exactly how much we should offset from the center to get to the bottom left
            return glyphInfo;
        }

 public static Vector GetOffsetVector(GlyphRun glyphRun, double symbolSize)
    {
        Rect rect = glyphRun.ComputeInkBoundingBox();

        //Vector offset = (rect.BottomLeft - rect.TopRight) / 2;
        Vector offset = new Vector(-rect.Width / 2, glyphRun.GlyphTypeface.CapsHeight * symbolSize / 2);
        return offset;
    }

0 个答案:

没有答案