1 /* 2 * "GEDKeeper", the personal genealogical database editor. 3 * Copyright (C) 2009-2017 by Sergey V. Zhdanovskih. 4 * 5 * This file is part of "GEDKeeper". 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 using System.Collections.Generic; 22 using System.IO; 23 using System.Text; 24 using BSLib; 25 using BSLib.Design.Graphics; 26 27 namespace GKCore.Export 28 { 29 /// <summary> 30 /// 31 /// </summary> 32 public class HTMLWriter : CustomWriter 33 { 34 private sealed class FontHandler: TypeHandler<string>, IFont 35 { 36 public string FontFamilyName 37 { 38 get { return string.Empty; } // dummy 39 } 40 41 public string Name 42 { 43 get { return string.Empty; } // dummy 44 } 45 46 public float Size 47 { 48 get { return 0; } // dummy 49 } 50 FontHandler(string handle)51 public FontHandler(string handle) : base(handle) 52 { 53 } 54 } 55 56 private StreamWriter fStream; 57 private readonly Dictionary<string, string> fStyles; 58 private int fTableCol, fTableColsCount; 59 private int fTableRow, fTableRowsCount; 60 HTMLWriter()61 public HTMLWriter() 62 { 63 fStyles = new Dictionary<string, string>(); 64 } 65 BeginWrite()66 public override void BeginWrite() 67 { 68 fStream = new StreamWriter(new FileStream(fFileName, FileMode.Create, FileAccess.Write), Encoding.UTF8); 69 70 fStream.WriteLine("<html>"); 71 fStream.WriteLine("<head>"); 72 fStream.WriteLine("<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\">"); 73 fStream.WriteLine("<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\"/>"); 74 fStream.WriteLine("<title>" + fDocumentTitle + "</title>"); 75 76 fStream.WriteLine("<style type=\"text/css\">"); 77 foreach (KeyValuePair<string, string> entry in fStyles) 78 { 79 fStream.WriteLine("." + entry.Key + " { " + entry.Value + " }"); 80 } 81 fStream.WriteLine("</style>"); 82 83 fStream.WriteLine("</head>"); 84 fStream.WriteLine("<body>"); 85 } 86 EndWrite()87 public override void EndWrite() 88 { 89 fStream.WriteLine("</body>"); 90 fStream.WriteLine("</html>"); 91 92 fStream.Flush(); 93 fStream.Close(); 94 } 95 EnablePageNumbers()96 public override void EnablePageNumbers() 97 { 98 } 99 NewPage()100 public override void NewPage() 101 { 102 } 103 NewLine(float spacingBefore = 0.0f, float spacingAfter = 0.0f)104 public override void NewLine(float spacingBefore = 0.0f, float spacingAfter = 0.0f) 105 { 106 } 107 AddParagraph(string text, IFont font, TextAlignment alignment)108 public override void AddParagraph(string text, IFont font, TextAlignment alignment) 109 { 110 fStream.WriteLine("<p class=\""+((FontHandler)font).Handle+"\">"+text+"</p>"); 111 } 112 AddParagraph(string text, IFont font)113 public override void AddParagraph(string text, IFont font) 114 { 115 fStream.WriteLine("<p class=\""+((FontHandler)font).Handle+"\">"+text+"</p>"); 116 } 117 AddParagraphAnchor(string text, IFont font, string anchor)118 public override void AddParagraphAnchor(string text, IFont font, string anchor) 119 { 120 fStream.WriteLine("<p class=\""+((FontHandler)font).Handle+"\"><a name=\""+anchor+"\">"+text+"</a></p>"); 121 } 122 AddParagraphLink(string text, IFont font, string link)123 public override void AddParagraphLink(string text, IFont font, string link) 124 { 125 fStream.WriteLine("<p class=\""+((FontHandler)font).Handle+"\"><a href=\"#"+link+"\">"+text+"</a></p>"); 126 } 127 AddParagraphLink(string text, IFont font, string link, IFont linkFont)128 public override void AddParagraphLink(string text, IFont font, string link, IFont linkFont) 129 { 130 fStream.WriteLine("<p class=\""+((FontHandler)font).Handle+"\"><a href=\"#"+link+"\">"+text+"</a></p>"); 131 } 132 CreateFont(string name, float size, bool bold, bool underline, IColor color)133 public override IFont CreateFont(string name, float size, bool bold, bool underline, IColor color) 134 { 135 string style; 136 137 style = "font-family: " + name; 138 style += "; font-size: " + size + "pt"; 139 style += "; color: \"#" + color.GetCode() + "\""; 140 if (bold) style += "; font-weight: bold"; 141 if (underline) style += "; text-decoration: underline"; 142 143 int index = fStyles.Count; 144 string key = "style_" + index; 145 fStyles.Add(key, style); 146 147 return new FontHandler(key); 148 } 149 BeginMulticolumns(int columnCount, float columnSpacing)150 public override void BeginMulticolumns(int columnCount, float columnSpacing) 151 { 152 } 153 EndMulticolumns()154 public override void EndMulticolumns() 155 { 156 } 157 BeginList()158 public override void BeginList() 159 { 160 fStream.WriteLine("<ul>"); 161 } 162 EndList()163 public override void EndList() 164 { 165 fStream.WriteLine("</ul>"); 166 } 167 AddListItem(string text, IFont font)168 public override void AddListItem(string text, IFont font) 169 { 170 fStream.WriteLine("<li class=\""+((FontHandler)font).Handle+"\">"+text+"</li>"); 171 } 172 AddListItemLink(string text, IFont font, string link, IFont linkFont)173 public override void AddListItemLink(string text, IFont font, string link, IFont linkFont) 174 { 175 string alink = ""; 176 if (!string.IsNullOrEmpty(link)) { 177 alink = "<a href=\"#" + link + "\">" + link + "</a>"; 178 } 179 180 fStream.WriteLine("<li class=\""+((FontHandler)font).Handle+"\">" + text + alink + "</li>"); 181 } 182 BeginParagraph(TextAlignment alignment, float spacingBefore, float spacingAfter, float indent = 0.0f, bool keepTogether = false)183 public override void BeginParagraph(TextAlignment alignment, 184 float spacingBefore, float spacingAfter, 185 float indent = 0.0f, bool keepTogether = false) 186 { 187 fStream.WriteLine("<p>"); 188 } 189 EndParagraph()190 public override void EndParagraph() 191 { 192 fStream.WriteLine("</p>"); 193 } 194 AddParagraphChunk(string text, IFont font)195 public override void AddParagraphChunk(string text, IFont font) 196 { 197 fStream.WriteLine(text); 198 } 199 AddParagraphChunkAnchor(string text, IFont font, string anchor)200 public override void AddParagraphChunkAnchor(string text, IFont font, string anchor) 201 { 202 fStream.WriteLine("<a name=\""+anchor+"\">"+text+"</a>"); 203 } 204 AddParagraphChunkLink(string text, IFont font, string link, bool sup = false)205 public override void AddParagraphChunkLink(string text, IFont font, string link, bool sup = false) 206 { 207 if (sup) fStream.WriteLine("<sup>"); 208 fStream.WriteLine("<a href=\"#"+link+"\">"+text+"</a>"); 209 if (sup) fStream.WriteLine("</sup>"); 210 } 211 AddNote(string text, IFont font)212 public override void AddNote(string text, IFont font) 213 { 214 } 215 AddImage(IImage image)216 public override void AddImage(IImage image) 217 { 218 } 219 BeginTable(int columnsCount, int rowsCount)220 public override void BeginTable(int columnsCount, int rowsCount) 221 { 222 fStream.WriteLine("<table>"); 223 fTableRowsCount = rowsCount; 224 fTableRow = 0; 225 fTableColsCount = columnsCount; 226 fTableCol = 0; 227 } 228 EndTable()229 public override void EndTable() 230 { 231 fStream.WriteLine("</table>"); 232 } 233 BeginTableRow(bool header = false)234 public override void BeginTableRow(bool header = false) 235 { 236 fStream.WriteLine("<tr>"); 237 } 238 EndTableRow()239 public override void EndTableRow() 240 { 241 fStream.WriteLine("</tr>"); 242 } 243 AddTableCell(string content, IFont font, TextAlignment alignment)244 public override void AddTableCell(string content, IFont font, TextAlignment alignment) 245 { 246 fStream.WriteLine("<td class=\""+((FontHandler)font).Handle+"\">" + content + "</td>"); 247 248 fTableCol += 1; 249 if (fTableCol == fTableColsCount) { 250 fTableRow += 1; 251 fTableCol = 0; 252 253 if (fTableRow < fTableRowsCount) { 254 } 255 } 256 } 257 } 258 } 259