@@ -7,42 +7,46 @@ namespace TMPro
77{
88 using static RubyTextMeshProDefinitions ;
99
10- public class RubyTextMeshPro : TextMeshPro
10+ public class RubyTextMeshPro : TextMeshPro , IRubyText
1111 {
12- [ Tooltip ( "v offset ruby. (em, px, %)." ) ] [ SerializeField ]
13- private string rubyVerticalOffset = "1em" ;
14-
15- [ Tooltip ( "ruby scale. (1=100%)" ) ] [ SerializeField ]
16- private float rubyScale = 0.5f ;
17-
18- [ Tooltip ( "The height of the ruby line can be specified. (em, px, %)." ) ] [ SerializeField ]
19- private string rubyLineHeight = "" ;
20-
21- [ FormerlySerializedAs ( "m_uneditedText" ) ] [ SerializeField ] [ TextArea ( 5 , 10 ) ]
12+ [ Tooltip ( "v offset ruby. (em, px, %)." ) ]
13+ [ SerializeField ]
14+ [ FormerlySerializedAs ( "rubyVerticalOffset" ) ]
15+ private string _rubyVerticalOffset = "1em" ;
16+ public string rubyVerticalOffset => this . _rubyVerticalOffset ;
17+
18+ [ Tooltip ( "ruby scale. (1=100%)" ) ]
19+ [ SerializeField ]
20+ [ FormerlySerializedAs ( "rubyScale" ) ]
21+ private float _rubyScale = 0.5f ;
22+ public float rubyScale => this . _rubyScale ;
23+
24+ [ Tooltip ( "The height of the ruby line can be specified. (em, px, %)." ) ]
25+ [ SerializeField ]
26+ [ FormerlySerializedAs ( "rubyLineHeight" ) ]
27+ private string _rubyLineHeight = "" ;
28+ public string rubyLineHeight => this . _rubyLineHeight ;
29+
30+ [ TextArea ( 5 , 10 ) ]
31+ [ SerializeField ]
32+ [ FormerlySerializedAs ( "m_uneditedText" ) ]
2233 private string _uneditedText ;
23-
24- [ Tooltip ( "ruby show type." ) ] [ SerializeField ]
25- private RubyShowType rubyShowType = RubyShowType . RUBY_ALIGNMENT ;
26-
27- [ Obsolete ( "This setter will be discontinued.Use uneditedText instead." ) ]
28- public string UnditedText
29- {
30- set
31- {
32- this . _uneditedText = value ;
33- this . SetTextCustom ( this . _uneditedText ) ;
34- }
35- }
36-
3734 public string uneditedText
3835 {
36+ get => this . _uneditedText ;
3937 set
4038 {
4139 this . _uneditedText = value ;
4240 this . SetTextCustom ( this . _uneditedText ) ;
4341 }
4442 }
4543
44+ [ Tooltip ( "ruby show type." ) ]
45+ [ SerializeField ]
46+ [ FormerlySerializedAs ( "rubyShowType" ) ]
47+ private RubyShowType _rubyShowType = RubyShowType . RUBY_ALIGNMENT ;
48+ public RubyShowType rubyShowType => this . _rubyShowType ;
49+
4650#if UNITY_EDITOR
4751
4852 protected override void OnValidate ( )
@@ -104,146 +108,9 @@ private string ReplaceRubyTags(string str)
104108 int dir = this . isRightToLeftText ? 1 : - 1 ;
105109 // Q. Why (m_isOrthographic ? 1 : 10f) => A. TMP_Text.cs L7622, L7625
106110 float hiddenSpaceW = dir * nonBreakSpaceW * ( this . m_isOrthographic ? 1 : 10f ) * this . rubyScale * fontSizeScale ;
107- // Replace <ruby> tags text layout.
108- MatchCollection matches = RubyTextMeshProDefinitions . RUBY_REGEX . Matches ( str ) ;
109-
110- foreach ( Match match in matches )
111- {
112- if ( match . Groups . Count != 5 )
113- {
114- continue ;
115- }
116-
117- string fullMatch = match . Groups [ 0 ] . ToString ( ) ;
118- string rubyText = match . Groups [ "ruby" ] . ToString ( ) ;
119- string baseText = match . Groups [ "val" ] . ToString ( ) ;
120-
121- float rubyTextW = this . GetPreferredValues ( rubyText ) . x * ( this . m_isOrthographic ? 1 : 10f ) * this . rubyScale ;
122- float baseTextW = this . GetPreferredValues ( baseText ) . x * ( this . m_isOrthographic ? 1 : 10f ) ;
123-
124- if ( this . m_enableAutoSizing )
125- {
126- rubyTextW *= fontSizeScale ;
127- baseTextW *= fontSizeScale ;
128- }
129-
130- float rubyTextOffset = dir * ( baseTextW / 2f + rubyTextW / 2f ) ;
131- float compensationOffset = - dir * ( ( baseTextW - rubyTextW ) / 2f ) ;
132- string replace = this . CreateReplaceValue ( baseText , rubyText , rubyTextOffset , compensationOffset , this . isRightToLeftText ) ;
133- str = str . Replace ( fullMatch , replace ) ;
134- }
135-
136- if ( ! string . IsNullOrWhiteSpace ( this . rubyLineHeight ) )
137- // warning! bad Know-how
138- // line-height tag is down the next line start.
139- // now line can't change, corresponding by putting a hidden ruby
140- {
141- str = $ "<line-height={ this . rubyLineHeight } ><voffset={ this . rubyVerticalOffset } ><size={ this . rubyScale * 100f } %>\u00A0 </size></voffset><space={ hiddenSpaceW } >" + str ;
142- }
111+ str = this . ReplaceRubyTags ( str , dir , fontSizeScale , hiddenSpaceW ) ;
143112
144113 return str ;
145114 }
146-
147- private string CreateReplaceValue ( string baseText , string rubyText , float rubyTextOffset , float compensationOffset , bool isRightToLeftText )
148- {
149- string replace = string . Empty ;
150-
151- switch ( this . rubyShowType )
152- {
153- case RubyShowType . BASE_ALIGNMENT :
154- replace = $ "<nobr>{ baseText } <space={ rubyTextOffset } ><voffset={ this . rubyVerticalOffset } ><size={ this . rubyScale * 100f } %>{ rubyText } </size></voffset><space={ compensationOffset } ></nobr>";
155- break ;
156-
157- case RubyShowType . RUBY_ALIGNMENT :
158- if ( isRightToLeftText )
159- {
160- if ( compensationOffset < 0 )
161- {
162- replace = $ "<nobr>{ baseText } <space={ rubyTextOffset } ><voffset={ this . rubyVerticalOffset } ><size={ this . rubyScale * 100f } %>{ rubyText } </size></voffset><space={ compensationOffset } ></nobr>";
163- }
164- else
165- {
166- replace = $ "<nobr><space={ - compensationOffset } >{ baseText } <space={ rubyTextOffset } ><voffset={ this . rubyVerticalOffset } ><size={ this . rubyScale * 100f } %>{ rubyText } </size></voffset></nobr>";
167- }
168- }
169- else
170- {
171- if ( compensationOffset < 0 )
172- {
173- replace = $ "<nobr><space={ - compensationOffset } >{ baseText } <space={ rubyTextOffset } ><voffset={ this . rubyVerticalOffset } ><size={ this . rubyScale * 100f } %>{ rubyText } </size></voffset></nobr>";
174- }
175- else
176- {
177- replace = $ "<nobr>{ baseText } <space={ rubyTextOffset } ><voffset={ this . rubyVerticalOffset } ><size={ this . rubyScale * 100f } %>{ rubyText } </size></voffset><space={ compensationOffset } ></nobr>";
178- }
179- }
180-
181- break ;
182- }
183-
184- return replace ;
185- }
186-
187- /// <summary>
188- /// TMP_Text ConvertToFloat
189- /// if (startIndex == 0) { lastIndex = 0; return -9999; } delete version
190- /// </summary>
191- protected float ConvertToFloatOrigin ( char [ ] chars , int startIndex , int lastIndex )
192- {
193- int endIndex = lastIndex ;
194- bool isIntegerValue = true ;
195- float decimalPointMultiplier = 0 ;
196-
197- // Set value multiplier checking the first character to determine if we are using '+' or '-'
198- int valueSignMultiplier = 1 ;
199-
200- if ( chars [ startIndex ] == '+' )
201- {
202- valueSignMultiplier = 1 ;
203- startIndex += 1 ;
204- }
205- else if ( chars [ startIndex ] == '-' )
206- {
207- valueSignMultiplier = - 1 ;
208- startIndex += 1 ;
209- }
210-
211- float value = 0 ;
212-
213- for ( int i = startIndex ; i < endIndex ; i ++ )
214- {
215- uint c = chars [ i ] ;
216-
217- if ( c is >= '0' and <= '9' || c == '.' )
218- {
219- if ( c == '.' )
220- {
221- isIntegerValue = false ;
222- decimalPointMultiplier = 0.1f ;
223- continue ;
224- }
225-
226- //Calculate integer and floating point value
227- if ( isIntegerValue )
228- {
229- value = value * 10 + ( c - 48 ) * valueSignMultiplier ;
230- }
231- else
232- {
233- value = value + ( c - 48 ) * decimalPointMultiplier * valueSignMultiplier ;
234- decimalPointMultiplier *= 0.1f ;
235- }
236-
237- continue ;
238- }
239-
240- if ( c == ',' )
241- {
242- return value ;
243- }
244- }
245-
246- return value ;
247- }
248115 }
249116}
0 commit comments