C2021-005 methods to get and set the font size in table cells

This commit is contained in:
John Jenko 2021-02-19 19:17:25 +00:00
parent 294b7f5618
commit 41660d97ae

View File

@ -2634,46 +2634,56 @@ namespace Volian.Controls.Library
}
}
//B2020-018: SetFontSize and SetFontSizeReplace were moved from StepTabRibbon so that the short cut keys could also
// access the code that puts boundaries on font size.
// SetFontSize: replace rtf string of selection or entire rtb
public void SetFontSize(bool increase)
// C2021-005 set the selected cell(s) or cell text to the passed in font size
public void SetFontSize(float newSize)
{
if (SelectedText == null || SelectedText == "")
Rtf = SetFontSizeReplace(Rtf, increase);
if (SelectedText == null || SelectedText == "") // table cell selected
Rtf = SetFontSizeReplace(Rtf, newSize);
else
{
// table cell text selected
int selst = SelectionStart;
int selln = SelectionLength;
SelectedRtf = SetFontSizeReplace(SelectedRtf, increase);
Select(selst, selln);
SelectedRtf = SetFontSizeReplace(SelectedRtf, newSize); // set font size of seleted text
Select(selst, selln); // re-select the cell text
}
}
private string SetFontSizeReplace(string rtf, bool increase)
// C2021-005 set the font size of the passed in RTF text
private string SetFontSizeReplace(string rtf, float newSize)
{
MatchCollection mc = Regex.Matches(rtf, @"\\fs([0-9]+)");
bool didMsg = false;
foreach (Match match in mc)
{
float sz = float.Parse(match.Groups[1].Value);
float repwith = increase ? sz + 1 : sz - 1;
if (repwith > 36 || repwith < 16) // Font size can be increased to 18 and decreased to 8 (note that \fs## has number 2 * pt)
{
if (!didMsg) // only put out message once.
{
MessageBox.Show(increase ? "Reached maximum font size, cannot increase." : "Reached minimum font size, cannot decrease.", "Warning font size change");
didMsg = true;
}
}
else
{
string repsz = match.Value; // use \fs## rather than number in replace in case text contains the number
string repwithsz = match.Value.Replace(sz.ToString(), ((int)repwith).ToString());
rtf = rtf.Replace(repsz, repwithsz);
}
float repwith = newSize * 2; // RTF font size is double the selected point size
string repsz = match.Value; // use \fs## rather than number in replace in case text contains the number
string repwithsz = match.Value.Replace(sz.ToString(), ((int)repwith).ToString());
rtf = rtf.Replace(repsz, repwithsz);
}
return rtf;
}
// C2021-005 parse the rtf for the font sizes.
// if there are different font sizes, then return 0
public float GetRTFFontSize()
{
string rtf;
if (SelectedText == null || SelectedText == "")
rtf = Rtf;
else
rtf = SelectedRtf;
MatchCollection mc = Regex.Matches(rtf, @"\\fs([0-9]+)");
bool differentFonts = false;
float lastSZ = 0;
foreach (Match match in mc)
{
float sz = float.Parse(match.Groups[1].Value);
if (lastSZ == 0)
lastSZ = sz;
else if (lastSZ != sz)
differentFonts = true;
}
return (differentFonts || lastSZ == 0) ? 0 : lastSZ/2; // divide the RTF font size by two to get the font point size
}
private static Form ParentForm(Control ctrl)
{
while (!(ctrl.Parent is Form || ctrl.Parent is EditItem))