Implement text styles.

- NEU: Überschriften und pathologische Werte werden jetzt besonders ausgezeichnet (fett).
This commit is contained in:
Daniel Kraus 2015-08-15 20:14:24 +02:00
parent 4cb2999297
commit 92a8faa972
8 changed files with 113 additions and 29 deletions

View File

@ -186,6 +186,6 @@ namespace Tests.Controller.Elements
return _markupStripper.Replace(s, string.Empty);
}
static readonly Regex _markupStripper = new Regex(@"</?b>");
static readonly Regex _markupStripper = new Regex(@"<[^>]+>");
}
}

View File

@ -31,8 +31,19 @@ namespace zaaReloaded2.Formatter
/// depending on whether there is text in the buffer or not.
/// </summary>
/// <remarks>
/// <para>
/// Linking several DocumentWriters permits a cascading work flow
/// with several buffers.
/// </para>
/// <para>
/// Markup support: The DocumentWriter supports basic markup to control
/// the text styles in the Word document.
/// </para>
/// <list type="unordered">
/// <item>&lt;b&gt; and &lt;/b&gt; - bold/unbold</item>
/// <item>&lt;style:NAME&gt; - set the paragraph or character style</item>
/// <item>&lt;/style&gt; - remove *character* style</item>
/// </list>
/// </remarks>
class DocumentWriter
{
@ -54,12 +65,6 @@ namespace zaaReloaded2.Formatter
/// </summary>
public bool HasBufferedText { get { return _buffer.Length > 0; } }
/// <summary>
/// Gets or sets the desired paragraph style when flushing into
/// a Document.
/// </summary>
public string ParagraphStyle { get; set; }
/// <summary>
/// Returns text without markup from the buffer.
/// </summary>
@ -137,10 +142,6 @@ namespace zaaReloaded2.Formatter
Selection s = Document.ActiveWindow.Selection;
s.ClearCharacterDirectFormatting();
s.ClearParagraphDirectFormatting();
if (!string.IsNullOrEmpty(ParagraphStyle))
{
s.set_Style(ParagraphStyle);
}
MarkupToDocument(_buffer.ToString());
}
if (Parent != null)
@ -187,22 +188,33 @@ namespace zaaReloaded2.Formatter
/// Parses a string containing markup (e.g., "&lt;b&gt;", "&lt;/b&gt;")
/// and writes formatted text to the current Document.
/// </summary>
/// <param name="text"></param>
void MarkupToDocument(string text)
{
string[] substrings = _markupRegex.Split(text);
Selection sel = Document.ActiveWindow.Selection;
foreach (string substring in substrings)
{
switch (substring)
{
case "<b>":
Document.ActiveWindow.Selection.Font.Bold = 1;
sel.Font.Bold = 1;
break;
case "</b>":
Document.ActiveWindow.Selection.Font.Bold = 0;
sel.Font.Bold = 0;
break;
case "</style>":
sel.ClearCharacterStyle();
break;
default:
Document.ActiveWindow.Selection.TypeText(substring);
Match styleMatch = _styleRegex.Match(substring);
if (styleMatch.Success)
{
sel.set_Style(styleMatch.Groups["style"].Value);
}
else
{
sel.TypeText(substring);
}
break;
}
}
@ -214,7 +226,10 @@ namespace zaaReloaded2.Formatter
StringBuilder _buffer;
// Put pattern in parentheses so they will not be discarded by Regex.Split
static readonly Regex _markupRegex = new Regex(@"(</?b>)");
// The splitting pattern must not contain subgroups!
static readonly Regex _markupRegex = new Regex(@"(<[^>]+>)");
static readonly Regex _styleRegex = new Regex(@"<style:(?<style>[^>]+)>");
#endregion
}

View File

