开发者

WPF word-wrapping: Missing words when using custom font

I'm using a custom font known as Pigiarniq in a WPF application. I have found that from time to time, words will disappear from TextBlocks that use this font. The words that disappear would have appeared at the end of the line they are on, or perhaps on the next line开发者_运维问答, which makes me think that something is wrong with the Text wrapping. I have finally (after much difficulty) isolated the problem to a small piece of code (NOTE: this is only a test to confirm the problem occurs. This is not part of my program):

<Window x:Class="Test_the_textbox_width.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="850">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid 
          HorizontalAlignment="Center">
    <TextBlock HorizontalAlignment="Left"
               Name="textBlock1"
               VerticalAlignment="Top"
               FontFamily="Pigiarniq%20Regular.ttf#Pigiarniq"
               TextWrapping="Wrap"
               FontSize="15"
               Margin="10">
        <TextBlock.Text>
             You do not need to enter both manually. Rates are calculated using true depositional 
            (uncompacted) thicknesses, rather than measured present-day thicknesses.
        </TextBlock.Text>
    </TextBlock>
    </Grid>

</Grid>

The word "thicknesses" that occurs after "(uncompacted)" does not appear. If I resize the window manually using the window handle, it will re-appear.

NOTE: I have put the TextBlock in a Grid element above because sometimes this problem occurs when the TextBlock is in a Grid. In another instance it was inside an InlineUIContainer. There were other instances as well.

I have found that this behaviour doesn't occur if I set the width explicitly, such as if I subscribe to the Loaded event on the TextBlock:

private void textBlock1_Loaded(object sender, RoutedEventArgs e)
    {
        textBlock1.Width = textBlock1.ActualWidth;
        //textBlock1.Width = Double.NaN;  //if you leave this in, you get the bad behaviour
    }

Very strange! Any ideas on how to prevent this from happening? My boss would really like to use this font, so it's not like I can just choose another font (I've never seen this happen with any other font). I'm also not too keen on the idea of watching every change in the layout and re-sizing every textblock appropriately that uses Pigiarniq.


We actually encountered the same problem with the default system font. This seems to be a bug in the textblock line break feature itself.

We found that in windows with fixed size, the line break sometimes ignores the last word. This happened for various sizes and sentences.

We couldn't find a real solution, so our workaround was to manually include the line breaks in the text.

We also did quite a bit of googling on the subject because i refused to believe that such an issue was undocumented..but it seems this is undocumented (or at least hard to find as of two months ago) and after some very rigorous testing i am convinced that this is in fact a bug.


I was able to workaround this by setting TextAlignment="Left" and HorizontalAlignment="Stretch" on the TextBlock.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