Why UE5 starts Japanese lines with ッ or ー (and a line break iterator that fixes it)
This one is subtle, and if you don't read Japanese you'd probably never notice it.
Here's a dialogue box from a test project of mine, using UE's default text wrapping:
Look at the line starts. キ / ャッシュレス (cashless) breaks right before a small ャ, and ショ / ッピング (shopping) before a small ッ. Japanese typesetting has a rule for this called kinsoku shori: a line may not start with small kana (ッ ャ ュ ョ …) or the long vowel mark ー. To a Japanese reader, a line that starts with ッ looks about as wrong as an English line starting with a comma. Nobody will file a bug, but it makes the game feel a bit cheap.
I make text plugins for Japanese in UE5, so I had to understand why the engine does this. It turned out to be a nice little rabbit hole through ICU and Slate (checked against UE 5.8). The good news: the fix is small, and you don't need to read Japanese to use it.
Short version
- Slate wraps text where ICU says it may break, and ICU follows the Unicode line breaking rules (UAX #14).
- In UAX #14, small kana and
ーare in a class calledCJ. In the "strict" styleCJcan't start a line; in the "normal" style it can. ICU uses normal for a Japanese locale, and UE takes the locale from the OS language. So on a Japanese PC, breaking beforeッis allowed. - Punctuation like
、。 」is a different class (CL) and is already fine.CJis the real culprit. - Fix: wrap the engine's iterator, throw away the break positions that violate kinsoku, and hand your iterator to the text layout.
1. Slate asks ICU where it may break
Slate's text layout (FTextLayout) asks an IBreakIterator for break opportunities while wrapping. The default comes from FBreakIterator::CreateLineBreakIterator(), which is ICU's line break iterator:
Engine/Source/Runtime/Core/Private/Internationalization/ICUBreakIterator.cpp:FICUBreakIteratorManager::CreateLineBreakIterator()clones the ICU line iterator ofGetDefaultCulture(), which is the OS language, not the game's current culture.Engine/Source/Runtime/Core/Private/Internationalization/ICULineBreakIterator.cpp: wraps it as anIBreakIterator.
2. Small kana belong to line-break class CJ
UAX #14 gives every character a line-break class. The ones that matter for Japanese:
CL(closing punctuation)、。 」)- NoEX(exclamation)!?- NoCJ(small kana, prolonged sound mark)ッャュョァー- Depends on the styleID(ideographs, kana)あ 漢- Yes
By default (rule LB1), CJ is treated as NS (non-starter), and ICU's root rules (line.brk) are the strict style. But ICU's data for the ja locale (and ko) switches the default to line_normal, where CJ is treated like ID. You can see it in the ICU data UE ships: Engine/Content/Internationalization/.../icudt78l/brkitr/ja.res maps line to line_normal.brk, while root.res maps it to line.brk.
Since UE picks the locale from the OS, my screenshot above is what you get on a Japanese Windows. On an English OS you may not be able to reproduce it at all, but your players in Japan will see it.
3. The fix: filter the engine's break positions
Keep the engine's break positions (so English, Korean and everything else still wrap the same way) and drop only the ones that violate kinsoku:
#include "Internationalization/BreakIterator.h"
#include "Internationalization/IBreakIterator.h"
// Characters that must not start a line (subset)
static bool IsLineStartProhibited(TCHAR C)
{
static const FString Chars = TEXT("、。,.・:;?!ー-~)」』】〕〉》ぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ");
int32 Index;
return Chars.FindChar(C, Index);
}
// Characters that must not end a line (subset)
static bool IsLineEndProhibited(TCHAR C)
{
static const FString Chars = TEXT("(「『【〔〈《");
int32 Index;
return Chars.FindChar(C, Index);
}
class FKinsokuLineBreakIterator final : public IBreakIterator
{
public:
FKinsokuLineBreakIterator() : Inner(FBreakIterator::CreateLineBreakIterator()) { }
using IBreakIterator::SetString;
using IBreakIterator::SetStringRef;
virtual void SetString(FString&& InString) override { Text = MoveTemp(InString); Rebuild(); }
virtual void SetStringRef(FStringView InString) override { Text = FString(InString); Rebuild(); }
virtual int32 GetCurrentPosition() const override { return Current; }
virtual int32 ResetToBeginning() override { return Current = 0; }
virtual int32 ResetToEnd() override { return Current = Text.Len(); }
virtual int32 MoveToNext() override { return MoveToCandidateAfter(Current); }
virtual int32 MoveToPrevious() override { return MoveToCandidateBefore(Current); }
virtual int32 MoveToCandidateBefore(const int32 InIndex) override
{
for (int32 i = Breaks.Num() - 1; i >= 0; --i)
{
if (Breaks[i] < InIndex) { return Current = Breaks[i]; }
}
return InIndex > 0 ? (Current = 0) : INDEX_NONE;
}
virtual int32 MoveToCandidateAfter(const int32 InIndex) override
{
for (const int32 Break : Breaks)
{
if (Break > InIndex) { return Current = Break; }
}
return INDEX_NONE;
}
private:
// Take every break position from the engine (ICU) and drop the ones that break kinsoku
void Rebuild()
{
Breaks.Reset();
Current = 0;
if (Text.IsEmpty()) { return; }
Inner->SetStringRef(FStringView(Text));
int32 Break;
while ((Break = Inner->MoveToNext()) != INDEX_NONE)
{
const bool bAtEnd = Break >= Text.Len();
if (bAtEnd || (!IsLineStartProhibited(Text[Break]) && !IsLineEndProhibited(Text[Break - 1])))
{
Breaks.Add(Break);
}
}
Inner->ClearString();
}
TSharedRef<IBreakIterator> Inner;
FString Text;
TArray<int32> Breaks;
int32 Current = 0;
};
Everything here lives in the Slate and SlateCore modules, so add both to your Build.cs (plus UMG for the widgets below).
Text Block (STextBlock)
STextBlock accepts it through its LineBreakPolicy argument. Subclass UMG's UTextBlock and override RebuildWidget, mirroring the engine's version with the policy added. Text, wrapping and fonts are applied later in SynchronizeProperties, as usual:
#include "Misc/EngineVersionComparison.h"
#include "Widgets/SInvalidationPanel.h"
#include "Widgets/Text/STextBlock.h"
TSharedRef<SWidget> UKinsokuTextBlock::RebuildWidget()
{
// Same as UTextBlock::RebuildWidget, plus LineBreakPolicy.
// (Simple Text Mode never wraps, so it is left out here.)
MyTextBlock = SNew(STextBlock)
.LineBreakPolicy(MakeShared<FKinsokuLineBreakIterator>())
#if !UE_VERSION_OLDER_THAN(5, 8, 0)
.OnAllFontFacesFinishLoading(OnAllFontFacesFinishLoading.IsBound() ? BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandleAllFontFacesFinishLoading) : FSimpleDelegate())
#endif
;
if (bWrapWithInvalidationPanel && !IsDesignTime())
{
return SNew(SInvalidationPanel)[MyTextBlock.ToSharedRef()];
}
return MyTextBlock.ToSharedRef();
}
Rich Text Block
SRichTextBlock has no LineBreakPolicy. Instead, subclass the marshaller (FRichTextLayoutMarshaller) and set the iterator on the layout in SetText. Every text change goes through it, so it always applies:
#include "Framework/Text/RichTextLayoutMarshaller.h"
#include "Framework/Text/TextLayout.h"
class FKinsokuRichTextMarshaller final : public FRichTextLayoutMarshaller
{
public:
FKinsokuRichTextMarshaller(TSharedPtr<IRichTextMarkupParser> InParser, TSharedPtr<IRichTextMarkupWriter> InWriter, TArray<TSharedRef<ITextDecorator>> InDecorators, const ISlateStyle* const InDecoratorStyleSet)
: FRichTextLayoutMarshaller(MoveTemp(InParser), MoveTemp(InWriter), MoveTemp(InDecorators), InDecoratorStyleSet) { }
virtual void SetText(const FString& SourceString, FTextLayout& TargetTextLayout) override
{
TargetTextLayout.SetLineBreakIterator(Iterator);
FRichTextLayoutMarshaller::SetText(SourceString, TargetTextLayout);
}
private:
TSharedRef<IBreakIterator> Iterator = MakeShared<FKinsokuLineBreakIterator>();
};
URichTextBlock::RebuildWidget creates its marshaller with FRichTextLayoutMarshaller::Create directly, so you also need to subclass URichTextBlock and override RebuildWidget to use yours:
#include "Misc/EngineVersionComparison.h"
#include "Widgets/Text/SRichTextBlock.h"
TSharedRef<SWidget> UKinsokuRichTextBlock::RebuildWidget()
{
// Same as URichTextBlock::RebuildWidget, with our marshaller
UpdateStyleData();
TArray<TSharedRef<ITextDecorator>> CreatedDecorators;
CreateDecorators(CreatedDecorators);
TSharedRef<FRichTextLayoutMarshaller> Marshaller = MakeShared<FKinsokuRichTextMarshaller>(CreateMarkupParser(), CreateMarkupWriter(), CreatedDecorators, StyleInstance.Get());
PRAGMA_DISABLE_DEPRECATION_WARNINGS
MyRichTextBlock = SNew(SRichTextBlock)
.TextStyle(bOverrideDefaultStyle ? &DefaultTextStyleOverride : &DefaultTextStyle)
.Marshaller(Marshaller)
#if !UE_VERSION_OLDER_THAN(5, 8, 0)
.OnAllFontFacesFinishLoading(OnAllFontFacesFinishLoading.IsBound() ? BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandleAllFontFacesFinishLoading) : FSimpleDelegate())
#endif
;
PRAGMA_ENABLE_DEPRECATION_WARNINGS
return MyRichTextBlock.ToSharedRef();
}
4. Result
Same text, same box width:
No line starts with ャ or ッ anymore, and キャッシュレス / ショッピング are no longer split in the wrong place.
5. Gotchas
…… and ――. These pairs must stay together. ICU already never breaks inside them (UAX #14 rules LB22, LB17 and LB28), and the iterator above only removes breaks, so normal wrapping is fine. They can only split with per-character wrapping (next point).
Text that no longer fits. Dropping break positions makes unbreakable runs longer. With the default Wrapping Policy (Default Wrapping), a run without a break position overflows the box. For narrow boxes, set Wrapping Policy to "Allow Per Character Wrapping" so it breaks inside a run only when it must. That fallback splits at any character and ignores your iterator (TextLayout.cpp), so kinsoku doesn't apply there.
Inline images and widgets. In Rich Text they sit in the text as zero-width spaces (U+200B). UAX #14 never breaks before a zero-width space (rule LB7), so ICU won't offer a break in front of an inline widget at all. If you want one there, add that position yourself (unless the previous character is one that must not end a line).
Typewriter reveal. Lay out the full text first and reveal characters within that layout, so line breaks don't jump while the text appears.
If you'd rather not write it yourself I ended up putting this, plus ruby (furigana) support, into a free plugin called JpTextKit (UE 5.6-5.8). There's also a drop-in "Text Block (JpTextKit)" that only does the line breaking, if that's all you need. It's on BOOTH, which has an English UI: https://engineer-sasaki.booth.pm/items/9188416
Questions, or a case where this still breaks wrong? Leave a comment - Japanese line breaking has more corner cases than I'd like to admit.
Engine source referenced (UE 5.8)
Engine/Source/Runtime/Core/Private/Internationalization/ICUBreakIterator.cppEngine/Source/Runtime/Core/Private/Internationalization/ICULineBreakIterator.cppEngine/Source/Runtime/Core/Public/Internationalization/Internationalization.h(GetDefaultCulture)Engine/Source/Runtime/Slate/Public/Widgets/Text/STextBlock.h(LineBreakPolicy)Engine/Source/Runtime/Slate/Public/Framework/Text/TextLayout.h(SetLineBreakIterator)Engine/Source/Runtime/Slate/Private/Framework/Text/TextLayout.cpp(wrapping,ETextWrappingPolicy)Engine/Source/Runtime/UMG/Private/Components/RichTextBlock.cpp(RebuildWidget)Engine/Content/Internationalization/All/icudt78l/brkitr/(root.res,ja.res)- Unicode Standard Annex #14: Unicode Line Breaking Algorithm
Comments
No comments yet. Start the discussion.