Skip to content

Commit bee4d79

Browse files
author
JunichiEnomoto
committed
refactor : Delete the same source
1 parent fdb0c87 commit bee4d79

File tree

6 files changed

+200
-270
lines changed

6 files changed

+200
-270
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using UnityEngine;
2+
3+
namespace TMPro
4+
{
5+
public interface IRubyText
6+
{
7+
#region TMP
8+
// The point size of the font.
9+
public bool enableAutoSizing { get; }
10+
// Sets Perspective Correction to Zero for Orthographic Camera mode & 0.875f for Perspective Camera mode.
11+
public bool isOrthographic { get; }
12+
// If true, from right to left. Otherwise, left to right.
13+
public bool isRightToLeftText { get; set; }
14+
// Function to Calculate the Preferred Width and Height of the text object given a certain string.
15+
public Vector2 GetPreferredValues(string str);
16+
17+
#endregion
18+
19+
#region RUBY_TMP
20+
// v offset ruby. (em, px, %).
21+
public string rubyVerticalOffset { get; }
22+
// ruby scale. (1=100%)
23+
public float rubyScale { get; }
24+
// The height of the ruby line can be specified. (em, px, %)
25+
public string rubyLineHeight { get; }
26+
// ruby show type.
27+
public RubyTextMeshProDefinitions.RubyShowType rubyShowType { get; }
28+
29+
#endregion
30+
31+
}
32+
}

Assets/RubyTextMeshPro/Source/IRubyText.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/RubyTextMeshPro/Source/RubyTextMeshPro.cs

Lines changed: 30 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Assets/RubyTextMeshPro/Source/RubyTextMeshProDefinitions.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,98 @@ public enum RubyShowType
1212

1313
// ruby tag
1414
public static readonly Regex RUBY_REGEX = new Regex(@"<r(uby)?=""?(?<ruby>[\s\S]*?)""?>(?<val>[\s\S]*?)<\/r(uby)?>");
15+
16+
public static string ReplaceRubyTags(this IRubyText targetRubyText, string str, int dir, float fontSizeScale, float hiddenSpaceW)
17+
{
18+
// Replace <ruby> tags text layout.
19+
MatchCollection matches = RubyTextMeshProDefinitions.RUBY_REGEX.Matches(str);
20+
21+
foreach (Match match in matches)
22+
{
23+
if (match.Groups.Count != 5)
24+
{
25+
continue;
26+
}
27+
28+
string fullMatch = match.Groups[0].ToString();
29+
string rubyText = match.Groups["ruby"].ToString();
30+
string baseText = match.Groups["val"].ToString();
31+
32+
float rubyTextW = targetRubyText.GetPreferredValues(rubyText).x * (targetRubyText.isOrthographic ? 1 : 10f) * targetRubyText.rubyScale;
33+
float baseTextW = targetRubyText.GetPreferredValues(baseText).x * (targetRubyText.isOrthographic ? 1 : 10f);
34+
35+
if (targetRubyText.enableAutoSizing)
36+
{
37+
rubyTextW *= fontSizeScale;
38+
baseTextW *= fontSizeScale;
39+
}
40+
41+
float rubyTextOffset = dir * (baseTextW / 2f + rubyTextW / 2f);
42+
float compensationOffset = -dir * ((baseTextW - rubyTextW) / 2f);
43+
string replace = targetRubyText.CreateReplaceValue(
44+
baseText,
45+
rubyText, rubyTextOffset,
46+
compensationOffset);
47+
str = str.Replace(fullMatch, replace);
48+
}
49+
50+
if (!string.IsNullOrWhiteSpace(targetRubyText.rubyLineHeight))
51+
// warning! bad Know-how
52+
// line-height tag is down the next line start.
53+
// now line can't change, corresponding by putting a hidden ruby
54+
{
55+
str = $"<line-height={targetRubyText.rubyLineHeight}><voffset={targetRubyText.rubyVerticalOffset}><size={targetRubyText.rubyScale * 100f}%>\u00A0</size></voffset><space={hiddenSpaceW}>" + str;
56+
}
57+
58+
return str;
59+
}
60+
61+
private static string CreateReplaceValue(
62+
this IRubyText targetRubyText,
63+
string baseText, string rubyText, float rubyTextOffset, float compensationOffset)
64+
{
65+
string replace = string.Empty;
66+
67+
switch (targetRubyText.rubyShowType)
68+
{
69+
case RubyShowType.BASE_ALIGNMENT:
70+
replace = targetRubyText.CreateBaseAfterRubyText(baseText, rubyTextOffset, rubyText, compensationOffset);
71+
break;
72+
73+
case RubyShowType.RUBY_ALIGNMENT:
74+
if (targetRubyText.isRightToLeftText)
75+
{
76+
replace = compensationOffset < 0 ?
77+
targetRubyText.CreateBaseAfterRubyText(baseText, rubyTextOffset, rubyText, compensationOffset):
78+
targetRubyText.CreateRubyAfterBaseText(baseText, rubyTextOffset, rubyText, compensationOffset);
79+
}
80+
else
81+
{
82+
replace = compensationOffset < 0 ?
83+
targetRubyText.CreateRubyAfterBaseText(baseText, rubyTextOffset, rubyText, compensationOffset):
84+
targetRubyText.CreateBaseAfterRubyText(baseText, rubyTextOffset, rubyText, compensationOffset);
85+
}
86+
87+
break;
88+
}
89+
90+
return replace;
91+
}
92+
93+
public static string CreateBaseAfterRubyText(
94+
this IRubyText targetRubyText, string baseText,float rubyTextOffset, string rubyText, float compensationOffset)
95+
{
96+
return
97+
$"<nobr>{baseText}<space={rubyTextOffset}><voffset={targetRubyText.rubyVerticalOffset}><size={targetRubyText.rubyScale * 100f}%>{rubyText}</size></voffset><space={compensationOffset}></nobr>";
98+
}
99+
100+
public static string CreateRubyAfterBaseText(
101+
this IRubyText targetRubyText, string baseText,float rubyTextOffset, string rubyText, float compensationOffset)
102+
{
103+
return
104+
$"<nobr><space={-compensationOffset}>{baseText}<space={rubyTextOffset}><voffset={targetRubyText.rubyVerticalOffset}><size={targetRubyText.rubyScale * 100f}%>{rubyText}</size></voffset><space={compensationOffset}></nobr>";
105+
}
106+
107+
15108
}
16109
}

0 commit comments

Comments
 (0)