Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit 61bfe10

Browse files
committed
refactor: add style factory to generate style
with tests~
1 parent a91e4af commit 61bfe10

File tree

5 files changed

+376
-29
lines changed

5 files changed

+376
-29
lines changed

md2docx.sln

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.28307.757
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "md2docx", "md2docx\md2docx.csproj", "{57F2D503-1BD2-452E-8B5B-D513E92F204E}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "md2docxTests", "md2docxTests\md2docxTests.csproj", "{F8387CFA-7F83-4688-B121-C5768456B48B}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "md2docxTests", "md2docxTests\md2docxTests.csproj", "{5389AD7C-77B7-46FE-8876-6546F2AC1E58}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -17,10 +17,10 @@ Global
1717
{57F2D503-1BD2-452E-8B5B-D513E92F204E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1818
{57F2D503-1BD2-452E-8B5B-D513E92F204E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1919
{57F2D503-1BD2-452E-8B5B-D513E92F204E}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{F8387CFA-7F83-4688-B121-C5768456B48B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{F8387CFA-7F83-4688-B121-C5768456B48B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{F8387CFA-7F83-4688-B121-C5768456B48B}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{F8387CFA-7F83-4688-B121-C5768456B48B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{5389AD7C-77B7-46FE-8876-6546F2AC1E58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{5389AD7C-77B7-46FE-8876-6546F2AC1E58}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{5389AD7C-77B7-46FE-8876-6546F2AC1E58}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{5389AD7C-77B7-46FE-8876-6546F2AC1E58}.Release|Any CPU.Build.0 = Release|Any CPU
2424
EndGlobalSection
2525
GlobalSection(SolutionProperties) = preSolution
2626
HideSolutionNode = FALSE

md2docx/StyleFactory.cs

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,111 @@ public class StyleFactory
1212
{
1313
public Style GenerateStyle(JObject jObject)
1414
{
15-
return new Style
15+
string size;
16+
if (int.TryParse((string)jObject["字体大小"], out int sz))
17+
{
18+
size = sz.ToString();
19+
}
20+
else
21+
{
22+
size = fontmap[(string)jObject["字体大小"]];
23+
}
24+
Style style = new Style
1625
{
1726
Type = StyleValues.Paragraph,
1827
StyleId = (string)jObject["名称"],
1928
StyleName = new StyleName { Val = (string)jObject["名称"] },
29+
StyleParagraphProperties = new StyleParagraphProperties
30+
{
31+
Justification = new Justification { Val = justmap[(string)jObject["对齐方式"]] }
32+
},
2033
StyleRunProperties = new StyleRunProperties
2134
{
2235
RunFonts = new RunFonts { Ascii = (string)jObject["英文字体"], HighAnsi = (string)jObject["英文字体"], ComplexScript = (string)jObject["英文字体"], EastAsia = (string)jObject["中文字体"] },
23-
FontSize = new FontSize { Val = (string)jObject["字体大小"] },
24-
FontSizeComplexScript = new FontSizeComplexScript { Val = (string)jObject["字体大小"] }
36+
FontSize = new FontSize { Val = size },
37+
FontSizeComplexScript = new FontSizeComplexScript { Val = size }
2538
}
2639
};
40+
if ((bool)jObject["录入大纲"])
41+
{
42+
style.StyleParagraphProperties.OutlineLevel = new OutlineLevel { Val = (int)jObject["大纲等级"] };
43+
}
44+
if ((bool)jObject["加粗"])
45+
{
46+
style.StyleRunProperties.Bold = new Bold();
47+
style.StyleRunProperties.BoldComplexScript = new BoldComplexScript();
48+
}
49+
if ((bool)jObject["斜体"])
50+
{
51+
style.StyleRunProperties.Italic = new Italic();
52+
style.StyleRunProperties.ItalicComplexScript = new ItalicComplexScript();
53+
}
54+
if ((bool)jObject["下划线"])
55+
{
56+
style.StyleRunProperties.Underline = new Underline();
57+
}
58+
if ((bool)jObject["删除线"])
59+
{
60+
style.StyleRunProperties.Strike = new Strike();
61+
}
62+
if ((bool)jObject["段前分页"])
63+
{
64+
style.StyleParagraphProperties.PageBreakBefore = new PageBreakBefore();
65+
}
66+
67+
if ((float)jObject["首行缩进"] != 0f)
68+
{
69+
style.StyleParagraphProperties.Indentation = new Indentation
70+
{
71+
FirstLineChars = (int)((float)jObject["首行缩进"] * 100)
72+
};
73+
}
74+
if ((float)jObject["段前后空行"] != 0f)
75+
{
76+
style.StyleParagraphProperties.SpacingBetweenLines = new SpacingBetweenLines
77+
{
78+
BeforeLines = (int)((float)jObject["段前后空行"] * 100),
79+
AfterLines = (int)((float)jObject["段前后空行"] * 100)
80+
};
81+
}
82+
if ((float)jObject["行距"] != 0f)
83+
{
84+
style.StyleParagraphProperties.SpacingBetweenLines = style.StyleParagraphProperties.SpacingBetweenLines ?? new SpacingBetweenLines();
85+
style.StyleParagraphProperties.SpacingBetweenLines.Line = ((int)((float)jObject["行距"] * 240)).ToString();
86+
style.StyleParagraphProperties.SpacingBetweenLines.LineRule = LineSpacingRuleValues.Auto;
87+
}
88+
89+
return style;
2790
}
91+
#region Chinese font mapping
92+
static readonly Dictionary<string, string> fontmap = new Dictionary<string, string>
93+
{
94+
{"初号", "84"},
95+
{"小初", "72"},
96+
{"一号", "52"},
97+
{"小一", "48"},
98+
{"二号", "44"},
99+
{"小二", "36"},
100+
{"三号", "32"},
101+
{"小三", "30"},
102+
{"四号", "28"},
103+
{"小四", "24"},
104+
{"五号", "21"},
105+
{"小五", "18"},
106+
{"六号", "15"},
107+
{"小六", "13"},
108+
{"七号", "11"},
109+
{"八号", "10"}
110+
};
111+
#endregion
112+
#region justification mapping
113+
static readonly Dictionary<string, JustificationValues> justmap = new Dictionary<string, JustificationValues>
114+
{
115+
{ "左对齐", JustificationValues.Left },
116+
{ "居中", JustificationValues.Center },
117+
{ "右对齐", JustificationValues.Right },
118+
{ "分散对齐", JustificationValues.Distribute }
119+
};
120+
#endregion
28121
}
29122
}

md2docx/md2docx.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,10 +1692,18 @@ private static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart st
16921692
{
16931693
RunFonts = new RunFonts { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "宋体", ComplexScript = "Times New Roman" },
16941694
Kern = new Kern { Val = 2U },
1695-
Languages = new Languages { Val = "en-US", EastAsia = "zh-CN", Bidi = "ar-SA"}
1695+
Languages = new Languages { Val = "en-US", EastAsia = "zh-CN", Bidi = "ar-SA" },
1696+
FontSize = new FontSize { Val = "24" },
1697+
FontSizeComplexScript = new FontSizeComplexScript { Val = "24" }
16961698
}
16971699
},
1698-
ParagraphPropertiesDefault = new ParagraphPropertiesDefault()
1700+
ParagraphPropertiesDefault = new ParagraphPropertiesDefault
1701+
{
1702+
ParagraphPropertiesBaseStyle = new ParagraphPropertiesBaseStyle
1703+
{
1704+
Indentation = new Indentation() { FirstLine = "200", FirstLineChars = 200 }
1705+
}
1706+
}
16991707
};
17001708

17011709
Style style1 = new Style

0 commit comments

Comments
 (0)