Skip to content

Commit e871262

Browse files
Junichi EnomotoJunichi Enomoto
authored andcommitted
fix : hiddenSpaceW offset.
1 parent bcbae9a commit e871262

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

Assets/RubyTextMeshPro/Source/RubyTextMeshPro.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,18 @@ private void SetTextCustom(string value)
5959
private string ReplaceRubyTags(string str)
6060
{
6161
if (string.IsNullOrEmpty(str)) return str;
62-
var pVal = GetPreferredValues("\u00A0");
63-
var hiddenSpaceW = GetPreferredValues("\u00A0").x * (m_isOrthographic ? 1 : 10f);
62+
// warning! bad Know-how
63+
// Can not get GetPreferredValues("\u00A0").x at width,
64+
// add string and calculate.
65+
// and Use GetPreferredValues, change this.m_maxFontSize value.
66+
var nonBreakSpaceW = GetPreferredValues("\u00A0a").x - GetPreferredValues("a").x;
67+
var fontSizeScale = 1f;
68+
if (this.m_enableAutoSizing)
69+
{
70+
fontSizeScale = (this.m_fontSize / this.m_maxFontSize);
71+
}
72+
var dir = isRightToLeftText ? 1 : -1;
73+
var hiddenSpaceW = dir * nonBreakSpaceW * (m_isOrthographic ? 1 : 10f) * rubyScale * fontSizeScale;
6474
// Replace <ruby> tags text layout.
6575
var matches = RubyRegex.Matches(str);
6676
var compensationOffset = 0f;
@@ -75,11 +85,9 @@ private string ReplaceRubyTags(string str)
7585
var baseTextW = GetPreferredValues(baseText).x * (m_isOrthographic ? 1 : 10f);
7686
if (this.m_enableAutoSizing)
7787
{
78-
var calc = (this.m_fontSize / this.m_maxFontSize);
79-
rubyTextW *= calc;
80-
baseTextW *= calc;
88+
rubyTextW *= fontSizeScale;
89+
baseTextW *= fontSizeScale;
8190
}
82-
var dir = isRightToLeftText ? 1 : -1;
8391
var rubyTextOffset = dir * (baseTextW / 2f + rubyTextW / 2f);
8492
compensationOffset = -dir * ((baseTextW - rubyTextW) / 2f);
8593
var replace = CreateReplaceValue(baseText, rubyText, rubyTextOffset, compensationOffset, isRightToLeftText);
@@ -90,7 +98,7 @@ private string ReplaceRubyTags(string str)
9098
// warning! bad Know-how
9199
// line-height tag is down the next line start.
92100
// now line can't change, corresponding by putting a hidden ruby
93-
str = $"<line-height={allVCompensationRubyLineHeight}><voffset={rubyVerticalOffset}><size={rubyScale * 100f}%>\u00A0</size></voffset><space={compensationOffset}>" + str;
101+
str = $"<line-height={allVCompensationRubyLineHeight}><voffset={rubyVerticalOffset}><size={rubyScale * 100f}%>\u00A0</size></voffset><space={hiddenSpaceW}>" + str;
94102
}
95103

96104
return str;
@@ -208,7 +216,6 @@ protected override void OnValidate()
208216

209217
SetTextCustom(m_uneditedText);
210218
}
211-
212219
#endif
213220
}
214221
}

Assets/RubyTextMeshPro/Source/RubyTextMeshProUGUI.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,18 @@ private void SetTextCustom(string value)
5959
private string ReplaceRubyTags(string str)
6060
{
6161
if (string.IsNullOrEmpty(str)) return str;
62-
var hiddenSpaceW = GetPreferredValues("\u00A0").x * (m_isOrthographic ? 1 : 10f);
62+
// warning! bad Know-how
63+
// Can not get GetPreferredValues("\u00A0").x at width,
64+
// add string and calculate.
65+
// and Use GetPreferredValues, change this.m_maxFontSize value.
66+
var nonBreakSpaceW = GetPreferredValues("\u00A0a").x - GetPreferredValues("a").x;
67+
var fontSizeScale = 1f;
68+
if (this.m_enableAutoSizing)
69+
{
70+
fontSizeScale = (this.m_fontSize / this.m_maxFontSize);
71+
}
72+
var dir = isRightToLeftText ? 1 : -1;
73+
var hiddenSpaceW = dir * nonBreakSpaceW * (m_isOrthographic ? 1 : 10f) * rubyScale * fontSizeScale;
6374
// Replace <ruby> tags text layout.
6475
var matches = RubyRegex.Matches(str);
6576
var compensationOffset = 0f;
@@ -74,11 +85,9 @@ private string ReplaceRubyTags(string str)
7485
var baseTextW = GetPreferredValues(baseText).x * (m_isOrthographic ? 1 : 10f);
7586
if (this.m_enableAutoSizing)
7687
{
77-
var calc = (this.m_fontSize / this.m_maxFontSize);
78-
rubyTextW *= calc;
79-
baseTextW *= calc;
88+
rubyTextW *= fontSizeScale;
89+
baseTextW *= fontSizeScale;
8090
}
81-
var dir = isRightToLeftText ? 1 : -1;
8291
var rubyTextOffset = dir * (baseTextW / 2f + rubyTextW / 2f);
8392
compensationOffset = -dir * ((baseTextW - rubyTextW) / 2f);
8493
var replace = CreateReplaceValue(baseText, rubyText, rubyTextOffset, compensationOffset, isRightToLeftText);
@@ -89,7 +98,7 @@ private string ReplaceRubyTags(string str)
8998
// warning! bad Know-how
9099
// line-height tag is down the next line start.
91100
// now line can't change, corresponding by putting a hidden ruby
92-
str = $"<line-height={allVCompensationRubyLineHeight}><voffset={rubyVerticalOffset}><size={rubyScale * 100f}%>\u00A0</size></voffset><space={compensationOffset}>" + str;
101+
str = $"<line-height={allVCompensationRubyLineHeight}><voffset={rubyVerticalOffset}><size={rubyScale * 100f}%>\u00A0</size></voffset><space={hiddenSpaceW}>" + str;
93102
}
94103

95104
return str;
@@ -143,10 +152,9 @@ protected override void OnValidate()
143152
base.OnValidate();
144153

145154
ForceMeshUpdate();
146-
155+
147156
SetTextCustom(m_uneditedText);
148157
}
149-
150158
#endif
151159
}
152160
}

0 commit comments

Comments
 (0)