【问题标题】:Setting character width设置字符宽度
【发布时间】:2017-09-18 20:23:48
【问题描述】:

有没有办法调整 WPF textBlock 或 GlyphRun 元素中某些字符的宽度?

我不想增加字符之间的间距,而是增加字符本身的宽度,如图所示:

以下代码在屏幕上打印字母“ABC abc”,我应该怎么做才能使一些字母的宽度为 200% 或 300%?

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="MainWindow" Height="200" Width="300">
    <Grid>
        <Image Stretch="None" SnapsToDevicePixels="True">
            <Image.Source>
                <DrawingImage x:Name="drawingImage" />
            </Image.Source>
        </Image>
    </Grid>
</Window>
public partial class MainWindow : Window
{

    GlyphTypeface glyphTypeface;
    double renderingEmSize, advanceWidth, advanceHeight;
    Point baselineOrigin;

    public MainWindow()
    {
        InitializeComponent();

        new Typeface("segoe ui").TryGetGlyphTypeface(out glyphTypeface);

        renderingEmSize = 12;

        advanceWidth = glyphTypeface.AdvanceWidths[0] * renderingEmSize;
        advanceHeight = glyphTypeface.Height * renderingEmSize;
        baselineOrigin = new Point(0, glyphTypeface.Baseline * renderingEmSize);

        drawingImage.Drawing = Render();

    }

    private Drawing Render()
    {
        var line = "ABC abc";

        var drawing = new DrawingGroup();
        using (var drawingContext = drawing.Open())
        {
            var glyphRun = ConvertTextToGlyphRun(
                glyphTypeface, renderingEmSize,
                advanceWidth, advanceHeight,
                baselineOrigin, line);

            var guidelines = new GuidelineSet();
            guidelines.GuidelinesX.Add(baselineOrigin.X);
            guidelines.GuidelinesY.Add(baselineOrigin.Y);
            drawingContext.PushGuidelineSet(guidelines);
            drawingContext.DrawGlyphRun(Brushes.Black, glyphRun);
            drawingContext.Pop();
        }

        return drawing;
    }

    static GlyphRun ConvertTextToGlyphRun(
        GlyphTypeface glyphTypeface, 
        double renderingEmSize, 
        double advanceWidth, 
        double advanceHeight,
        Point baselineOrigin,
        string line)
    {
        var glyphIndices = new List<ushort>();
        var advanceWidths = new List<double>();
        var glyphOffsets = new List<Point>();

        var y = baselineOrigin.Y;
        var x = baselineOrigin.X;

        for (int j = 0; j < line.Length; ++j)
        {
            var glyphIndex = glyphTypeface.CharacterToGlyphMap[line[j]];
            glyphIndices.Add(glyphIndex);
            advanceWidths.Add(0);
            glyphOffsets.Add(new Point(x, y));
            x += glyphTypeface.AdvanceWidths[glyphIndex] * renderingEmSize;
        }

        return new GlyphRun(
            glyphTypeface,
            0,
            false,
            renderingEmSize,
            glyphIndices,
            baselineOrigin,
            advanceWidths,
            glyphOffsets,
            null, null, null, null, null);
    }
}

【问题讨论】:

  • 不重复我不想增加字符之间的间距,而是增加字符本身的宽度
  • @DavidDay 恰恰相反。虽然它询问如何“修改字符之间的间距”,但这里的提问者说他​​们“想要增加字符之间的间距”
  • 有很多方法,使您的问题非常广泛。也就是说,如果你对 CSS“解决方案”感到满意,它可以缩放整个文本块、字符和间距,你可以在 WPF 中做同样的事情,通过在文本元素上设置一个转换(例如TextBlock )。您如何发布一些代码,即一个好的minimal reproducible example,展示您尝试过的内容,并解释具体您仍然无法弄清楚的内容?
  • 另请参阅FontStretch 属性,具体取决于您希望能够控制宽度的精度级别。

标签: c# .net wpf vb.net


【解决方案1】:

在代码中添加两行进行转换:

drawingContext.PushGuidelineSet(guidelines);
drawingContext.PushTransform(new ScaleTransform(2, 1)); // <= new line
drawingContext.DrawGlyphRun(Brushes.Black, glyphRun);
drawingContext.Pop();                                   // <= new line
drawingContext.Pop();

来自here

【讨论】:

    【解决方案2】:

    我觉得应该有帮助

    <StackPanel>
        <TextBox x:Name="originalABC">ABC</TextBox>
        <TextBlock x:Name="transformedABC">
            <TextBlock.RenderTransform>
                <ScaleTransform CenterX="0" CenterY="0" ScaleX="3" ScaleY="1" />
            </TextBlock.RenderTransform>
            ABC
        </TextBlock>
    </StackPanel>
    

    screenshot

    【讨论】:

    • 谢谢,有没有办法只让某些字符比其他字符宽而不是所有文本?
    猜你喜欢
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2014-02-16
    • 2010-09-25
    • 2013-03-02
    • 2022-07-19
    • 1970-01-01
    • 2013-09-02
    相关资源
    最近更新 更多