@ -121,8 +121,7 @@ namespace zaaReloaded2.Formatter
{
if (!CanRun) throw new NoLaboratoryDataException("No laboratory data to format.");
CreateParagraphStyle();
_secondaryBuffer.ParagraphStyle = zaaReloaded2.Properties.Settings.Default.ParagraphStyleName;
CreateStyles();
int current = 0;
while (current < Settings.Elements.Count)
{
@ -242,9 +241,9 @@ namespace zaaReloaded2.Formatter
}
/// <summary>
/// Creates a zaaReloaded2 paragraph style in the document.
/// Creates a paragraph and character styles in the document.
/// </summary>
public void CreateParagraphStyle()
public void CreateStyles()
{
if (Document != null)
{
@ -253,19 +252,55 @@ namespace zaaReloaded2.Formatter
// paragraph style than by using a try...catch construction.
try
{
style = Document.Styles[Properties.Settings.Default.ParagraphStyleName];
style = Document.Styles[Properties.Settings.Default.StyleParagraph];
}
catch
{
style = Document.Styles.Add(Properties.Settings.Default.ParagraphStyleName);
// Add default paragraph style for laboratory
style = Document.Styles.Add(Properties.Settings.Default.StyleParagraph);
style.Font.Size = 10; // pt
style.Font.Bold = 0;
style.Font.Italic = 0;
style.Font.Underline = 0;
style.ParagraphFormat.SpaceAfter = 0;
style.ParagraphFormat.SpaceBefore = 0;
style.ParagraphFormat.LeftIndent = 36; // pt
style.ParagraphFormat.FirstLineIndent = -36; // pt
style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
}
try
{
style = Document.Styles[Properties.Settings.Default.StyleHeader];
}
catch
{
// Add header paragraph style for laboratory
style = Document.Styles.Add(Properties.Settings.Default.StyleHeader);
style.Font.Size = 10; // pt
style.Font.Bold = 1;
style.Font.Italic = 0;
style.Font.Underline = WdUnderline.wdUnderlineSingle;
style.ParagraphFormat.SpaceAfter = 0;
style.ParagraphFormat.SpaceBefore = 12;
style.ParagraphFormat.LeftIndent = 36; // pt
style.ParagraphFormat.FirstLineIndent = -36; // pt
style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
style.set_NextParagraphStyle(Document.Styles[Properties.Settings.Default.StyleParagraph]);
}
try
{
style = Document.Styles[Properties.Settings.Default.StyleAbnormal];
}
catch
{
// Add character style for abnormal parameters
style = Document.Styles.Add(
Properties.Settings.Default.StyleAbnormal,
WdStyleType.wdStyleTypeCharacter);
style.Font.Bold = 1;
}
}
}

View File

@ -163,7 +163,9 @@ namespace zaaReloaded2.Formatter
);
if (!LabItem.IsNormal)
{
output = String.Format("<b>{0}</b>", output);
output = String.Format(
"<b>{0}</b>",
output);
}
formatter.Write(output);

View File

@ -68,10 +68,12 @@ namespace zaaReloaded2.Formatter
static string FormatHeader(string text)
{
return String.Format("{0}<b>Laborwerte vom {1}:</b>{2}",
return String.Format("{0}<style:{1}>Laborwerte vom {2}:{3}<style:{4}>",
Environment.NewLine,
Properties.Settings.Default.StyleHeader,
text,
Environment.NewLine
Environment.NewLine,
Properties.Settings.Default.StyleParagraph
);
}

View File

@ -250,9 +250,9 @@ namespace zaaReloaded2.Properties {
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("zaaReloaded2-Laborwerte")]
public string ParagraphStyleName {
public string StyleParagraph {
get {
return ((string)(this["ParagraphStyleName"]));
return ((string)(this["StyleParagraph"]));
}
}
@ -303,5 +303,23 @@ namespace zaaReloaded2.Properties {
this["ImportExportPath"] = value;
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("zaaReloaded2-Überschrift")]
public string StyleHeader {
get {
return ((string)(this["StyleHeader"]));
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("zaaReloaded2-Pathologisch")]
public string StyleAbnormal {
get {
return ((string)(this["StyleAbnormal"]));
}
}
}
}

View File

@ -74,7 +74,7 @@
<Setting Name="LastSettings" Type="System.Guid" Scope="User">
<Value Profile="(Default)">00000000-0000-0000-0000-000000000000</Value>
</Setting>
<Setting Name="ParagraphStyleName" Type="System.String" Scope="Application">
<Setting Name="StyleParagraph" Type="System.String" Scope="Application">
<Value Profile="(Default)">zaaReloaded2-Laborwerte</Value>
</Setting>
<Setting Name="ReferenceStyle" Type="zaaReloaded2.Formatter.ReferenceStyle" Scope="Application">
@ -92,5 +92,11 @@
<Setting Name="ImportExportPath" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="StyleHeader" Type="System.String" Scope="Application">
<Value Profile="(Default)">zaaReloaded2-Überschrift</Value>
</Setting>
<Setting Name="StyleAbnormal" Type="System.String" Scope="Application">
<Value Profile="(Default)">zaaReloaded2-Pathologisch</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -92,7 +92,7 @@
<setting name="DefaultItemsDrugs" serializeAs="String">
<value>Medikamente: TAC, CSA, SIR, Vancomycin, Gentamicin, Tobramicin</value>
</setting>
<setting name="ParagraphStyleName" serializeAs="String">
<setting name="StyleParagraph" serializeAs="String">
<value>zaaReloaded2-Laborwerte</value>
</setting>
<setting name="ReferenceStyle" serializeAs="String">
@ -107,6 +107,12 @@
<setting name="SerializationVersion" serializeAs="String">
<value>1</value>
</setting>
<setting name="StyleHeader" serializeAs="String">
<value>zaaReloaded2-Überschrift</value>
</setting>
<setting name="StyleAbnormal" serializeAs="String">
<value>zaaReloaded2-Pathologisch</value>
</setting>
</zaaReloaded2.Properties.Settings>
</applicationSettings>
<userSettings>