[RT]
This commit is contained in:
@@ -47,7 +47,7 @@ internal static class DynamicReplaceToTableExt
|
||||
string matchedText = paraText.Substring(pStart, pEnd + suffix.Length - pStart);
|
||||
|
||||
// Извлекаем свойства шрифта из Run-а, содержащего совпадение
|
||||
var fontProps = ExtractFontPropsFromParagraph(paragraph, matchedText);
|
||||
var fontProps = paragraph.ExtractFontProps(matchedText);
|
||||
|
||||
if (fontProps is { } props)
|
||||
fontProps = props.Override(baseFont);
|
||||
@@ -95,122 +95,4 @@ internal static class DynamicReplaceToTableExt
|
||||
parent.RemoveChild(paragraph);
|
||||
parent.InsertAt(table, paraIndex);
|
||||
}
|
||||
|
||||
private static FontProps? ExtractFontPropsFromParagraph(Paragraph paragraph, string searchText)
|
||||
{
|
||||
if (paragraph is null || string.IsNullOrEmpty(searchText))
|
||||
return null;
|
||||
|
||||
var runs = paragraph.Descendants<Run>().ToList();
|
||||
if (runs.Count == 0)
|
||||
return null;
|
||||
|
||||
var runInfos = new List<(Run Run, int Start, int End, string Text)>();
|
||||
int currentPosition = 0;
|
||||
|
||||
foreach (var run in runs)
|
||||
{
|
||||
var runText = GetRunText(run);
|
||||
if (!string.IsNullOrEmpty(runText))
|
||||
{
|
||||
runInfos.Add((run, currentPosition, currentPosition + runText.Length, runText));
|
||||
currentPosition += runText.Length;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var (run, start, end, text) in runInfos)
|
||||
{
|
||||
if (text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
return CreateFontPropsFromRun(run);
|
||||
}
|
||||
|
||||
var fullText = string.Concat(runInfos.Select(r => r.Text));
|
||||
int textIndex = fullText.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (textIndex >= 0)
|
||||
{
|
||||
var intersectingRun = runInfos.FirstOrDefault(r =>
|
||||
r.Start <= textIndex && r.End > textIndex);
|
||||
|
||||
if (intersectingRun.Run is not null)
|
||||
return CreateFontPropsFromRun(intersectingRun.Run);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static FontProps CreateFontPropsFromRun(Run run)
|
||||
{
|
||||
var runProperties = run.RunProperties;
|
||||
if (runProperties is null)
|
||||
return new FontProps();
|
||||
|
||||
var fontProps = new FontProps();
|
||||
|
||||
var runFonts = runProperties.GetFirstChild<RunFonts>();
|
||||
if (runFonts is not null)
|
||||
{
|
||||
fontProps = fontProps with
|
||||
{
|
||||
FontFamily = runFonts.Ascii ?? runFonts.HighAnsi ?? runFonts.ComplexScript
|
||||
};
|
||||
}
|
||||
|
||||
var fontSize = runProperties.GetFirstChild<FontSize>();
|
||||
if (fontSize is not null && !string.IsNullOrEmpty(fontSize.Val) &&
|
||||
uint.TryParse(fontSize.Val, out uint halfPoints))
|
||||
{
|
||||
fontProps = fontProps with { Size = halfPoints / 2.0 };
|
||||
}
|
||||
|
||||
var bold = runProperties.GetFirstChild<Bold>();
|
||||
if (bold is not null)
|
||||
{
|
||||
bool isBold = true;
|
||||
if (bold.Val is not null)
|
||||
isBold = bold.Val.Value;
|
||||
fontProps = fontProps with { IsBold = isBold };
|
||||
}
|
||||
|
||||
var italic = runProperties.GetFirstChild<Italic>();
|
||||
if (italic is not null)
|
||||
{
|
||||
bool isItalic = true;
|
||||
if (italic.Val is not null)
|
||||
isItalic = italic.Val.Value;
|
||||
fontProps = fontProps with { IsItalic = isItalic };
|
||||
}
|
||||
|
||||
var underline = runProperties.GetFirstChild<Underline>();
|
||||
if (underline is not null && underline.Val is not null)
|
||||
{
|
||||
fontProps = fontProps with { Underline = underline.Val.Value };
|
||||
}
|
||||
|
||||
var color = runProperties.GetFirstChild<Color>();
|
||||
if (color is not null && !string.IsNullOrEmpty(color.Val))
|
||||
{
|
||||
fontProps = fontProps with { Color = color.Val };
|
||||
}
|
||||
|
||||
var verticalAlignment = runProperties.GetFirstChild<VerticalTextAlignment>();
|
||||
if (verticalAlignment is not null && verticalAlignment.Val is not null)
|
||||
{
|
||||
fontProps = fontProps with { SubSup = verticalAlignment.Val.Value };
|
||||
}
|
||||
|
||||
return fontProps;
|
||||
}
|
||||
|
||||
private static string GetRunText(Run run)
|
||||
{
|
||||
if (run is null) return string.Empty;
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (var text in run.Elements<Text>())
|
||||
{
|
||||
sb.Append(text.Text ?? string.Empty);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user