11using System ;
2- using System . Security . Cryptography . X509Certificates ;
32using System . Text ;
43using System . Text . RegularExpressions ;
54
@@ -15,9 +14,10 @@ public enum RubyShowType
1514 }
1615
1716 // ruby tag
18- public static readonly Regex RUBY_REGEX = new Regex ( @"<r(uby)?=""?(?<ruby>[\s\S]*?)""?>(?<val>[\s\S]*?)<\/r(uby)?>" ) ;
17+ public static readonly Regex RUBY_REGEX = new ( @"<r(uby)?=""?(?<ruby>[\s\S]*?)""?>(?<val>[\s\S]*?)<\/r(uby)?>|<ruby>(?<base>[\s\S]*?)<rt>(?<rubyText>[\s\S]*?)<\/rt><\/ruby>|<r>(?<base>[\s\S]*?)<rt>(?<rubyText>[\s\S]*?)<\/rt><\/r>" ) ;
18+
1919 private static Lazy < StringBuilder > stringBuilder = new Lazy < StringBuilder > ( ) ;
20-
20+
2121 public static string ReplaceRubyTags ( this IRubyText targetRubyText , string str , int dir , float fontSizeScale , float hiddenSpaceW )
2222 {
2323 // Replace <ruby> tags text layout.
@@ -35,7 +35,20 @@ public static string ReplaceRubyTags(this IRubyText targetRubyText, string str,
3535 {
3636 foreach ( Match match in matches )
3737 {
38- if ( match . Groups . Count != 5 )
38+ string baseText ;
39+ string rubyText ;
40+
41+ if ( match . Groups [ "base" ] . Success && match . Groups [ "rubyText" ] . Success )
42+ {
43+ baseText = match . Groups [ "base" ] . Value ;
44+ rubyText = match . Groups [ "rubyText" ] . Value ;
45+ }
46+ else if ( match . Groups [ "val" ] . Success && match . Groups [ "ruby" ] . Success )
47+ {
48+ baseText = match . Groups [ "val" ] . Value ;
49+ rubyText = match . Groups [ "ruby" ] . Value ;
50+ }
51+ else
3952 {
4053 continue ;
4154 }
@@ -48,10 +61,6 @@ public static string ReplaceRubyTags(this IRubyText targetRubyText, string str,
4861 stringBuilder . Append ( unmatchPart ) ;
4962 }
5063
51- string fullMatch = match . Groups [ 0 ] . ToString ( ) ;
52- string rubyText = match . Groups [ "ruby" ] . ToString ( ) ;
53- string baseText = match . Groups [ "val" ] . ToString ( ) ;
54-
5564 float rubyTextW = targetRubyText . GetPreferredValues ( rubyText ) . x * ( targetRubyText . isOrthographic ? 1 : 10f ) *
5665 targetRubyText . rubyScale ;
5766 float baseTextW = targetRubyText . GetPreferredValues ( baseText ) . x * ( targetRubyText . isOrthographic ? 1 : 10f ) ;
@@ -72,27 +81,27 @@ public static string ReplaceRubyTags(this IRubyText targetRubyText, string str,
7281
7382 stringBuilder . Append ( replace ) ;
7483 }
75-
84+
7685 Match lastMatch = matches [ matches . Count - 1 ] ;
7786 stringBuilder . Append ( str . Substring ( lastMatch . Index + lastMatch . Length ) ) ;
7887 }
7988
8089 if ( ! string . IsNullOrWhiteSpace ( targetRubyText . rubyLineHeight ) )
90+ {
8191 // warning! bad Know-how
8292 // line-height tag is down the next line start.
8393 // now line can't change, corresponding by putting a hidden ruby
84- {
85- stringBuilder . Insert ( 0 , $ "<line-height={ targetRubyText . rubyLineHeight } ><voffset={ targetRubyText . rubyVerticalOffset } ><size={ targetRubyText . rubyScale * 100f } %>\u00A0 </size></voffset><space={ hiddenSpaceW } >") ;
94+ stringBuilder . Insert ( 0 , $ "<line-height={ targetRubyText . rubyLineHeight } ><voffset={ targetRubyText . rubyVerticalOffset } ><size={ targetRubyText . rubyScale * 100f } %>\u00A0 </size></voffset><space={ hiddenSpaceW } >") ;
8695 }
87-
96+
8897 return stringBuilder . ToString ( ) ;
8998 }
9099
91100 private static string CreateReplaceValue (
92- this IRubyText targetRubyText ,
101+ this IRubyText targetRubyText ,
93102 int dir ,
94- string baseText , float baseTextW ,
95- string rubyText , float rubyTextW ,
103+ string baseText , float baseTextW ,
104+ string rubyText , float rubyTextW ,
96105 ref float currentTextW , ref float rubyCurrentTextW )
97106 {
98107 float baseTextDirW = dir * baseTextW ;
@@ -127,21 +136,23 @@ private static string CreateReplaceValue(
127136 {
128137 if ( compensationOffset < 0 )
129138 {
130- replace = targetRubyText . CreateRubyAfterBaseText ( baseText , rubyText , rubyTextOffset , compensationOffset ) ;
139+ replace = targetRubyText . CreateRubyAfterBaseText ( baseText , rubyText , rubyTextOffset , compensationOffset ) ;
131140 currentTextW += rubyTextDirW ;
132141 }
133142 else
134143 {
135- replace = targetRubyText . CreateBaseAfterRubyText ( baseText , rubyText , rubyTextOffset , compensationOffset ) ;
144+ replace = targetRubyText . CreateBaseAfterRubyText ( baseText , rubyText , rubyTextOffset , compensationOffset ) ;
136145 currentTextW += baseTextDirW ;
137146 }
138147 }
139148 break ;
149+
140150 case RubyShowType . BASE_NO_OVERRAP_RUBY_ALIGNMENT :
141151 stringBuilder . Value . Clear ( ) ;
142152 float rubyCurrentTextOffsetW = currentTextW + ( baseTextDirW + rubyTextOffset ) - rubyCurrentTextW ;
143153
144- if ( 0f > rubyCurrentTextOffsetW ) {
154+ if ( 0f > rubyCurrentTextOffsetW )
155+ {
145156 stringBuilder . Value . Append ( $ "<space={ - rubyCurrentTextOffsetW } >") ;
146157 rubyCurrentTextW = rubyCurrentTextW + rubyTextDirW + targetRubyText . rubyMargin ;
147158 currentTextW += - rubyCurrentTextOffsetW ;
@@ -166,17 +177,13 @@ private static string CreateReplaceValue(
166177 public static string CreateBaseAfterRubyText (
167178 this IRubyText targetRubyText , string baseText , string rubyText , float rubyTextOffset , float compensationOffset )
168179 {
169- return
170- $ "<nobr>{ baseText } <space={ rubyTextOffset } ><voffset={ targetRubyText . rubyVerticalOffset } ><size={ targetRubyText . rubyScale * 100f } %>{ rubyText } </size></voffset><space={ compensationOffset } ></nobr>";
180+ return $ "<nobr>{ baseText } <space={ rubyTextOffset } ><voffset={ targetRubyText . rubyVerticalOffset } ><size={ targetRubyText . rubyScale * 100f } %>{ rubyText } </size></voffset><space={ compensationOffset } ></nobr>";
171181 }
172182
173183 public static string CreateRubyAfterBaseText (
174184 this IRubyText targetRubyText , string baseText , string rubyText , float rubyTextOffset , float compensationOffset )
175185 {
176- return
177- $ "<nobr><space={ - compensationOffset } >{ baseText } <space={ rubyTextOffset } ><voffset={ targetRubyText . rubyVerticalOffset } ><size={ targetRubyText . rubyScale * 100f } %>{ rubyText } </size></voffset><space={ compensationOffset } ></nobr>";
186+ return $ "<nobr><space={ - compensationOffset } >{ baseText } <space={ rubyTextOffset } ><voffset={ targetRubyText . rubyVerticalOffset } ><size={ targetRubyText . rubyScale * 100f } %>{ rubyText } </size></voffset><space={ compensationOffset } ></nobr>";
178187 }
179-
180-
181188 }
182- }
189+ }
0 commit comments