diff --git a/PROMS/Volian.Svg.Library/DisplayText.cs b/PROMS/Volian.Svg.Library/DisplayText.cs new file mode 100644 index 00000000..84fde2d0 --- /dev/null +++ b/PROMS/Volian.Svg.Library/DisplayText.cs @@ -0,0 +1,671 @@ +using System; +using System.Collections.Generic; +using System.Text; +//using System.Windows.Forms; +using System.Text.RegularExpressions; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public class DisplayText + { + #region Properties + // list of 'pieces of text' for this item. Pieces include symbols, ros, + // transitions & plain text. + private List _DisplayTextElementList; + public List DisplayTextElementList + { + get { return _DisplayTextElementList; } + set { _DisplayTextElementList = value; } + } + // dictionary for the font table for this item. Note that this may + // go away (it is not really used). + private Dictionary _dicRtfFontTable; + public Dictionary dicRtfFontTable + { + get { return _dicRtfFontTable; } + set { _dicRtfFontTable = value; } + } + public string OriginalText; // compare for save to see if change. + #endregion + #region Constructors + private Font TextFont; + + /// + /// DisplayText constructor: + /// Creates a DisplayText object that converts the database text into a list of + /// displayTextElement elements. + /// Arguments are: + /// ItemInfo itemInfo - the item whose text will be resolved + /// E_EditPrintMode ep_mode - edit or print. + /// E_ViewMode vw_mode - view or edit. + /// + public DisplayText(string text) + { + DisplayTextElementList = new List(); + OriginalText = text; + TextFont = new Font("Prestige Elite Tall",12); + //TextFont = GetItemFont(); + + // if in print mode or view mode, do replace words. Only if in edit mode are replace + // words left as is. + //_MyFormat = itemInfo.ActiveFormat; + //if (epMode == E_EditPrintMode.Print || vwMode == E_ViewMode.View) text = DoReplaceWords(text); + + // as a precaution, convert any \~ to \u160?. This is for Hard spaces.. see the commentary in the + // save portion of this code for an explanation. + text = text.Replace(@"\~", @"\u160?"); + + // replace the dash/hyphen or whatever you want to call it, with a hard hyphen. The 16-bit program + // treated the dash/hyphen as such. Translate back on any data saves. + text = text.Replace(@"-", @"\u8209?"); + + // displayTextElement List items are created for anything that is handled differently in RTB, i.e. + // symbols, ros, trans, text. + int startIndex = 0; + int index = -1; + while ((index = FindTokenChar(text, startIndex)) > -1) + { + // Do any 'plain' text that preceeds the token. + if (index > startIndex) DoTextElement(text, startIndex, index); + + // Now do any other types + if (text[index] == '\x15') + index = DoRO(text, index); + else if (text[index] == '\x252C' || text[index] == '\x2566') + index = DoTran(text, index); + else + index = DoSymbol(text, startIndex, index); + startIndex = index; // +1; + if (startIndex >= text.Length) break; + } + // Add any remaining text. + if (startIndex < text.Length) DoTextElement(text, startIndex, index); + } + #endregion + #region SaveData + //public bool Save(string rtf) + //{ + // try + // { + // List origList = GetLinkList(DisplayTextElementList); + // // massage string to store in DisplayTextElementList... + // RtfToDisplayTextElements(rtf); + // // take the list & convert to data in the format to save to the database. + // StringBuilder sret = new StringBuilder(); + // foreach (displayTextElement vte in DisplayTextElementList) + // { + // if (vte.Type == E_TextElementType.Text || vte.Type == E_TextElementType.Symbol) + // sret.Append(vte.Text); + // else if (vte.Type == E_TextElementType.ReferencedObject) + // sret.Append(ToData_RO((displayLinkElement)vte)); + // else if (vte.Type == E_TextElementType.Transition || vte.Type == E_TextElementType.TransitionRange) + // sret.Append(ToData_Trans((displayLinkElement)vte)); + // } + // string modtext = sret.ToString(); + // if (modtext != OriginalText) + // { + // Item itm = _MyItemInfo.Get(); + // // check for different text, i.e. text from this itm doesn't match + // // original text. + // if (OriginalText != itm.MyContent.Text) + // { + // Console.WriteLine("Save Failed because text changed outside of this edit session."); + // return false; + // } + // // Compare ro/transition lists and delete or add any to the item for any ros/transitions that have been + // // added/deleted or modified. + // ProcessRoTranChanges(itm, origList); + // itm.MyContent.Text = modtext; + // itm.Save(); + // OriginalText = modtext; + // } + // else + // return true; // no text changed, but did not fail so return true. + // } + // catch (Exception ex) + // { + // Console.WriteLine("Save Failed with error: {0}", ex.Message); + // return false; + // } + // return true; + //} + private List GetLinkList(List locDisplayTextElementList) + { + List retList = new List(); + foreach (displayTextElement vte in locDisplayTextElementList) + { + if (vte.Type == E_TextElementType.ReferencedObject || vte.Type == E_TextElementType.TransitionRange || vte.Type == E_TextElementType.Transition) + { + displayLinkElement tmp = (displayLinkElement)vte; + displayLinkElement copy_vte = new displayLinkElement(); + copy_vte.Type = tmp.Type; + copy_vte.Link = tmp.Link; + copy_vte.Text = tmp.Text; + retList.Add(copy_vte); + } + } + return retList; + } + private void RtfToDisplayTextElements(string rtf) + { + // For hardspaces, the windows richtextbox does some 'quirky' things: + // A unicode representation of \u160? is sent INTO the rtb. Coming out, + // that \u160? was translated to a \~ (by the underlying windows rtb). + // Note that if the \~ is sent to the rtb, it is treated as a regular space, + // i.e. no longer a hardspace, and actually is converted to a regular space. + // SO, on the way out, convert any \~ to \u160? + string noExtraRtfStr = rtf.Replace(@"\~", @"\u160?"); + + + // GetFontTable returns a non-negative number font number in the + // font table for the unicode font, if it is used (otherwise -1) + //int unicodeFont = GetFontTable(rtb.Rtf); + + // strip off all rtf commands... + noExtraRtfStr = StripRtfCommands(noExtraRtfStr); + //Console.WriteLine("StripRtf: {0}", noExtraRtfStr); + + // Also, set back the hard dash to a regular dash... Do this after the removal + // of other rtf commands though since the \u8209 was surrounded by font commands + // that, without there removal first, was also removing the dash. Have it with + // a space & without because if it is at the end, there will be no space (leave + // it to rtf processing to make it more complicated) + noExtraRtfStr = noExtraRtfStr.Replace(@"\u8209? ", @"-"); + noExtraRtfStr = noExtraRtfStr.Replace(@"\u8209?", @"-"); + + DisplayTextElementList.Clear(); + int startIndex = 0; + int index = -1; + while ((index = FindRtfChar(noExtraRtfStr, startIndex)) > -1) + { + int fndindx = -1; + // Do any 'plain' text that preceeds the token. + if (index > startIndex) + index = SaveTextElement(noExtraRtfStr, startIndex, index); + if ((fndindx = noExtraRtfStr.IndexOf(@"\protect", index)) == index) + index = SaveLink(noExtraRtfStr, index); + else + index = SaveSymbolTE(noExtraRtfStr, index); + startIndex = index + 1; + if (startIndex >= noExtraRtfStr.Length) break; + } + // Add any remaining text. + if (startIndex < noExtraRtfStr.Length) DoTextElement(noExtraRtfStr, startIndex, index); + //Console.WriteLine(noExtraRtfStr); + } + private int SaveTextElement(string data, int startIndex, int index) + { + displayTextElement vte = new displayTextElement(); + vte.Type = E_TextElementType.Text; + int len = (index == -1) ? data.Length - startIndex : index - startIndex; + vte.Text = data.Substring(startIndex, len); + DisplayTextElementList.Add(vte); + return index; + } + private int SaveSymbolTE(string data, int startIndex) + { + displayLinkElement vte = new displayLinkElement(); + vte.Type = E_TextElementType.Symbol; + // symbols are just the unicode/rtf command, no font associated with it + // by the time it gets here... A symbol can be represented by \'xy or \uxyz? + // if the \'xy is used the length of the symbol number will always be two, + // otherwise find the index of the '?' to find the end. + int endindx = -1; + if (data[startIndex + 1] == '\'') endindx = startIndex + 3; + else endindx = data.IndexOf("?", startIndex); + if (endindx == -1) return startIndex; // not found - error + vte.Text = data.Substring(startIndex, endindx - startIndex + 1); + DisplayTextElementList.Add(vte); + if (endindx + 1 < data.Length && data[endindx + 1] != ' ') return endindx; + return endindx + 1; // add one to get past the space that was added after a symbol (to end the font cmd) + } + private int SaveLink(string data, int stIndex) + { + ////displayLinkElement vte = new displayLinkElement(); + //// find the end of the link text, i.e. either the ending \v0 or \protect0. The win32 rtf box can + //// put these in either order, take the lowest index. + //int startIndex = stIndex + @"\protect ".Length; + //int indx_v0 = data.IndexOf(@"\v0", startIndex); + //int indx_protect0 = data.IndexOf(@"\protect0", startIndex); + //int indx = indx_v0 < indx_protect0 ? indx_v0 : indx_protect0; + //LinkText lt = new LinkText(data.Substring(startIndex, indx - startIndex)); + //displayLinkElement vte = new displayLinkElement(); + //vte.Type = (E_TextElementType)lt.MyParsedLinkType; + //vte.Text = lt.MyValue; + //vte.Link = lt.MyLink; + //DisplayTextElementList.Add(vte); + //return (indx_v0 < indx_protect0 ? indx_protect0 + 1 : indx_v0 + 1); + ////if (lt.MyParsedLinkType == ParsedLinkType.ReferencedObject) return + ////// first find if ReferencedObject or trans, the ReferencedObject has a #R. + ////int istart = data.IndexOf("#Link:", startIndex); + ////if (data[istart + 6] == 'R') return SaveROTE(data, startIndex); + ////else if (data.Substring(istart+6,11) == "TransitionR") + //// return SaveTranTE(data, startIndex, E_TextElementType.TRANS_Range); + ////return SaveTranTE(data, startIndex, E_TextElementType.TRANS_Single); + return 0; + } + //private int SaveTranTE(string data, int startIndex, E_TextElementType type) + //{ + // displayLinkElement vte = new displayLinkElement(); + + // vte.Type = type; + // // \protect {transition text} \v #Link:Transition(Range): 1 2 3\protect0\v0 + // // where 1 2 3 are transition type and ids + // int indx = data.IndexOf("\\v #Link:Transition", startIndex); + // // use '9' to get past \\protect + // vte.Text = data.Substring(startIndex + 9, indx - startIndex - 9); + // // the link is the text between the #Link... and the next '\\'. + // int iend = data.IndexOf("\\", indx+1); + // int bindx = data.IndexOf("#", indx); + // vte.Link = data.Substring(bindx, iend - bindx); + // DisplayTextElementList.Add(vte); + // // unfortunately, the rtf box may add the \protect0 \v0 in either order + // // and with or without one or more spaces between then. The number of text + // // characters = 11 for \protect0\v0 - find the number of spaces between them + // // if any? + // int nsp = 0; + // for (int c = iend; c < iend + 11; c++) if (data[c] == ' ') nsp++; + // return iend + 11 + nsp; + //} + //private int SaveROTE(string data, int startIndex) + //{ + // displayLinkElement vte = new displayLinkElement(); + // vte.Type = E_TextElementType.ReferencedObject; + // // \protect {rovalue} \v #Link:ReferencedObject (RoUsageId) {ROID}\protect0\v0 + // int indx = data.IndexOf("\\v #Link:ReferencedObject", startIndex); + // // use '9' to get past \\protect + // vte.Text = data.Substring(startIndex + 9, indx - startIndex - 9); + // // the link is the text between the #Link... and the next '\\'. + // int iend = data.IndexOf("\\", indx); + // int bindx = data.IndexOf("#", indx); + // vte.Link = data.Substring(bindx, iend - bindx); + // DisplayTextElementList.Add(vte); + // // unfortunately, the rtf box may add the \protect0 \v0 in either order + // // and with or without one or more spaces between then. The number of text + // // characters = 11 for \protect0\v0 - find the number of spaces between them + // // if any? + // int nsp = 0; + // for (int c = iend; c < iend + 11; c++) if (data[c] == ' ') nsp++; + // return iend + 11 + nsp; + //} + private int FindRtfChar(string text, int startIndex) + { + int prindx = text.IndexOf("\\protect", startIndex); + int symindx1 = text.IndexOf("\\'", startIndex); + int symindx2 = text.IndexOf("\\u", startIndex); + if (symindx2 > -1) + { + if (text[symindx2 + 2] == 'l') symindx2 = -1; // don't process underlines + } + if (prindx == -1 && symindx1 == -1 && symindx2 == -1) return -1; + if (prindx == -1) prindx = text.Length + 1; + if (symindx1 == -1) symindx1 = text.Length + 1; + if (symindx2 == -1) symindx2 = text.Length + 1; + + // Which token has smallest number, to determine which item is next + // in the string. If it is a symbol - see if it has a font specifier + // first. + int symindx = symindx1 < symindx2 ? symindx1 : symindx2; + int smallest = (prindx < symindx ? prindx : symindx); + return smallest; + } + private int GetFontTable(string rtf) + { + dicRtfFontTable = new Dictionary(); + // return unicode (symbol) font number, if it exists, to expedite finding + // the font for symbols. + int unicodeFont = -1; + int bindx = rtf.IndexOf(@"{\fonttbl"); + if (bindx < -1) return -1; + int eindx = rtf.IndexOf("}}", bindx); + // get font table string and then do regular expressions to get font number + // with font name. + string tbl = rtf.Substring(bindx + 9, eindx - bindx - 8); + tbl = tbl.Replace("{", "<"); + tbl = tbl.Replace("}", ">"); + string pat = @"(?:<\\f)([0-9]+)(?:[\S]+ )([\w ]+)"; + StringBuilder sb = new StringBuilder(); + foreach (Match m in Regex.Matches(tbl, pat)) + { + int num = Convert.ToInt32(m.Result("${1}")); + string nam = m.Result("${2}"); + dicRtfFontTable.Add(num, nam); + if ((unicodeFont == -1) && (nam == "Arial Unicode MS")) unicodeFont = num; + } + return unicodeFont; + } + private string RemoveRtfStyles(string rtf) + { + string retval = rtf; + // remove rtf commands for any styles that were added. Note that if + // the entire item has a style, and also contains 'pieces' of text with + // the same style, the underlying rtf box removes the embedded rtf commands, + // for example, if the entire step is bolded, and 'THEN' has bold on/off + // surrounding it, the rtf box removes the bold around the 'THEN' + // These remove the command with a following space or the command alone, + // either case may exist, because if there are rtf commands following the + // style command, there will be no space character following the style command. + if ((TextFont.Style & FontStyle.Bold) > 0) + { + retval = Regex.Replace(retval, @"\\ulnone\\b0 ?", "\\ulnone"); + retval = Regex.Replace(retval, @"\\b0 ?", ""); + retval = Regex.Replace(retval, @"\\ul\\b ?", "\\ul"); + retval = Regex.Replace(retval, @"\\b ?", ""); + } + if ((TextFont.Style & FontStyle.Underline) > 0) + { + retval = Regex.Replace(retval, @"\\ul0 ?", ""); + retval = Regex.Replace(retval, @"\\ul ?", ""); + } + if ((TextFont.Style & FontStyle.Italic) > 0) + { + retval = Regex.Replace(retval, @"\\i0 ?", ""); + retval = Regex.Replace(retval, @"\\i ?", ""); + } + return retval; + } + public static string ReplaceRTFClause(Match m) + { + switch (m.Value[1]) + { + case 'u': + if (Regex.IsMatch(m.Value, @"\\u[0-9]+")) + return m.Value; // Special Charcaters + if (Regex.IsMatch(m.Value, @"\\ulnone")) + return m.Value; + if (Regex.IsMatch(m.Value, @"\\ul.*")) + return m.Value; // Underline + break; + case '\'': // Special Character + return m.Value; + case 'b': // Bold + return m.Value; + case 's': // sub or super.... + if (m.Value == @"\sub") return m.Value; + if (m.Value == @"\super") return m.Value; + break; + case 'n': // nosubsuper... + if (m.Value == @"\nosupersub") return m.Value; + break; + case 'i': // Italics + return m.Value; + case 'v': // save link hidden info + if (m.Value == @"\v") return m.Value; // part of link + if (Regex.IsMatch(m.Value, @"\\v0")) + return m.Value; // hidden info off + break; + case 'p': + if (m.Value == @"\par") return "\r\n"; + if (m.Value == @"\protect") + return m.Value; + if (m.Value == @"\protect0") + return m.Value; + break; + } + return "";//Strip All + } + public static string StripRtfCommands(string rtf) + { + string retval = Regex.Replace(rtf, @"[\r\n]", "", RegexOptions.Singleline); // Strip Carriage Returns and Newlines + retval = Regex.Replace(retval, @"^\{(.*)\}$", "$1", RegexOptions.Singleline); // Strip Opening and Closing Braces + retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces + retval = Regex.Replace(retval, @"\{[^{]*?\}", "", RegexOptions.Singleline); // Strip Clauses - remove anything from curly braces + retval = Regex.Replace(retval, @"\\[^ \\?]+", new MatchEvaluator(ReplaceRTFClause)); // take backslash xyz and evaluates them + // remove a space if there is one as the first character.. + if (retval[0] == ' ') retval = retval.Remove(0, 1); + // remove \r\n at end of string - this was added with the \par at the end of string by the rtf box + if (retval.Substring(retval.Length - 2, 2) == "\r\n") retval = retval.Remove(retval.Length - 2, 2); + // TODO: retval = RemoveRtfStyles(retval); + + return retval; + } + private string ToData_RO(displayLinkElement vte) + { + // get past the #Link:ReferencedObject part of the link. + return String.Format("\x15ReferencedObjectv RO\\v0 {0}\\v #{1}\\v0", vte.Text, vte.Link.Substring(23, vte.Link.Length - 23)); + } + private string ToData_Trans(displayLinkElement vte) + { + char trchar = vte.Type == E_TextElementType.Transition ? '\x252C' : '\x2566'; + // depending on type, get past the #Link:Transition: or #Link:TransitionRange: part + // of text. + int indx = vte.Type == E_TextElementType.Transition ? 18 : 23; + return String.Format("{0}\\v TRAN\\v0 {1}\\v {2}\\v0", trchar, vte.Text, vte.Link.Substring(indx, vte.Link.Length - indx)); + } + #endregion + //#region StyleData + //private VE_Font GetItemFont() + //{ + // VE_Font font = null; + // FormatInfo format = _MyItemInfo.ActiveFormat; + // int type = (int)_MyItemInfo.MyContent.Type; + // switch (type / 10000) + // { + // case 0: // procedure + // font = format.PlantFormat.FormatData.Font; + // break; + // case 1: // section + // font = format.PlantFormat.FormatData.SectData.SectionHeader.Font; + // break; + // case 2: // step types + // int typindx = type - 20000; // what to do for other types rather than steps + // font = format.PlantFormat.FormatData.StepDataList[typindx].Font; + // break; + // } + // TextFont = font; + // return font; + //} + //#endregion + #region DoListElements + private int FindTokenChar(string txt, int startIndex) + { + // tokens are ro, transitions and possible symbol + char[] tok = { '\x15', '\x252C', '\x2566', '\\' }; + bool done = false; + // If there is only an rtf token from the indexed position on, don't return the + // IndexOfAny index value, because it will by one character past the '\' and throw + // of the return value of where in the string the text should be saved. For example + // for the string '\b text \v somevalue \v0\b0', the first time through the while + // loop has the index at the '\b' char of the b0. + //int savstartIndex = startIndex; + while (!done) + { + int indx = txt.IndexOfAny(tok, startIndex); + if (indx < 0) return indx; + if (txt[indx] != '\\') return indx; + // see if symbol (but not underline) or another rtf command: has a 'u' + // followed by a non-underline or single quote, and if so, return it. + // Otherwise, get next index, must have been a slash or other rtf command. + if (((txt[indx + 1] == 'u' && txt[indx + 2] != 'l')) || (txt[indx + 1] == '\'')) return indx; + startIndex = indx + 1; + } + return -1; + } + private int DoTextElement(string text, int startIndex, int index) + { + displayTextElement vte = new displayTextElement(); + vte.Type = E_TextElementType.Text; + int len = (index == -1) ? text.Length - startIndex : index - startIndex; + vte.Text = text.Substring(startIndex, len); + DisplayTextElementList.Add(vte); + return index + 1; + } + private string CreateLink(E_TextElementType type, string linktxt) + { + string retlink = ""; + if (type == E_TextElementType.ReferencedObject) + retlink = "#Link:ReferencedObject:" + linktxt; + else if (type == E_TextElementType.Transition) + retlink = "#Link:Transition:" + linktxt; + else + retlink = "#Link:TransitionRange:" + linktxt; + + return retlink; + } + private int DoRO(string text, int index) + { + displayLinkElement vte = new displayLinkElement(); + vte.Type = E_TextElementType.ReferencedObject; + int iend = text.IndexOf(@"\v", index); + + vte.Text = text.Substring(index + 1, iend - index - 1); + int istart = text.IndexOf("#", index); + iend = text.IndexOf(@"\v0", istart); + vte.Link = text.Substring(istart, iend - istart); + DisplayTextElementList.Add(vte); + return iend + 3; + } + //private string FixTransition(string link, string text) + //{ + // int transitionID = Convert.ToInt32(link.Split(" ".ToCharArray())[1]); + // // Find the transition + // foreach (TransitionInfo ti in _MyItemInfo.MyContent.ContentTransitions) + // { + // if (ti.TransitionID == transitionID) + // { + // //string path = ti.PathTo.Replace(" Section PROCEDURE STEPS ", ", "); + // //path = path.Replace(" Section PROCEDURE STEPS", ""); + // string path = ti.ResolvePathTo(_MyFormat, _MyItemInfo, ItemInfo.Get(ti.ToID), ti.RangeID == 0 ? null : ItemInfo.Get(ti.RangeID)); + // return path; + // } + // } + // return text; + //} + private int DoTran(string text, int index) + { + //displayLinkElement vte = new displayLinkElement(); + //if (text[index] == '\x252C') vte.Type = E_TextElementType.Transition; + //else vte.Type = E_TextElementType.TransitionRange; // '\x2566'\ + //int iend = text.IndexOf(@"\v", index); + //vte.Text = text.Substring(index + 1, iend - index - 1); + //int istart = text.IndexOf("#", index); + //iend = text.IndexOf(@"\v0", istart); + //vte.Link = text.Substring(istart, iend - istart); + //vte.Text = FixTransition(vte.Link, vte.Text); // TODO: Transition text resolution? + //DisplayTextElementList.Add(vte); + //return iend + 3; + return 0; + } + private int DoSymbol(string text, int startIndex, int index) + { + displayTextElement vte = new displayTextElement(); + vte.Type = E_TextElementType.Symbol; + // symbols are the unicode/rtf command. A symbol can be represented by \'xy or + // in the text from the database \uxyz?. If the \'xy is used the length of the + // symbol number will always be two, otherwise find the index of the '?' to + // find the end. + int endindx = -1; + if (text[index + 1] == '\'') endindx = index + 3; + else endindx = text.IndexOf("?", index); + vte.Text = text.Substring(index, endindx - index + 1); + DisplayTextElementList.Add(vte); + // return the position just past the symbol. + return endindx + 1; + } + #endregion + #region ReplaceWords + //private ReplaceStr _rs; + //private string ReplaceIt(Match m) + //{ + // string s = m.ToString(); + // string t = s.Replace(_rs.ReplaceWord, _rs.ReplaceWith); + // return m.ToString().Replace(_rs.ReplaceWord, _rs.ReplaceWith); + //} + //private string DoReplaceWords(string Text) + //{ + // ReplaceStrList rsl = _MyFormat.PlantFormat.FormatData.SectData.ReplaceStrList; + // foreach (ReplaceStr rs in rsl) + // { + // if (_MyItemInfo.MyContent.Type < 20000) return Text; // for now only replace in steps. + // bool replaceit = false; + + // // note that the order of this check is important. Check in this order... + // // background here + // if (_MyItemInfo.IsHigh && (rs.Flag & E_ReplaceFlags.High) > 0) replaceit = true; + // else if ((_MyItemInfo.IsTable || _MyItemInfo.IsFigure) && (rs.Flag & E_ReplaceFlags.Table) > 0) replaceit = true; + // else if (_MyItemInfo.IsInRNO && (rs.Flag & E_ReplaceFlags.RNO) > 0) replaceit = true; + // else if (_MyItemInfo.IsCaution && (rs.Flag & E_ReplaceFlags.Caution) > 0) replaceit = true; + // else if (_MyItemInfo.IsNote && (rs.Flag & E_ReplaceFlags.Note) > 0) replaceit = true; + // else if (_MyItemInfo.IsInFirstLevelSubStep && (rs.Flag & E_ReplaceFlags.Substep) > 0) replaceit = true; + // else if (_MyItemInfo.IsAccPages & (rs.Flag & E_ReplaceFlags.Attach) > 0) replaceit = true; + + // if (replaceit) + // { + // // CASEINSENS: Do ReplaceWords for all words that match, regardless of case, and replace + // // with the ReplaceWith string as is + // if ((rs.Flag & E_ReplaceFlags.CaseInsens) > 0) + // { + // string res = ""; + // string fortest = Text.ToUpper(); + // string pat = @"(?<=\W|^)" + rs.ReplaceWord.ToUpper() + @"(?=\W|$)"; + // int cpindx = 0; + // foreach (Match m in Regex.Matches(fortest, pat)) + // { + // res += Text.Substring(cpindx, m.Index - cpindx); + // cpindx += (m.Index - cpindx); + // res += rs.ReplaceWith; + // cpindx += rs.ReplaceWord.Length; + // } + // if (cpindx < Text.Length) res += Text.Substring(cpindx, Text.Length - cpindx); + // Text = res; + // } + // // CASEINSENSALL: Do ReplaceWords for all words that match the ReplaceWord, regardless of case + // else if ((rs.Flag & E_ReplaceFlags.CaseInsensAll) > 0) + // { + // // not in hlp + // } + // // CASEINSENSFIRST: Do ReplaceWords for all words that exactly match the ReplaceWord, + // // except the case where the first character may be different + // else if ((rs.Flag & E_ReplaceFlags.CaseInsensFirst) > 0) + // { + // // not in hlp + // } + // else + // { + // string pat = @"(?<=\W|^)" + rs.ReplaceWord + @"(?=\W|$)"; + // Text = Regex.Replace(Text, pat, rs.ReplaceWith); + // } + + // } + // } + // return Text; + //} + #endregion + } + #region displayTextElementClass + public enum E_TextElementType : uint + { + Text = 0, + Transition = 1, + TransitionRange = 2, + ReferencedObject = 3, + Symbol = 4 + }; + public class displayTextElement + { + private E_TextElementType _Type; + public E_TextElementType Type + { + get { return _Type; } + set { _Type = value; } + } + private string _Text; + public string Text + { + get { return _Text; } + set { _Text = value; } + } + } + public class displayLinkElement : displayTextElement + { + private string _Link; + public string Link + { + get { return _Link; } + set { _Link = value; } + } + } + #endregion + +} \ No newline at end of file diff --git a/PROMS/Volian.Svg.Library/FontFind.cs b/PROMS/Volian.Svg.Library/FontFind.cs new file mode 100644 index 00000000..1ddad575 --- /dev/null +++ b/PROMS/Volian.Svg.Library/FontFind.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; +using Microsoft.Win32; + +namespace Volian.Svg.Library +{ + //public static class FontFind + //{ + // public static string FileName(string fontName) + // { + // string baseFont = string.Empty; + // Regex find = new Regex(fontName + @".* ?\(.*\)", RegexOptions.IgnoreCase); + // RegistryKey regKey = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Fonts"); + // string[] values = regKey.GetValueNames(); + // baseFont = regKey.GetValue(values[0]).ToString(); + // foreach (string valueName in regKey.GetValueNames()) + // { + // if (find.IsMatch(valueName)) + // return regKey.GetValue(valueName).ToString(); + // } + // return baseFont; + // } + // public static string FullFileName(string fontName) + // { + // return @"c:\windows\fonts\" + FileName(fontName); + // } + //} +} diff --git a/PROMS/Volian.Svg.Library/Graphics.cs b/PROMS/Volian.Svg.Library/Graphics.cs new file mode 100644 index 00000000..4404e37b --- /dev/null +++ b/PROMS/Volian.Svg.Library/Graphics.cs @@ -0,0 +1,195 @@ +using System; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Collections; + +namespace Volian.Svg.Library +{ + public abstract partial class SvgPart + { + public abstract void Draw(Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent); + } + public partial class SvgPartInheritance : SvgPart + { + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + } + } + public partial class Svg : SvgGroup + { + public void Draw(Graphics graphics) + { + //SetupInheritance(); + //Console.WriteLine("{0}, DPI {1}", graphics.VisibleClipBounds, graphics.DpiX); + SvgParts.Draw(graphics, new SvgScale(DeviceDPI(graphics), graphics.VisibleClipBounds, _Width, _Height, _ViewBox), this, this); + //SvgParts.Draw(graphics, new SvgScale(100, graphics.VisibleClipBounds, _Width, _Height, _ViewBox)); + } + public static float DeviceDPI(Graphics g) + { + PointF[] tmp = { new PointF(g.DpiX, g.DpiY) }; + g.TransformPoints(CoordinateSpace.World, CoordinateSpace.Device, tmp); + return (float)Math.Round((double)tmp[0].X, 0); + } + } + public partial class SvgArc : SvgShapePart + { + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + //_MyLineSettings.Scale = scale.M(1F); + if (FillColor != Color.Transparent) + using (Brush brush = new SolidBrush(FillColor)) + graphics.FillEllipse(brush, scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); + if (OutlineWidth.Value != 0F && OutlineColor != Color.Empty) + using (Pen pen = new Pen(OutlineColor, scale.M(OutlineWidth))) + graphics.DrawEllipse(pen, scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); + } + } + public partial class SvgCircle : SvgShapePart + { + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + //_MyLineSettings.Scale = scale.M(1F); + if (FillColor != Color.Transparent) + using (Brush brush = new SolidBrush(FillColor)) + graphics.FillEllipse(brush, scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); + if (OutlineWidth.Value != 0F && OutlineColor != Color.Empty) + using (Pen pen = new Pen(OutlineColor, scale.M(OutlineWidth))) + graphics.DrawEllipse(pen, scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); + } + } + public partial class SvgDefine : SvgGroup + { + public override void Draw(Graphics graphics, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) + { + // Don't draw + //_SvgParts.Draw(graphics, myScale); + } + } + public partial class SvgEllipse : SvgShapePart + { + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + if (FillColor != Color.Transparent) + using (Brush brush = new SolidBrush(FillColor)) + graphics.FillEllipse(brush, scale.X(CX - RX), scale.Y(CY - RY), scale.M(2 * RX), scale.M(2 * RY)); + if (OutlineWidth.Value != 0F && OutlineColor != Color.Empty) + using (Pen pen = new Pen(OutlineColor, scale.M(OutlineWidth))) + graphics.DrawEllipse(pen, scale.X(CX - RX), scale.Y(CY - RY), scale.M(2 * RX), scale.M(2 * RY)); + } + } + public partial class SvgGroup : SvgPartInheritance + { + public override void Draw(Graphics graphics, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + _SvgParts.Draw(graphics, myScale, mySvg, this); + } + } + public partial class SvgImage : SvgPart + { + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + graphics.DrawImage(Image.FromFile(ImagePath), scale.X(X), scale.Y(Y), scale.M(Width), scale.M(Height)); + } + } + public partial class SvgLine : SvgLinePart + { + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + if (LineColor != Color.Empty) + { + Color myColor = AdjustColorForWidth(LineColor, scale.M(LineWidth)); + using (Pen pen = new Pen(LineColor, scale.M(LineWidth))) + graphics.DrawLine(pen, scale.X(X1), scale.Y(Y1), scale.X(X2), scale.Y(Y2)); + } + } + } + public partial class SvgParts : CollectionBase + { + public void Draw(Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + foreach (SvgPart svgPart in List) + svgPart.Draw(graphics,scale,mySvg, myParent); + } + } + public partial class SvgRectangle : SvgShapePart + { + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + if (FillColor != Color.Transparent) + using (Brush brush = new SolidBrush(FillColor)) + graphics.FillRectangle(brush, scale.X(X),scale.Y(Y),scale.M(Width),scale.M(Height)); + if (OutlineWidth.Value != 0F && OutlineColor != Color.Empty) + using (Pen pen = new Pen(OutlineColor, scale.M(OutlineWidth))) + graphics.DrawRectangle(pen, scale.X(X),scale.Y(Y), scale.M(Width), scale.M(Height)); + } + } + public partial class SvgRtf : SvgShapePart + { + //private static float _Factor = 1.085F; // 20 point Arial Font + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + _MyFontSettings.Scale = scale.M(1F); + } + private float YAdjustment(Graphics gr, Font fnt) + { + int yAsc = fnt.FontFamily.GetCellAscent(fnt.Style); + int yLS = fnt.FontFamily.GetLineSpacing(fnt.Style); + float gth = fnt.GetHeight(gr); + float fAsc = (yAsc * gth) / yLS; + return -fAsc; + } + } + public partial class SvgText : SvgShapePart + { + //private static float _Factor = 1.085F; // 20 point Arial Font + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + _MyFontSettings.Scale = scale.M(1F); + StringFormat stringFormat = StringFormat.GenericTypographic; + float YAdj = YAdjustment(graphics, Font); + using (Brush brush = new System.Drawing.SolidBrush(FillColor)) + graphics.DrawString(Text, Font, brush, scale.X(X), scale.Y(Y) + YAdj, stringFormat); + SizeF sf = graphics.MeasureString(Text, Font, graphics.VisibleClipBounds.Size, stringFormat); + if (OutlineWidth.Value != 0 && OutlineColor != Color.Empty) + using (GraphicsPath path = GetStringPath(Text, Svg.DeviceDPI(graphics), new PointF(scale.X(X), scale.Y(Y) + YAdj), Font, stringFormat)) + using (Pen myPen = new Pen(OutlineColor, scale.M(OutlineWidth))) + graphics.DrawPath(myPen, path); + } + GraphicsPath GetStringPath(string s, float dpi, PointF loc, Font font, StringFormat format) + { + GraphicsPath path = new GraphicsPath(); + // Convert font size into appropriate coordinates + float emSize = dpi * font.SizeInPoints / 72; + path.AddString(s, font.FontFamily, (int)font.Style, emSize, loc, format); + return path; + } + private float YAdjustment(Graphics gr, Font fnt) + { + int yAsc = fnt.FontFamily.GetCellAscent(fnt.Style); + //int yDesc = fnt.FontFamily.GetCellDescent(fnt.Style); + //int yEm = fnt.FontFamily.GetEmHeight(fnt.Style); + int yLS = fnt.FontFamily.GetLineSpacing(fnt.Style); + float gth = fnt.GetHeight(gr); + float fh = fnt.Size; + float fAsc = (yAsc * gth) / yLS; + return -fAsc; + } + } + public partial class SvgUse : SvgPartInheritance + { + public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + mySvg[_UseID.Substring(1)].Draw(graphics, scale.AdjustOrigin(X, Y), mySvg, this); + } + } +} diff --git a/PROMS/Volian.Svg.Library/Svg.cs b/PROMS/Volian.Svg.Library/Svg.cs new file mode 100644 index 00000000..d3cd6c00 --- /dev/null +++ b/PROMS/Volian.Svg.Library/Svg.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; + +namespace Volian.Svg.Library +{ + [XmlRoot("svg")] + public partial class Svg : SvgGroup + { + #region XML Serializer Namespaces + private XmlSerializerNamespaces _Namespaces = null; + [XmlNamespaceDeclarations] + public XmlSerializerNamespaces Namespaces + { + get + { + if (_Namespaces == null) + { + _Namespaces = new XmlSerializerNamespaces(); + _Namespaces.Add("xlink", "http://www.w3.org/1999/xlink"); + } + return _Namespaces; + } + set { _Namespaces = value; } + } + #endregion + #region Width + private SvgMeasurement _Width = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Width + { + get { return _Width; } + set { _Width = value; } + } + [System.ComponentModel.DefaultValueAttribute("0")] + [XmlAttribute("width")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Width_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Width); } + set { _Width = SvgXmlConverter.GetObject(value); } + } + private string _Watermark=""; + [XmlAttribute("watermark")] + public string Watermark + { + get { return _Watermark; } + set { _Watermark = value; } + } + public void SetValidWaterMark(string token, string watermark) + { + if (token == "{" + watermark.ToUpper() + "PAGE}" ) + _Watermark = watermark; + } + #endregion + #region Height + private SvgMeasurement _Height = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Height + { + get { return _Height; } + set { _Height = value; } + } + [System.ComponentModel.DefaultValueAttribute("0")] + [XmlAttribute("height")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Height_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Height); } + set { _Height = SvgXmlConverter.GetObject(value); } + } + #endregion + #region ViewBox + private SvgViewBox _ViewBox = new SvgViewBox("0 0 0 0"); + [XmlIgnore] + public SvgViewBox ViewBox + { + get { return _ViewBox; } + set { _ViewBox = value; } + } + [System.ComponentModel.DefaultValueAttribute("0 0 0 0")] + [XmlAttribute("viewBox")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string SVGViewBox_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_ViewBox); } + set { _ViewBox = SvgXmlConverter.GetObject(value); } + } + #endregion + //#region Setup Inheritance + //private void SetupInheritance() + //{ + // SvgParts.SetupInheritance(_MyInheritedSettings); + //} + ////override internal void SetupInheritance(SvgInheritedSettings myParentsSettings) + ////{ + //// _MyInheritedSettings.MyParentsSettings = myParentsSettings; + //// SvgParts.SetupInheritance(_MyInheritedSettings); + ////} + //#endregion + #region Part Lookup + private Dictionary _LookUp; + [XmlIgnore] + private Dictionary LookUp + { + get + { + if (_LookUp == null) + { + _LookUp = new Dictionary(); + AddLookup(_LookUp); + } + return _LookUp; + } + } + public SvgPart this[string id] + { get { return LookUp[id]; } } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgArc.cs b/PROMS/Volian.Svg.Library/SvgArc.cs new file mode 100644 index 00000000..407d26d1 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgArc.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgArc : SvgShapePart + { + #region ctor + public SvgArc() { ;} + public SvgArc(PointF location, float radius, float startAngle, float endAngle, Color fillColor, Color lineColor, float lineWidth) + { + _CX.Value = location.X; + _CY.Value = location.Y; + _Radius.Value = radius; + FillColor = fillColor; + LineColor = lineColor; + LineWidth = new SvgMeasurement(lineWidth); + } + //XMLElementAttribute(ElementName = "PREFIX", IsNullable = false) + #endregion + #region Location + private SvgMeasurement _CX = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement CX + { + get { return _CX; } + set { _CX = value; } + } + [XmlAttribute("cx")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string CX_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_CX); } + set { _CX = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _CY = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement CY + { + get { return _CY; } + set { _CY = value; } + } + [XmlAttribute("cy")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string CY_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_CY); } + set { _CY = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Radius + private SvgMeasurement _Radius = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Radius + { + get { return _Radius; } + set { _Radius = value; } + } + [XmlAttribute("r")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Radius); } + set { _Radius = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Angles + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgCircle.cs b/PROMS/Volian.Svg.Library/SvgCircle.cs new file mode 100644 index 00000000..5c1b8500 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgCircle.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgCircle : SvgShapePart + { + #region ctor + public SvgCircle() { ;} + public SvgCircle(PointF location, float radius, Color fillColor, Color lineColor, float lineWidth) + { + _CX.Value = location.X; + _CY.Value = location.Y; + _Radius.Value = radius; + FillColor = fillColor; + LineColor = lineColor; + LineWidth = new SvgMeasurement(lineWidth); + } + //XMLElementAttribute(ElementName = "PREFIX", IsNullable = false) + #endregion + #region Location + private SvgMeasurement _CX = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement CX + { + get { return _CX; } + set { _CX = value; } + } + [XmlAttribute("cx")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string CX_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_CX); } + set { _CX = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _CY = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement CY + { + get { return _CY; } + set { _CY = value; } + } + [XmlAttribute("cy")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string CY_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_CY); } + set { _CY = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Radius + private SvgMeasurement _Radius = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Radius + { + get { return _Radius; } + set { _Radius = value; } + } + [XmlAttribute("r")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Radius); } + set { _Radius = SvgXmlConverter.GetObject(value); } + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgDefine.cs b/PROMS/Volian.Svg.Library/SvgDefine.cs new file mode 100644 index 00000000..30a64e56 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgDefine.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgDefine : SvgGroup + { + // Nothing here - Draw definitions in individual modules + } +} diff --git a/PROMS/Volian.Svg.Library/SvgEllipse.cs b/PROMS/Volian.Svg.Library/SvgEllipse.cs new file mode 100644 index 00000000..bd72768d --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgEllipse.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgEllipse : SvgShapePart + { + #region ctor + public SvgEllipse() { ;} + public SvgEllipse(PointF location, float radiusX, float radiusY, Color fillColor, Color lineColor, float lineWidth) + { + _CX.Value = location.X; + _CY.Value = location.Y; + _RX.Value = radiusX; + _RY.Value = radiusY; + FillColor = fillColor; + LineColor = lineColor; + LineWidth = new SvgMeasurement(lineWidth); + } + //XMLElementAttribute(ElementName = "PREFIX", IsNullable = false) + #endregion + #region Location + private SvgMeasurement _CX = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement CX + { + get { return _CX; } + set { _CX = value; } + } + [XmlAttribute("cx")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string CX_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_CX); } + set { _CX = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _CY = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement CY + { + get { return _CY; } + set { _CY = value; } + } + [XmlAttribute("cy")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string CY_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_CY); } + set { _CY = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Size + private SvgMeasurement _RX = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement RX + { + get { return _RX; } + set { _RX = value; } + } + [XmlAttribute("rx")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string RX_RXmlSurrogate + { + get { return SvgXmlConverter.GetString(_RX); } + set { _RX = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _RY = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement RY + { + get { return _RY; } + set { _RY = value; } + } + [XmlAttribute("ry")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string RY_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_RY); } + set { _RY = SvgXmlConverter.GetObject(value); } + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgFillSettings.cs b/PROMS/Volian.Svg.Library/SvgFillSettings.cs new file mode 100644 index 00000000..5c371699 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgFillSettings.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public class SvgFillSettings + { + #region ctor + public SvgFillSettings() { ;} + public SvgFillSettings(Color myColor) + { + _Fill = ColorTranslator.ToHtml(myColor); + } + //public SvgFillSettings(string myFill) + //{ + // _Fill = myFill; + //} + #endregion + #region Inheritance + private SvgFillSettings _MyParentsSettings = null; + public SvgFillSettings MyParentsSettings + { + get { return _MyParentsSettings; } + set + { + _MyParentsSettings = value; + } + } + #endregion + #region Internal Inherited Properties + protected string InheritedFill + { + get + { + // Fill = "", "0" - Default, otherwise the value + if (_Fill != "" && _Fill != "0" && _Fill.ToLower() != "empty") return _Fill; // Current Value + if (_MyParentsSettings != null) return _MyParentsSettings.InheritedFill; // Inherited + return "Black"; // Default + } + } + #endregion + #region Inherited Values + public Color FillColor + { + get + { + // Fill = "None" - Eliminate, otherwise the value + string myFill = InheritedFill; + if (myFill.ToLower() == "none") // Eliminate + return Color.Transparent; + return ColorTranslator.FromHtml(myFill); + } + set + { + if (value == Color.Transparent || value == Color.Empty) + _Fill = "none"; + else + _Fill = ColorTranslator.ToHtml(value); + } + } + #endregion + #region Properties + private string _Fill = string.Empty; // Default value is None + public string Fill + { + get { return _Fill; } + set { _Fill = value; } + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgFontSettings.cs b/PROMS/Volian.Svg.Library/SvgFontSettings.cs new file mode 100644 index 00000000..f6836138 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgFontSettings.cs @@ -0,0 +1,159 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; + +namespace Volian.Svg.Library +{ + class SvgFontSettings + { + #region ctor + public SvgFontSettings() { ;} + public SvgFontSettings(Font myFont) + { + _FontFamily = myFont.FontFamily.Name; + _FontSize = myFont.SizeInPoints.ToString() + "PT"; + _SVGFontStyle = ((myFont.Style & FontStyle.Italic) == FontStyle.Italic ? "italic" : "normal"); + _FontWeight = ((myFont.Style & FontStyle.Bold) == FontStyle.Bold ? "bold" : "normal"); + _TextDecoration = ((myFont.Style & FontStyle.Underline) == FontStyle.Underline ? "underline" : "normal"); + } + //public SvgFontSettings(string myFontFamily, string myFontSize, string mySvgStyle, string myFontWeight, string myTextDecoration) + //{ + // _FontFamilyName = myFontFamily; + // _FontSize = myFontSize; + // _SVGFontStyle = mySvgStyle; + // _FontWeight = myFontWeight; + // _TextDecoration = myTextDecoration; + //} + #endregion + #region Inheritance + private SvgFontSettings _MyParentsSettings = null; + public SvgFontSettings MyParentsSettings + { + get { return _MyParentsSettings; } + set + { + _MyParentsSettings = value; + } + } + #endregion + #region Internal Inherited Properties + protected string InheritedFontFamily + { + get + { + if (_FontFamily != "") return _FontFamily; + if (_MyParentsSettings != null) return _MyParentsSettings.InheritedFontFamily; // Default + return "Arial"; // Absolute Default + } + } + protected string InheritedFontSize + { + get + { + if (_FontSize != "") return _FontSize; // Current Value + if (_MyParentsSettings != null) return _MyParentsSettings.InheritedFontSize; // Default + return "9PT"; // Absolute Default + } + } + protected string InheritedFontStyle + { + get + { + if (_SVGFontStyle != "") return _SVGFontStyle; // Current Value + if (_MyParentsSettings != null) return _MyParentsSettings.InheritedFontStyle;// Default + return "normal"; // Absolute Default + } + } + protected string InheritedFontWeight + { + get + { + if (_FontWeight != "") return _FontWeight;// Current Value + if (_MyParentsSettings != null) return _MyParentsSettings.InheritedFontWeight;// Default + return "normal"; // Absolute Default + } + } + protected string InheritedTextDecoration + { + get + { + if (_TextDecoration != "") return _TextDecoration;// Current Value + if (_MyParentsSettings != null) return _MyParentsSettings.InheritedTextDecoration;// Default + return "normal"; // Absolute Default + } + } + #endregion + #region Scale + private float _Scale = 1; + internal float Scale + { + get { return _Scale; } + set { _Scale = value; Font = null; } + } + #endregion + #region Inherited Values + private Font _Font=null; + public Font Font + { + get + { + if (_Font == null) + { + _Font = new Font(InheritedFontFamily, _Scale * SvgMeasurement.StringToPoints(InheritedFontSize), + (InheritedFontStyle == "italic" ? FontStyle.Italic : FontStyle.Regular) | + (InheritedFontWeight == "bold" ? FontStyle.Bold : FontStyle.Regular) | + (InheritedTextDecoration == "underline" ? FontStyle.Underline : FontStyle.Regular) + , GraphicsUnit.Point); + } + return _Font; + } + set + { + if (_Font != null) _Font.Dispose(); + if (value != null) + { + _FontFamily = value.FontFamily.Name; + _FontSize = value.SizeInPoints.ToString() + "PT"; + _SVGFontStyle = ((value.Style & FontStyle.Italic) == FontStyle.Italic ? "italic" : "normal"); + _FontWeight = ((value.Style & FontStyle.Bold) == FontStyle.Bold ? "bold" : "normal"); + _TextDecoration = ((value.Style & FontStyle.Underline) == FontStyle.Underline ? "underline" : "normal"); + } + _Font = value; + } + } + #endregion + #region Public Properties + private string _FontFamily = string.Empty; + public string FontFamily + { + get { return _FontFamily; } + set { _FontFamily = value; Font = null; } + } + private string _FontSize = string.Empty; + public string FontSize + { + get { return _FontSize; } + set { _FontSize = value; Font = null; } + } + private string _SVGFontStyle = string.Empty; + public string SVGFontStyle + { + get { return _SVGFontStyle; } + set { _SVGFontStyle = value; Font = null; } + } + private string _FontWeight = string.Empty; + public string FontWeight + { + get { return _FontWeight; } + set { _FontWeight = value; Font = null; } + } + private string _TextDecoration = string.Empty; + public string TextDecoration + { + get { return _TextDecoration; } + set { _TextDecoration = value; Font = null; } + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgGroup.cs b/PROMS/Volian.Svg.Library/SvgGroup.cs new file mode 100644 index 00000000..91a5f79a --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgGroup.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgGroup : SvgPartInheritance + { + #region Description + private string _Description = string.Empty; + [System.ComponentModel.DefaultValueAttribute("")] + [XmlElement("desc")] + public string Description + { + get { return _Description; } + set { _Description = value; } + } + #endregion + #region Title + private string _Title = string.Empty; + [System.ComponentModel.DefaultValueAttribute("")] + [XmlElement("title")] + public string Title + { + get { return _Title; } + set { _Title = value; } + } + #endregion + #region Parts + // SVG Parts + SvgParts _SvgParts = new SvgParts(); + [XmlElement("foreignObject", typeof(SvgRtf))] + [XmlElement("use", typeof(SvgUse))] + [XmlElement("image", typeof(SvgImage))] + [XmlElement("text", typeof(SvgText))] + [XmlElement("line", typeof(SvgLine))] + [XmlElement("circle", typeof(SvgCircle))] + [XmlElement("rect", typeof(SvgRectangle))] + [XmlElement("ellipse", typeof(SvgEllipse))] + [XmlElement("svg", typeof(Svg))] + [XmlElement("g", typeof(SvgGroup))] + [XmlElement("defs", typeof(SvgDefine))] + public SvgParts SvgParts + { + get { return _SvgParts; } + set { _SvgParts = value; } + } + public SvgPart Add(SvgPart svgPart) + { + _SvgParts.Add(svgPart); + return svgPart; + } + public void RemoveAt(int i) + { + _SvgParts.RemoveAt(i); + } + public int Count + { get { return _SvgParts.Count; } } + #endregion + #region Dictionary of Parts + internal override void AddLookup(Dictionary lookUp) + { + base.AddLookup(lookUp); + SvgParts.AddLookup(lookUp); + } + #endregion + public override string ToString() + { + return ID; + } + } +} \ No newline at end of file diff --git a/PROMS/Volian.Svg.Library/SvgImage.cs b/PROMS/Volian.Svg.Library/SvgImage.cs new file mode 100644 index 00000000..012cae61 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgImage.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgImage : SvgPart + { + #region ctor + public SvgImage() { ;} + public SvgImage(PointF location, SizeF size, string path) + { + _X.Value = location.X; + _Y.Value = location.Y; + _Width.Value = size.Width; + _Height.Value = size.Height; + _ImagePath = path; + } + #endregion + #region Location + private SvgMeasurement _X = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement X + { + get { return _X; } + set { _X = value; } + } + [XmlAttribute("x")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_X); } + set { _X = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Y = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Y + { + get { return _Y; } + set { _Y = value; } + } + [XmlAttribute("y")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Y_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Y); } + set { _Y = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Size + private SvgMeasurement _Width = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Width + { + get { return _Width; } + set { _Width = value; } + } + [XmlAttribute("width")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Width_WidthmlSurrogate + { + get { return SvgXmlConverter.GetString(_Width); } + set { _Width = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Height = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Height + { + get { return _Height; } + set { _Height = value; } + } + [XmlAttribute("height")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Height_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Height); } + set { _Height = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Path to Image File + private string _ImagePath = null; + [XmlAttribute(AttributeName = "href", Namespace = "http://www.w3.org/1999/xlink")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string ImagePath + { + get { return _ImagePath; } + set { _ImagePath = value; } + } + #endregion + } +} \ No newline at end of file diff --git a/PROMS/Volian.Svg.Library/SvgInheritedSettings.cs b/PROMS/Volian.Svg.Library/SvgInheritedSettings.cs new file mode 100644 index 00000000..702ad9cc --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgInheritedSettings.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Volian.Svg.Library +{ + internal class SvgInheritedSettings + { + #region MyParent + SvgInheritedSettings _MyParentsSettings = null; + internal SvgInheritedSettings MyParentsSettings + { + get { return _MyParentsSettings; } + set + { + _MyParentsSettings = value; + MyLineSettings.MyParentsSettings = value.MyLineSettings; + MyFillSettings.MyParentsSettings = value.MyFillSettings; + MyFontSettings.MyParentsSettings = value.MyFontSettings; + } + } + #endregion + #region Line Settings + private SvgLineSettings _MyLineSettings = new SvgLineSettings(); + public SvgLineSettings MyLineSettings + { + get { return _MyLineSettings; } + set { _MyLineSettings = value; } + } + public string Stroke + { + get { return _MyLineSettings.Stroke; } + set { _MyLineSettings.Stroke = value; } + } + public string StrokeWidth + { + get { return _MyLineSettings.StrokeWidth; } + set { _MyLineSettings.StrokeWidth = value; } + } + #endregion + #region Fill Settings + private SvgFillSettings _MyFillSettings = new SvgFillSettings(); + internal SvgFillSettings MyFillSettings + { + get { return _MyFillSettings; } + set { _MyFillSettings = value; } + } + public string Fill + { + get { return _MyFillSettings.Fill; } + set { _MyFillSettings.Fill = value; } + } + #endregion + #region Font Settings + private SvgFontSettings _MyFontSettings = new SvgFontSettings(); + internal SvgFontSettings MyFontSettings + { + get { return _MyFontSettings; } + set { _MyFontSettings = value; } + } + public string FontFamily + { + get { return _MyFontSettings.FontFamily; } + set { _MyFontSettings.FontFamily = value; } + } + public string FontSize + { + get { return _MyFontSettings.FontSize; } + set { _MyFontSettings.FontSize = value; } + } + public string SVGFontStyle + { + get { return _MyFontSettings.SVGFontStyle; } + set { _MyFontSettings.SVGFontStyle = value; } + } + public string FontWeight + { + get { return _MyFontSettings.FontWeight; } + set { _MyFontSettings.FontWeight = value; } + } + public string TextDecoration + { + get { return _MyFontSettings.TextDecoration; } + set { _MyFontSettings.TextDecoration = value; } + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgLine.cs b/PROMS/Volian.Svg.Library/SvgLine.cs new file mode 100644 index 00000000..28e135e8 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgLine.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgLine : SvgLinePart + { + #region ctor + public SvgLine() { ;} + public SvgLine(PointF location1, PointF location2, Color lineColor, float lineWidth) + { + _X1.Value = location1.X; + _Y1.Value = location1.Y; + _X2.Value = location2.X; + _Y2.Value = location2.Y; + LineColor = lineColor; + LineWidth = new SvgMeasurement(lineWidth); + } + //XMLElementAttribute(ElementName = "PREFIX", IsNullable = false) + #endregion + #region Location1 + private SvgMeasurement _X1 = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement X1 + { + get { return _X1; } + set { _X1 = value; } + } + [XmlAttribute("x1")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X1_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_X1); } + set { _X1 = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Y1 = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Y1 + { + get { return _Y1; } + set { _Y1 = value; } + } + [XmlAttribute("y1")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Y1_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Y1); } + set { _Y1 = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Location2 + private SvgMeasurement _X2 = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement X2 + { + get { return _X2; } + set { _X2 = value; } + } + [XmlAttribute("x2")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X2_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_X2); } + set { _X2 = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Y2 = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Y2 + { + get { return _Y2; } + set { _Y2 = value; } + } + [XmlAttribute("y2")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Y_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Y2); } + set { _Y2 = SvgXmlConverter.GetObject(value); } + } + #endregion + #region LineColor + //protected Color _LineColor = Color.Empty; + //[XmlIgnoreAttribute()] + //public Color LineColor + //{ + // get { return _LineWidth.Value == 0 ? Color.LightGray : _LineColor == Color.Empty ? Color.Gray : _LineColor; } + // set { _LineColor = value; } + //} + private Color AdjustColorForWidth(Color myColor, float myWidth) + { + if (myWidth > 1F) return myColor; + //Console.WriteLine("Line Width = {0}, Alpha = {1}", myWidth, Convert.ToInt32(myWidth * 255)); + return Color.FromArgb(Convert.ToInt32(myWidth * 255), myColor); + } + // Serializes the 'BackgroundColor' Color to XML. + //[System.ComponentModel.DefaultValueAttribute("none")] + //[XmlAttribute("stroke")] + //public string Stroke + //{ + // get { return _LineColor == Color.Empty ? "none" : _LineColor == Color.Black ? "" : ColorTranslator.ToHtml(_LineColor); } + // set { _LineColor = (value == "none" ? Color.Empty : (value == "" ? Color.Empty : ColorTranslator.FromHtml(value))); } + //} + #endregion + #region LineWidth + //private SvgMeasurement GetLineWidth() + //{ + // if (_LineWidth.Value == -1) return new SvgMeasurement(1F);// Minimum Strokewidth + // if (_LineWidth.Value == 0) return new SvgMeasurement(.01F);// Minimum Strokewidth + // if (_LineColor == Color.Empty) return new SvgMeasurement( .01F);// No Color - Minimum Strokewidth + // return _LineWidth; + //} + //private SvgMeasurement _LineWidth = new SvgMeasurement(); + //[XmlIgnore] + //public SvgMeasurement LineWidth + //{ + // get { return _LineWidth; } + // set { _LineWidth = value; } + //} + //[XmlAttribute("stroke-width")] + //[Browsable(false)] + //[EditorBrowsable(EditorBrowsableState.Never)] + //public string LineWidth_XmlSurrogate + //{ + // get + // { + // if (_LineWidth.Value == -1) + // return "none"; + // return VolianXmlConverter.GetString(_LineWidth); + // } + // set + // { + // if (value == "none") + // _LineWidth = VolianXmlConverter.GetObject("-1"); + // else + // _LineWidth = VolianXmlConverter.GetObject(value); + // } + //} + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgLinePart.cs b/PROMS/Volian.Svg.Library/SvgLinePart.cs new file mode 100644 index 00000000..9dead6ea --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgLinePart.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; +using System.Xml.Serialization; +using System.ComponentModel; + +namespace Volian.Svg.Library +{ + [TypeConverter(typeof(ExpandableObjectConverter))] + public abstract class SvgLinePart : SvgPart + { + #region Line Settings + protected SvgLineSettings _MyLineSettings = new SvgLineSettings(); + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("stroke")] + public string Stroke + { + get { return _MyLineSettings.Stroke; } + set { _MyLineSettings.Stroke = value; } + } + [XmlIgnore] + protected Color LineColor + { + get { return _MyLineSettings.LineColor; } + set { _MyLineSettings.LineColor = value; } + } + [XmlIgnore] + protected Color OutlineColor + { + get { return _MyLineSettings.OutlineColor; } + set { _MyLineSettings.OutlineColor = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("stroke-width")] + public string StrokeWidth + { + get { return _MyLineSettings.StrokeWidth; } + set { _MyLineSettings.StrokeWidth = value; } + } + [XmlIgnore] + protected SvgMeasurement LineWidth + { + get { return _MyLineSettings.LineWidth; } + set { _MyLineSettings.LineWidth = value; } + } + [XmlIgnore] + protected SvgMeasurement OutlineWidth + { + get { return _MyLineSettings.OutlineWidth; } + set { _MyLineSettings.OutlineWidth = value; } + } + #endregion + #region Setup Inheritance + override internal void SetupInheritance(SvgInheritedSettings myParentsSettings) + { + _MyLineSettings.MyParentsSettings = myParentsSettings.MyLineSettings; + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgLineSettings.cs b/PROMS/Volian.Svg.Library/SvgLineSettings.cs new file mode 100644 index 00000000..b995d0d7 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgLineSettings.cs @@ -0,0 +1,167 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; +using System.ComponentModel; +using System.Xml.Serialization; + +namespace Volian.Svg.Library +{ + public class SvgLineSettings + { + #region ctor + public SvgLineSettings() { ;} + public SvgLineSettings(Color myColor, SvgMeasurement myWidth) + { + _Stroke = ColorTranslator.ToHtml(myColor); + _StrokeWidth = myWidth.ToString(); + } + //public SvgLineSettings(string myStroke, string myStrokeWidth) + //{ + // _Stroke = myStroke; + // _StrokeWidth = myStrokeWidth; + //} + #endregion + #region Inheritance + private SvgLineSettings _MyParentsSettings = null; + public SvgLineSettings MyParentsSettings + { + get { return _MyParentsSettings; } + set + { + _MyParentsSettings = value; + } + } + #endregion + #region Internal Inherited Properties + protected string InheritedStroke + { + get + { + // Stroke = "", "0" - Default, otherwise the value + if (_Stroke != "" && _Stroke != "0") return _Stroke; // Current Value + if (_MyParentsSettings != null) return _MyParentsSettings.InheritedStroke;// Inherited + return "None"; // Default + } + } + protected string InheritedLineWidth + { + get + { + // StrokeWidth = "", "none" - Default, otherwise the value + if (_StrokeWidth != "" && _StrokeWidth != "none") return _StrokeWidth; // Current Value + if (_MyParentsSettings != null) return _MyParentsSettings.InheritedLineWidth; // Inherited + return "1PX"; // Default + } + } + //protected string InheritedOutlineWidth + //{ + // get + // { + // // StrokeWidth = "", "none" - Default, otherwise the value + // if (_StrokeWidth != "" && _StrokeWidth.ToLower() != "none") return _StrokeWidth; // Current Value + // if (_MyParent != null) return _MyParent.InheritedLineWidth; // Inherited + // return "1PX"; // Default + // } + //} + #endregion + #region Inherited Values + public SvgMeasurement LineWidth + { + get + { + // Stroke = "None" - Light, otherwise the value + // StrokeWidth = "None" Light, otherwise the value + string myStroke = InheritedStroke; + string myWidth = InheritedLineWidth; + //if (myStroke.ToLower() == "none" || myWidth.ToLower() == "none") // Light + // return new SvgMeasurement(.001F, E_MeasurementUnits.PX); + SvgMeasurement myMeasurement = new SvgMeasurement(myWidth); + //myMeasurement.Value *= _Scale; + return myMeasurement; + } + set + { + _StrokeWidth = value.ToString(); + } + } + public SvgMeasurement OutlineWidth + { + get + { + // Stroke = "None" - Eliminate, otherwise the value + // StrokeWidth = "0" Eliminate, otherwise the value + string myStroke = InheritedStroke; + //string myWidth = InheritedOutlineWidth; + string myWidth = InheritedLineWidth; + if (myStroke.ToLower() == "none" || myWidth == "0") // Eliminate + return new SvgMeasurement(0, E_MeasurementUnits.PX); + SvgMeasurement myMeasurement = new SvgMeasurement(myWidth); + //myMeasurement.Value *= _Scale; + return myMeasurement; + } + set + { + _StrokeWidth = value.ToString(); + } + } + public Color LineColor + { + get + { + // Stroke = "None" - Eliminate, otherwise the value + // StrokeWidth = "None" Eliminate, otherwise the value + string myStroke = InheritedStroke; + string myWidth = InheritedLineWidth; + if (myStroke.ToLower() == "none" || myWidth.ToLower() == "none" || myWidth == "0") // Light + return Color.LightGray; + return ColorTranslator.FromHtml(myStroke); + } + set + { + if (value == Color.Empty) + _Stroke = "none"; + else + _Stroke = ColorTranslator.ToHtml(value); + } + + } + public Color OutlineColor + { + get + { + // Stroke = "None" - Eliminate, otherwise the value + // StrokeWidth = "0" Eliminate, otherwise the value + string myStroke = InheritedStroke; + //string myWidth = InheritedOutlineWidth; + string myWidth = InheritedLineWidth; + if (myStroke.ToLower() == "none" || myWidth == "0") // Eliminate + return Color.Empty; + return ColorTranslator.FromHtml(myStroke); + } + set + { + if (value == Color.Empty) + _Stroke = "none"; + else + _Stroke = ColorTranslator.ToHtml(value); + } + + } + #endregion + #region Properties + private string _Stroke = string.Empty; // Default value is None + public string Stroke + { + get { return _Stroke; } + set { _Stroke = value; } + } + private string _StrokeWidth = string.Empty; // Default value is 1PX + public string StrokeWidth + { + get { return _StrokeWidth; } + set { _StrokeWidth = value; } + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgMeasurement.cs b/PROMS/Volian.Svg.Library/SvgMeasurement.cs new file mode 100644 index 00000000..4f5df19b --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgMeasurement.cs @@ -0,0 +1,250 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.ComponentModel; +using System.Drawing; +using System.Windows.Forms; +using System.Text.RegularExpressions; + +namespace Volian.Svg.Library +{ + [TypeConverter(typeof(MeasurementTypeConverter))] + public class SvgMeasurement + { + #region Operators + public static SvgMeasurement operator +(SvgMeasurement x1, SvgMeasurement x2) + { + E_MeasurementUnits tmpUnits = x1.Units; + SvgMeasurement tmp = new SvgMeasurement(x1.GetSizeInPoints(DPI) + x2.GetSizeInPoints(DPI),E_MeasurementUnits.PT); + tmp.Units = x1.Units; + return tmp; + } + public static SvgMeasurement operator -(SvgMeasurement x1, SvgMeasurement x2) + { + E_MeasurementUnits tmpUnits = x1.Units; + SvgMeasurement tmp = new SvgMeasurement(x1.GetSizeInPoints(DPI) - x2.GetSizeInPoints(DPI), E_MeasurementUnits.PT); + tmp.Units = x1.Units; + return tmp; + } + public static SvgMeasurement operator *(SvgMeasurement x1, float scaler) + { + E_MeasurementUnits tmpUnits = x1.Units; + SvgMeasurement tmp = new SvgMeasurement(x1.GetSizeInPoints(DPI) * scaler, E_MeasurementUnits.PT); + tmp.Units = x1.Units; + return tmp; + } + public static SvgMeasurement operator *(float scaler, SvgMeasurement x1) + { + E_MeasurementUnits tmpUnits = x1.Units; + SvgMeasurement tmp = new SvgMeasurement(x1.GetSizeInPoints(DPI) * scaler, E_MeasurementUnits.PT); + tmp.Units = x1.Units; + return tmp; + } + #endregion + #region DPI + private static float _DPI; + public static float DPI + { + get + { + if (_DPI == 0) + _DPI = 96; + return _DPI; + } + } + #endregion + #region ctor + public SvgMeasurement() { ;} + public SvgMeasurement(int value) + { + _Value = Convert.ToSingle(value); + _Units = E_MeasurementUnits.PX; + } + public SvgMeasurement(float value) + { + _Value = value; + _Units = E_MeasurementUnits.PX; + } + public SvgMeasurement(float value, E_MeasurementUnits units) + { + _Value = value; + _Units = units; + } + public SvgMeasurement(string val) + { + MatchCollection mc = Regex.Matches(val.ToLower(), "([+-]?[.0-9]+)|(in|cm|mm|em|ex|pc|pt|px|percentage)"); + _Value = Convert.ToSingle(mc[0].Value); + if (mc.Count == 2) + { + //Console.WriteLine("{0} - '{1}'", val, mc[1].Value.ToUpper()); + _Units = (E_MeasurementUnits)Enum.Parse(typeof(E_MeasurementUnits), mc[1].Value.ToUpper()); + } + else + _Units = E_MeasurementUnits.PX; + } + #endregion + #region Properties + private E_MeasurementUnits _Units; + public E_MeasurementUnits Units + { + get { return _Units; } + set + { + float tmp = GetSizeInPoints(DPI); + _Units = value; + _Value *= (tmp / GetSizeInPoints(DPI)); + } + } + private float _Value; + public float Value + { + get { return _Value; } + set { _Value = value; } + } + #endregion + #region ToString + public override string ToString() + { + switch (_Units) + { + case E_MeasurementUnits.PX: // Default + return string.Format("{0}", _Value); + case E_MeasurementUnits.PERCENTAGE: // Precentage is preceded by a space + return string.Format("{0} {1}", _Value, _Units); + } + return string.Format("{0}{1}", _Value, _Units); + } + #endregion + //public float SizeInPixels + //{ + // get + // { + // switch (_Units) + // { + // case E_MeasurementUnits.PT: //Points - 72 per inch + // return _Value * DPI / 72F; + // case E_MeasurementUnits.PC: //Picas - 6 per inch + // return _Value * DPI / 6F; + // case E_MeasurementUnits.IN: //inches + // return _Value * DPI; + // case E_MeasurementUnits.CM: // Centimeter + // return _Value * DPI / 2.54F; + // case E_MeasurementUnits.MM: // Millimeter + // return _Value * DPI / 25.4F; + // case E_MeasurementUnits.EM: // EMs + // return _Value * DPI / 8; + // case E_MeasurementUnits.EX: // EXs + // return _Value * DPI / 16; + // default: + // return _Value; + // } + // } + //} + public float GetSizeInPoints(float myDPI) + { + switch (_Units) + { + case E_MeasurementUnits.PX: //Pixels myDPI per inch + return _Value * 72F / myDPI; + case E_MeasurementUnits.PC: //Picas - 6 per inch + return _Value * 72F / 6F; + case E_MeasurementUnits.IN: //inches + return _Value * 72F; + case E_MeasurementUnits.CM: // Centimeter + return _Value * 72F / 2.54F; + case E_MeasurementUnits.MM: // Millimeter + return _Value * 72F / 25.4F; + case E_MeasurementUnits.EM: // EMs + return _Value * 72F / 8; + case E_MeasurementUnits.EX: // EXs + return _Value * 72F / 16; + default: // Points + return _Value; + } + } + //public float SizeInPoints + //{ get { return SizeInPixels * 72 / DPI; } } + //public float SizeInInches + //{ get { return SizeInPixels / DPI; } } + //public float SizeInCentimeters + //{ get { return SizeInPixels * 2.54F / DPI; } } + //public float SizeInMillimeters + //{ get { return SizeInPixels * 25.4F / DPI; } } + //public float SizeInEms + //{ get { return SizeInPixels * 5 / (6 * DPI); } } + //public float SizeInExs + //{ get { return SizeInPixels * 5 / (6 * DPI); } } + //public float SizeInPicas + //{ get { return SizeInPixels * 6 / DPI; } } + //public Unit SizeInUnits + //{ get { return new Unit((double)SizeInPoints, UnitTypeEnum.Point); } } + public float GetSizeInPixels(float myDPI) + { return GetSizeInPoints(myDPI) * myDPI / 72F;} + public float GetSizeInInches(float myDPI) + { return GetSizeInPoints(myDPI) / 72F; } + public float GetSizeInCentimeters(float myDPI) + { return GetSizeInPoints(myDPI) * 2.54F / 72F; } + public float GetSizeInMillimeters(float myDPI) + { return GetSizeInPoints(myDPI) * 25.4F / 72F; } + public float GetSizeInEms(float myDPI) + { return GetSizeInPoints(myDPI) * 5 / (6 * 72F); } + public float GetSizeInExs(float myDPI) + { return GetSizeInPoints(myDPI) * 5 / (6 * 72F); } + public float GetSizeInPicas(float myDPI) + { return GetSizeInPoints(myDPI) * 6 / 72F; } + //public Unit GetSizeInUnits(float myDPI) + //{ return new Unit((double)GetSizeInPoints(myDPI), UnitTypeEnum.Point); } + public static float StringToPoints(string value) + { + SvgMeasurement tmp = new SvgMeasurement(value); + float myPoints = tmp.GetSizeInPoints(72); + return myPoints; + } + + } + public enum E_MeasurementUnits : int + { + [Description("Pixels")] + PX, + [Description("Points")] + PT, + [Description("Inches")] + IN, + [Description("Centimeters")] + CM, + [Description("Millimeters")] + MM, + [Description("EMs")] + EM, + [Description("EXs")] + EX, + [Description("Percentage")] + PERCENTAGE, + [Description("Picas")] + PC + } + public class MeasurementTypeConverter : ExpandableObjectConverter + { + public override bool CanConvertTo(ITypeDescriptorContext context, Type t) + { + return t == typeof(String); + } + public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType) + { + if (destType == typeof(string) && value is SvgMeasurement) + { + return value.ToString(); + } + return base.ConvertTo(context, culture, value, destType); + } + public override bool CanConvertFrom(ITypeDescriptorContext context, Type t) + { + return t == typeof(String); + } + public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object val) + { + SvgMeasurement measurement = new SvgMeasurement((string)val); + return measurement; + } + } +} diff --git a/PROMS/Volian.Svg.Library/SvgPart.cs b/PROMS/Volian.Svg.Library/SvgPart.cs new file mode 100644 index 00000000..2f509335 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgPart.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using System.Xml.Serialization; +using System.Drawing; +using System.ComponentModel; +using System.Collections.Generic; + +namespace Volian.Svg.Library +{ + + [TypeConverter(typeof(ExpandableObjectConverter))] + public abstract partial class SvgPart + { + #region ID + protected string _ID = string.Empty; + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("id")] + public string ID + { + get { return _ID; } + set { _ID = value; } + } + #endregion + //#region Parent and Containing Svg + //// ToDo: MyParent + //private SvgPartGrouping _MyParent; + //[XmlIgnore()] + //public SvgPartGrouping MyParent + //{ + // get { return _MyParent; } + // set { _MyParent = value; } + //} + //// ToDo: MySvg + //private Svg _MySvg; + //public Svg MySvg + //{ + // get { return _MySvg; } + // set { _MySvg = value; } + //} + //internal void SetParent(SvgPartGrouping myParent) + //{ + // _MyParent = myParent; + // _MySvg = myParent.MySvg == null? MyParent : myParent.MySvg; + //} + //#endregion + #region Setup Inheritance + virtual internal void SetupInheritance(SvgInheritedSettings myParentsSettings) { ; } + #endregion + #region Dictionary of Parts + internal virtual void AddLookup(Dictionary lookUp) + { + if (_ID != string.Empty && !lookUp.ContainsKey(_ID)) + lookUp.Add(_ID, this); + } + #endregion + } +} \ No newline at end of file diff --git a/PROMS/Volian.Svg.Library/SvgPartInheritance.cs b/PROMS/Volian.Svg.Library/SvgPartInheritance.cs new file mode 100644 index 00000000..aaf48f24 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgPartInheritance.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgPartInheritance : SvgPart + { + #region InheritedSettings + internal SvgInheritedSettings _MyInheritedSettings = new SvgInheritedSettings(); + [XmlIgnore()] + internal SvgInheritedSettings MyInheritedSettings + { + get { return _MyInheritedSettings; } + set { _MyInheritedSettings = value; } + } + #region Line Settings + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("stroke")] + public string Stroke + { + get { return _MyInheritedSettings.Stroke; } + set { _MyInheritedSettings.Stroke = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("stroke-width")] + public string StrokeWidth + { + get { return _MyInheritedSettings.StrokeWidth; } + set { _MyInheritedSettings.StrokeWidth = value; } + } + #endregion + #region Fill Settings + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("fill")] + public string Fill + { + get { return _MyInheritedSettings.Fill; } + set { _MyInheritedSettings.Fill = value; } + } + #endregion + #region Font Settings + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-family")] + public string FontFamily + { + get { return _MyInheritedSettings.FontFamily; } + set { _MyInheritedSettings.FontFamily = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-size")] + public string FontSize + { + get { return _MyInheritedSettings.FontSize; } + set { _MyInheritedSettings.FontSize = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-style")] + public string SVGFontStyle + { + get { return _MyInheritedSettings.SVGFontStyle; } + set { _MyInheritedSettings.SVGFontStyle = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-weight")] + public string FontWeight + { + get { return _MyInheritedSettings.FontWeight; } + set { _MyInheritedSettings.FontWeight = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("text-decoration")] + public string TextDecoration + { + get { return _MyInheritedSettings.TextDecoration; } + set { _MyInheritedSettings.TextDecoration = value; } + } + #endregion + #endregion + #region Setup Inheritance + override internal void SetupInheritance(SvgInheritedSettings myParentsSettings) + { + _MyInheritedSettings.MyParentsSettings = myParentsSettings; + //_SvgParts.SetupInheritance(_MyInheritedSettings); + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgParts.cs b/PROMS/Volian.Svg.Library/SvgParts.cs new file mode 100644 index 00000000..0b91a63d --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgParts.cs @@ -0,0 +1,394 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Drawing; +using System.ComponentModel; +using System.Text; + +namespace Volian.Svg.Library +{ + /// + /// + /// A collection that stores objects. + /// + /// + /// + [Serializable()] + [TypeConverter(typeof(SVGPartsConverter))] + public partial class SvgParts : CollectionBase, ICustomTypeDescriptor + { + /// Notifies when the collection has been modified. + public event EventHandler OnItemsChanged; + + /// Notifies that an item has been added. + public event SVGPartHandler OnItemAdd; + + /// Notifies that items have been added. + public event SVGPartHandler OnItemsAdd; + + /// Notifies that an item has been removed. + public event SVGPartHandler OnItemRemove; + + /// + /// + /// Initializes a new instance of . + /// + /// + public SvgParts() + { + } + + /// + /// + /// Initializes a new instance of based on another . + /// + /// + /// + /// A from which the contents are copied + /// + public SvgParts(SvgParts value) + { + this.AddRange(value); + } + + /// + /// + /// Initializes a new instance of containing any array of objects. + /// + /// + /// + /// A array of objects with which to intialize the collection + /// + public SvgParts(SvgPart[] value) + { + this.AddRange(value); + } + + /// + /// Represents the entry at the specified index of the . + /// + /// The zero-based index of the entry to locate in the collection. + /// + /// The entry at the specified index of the collection. + /// + /// is outside the valid range of indexes for the collection. + public SvgPart this[int index] + { + get { return ((SvgPart)(List[index])); } + set { List[index] = value; } + } + //#region SetupInheritance + //internal void SetupInheritance(SvgInheritedSettings myParentsSettings) + //{ + // foreach (SvgPart svgPart in List) + // svgPart.SetupInheritance(myParentsSettings); + //} + //#endregion + #region Dictionary of Parts + internal void AddLookup(Dictionary lookUp) + { + foreach (SvgPart svgPart in List) + svgPart.AddLookup(lookUp); + } + #endregion + internal static void ShowException(Exception ex) + { + StringBuilder sb = new StringBuilder(); + string sep = ""; + for (Exception ex1 = ex; ex1 != null; ex1 = ex1.InnerException) + { + sb.Append(sep + string.Format("{0} - {1}", ex1.GetType().Name, ex1.Message)); + sep = "\r\n"; + } + Console.WriteLine(sb); + } + /// + /// Adds a with the specified value to the + /// . + /// + /// The to add. + /// + /// The index at which the new element was inserted. + /// + /// + public int Add(SvgPart value) + { + int ndx = List.Add(value); + if (OnItemAdd != null) { OnItemAdd(this, new SVGPartArgs(value)); } + if (OnItemsChanged != null) { OnItemsChanged(value, EventArgs.Empty); } + return ndx; + } + + /// + /// Copies the elements of an array to the end of the . + /// + /// + /// An array of type containing the objects to add to the collection. + /// + /// + /// None. + /// + /// + public void AddRange(SvgPart[] value) + { + for (int i = 0; i < value.Length; i++) + { + this.Add(value[i]); + } + if (OnItemsAdd != null) { OnItemsAdd(this, new SVGPartArgs(value)); } + if (OnItemsChanged != null) { OnItemsChanged(value, EventArgs.Empty); } + } + + /// + /// + /// Adds the contents of another to the end of the collection. + /// + /// + /// + /// A containing the objects to add to the collection. + /// + /// + /// None. + /// + /// + public void AddRange(SvgParts value) + { + for (int i = 0; i < value.Count; i++) + { + this.Add(value[i]); + } + if (OnItemsAdd != null) { OnItemsAdd(this, new SVGPartArgs(value)); } + if (OnItemsChanged != null) { OnItemsChanged(value, EventArgs.Empty); } + } + + /// + /// Gets a value indicating whether the + /// contains the specified . + /// + /// The to locate. + /// + /// if the is contained in the collection; + /// otherwise, . + /// + /// + public bool Contains(SvgPart value) + { + return List.Contains(value); + } + + /// + /// Copies the values to a one-dimensional instance at the + /// specified index. + /// + /// The one-dimensional that is the destination of the values copied from . + /// The index in where copying begins. + /// + /// None. + /// + /// is multidimensional. -or- The number of elements in the is greater than the available space between and the end of . + /// is . + /// is less than 's lowbound. + /// + public void CopyTo(SvgPart[] array, int index) + { + List.CopyTo(array, index); + } + + /// + /// Returns the index of a in + /// the . + /// + /// The to locate. + /// + /// The index of the of in the + /// , if found; otherwise, -1. + /// + /// + public int IndexOf(SvgPart value) + { + return List.IndexOf(value); + } + + /// + /// Inserts a into the at the specified index. + /// + /// The zero-based index where should be inserted. + /// The to insert. + /// None. + /// + public void Insert(int index, SvgPart value) + { + List.Insert(index, value); + if (OnItemAdd != null) { OnItemAdd(this, new SVGPartArgs(value)); } + if (OnItemsChanged != null) { OnItemsChanged(value, EventArgs.Empty); } + } + + /// + /// Removes a specific from the + /// . + /// + /// The to remove from the . + /// None. + /// is not found in the Collection. + public void Remove(SvgPart value) + { + List.Remove(value); + if (OnItemRemove != null) { OnItemRemove(this, new SVGPartArgs(value)); } + if (OnItemsChanged != null) { OnItemsChanged(value, EventArgs.Empty); } + } + #region ICustomTypeDescriptor impl + public String GetClassName() + { return TypeDescriptor.GetClassName(this, true); } + public AttributeCollection GetAttributes() + { return TypeDescriptor.GetAttributes(this, true); } + public String GetComponentName() + { return TypeDescriptor.GetComponentName(this, true); } + public TypeConverter GetConverter() + { return TypeDescriptor.GetConverter(this, true); } + public EventDescriptor GetDefaultEvent() + { return TypeDescriptor.GetDefaultEvent(this, true); } + public PropertyDescriptor GetDefaultProperty() + { return TypeDescriptor.GetDefaultProperty(this, true); } + public object GetEditor(Type editorBaseType) + { return TypeDescriptor.GetEditor(this, editorBaseType, true); } + public EventDescriptorCollection GetEvents(Attribute[] attributes) + { return TypeDescriptor.GetEvents(this, attributes, true); } + public EventDescriptorCollection GetEvents() + { return TypeDescriptor.GetEvents(this, true); } + public object GetPropertyOwner(PropertyDescriptor pd) + { return this; } + /// + /// Called to get the properties of this type. Returns properties with certain + /// attributes. this restriction is not implemented here. + /// + /// + /// + public PropertyDescriptorCollection GetProperties(Attribute[] attributes) + { return GetProperties(); } + /// + /// Called to get the properties of this type. + /// + /// + public PropertyDescriptorCollection GetProperties() + { + // Create a collection object to hold property descriptors + PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null); + // Iterate the list + for (int i = 0; i < this.Count; i++) + { + // Create a property descriptor for the item and add to the property descriptor collection + SvgPartsPropertyDescriptor pd = new SvgPartsPropertyDescriptor(this, i); + pds.Add(pd); + } + // return the property descriptor collection + return pds; + } + #endregion + + /// Event arguments for the SVGParts collection class. + public class SVGPartArgs : EventArgs + { + private SvgParts t; + + /// Default constructor. + public SVGPartArgs() + { + t = new SvgParts(); + } + + /// Initializes with a SVGPart. + /// Data object. + public SVGPartArgs(SvgPart t) + : this() + { + this.t.Add(t); + } + + /// Initializes with a collection of SVGPart objects. + /// Collection of data. + public SVGPartArgs(SvgParts ts) + : this() + { + this.t.AddRange(ts); + } + + /// Initializes with an array of SVGPart objects. + /// Array of data. + public SVGPartArgs(SvgPart[] ts) + : this() + { + this.t.AddRange(ts); + } + + /// Gets or sets the data of this argument. + public SvgParts SVGParts + { + get { return t; } + set { t = value; } + } + } + + /// SVGParts event handler. + public delegate void SVGPartHandler(object sender, SVGPartArgs e); + #region Property Descriptor + /// + /// Summary description for CollectionPropertyDescriptor. + /// + public partial class SvgPartsPropertyDescriptor : vlnListPropertyDescriptor + { + private SvgPart Item { get { return (SvgPart)_Item; } } + public SvgPartsPropertyDescriptor(SvgParts collection, int index) : base(collection, index) { ;} + public override string DisplayName + { get { return Item.GetType().Name; } } + } + #endregion + [Serializable()] + public partial class vlnListPropertyDescriptor : PropertyDescriptor + { + protected object _Item = null; + public vlnListPropertyDescriptor(System.Collections.IList collection, int index) + : base("#" + index.ToString(), null) + { _Item = collection[index]; } + public override bool CanResetValue(object component) + { return true; } + public override Type ComponentType + { get { return _Item.GetType(); } } + public override object GetValue(object component) + { return _Item; } + public override bool IsReadOnly + { get { return false; } } + public override Type PropertyType + { get { return _Item.GetType(); } } + public override void ResetValue(object component) + { ;} + public override bool ShouldSerializeValue(object component) + { return true; } + public override void SetValue(object component, object value) + { /*_Item = value*/;} + //public override AttributeCollection Attributes + //{ get { return new AttributeCollection(null); } } + public override string DisplayName + { get { return _Item.ToString(); } } + public override string Description + { get { return _Item.ToString(); } } + public override string Name + { get { return _Item.ToString(); } } + + } // Class + #region Converter + internal class SVGPartsConverter : ExpandableObjectConverter + { + public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType) + { + if (destType == typeof(string) && value is SvgParts) + { + // Return department and department role separated by comma. + return ((SvgParts)value).List.Count.ToString() + " SVG Drawing Parts"; + } + return base.ConvertTo(context, culture, value, destType); + } + } + #endregion + + } +} \ No newline at end of file diff --git a/PROMS/Volian.Svg.Library/SvgRectangle.cs b/PROMS/Volian.Svg.Library/SvgRectangle.cs new file mode 100644 index 00000000..d406f224 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgRectangle.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgRectangle : SvgShapePart + { + #region ctor + public SvgRectangle() { ;} + public SvgRectangle(PointF location, SizeF size, Color fillColor, Color lineColor, float lineWidth) + { + _X.Value = location.X; + _Y.Value = location.Y; + _Width.Value = size.Width; + _Height.Value = size.Height; + FillColor = fillColor; + LineColor = lineColor; + LineWidth = new SvgMeasurement(lineWidth); + } + //XMLElementAttribute(ElementName = "PREFIX", IsNullable = false) + #endregion + #region Location + private SvgMeasurement _X = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement X + { + get { return _X; } + set { _X = value; } + } + [XmlAttribute("x")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_X); } + set { _X = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Y = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Y + { + get { return _Y; } + set { _Y = value; } + } + [XmlAttribute("y")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Y_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Y); } + set { _Y = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Size + private SvgMeasurement _Width = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Width + { + get { return _Width; } + set { _Width = value; } + } + [XmlAttribute("width")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Width_WidthmlSurrogate + { + get { return SvgXmlConverter.GetString(_Width); } + set { _Width = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Height = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Height + { + get { return _Height; } + set { _Height = value; } + } + [XmlAttribute("height")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Height_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Height); } + set { _Height = SvgXmlConverter.GetObject(value); } + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgRtf.cs b/PROMS/Volian.Svg.Library/SvgRtf.cs new file mode 100644 index 00000000..c3996d1f --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgRtf.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Xml.Serialization; +using System.ComponentModel; + +namespace Volian.Svg.Library +{ + public partial class SvgRtf : SvgShapePart + { + #region ctor + public SvgRtf() { ;} + public SvgRtf(PointF location, float width, string rtf, Font font, Color fillColor) + { + _X.Value = location.X; + _Y.Value = location.Y; + _Width.Value = width; + Rtf = rtf; + Font = font; + FillColor = fillColor; + LineColor = Color.Empty; + LineWidth = new SvgMeasurement(0); + } + #endregion + #region Location + private SvgMeasurement _X = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement X + { + get { return _X; } + set { _X = value; } + } + [XmlAttribute("x")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_X); } + set { _X = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Y = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Y + { + get { return _Y; } + set { _Y = value; } + } + [XmlAttribute("y")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Y_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Y); } + set { _Y = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Size + private SvgMeasurement _Width = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Width + { + get { return _Width; } + set { _Width = value; } + } + [XmlAttribute("width")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Width_WidthmlSurrogate + { + get { return SvgXmlConverter.GetString(_Width); } + set { _Width = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Height = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Height + { + get { return _Height; } + set { _Height = value; } + } + [XmlAttribute("height")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Height_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Height); } + set { _Height = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Rtf + private string _Rtf; + [XmlText()] + public string Rtf + { + get { return _Rtf; } + set { _Rtf = value; } + } + #endregion + #region Required Extensions + private string _RequiredExtensions = "http://Volian.Com/EmbeddedRTF"; + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + [XmlAttribute("requiredExtensions")] + public string RequiredExtensions + { + get { return _RequiredExtensions; } + set { _RequiredExtensions = value; } + } + #endregion + #region Font Settings + private SvgFontSettings _MyFontSettings = new SvgFontSettings(); + [XmlIgnore] + public Font Font + { + get { return _MyFontSettings.Font; } + set { _MyFontSettings.Font = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-family")] + public string FontFamily + { + get { return _MyFontSettings.FontFamily; } + set { _MyFontSettings.FontFamily = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-size")] + public string FontSize + { + get { return _MyFontSettings.FontSize; } + set { _MyFontSettings.FontSize = value; } + } + [System.ComponentModel.DefaultValueAttribute("normal")] + [XmlAttribute("font-style")] + public string SVGFontStyle + { + get { return _MyFontSettings.SVGFontStyle; } + set { _MyFontSettings.SVGFontStyle = value; } + } + [System.ComponentModel.DefaultValueAttribute("normal")] + [XmlAttribute("font-weight")] + public string FontWeight + { + get { return _MyFontSettings.FontWeight; } + set { _MyFontSettings.FontWeight = value; } + } + [System.ComponentModel.DefaultValueAttribute("normal")] + [XmlAttribute("text-decoration")] + public string TextDecoration + { + get { return _MyFontSettings.TextDecoration; } + set { _MyFontSettings.TextDecoration = value; } + } + #endregion + + #region Setup Inheritance + override internal void SetupInheritance(SvgInheritedSettings myParentsSettings) + { + base.SetupInheritance(myParentsSettings); + _MyFontSettings.MyParentsSettings = myParentsSettings.MyFontSettings; + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgScale.cs b/PROMS/Volian.Svg.Library/SvgScale.cs new file mode 100644 index 00000000..db3d23c5 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgScale.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public class SvgScale + { + private float _XLowerLimit; + public float XLowerLimit + { + get { return _XLowerLimit; } + set { _XLowerLimit = value; } + } + private float _YLowerLimit; + public float YLowerLimit + { + get { return _YLowerLimit; } + set { _YLowerLimit = value; } + } + private float _Scale; // Scale from Logical to Physical + public float Scale + { get { return _Scale; } } + private float _Width; // Physical Width + public float Width + { get { return _Width; } } + private float _Height; // Physical Height + public float Height + { get { return _Height; } } + public float XUpperLimit + { get { return _Width * _Scale + _XLowerLimit; } } + public float YUpperLimit + { get { return _Height * _Scale + _YLowerLimit; } } + private float _DPI; + public float DPI + { get { return _DPI; } } + private SvgScale(SvgScale tmp,SvgMeasurement newX, SvgMeasurement newY) + { + _DPI = tmp.DPI; + _Scale = tmp.Scale; + _Width = tmp.Width; + _Height = tmp.Height; + _XLowerLimit = tmp.XLowerLimit - newX.GetSizeInPixels(DPI); + _YLowerLimit = tmp.YLowerLimit - newY.GetSizeInPixels(DPI); + } + public SvgScale(float myDPI, RectangleF bounds,SvgMeasurement width, SvgMeasurement height, SvgViewBox myViewBox) + { + // TODO: Should I convert to pixels or something fixed (Inches, Points, Picas, CMs, MMs, Twips) + _DPI = myDPI; + float outputWidth = width.GetSizeInPixels(100); + if(outputWidth == 0) outputWidth = bounds.Width; + float outputHeight = height.GetSizeInPixels(100); + if (outputHeight == 0) outputHeight = bounds.Height; + float inputWidth = myViewBox.Width; + if (inputWidth == 0) inputWidth = outputWidth; + float scalex = outputWidth / inputWidth; + float inputHeight = myViewBox.Height; + if (inputHeight == 0) inputHeight = outputHeight; + float scaley = outputHeight / inputHeight; + _Scale = (scalex < scaley) ? scaley : scalex; + //Console.WriteLine("'Scale',{0}",_Scale); + _Width = outputWidth / _Scale; + _Height = outputHeight / _Scale; + _XLowerLimit = myViewBox.X + (inputWidth - _Width) / 2; + _YLowerLimit = myViewBox.Y + (inputHeight - _Height) / 2; + //if (myDPI != 96) Console.WriteLine("DPI {0}", myDPI); + //Console.WriteLine("Scale,{0},{1},{2},{3},{4},{5},{6}", Scale, Width, Height, XLowerLimit, XUpperLimit, YLowerLimit, YUpperLimit); + } + public SvgScale AdjustOrigin(SvgMeasurement newX, SvgMeasurement newY) + { + return new SvgScale(this, newX, newY); + } + public float X(float x) + { + return _Scale * (x - XLowerLimit); + } + public float Y(float y) + { + return _Scale * (y - YLowerLimit); + } + public float Y(iTextSharp.text.pdf.PdfContentByte cb, float y) + { + return cb.PdfDocument.PageSize.Height - _Scale * (y - YLowerLimit); + } + public float M(float m) + { + return _Scale * m; + } + public float X(SvgMeasurement x) + { + float retval = _Scale * (x.GetSizeInPixels(DPI) - XLowerLimit); + return retval; + } + public float Y(SvgMeasurement y) + { + return _Scale * (y.GetSizeInPixels(DPI) - YLowerLimit); + } + public float Y(iTextSharp.text.pdf.PdfContentByte cb, SvgMeasurement y) + { + float yOffset = 0; // = (cb.PdfWriter.CurrentPageNumber -1) * .8F * cb.PdfDocument.PageSize.Height; + float myY = yOffset + cb.PdfDocument.PageSize.Height - _Scale * (y.GetSizeInPixels(DPI) - YLowerLimit); + if (myY < .1F * cb.PdfDocument.PageSize.Height) + { + cb.PdfDocument.NewPage(); + myY += .8F * cb.PdfDocument.PageSize.Height; + } + return myY; + } + public float YY(SvgMeasurement y) + { + return -_Scale * y.GetSizeInPixels(DPI); + } + public float M(SvgMeasurement m) + { + return _Scale * m.GetSizeInPixels(DPI); + } + } +} diff --git a/PROMS/Volian.Svg.Library/SvgSerializer.cs b/PROMS/Volian.Svg.Library/SvgSerializer.cs new file mode 100644 index 00000000..c04a7754 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgSerializer.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.Xml; +using System.Xml.Schema; +using System.IO; + +namespace Volian.Svg.Library +{ + public static class SvgSerializer where T : class + { + public static string StringSerialize(T t) + { + string strOutput = string.Empty; + XmlSerializer xs = new XmlSerializer(typeof(T),"http://www.w3.org/2000/svg"); + using (MemoryStream ms = new MemoryStream()) + { + xs.Serialize( new NonXsiTextWriter(ms,Encoding.UTF8), t); + //xs.Serialize(ms, t); + ms.Position = 0; + StreamReader sr = new StreamReader(ms); + strOutput = sr.ReadToEnd(); + ms.Close(); + } + return strOutput; + } + public static T StringDeserialize(string s) + { + T t; + XmlSerializer xs = new XmlSerializer(typeof(T), "http://www.w3.org/2000/svg"); + UTF8Encoding enc = new UTF8Encoding(); + Byte[] arrBytData = enc.GetBytes(s); + using (MemoryStream ms = new MemoryStream(arrBytData)) + { + t = (T)xs.Deserialize(ms); + } + return t; + } + public static void WriteFile(T t, string fileName) + { + string strOutput = string.Empty; + XmlSerializer xs = new XmlSerializer(typeof(T), "http://www.w3.org/2000/svg"); + using (FileStream fs = new FileStream(fileName, FileMode.Create)) + { + xs.Serialize(new NonXsiTextWriter(fs,Encoding.UTF8), t); + fs.Close(); + } + } + public static T ReadFile(string fileName) + { + T t; + XmlSerializer xs = new XmlSerializer(typeof(T), "http://www.w3.org/2000/svg"); + using (FileStream fs = new FileStream(fileName, FileMode.Open)) + { + t = (T)xs.Deserialize(fs); + } + return t; + } + } + public class NonXsiTextWriter : XmlTextWriter + { + public NonXsiTextWriter( TextWriter w ) : base( w ) {} + public NonXsiTextWriter( Stream w, Encoding encoding ) : base( w, encoding ) { + this.Formatting = Formatting.Indented; + } + public NonXsiTextWriter( string filename, Encoding encoding ) : base( filename, encoding ) {} + bool _skip = false; + public override void WriteStartAttribute( string prefix, string localName, string ns ) + { + if ((prefix == "xmlns" && (localName == "xsd" || localName == "xsi")) || // Omits XSD and XSI declarations. + ns == XmlSchema.InstanceNamespace ) // Omits all XSI attributes. + { + _skip = true; + return; + } + if(localName == "xlink_href") + base.WriteStartAttribute(prefix, "xlink:href", ns); + else + base.WriteStartAttribute( prefix, localName, ns ); + } + public override void WriteString( string text ) { + if ( _skip ) return; + base.WriteString( text ); + } + public override void WriteEndAttribute() + { + if ( _skip ) + { // Reset the flag, so we keep writing. + _skip = false; + return; + } + base.WriteEndAttribute(); + } + } +} diff --git a/PROMS/Volian.Svg.Library/SvgShapePart.cs b/PROMS/Volian.Svg.Library/SvgShapePart.cs new file mode 100644 index 00000000..58b4d427 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgShapePart.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; +using System.Xml.Serialization; +using System.Drawing; +using System.ComponentModel; + +namespace Volian.Svg.Library +{ + [TypeConverter(typeof(ExpandableObjectConverter))] + public abstract class SvgShapePart : SvgLinePart + { + #region Fill Settings + protected SvgFillSettings _MyFillSettings = new SvgFillSettings(); + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("fill")] + public string Fill + { + get { return _MyFillSettings.Fill; } + set { _MyFillSettings.Fill = value; } + } + [XmlIgnore] + protected Color FillColor + { + get { return _MyFillSettings.FillColor; } + set { _MyFillSettings.FillColor = value; } + } + #endregion + #region Setup Inheritance + override internal void SetupInheritance(SvgInheritedSettings myParentsSettings) + { + base.SetupInheritance(myParentsSettings); + _MyFillSettings.MyParentsSettings = myParentsSettings.MyFillSettings; + } + #endregion + } +} \ No newline at end of file diff --git a/PROMS/Volian.Svg.Library/SvgTest.cs b/PROMS/Volian.Svg.Library/SvgTest.cs new file mode 100644 index 00000000..a0eaaab2 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgTest.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.Drawing; + +namespace Volian.Svg.Library +{ + [XmlRoot("svg-test")] + public class SvgTest + { + public SvgTest() + { + _MyLineSettings = new SvgLineSettings(Color.Red,new SvgMeasurement(3, E_MeasurementUnits.PX)); + _MyFillSettings = new SvgFillSettings(Color.Cyan); + _MyFontSettings = new SvgFontSettings(new Font("Cambria", 20, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline, GraphicsUnit.Point)); + } + #region Line Settings + private SvgLineSettings _MyLineSettings; + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("stroke")] + public string Stroke + { + get { return _MyLineSettings.Stroke; } + set { _MyLineSettings.Stroke = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("stroke-width")] + public string StrokeWidth + { + get { return _MyLineSettings.StrokeWidth; } + set { _MyLineSettings.StrokeWidth = value; } + } + #endregion + #region Fill Settings + private SvgFillSettings _MyFillSettings; + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("fill")] + public string Fill + { + get { return _MyFillSettings.Fill; } + set { _MyFillSettings.Fill = value; } + } + #endregion + #region Font Settings + private SvgFontSettings _MyFontSettings; + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-family")] + public string FontFamily + { + get { return _MyFontSettings.FontFamily; } + set { _MyFontSettings.FontFamily = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-size")] + public string FontSize + { + get { return _MyFontSettings.FontSize; } + set { _MyFontSettings.FontSize = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-style")] + public string SVGFontStyle + { + get { return _MyFontSettings.SVGFontStyle; } + set { _MyFontSettings.SVGFontStyle = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-weight")] + public string FontWeight + { + get { return _MyFontSettings.FontWeight; } + set { _MyFontSettings.FontWeight = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("text-decoration")] + public string TextDecoration + { + get { return _MyFontSettings.TextDecoration; } + set { _MyFontSettings.TextDecoration = value; } + } + #endregion + } +} diff --git a/PROMS/Volian.Svg.Library/SvgText.cs b/PROMS/Volian.Svg.Library/SvgText.cs new file mode 100644 index 00000000..6545c6f1 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgText.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Xml.Serialization; +using System.ComponentModel; + +namespace Volian.Svg.Library +{ + public partial class SvgText : SvgShapePart + { + #region ctor + public SvgText() { ;} + public SvgText(PointF location, string text, Font font, Color fillColor) + { + _X.Value = location.X; + _Y.Value = location.Y; + Text = text; + Font = font; + FillColor = fillColor; + LineColor = Color.Empty; + LineWidth = new SvgMeasurement(0); + } + #endregion + #region Location + private SvgJustify _Justify = SvgJustify.Left; + [XmlAttribute("Justify")] + public SvgJustify Justify + { + get { return _Justify; } + set { _Justify = value; } + } + private SvgMeasurement _X = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement X + { + get { return _X; } + set { _X = value; } + } + [XmlAttribute("x")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_X); } + set { _X = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Y = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Y + { + get { return _Y; } + set { _Y = value; } + } + [XmlAttribute("y")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Y_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Y); } + set { _Y = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Text + private string _Text; + [XmlText()] + public string Text + { + get { return _Text; } + set { _Text = value; } + } + #endregion + #region Font Settings + private SvgFontSettings _MyFontSettings = new SvgFontSettings(); + [XmlIgnore] + public Font Font + { + get + { + if (_MyFontSettings.Font.Size < 10) + Console.WriteLine("It didn't work!"); + return _MyFontSettings.Font; + } + set + { + if (value.Size < 10) + Console.WriteLine("It didn't work!"); + _MyFontSettings.Font = value; + } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-family")] + public string FontFamily + { + get { return _MyFontSettings.FontFamily; } + set { _MyFontSettings.FontFamily = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-size")] + public string FontSize + { + get { return _MyFontSettings.FontSize; } + set { _MyFontSettings.FontSize = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-style")] + public string SVGFontStyle + { + get { return _MyFontSettings.SVGFontStyle; } + set { _MyFontSettings.SVGFontStyle = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("font-weight")] + public string FontWeight + { + get { return _MyFontSettings.FontWeight; } + set { _MyFontSettings.FontWeight = value; } + } + [System.ComponentModel.DefaultValueAttribute("")] + [XmlAttribute("text-decoration")] + public string TextDecoration + { + get { return _MyFontSettings.TextDecoration; } + set { _MyFontSettings.TextDecoration = value; } + } + #endregion + #region Setup Inheritance + override internal void SetupInheritance(SvgInheritedSettings myParentsSettings) + { + base.SetupInheritance(myParentsSettings); + _MyFontSettings.MyParentsSettings = myParentsSettings.MyFontSettings; + } + #endregion + public override string ToString() + { + return string.Format("({0}, {1}) - '{2}'",this.X,this.Y,this.Text); + } + } + public enum SvgJustify + { + Left = 0, + Center = 1, + Right =2 + } +} diff --git a/PROMS/Volian.Svg.Library/SvgUse.cs b/PROMS/Volian.Svg.Library/SvgUse.cs new file mode 100644 index 00000000..26054bef --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgUse.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml.Serialization; +using System.ComponentModel; +using System.Drawing; + +namespace Volian.Svg.Library +{ + public partial class SvgUse : SvgPartInheritance + { + #region ctor + public SvgUse() { ;} + public SvgUse(PointF location, SizeF size, string id) + { + _X.Value = location.X; + _Y.Value = location.Y; + _Width.Value = size.Width; + _Height.Value = size.Height; + _ID = id; + } + #endregion + #region Location + private SvgMeasurement _X = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement X + { + get { return _X; } + set { _X = value; } + } + [XmlAttribute("x")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string X_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_X); } + set { _X = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Y = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Y + { + get { return _Y; } + set { _Y = value; } + } + [XmlAttribute("y")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Y_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Y); } + set { _Y = SvgXmlConverter.GetObject(value); } + } + #endregion + #region Size + private SvgMeasurement _Width = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Width + { + get { return _Width; } + set { _Width = value; } + } + [XmlAttribute("width")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Width_WidthmlSurrogate + { + get { return SvgXmlConverter.GetString(_Width); } + set { _Width = SvgXmlConverter.GetObject(value); } + } + private SvgMeasurement _Height = new SvgMeasurement(); + [XmlIgnore] + public SvgMeasurement Height + { + get { return _Height; } + set { _Height = value; } + } + [XmlAttribute("height")] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public string Height_XmlSurrogate + { + get { return SvgXmlConverter.GetString(_Height); } + set { _Height = SvgXmlConverter.GetObject(value); } + } + #endregion + #region ID to be used + private string _UseID = null; + [XmlAttribute(AttributeName = "href", Namespace = "http://www.w3.org/1999/xlink")] + public string UseID + { + get { return _UseID; } + set { _UseID = value; } + } + #endregion + #region Setup Inheritance + override internal void SetupInheritance(SvgInheritedSettings myParentsSettings) + { + _MyInheritedSettings.MyParentsSettings = myParentsSettings; + } + #endregion + public override string ToString() + { + return string.Format("({0}, {1}) Template '{2}'", this.X, this.Y, this.UseID); + } + } +} diff --git a/PROMS/Volian.Svg.Library/SvgViewBox.cs b/PROMS/Volian.Svg.Library/SvgViewBox.cs new file mode 100644 index 00000000..1098d550 --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgViewBox.cs @@ -0,0 +1,79 @@ + using System; + using System.Collections.Generic; + using System.Text; + using System.Drawing; + using System.ComponentModel; + +namespace Volian.Svg.Library +{ + [TypeConverter(typeof(ViewBoxTypeConverter))] + public class SvgViewBox + { + #region ctor + public SvgViewBox() { ;} + public SvgViewBox(float x, float y, float width, float height) + { + _MyRectangleF = new RectangleF(x, y, width, height); + } + public SvgViewBox(string val) + { + string[] parms = ((string)val).Split(' '); + _MyRectangleF = new RectangleF(Convert.ToSingle(parms[0]), Convert.ToSingle(parms[1]), Convert.ToSingle(parms[2]), Convert.ToSingle(parms[3])); + } + #endregion + #region Public Properties + private RectangleF _MyRectangleF = new RectangleF(); + public float X + { + get { return _MyRectangleF.X; } + set { _MyRectangleF.X = value; } + } + public float Y + { + get { return _MyRectangleF.Y; } + set { _MyRectangleF.Y = value; } + } + public float Width + { + get { return _MyRectangleF.Width; } + set { _MyRectangleF.Width = value; } + } + public float Height + { + get { return _MyRectangleF.Height; } + set { _MyRectangleF.Height = value; } + } + #endregion + #region ToString + public override string ToString() + { + return string.Format("{0} {1} {2} {3}", X, Y, Width, Height); + } + #endregion + } + public class ViewBoxTypeConverter : ExpandableObjectConverter + { + public override bool CanConvertTo(ITypeDescriptorContext context, Type t) + { + return t == typeof(String); + } + public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType) + { + if (destType == typeof(string) && value is SvgViewBox) + { + return value.ToString(); + } + return base.ConvertTo(context, culture, value, destType); + } + public override bool CanConvertFrom(ITypeDescriptorContext context, Type t) + { + return t == typeof(String); + } + public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object val) + { + string[] parms = ((string)val).Split(' '); + SvgViewBox viewBox = new SvgViewBox(Convert.ToSingle(parms[0]), Convert.ToSingle(parms[1]), Convert.ToSingle(parms[2]), Convert.ToSingle(parms[3])); + return viewBox; + } + } +} diff --git a/PROMS/Volian.Svg.Library/SvgXMLConverter.cs b/PROMS/Volian.Svg.Library/SvgXMLConverter.cs new file mode 100644 index 00000000..815e7d9d --- /dev/null +++ b/PROMS/Volian.Svg.Library/SvgXMLConverter.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.ComponentModel; + +namespace Volian.Svg.Library +{ + public static class SvgXmlConverter where T : class + { + public static string GetString(T myObject) + { + if (myObject == null) return null; + TypeConverter FontConverter = TypeDescriptor.GetConverter(typeof(T)); + return FontConverter.ConvertToInvariantString(myObject); + } + public static T GetObject(string value) + { + TypeConverter converter = TypeDescriptor.GetConverter(typeof(T)); + return (T)converter.ConvertFromInvariantString(value); + } + } +} diff --git a/PROMS/Volian.Svg.Library/Volian.Svg.Library.csproj b/PROMS/Volian.Svg.Library/Volian.Svg.Library.csproj new file mode 100644 index 00000000..bce6ab8b --- /dev/null +++ b/PROMS/Volian.Svg.Library/Volian.Svg.Library.csproj @@ -0,0 +1,111 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {293911B5-C602-483F-A97F-F962EEFB3CAE} + Library + Properties + Volian.Svg.Library + Volian.Svg.Library + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + x86 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + bin\x86\Debug\ + DEBUG;TRACE + full + x86 + prompt + + + bin\x86\Release\ + TRACE + true + pdbonly + x86 + prompt + + + + False + ..\..\..\Downloads\Software\CodeProject\RtfConverter\Interpreter\bin\Debug\Itenso.Rtf.Interpreter.dll + + + False + ..\..\..\Downloads\Software\CodeProject\RtfConverter\Parser\bin\Debug\Itenso.Rtf.Parser.dll + + + False + ..\..\..\Downloads\Software\PDF\iText\itextsharp-4.1.2-dll\itextsharp.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/PROMS/Volian.Svg.Library/iTextSharp.cs b/PROMS/Volian.Svg.Library/iTextSharp.cs new file mode 100644 index 00000000..b955de29 --- /dev/null +++ b/PROMS/Volian.Svg.Library/iTextSharp.cs @@ -0,0 +1,1053 @@ +using System; +using System.Collections; +using System.Text; +using System.Collections.Generic; +//using System.Text; +using System.Xml.Serialization; +using iTextSharp.text; +using iTextSharp.text.pdf; +using iTextSharp.text.factories; +using Itenso.Rtf; +using Itenso.Rtf.Parser; +using Itenso.Rtf.Interpreter; +//using Itenso.Rtf.Model; +using Itenso.Rtf.Support; +using Microsoft.Win32; + +namespace Volian.Svg.Library +{ + public static class VolianPdf + { + public static int PageCount(string fileName) + { + PdfReader pdfr = new PdfReader(fileName); + int retval = pdfr.NumberOfPages; + pdfr.Close(); + return retval; + } + } + internal static class SvgITextLibrary + { + public static void StrokeAndFill(PdfContentByte cb, bool stroke, bool fill) + { + switch ((stroke ? 1 : 0) + (fill ? 2 : 0)) + { + case 0: // No Stroke or Fill + break; + case 1: // Stroke + cb.Stroke(); + break; + case 2: // Fill + cb.Fill(); + break; + case 3: // Stroke and Fill + cb.FillStroke(); + break; + } + } + } + public abstract partial class SvgPart + { + public abstract void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent); + public abstract void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent); + } + public partial class SvgPartInheritance : SvgPart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + } + } + // TODO: Pages + // TODO: Page Background + public delegate string SvgProcessTextEvent(object sender, SvgProcessTextArgs args); + public class SvgProcessTextArgs + { + private string _MyText; + public string MyText + { + get { return _MyText; } + } + public SvgProcessTextArgs(string myText) + { + _MyText = myText; + } + } + public partial class Svg : SvgGroup + { + public event SvgProcessTextEvent ProcessText; + internal string OnProcessText(string myText) + { + if (ProcessText != null) + return ProcessText(this, new SvgProcessTextArgs(myText)); + return myText; + } + private Dictionary _Templates; + public PdfTemplate GetTemplate(string name, PdfContentByte cb) + { + if (!_Templates.ContainsKey(name)) + { + PdfTemplate tmp = cb.CreateTemplate(100, 100); + _Templates.Add(name, tmp); + //tmp.BoundingBox = new iTextSharp.text.Rectangle(-200,-200,700,200); + } + return _Templates[name]; + } + public static void FixBoundingBox(PdfTemplate tmp, float xll, float yll, float xul, float yul) + { + float left = tmp.BoundingBox.Left; + float bottom = tmp.BoundingBox.Bottom; + float right = tmp.BoundingBox.Right; + float top = tmp.BoundingBox.Top; + left = (xll < xul ? (xll < left ? xll : left) : (xul < left ? xul : left)); + right = (xll > xul ? (xll > right ? xll : right) : (xul > right ? xul : right)); + bottom = (yll < yul ? (yll < bottom ? yll : bottom) : (yul < bottom ? yul : bottom)); + top = (yll > yul ? (yll > top ? yll : top) : (yul > top ? yul : top)); + tmp.BoundingBox = new iTextSharp.text.Rectangle(xll, yll, xul, yul); + } + private PdfContentByte _MyContentByte; + [XmlIgnore] + public PdfContentByte MyContentByte + { + get { return _MyContentByte; } + set { _MyContentByte = value; } + } + private float _LeftMargin = 0; + public float LeftMargin + { + get { return _LeftMargin; } + set { _LeftMargin = value; } + } + private float _TopMargin = 0; + public float TopMargin + { + get { return _TopMargin; } + set { _TopMargin = value; } + } + public void Draw(PdfContentByte cb) + { + //myPdf.Clear(); + _MyContentByte = cb; + _Templates = new Dictionary(); + //RegisterFonts(); + SvgScale scale = new SvgScale(72, new System.Drawing.RectangleF(0, 0, cb.PdfWriter.PageSize.Width, cb.PdfWriter.PageSize.Height), _Width, _Height, _ViewBox); + scale.XLowerLimit-=_LeftMargin; + scale.YLowerLimit -= _TopMargin; + SvgParts.Draw(cb, scale, this, this); //72 - Points + } + public static iTextSharp.text.Font GetFont(string fontName) + { + RegisterFont(fontName); + return FontFactory.GetFont(fontName); + } + //private void RegisterFonts() + //{ + // //if (!FontFactory.IsRegistered("Arial")) + // //{ + // // RegisterFont("Prestige Elite Tall"); + // //} + //} + public static void RegisterFont(string fontName) + { + if (!FontFactory.IsRegistered(fontName)) + { + foreach (string name in FontKey.GetValueNames()) + { + if (name.StartsWith(fontName)) + { + string fontFile = (string) FontKey.GetValue(name); + FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile); + } + } + } + } + private static RegistryKey _FontKey = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows NT").OpenSubKey("CurrentVersion").OpenSubKey("Fonts"); + public static RegistryKey FontKey + { get { return _FontKey; } } + private static string _FontFolder = (String)Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("Shell Folders").GetValue("Fonts"); + public static string FontFolder + { get { return _FontFolder; } } + private static int _StackTraceSkip = 1; + private static DateTime _LastTime = DateTime.Now; + private static DateTime _StartTime = DateTime.Now; + public static void ResetWhen() + { + _StartTime = _LastTime = DateTime.Now; + } + public static void WhenAndWhere() + { + DateTime thisTime = DateTime.Now; + System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(_StackTraceSkip, true); + Console.WriteLine("{0:000000},{1:000000},'{2}',{3},'{4}'", TimeSpan.FromTicks(thisTime.Ticks - _StartTime.Ticks).TotalMilliseconds, + TimeSpan.FromTicks(thisTime.Ticks - _LastTime.Ticks).TotalMilliseconds, sf.GetMethod().Name, sf.GetFileLineNumber() + ,sf.GetFileName()); + _LastTime = thisTime; + } + } + public partial class SvgArc : SvgShapePart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + cb.SaveState(); + SetupInheritance(myParent.MyInheritedSettings); + if (FillColor != System.Drawing.Color.Transparent) + cb.SetColorFill(new Color(FillColor)); + if (OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty) + { + cb.SetLineWidth(scale.M(OutlineWidth)); + cb.SetColorStroke(new Color(OutlineColor)); + } + cb.Ellipse(scale.X(CX - Radius), scale.Y(cb, CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); + SvgITextLibrary.StrokeAndFill(cb, (OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty), (FillColor != System.Drawing.Color.Transparent)); + cb.RestoreState(); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + tmp.SaveState(); + SetupInheritance(myParent.MyInheritedSettings); + if (FillColor != System.Drawing.Color.Transparent) + tmp.SetColorFill(new Color(FillColor)); + if (OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty) + { + tmp.SetLineWidth(scale.M(OutlineWidth)); + tmp.SetColorStroke(new Color(OutlineColor)); + } + tmp.Ellipse(scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); + SvgITextLibrary.StrokeAndFill(tmp, (OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty), (FillColor != System.Drawing.Color.Transparent)); + tmp.RestoreState(); + } + } + public partial class SvgCircle : SvgShapePart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + cb.SaveState(); + bool fill = FillColor != System.Drawing.Color.Transparent; + bool stroke = OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty; + if (fill) + cb.SetColorFill(new Color(FillColor)); + if (stroke) + { + cb.SetLineWidth(scale.M(OutlineWidth)); + cb.SetColorStroke(new Color(OutlineColor)); + } + float x1 = scale.X(CX - Radius); + float y1 = scale.Y(cb, CY - Radius); + float x2 = scale.X(CX + Radius); + float y2 = scale.Y(cb, CY + Radius); + cb.Ellipse(x1, y1, x2, y2); + SvgITextLibrary.StrokeAndFill(cb, stroke, fill); + cb.RestoreState(); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + tmp.SaveState(); + bool fill = FillColor != System.Drawing.Color.Transparent; + bool stroke = OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty; + if (fill) + tmp.SetColorFill(new Color(FillColor)); + if (stroke) + { + tmp.SetLineWidth(scale.M(OutlineWidth)); + tmp.SetColorStroke(new Color(OutlineColor)); + } + tmp.Ellipse(scale.X(CX - Radius), scale.Y(CY - Radius), scale.X(CX + Radius), scale.Y(CY + Radius)); + SvgITextLibrary.StrokeAndFill(tmp, stroke, fill); + tmp.RestoreState(); + Svg.FixBoundingBox(tmp, scale.X(CX - Radius), scale.Y(CY - Radius), scale.X(CX + Radius), scale.Y(CY + Radius)); + } + } + public partial class SvgDefine : SvgGroup + { + public override void Draw(PdfContentByte cb, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) + { + // TODO: Could I try creating a template + // Don't draw + // SvgParts.Draw(cb, myScale, mySvg, myParent); + PdfTemplate tmp = null; + if (ID != "") tmp = mySvg.GetTemplate(ID, cb); // the size will be adjusted as items are added. + SvgParts.Draw(tmp, myScale, mySvg, myParent); + } + public override void Draw(PdfTemplate tmp, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) + { + // TODO: Could I try creating a template + // Don't draw + // SvgParts.Draw(cb, myScale, mySvg, myParent); + //PdfTemplate tmp2 = mySvg.GetTemplate(ID); // the size will be adjusted as items are added. + //SvgParts.Draw(tmp2, myScale, mySvg, myParent); + } + } + public partial class SvgUse : SvgPartInheritance + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + // TODO: Could I use the template + SetupInheritance(myParent.MyInheritedSettings); + mySvg[_UseID].Draw(cb, scale.AdjustOrigin(X, Y), mySvg, this); + //cb.AddTemplate(mySvg.GetTemplate(_UseID.Substring(1),cb), scale.X(X), scale.Y(cb, Y)); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + // TODO: Could I use the template + SetupInheritance(myParent.MyInheritedSettings); + //mySvg[_UseID.Substring(1)].Draw(tmp, scale.AdjustOrigin(X, Y), mySvg, this); + mySvg[_UseID].Draw(tmp, scale.AdjustOrigin(X, Y), mySvg, this); + } + } + public partial class SvgEllipse : SvgShapePart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + cb.SaveState(); + bool fill = FillColor != System.Drawing.Color.Transparent; + bool stroke = OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty; + if (fill) + cb.SetColorFill(new Color(FillColor)); + if (stroke) + { + cb.SetLineWidth(scale.M(OutlineWidth)); + cb.SetColorStroke(new Color(OutlineColor)); + } + cb.Ellipse(scale.X(CX - RX), scale.Y(cb, CY - RY), scale.X(CX + RX), scale.Y(cb, CY + RY)); + SvgITextLibrary.StrokeAndFill(cb, stroke, fill); + cb.RestoreState(); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + tmp.SaveState(); + bool fill = FillColor != System.Drawing.Color.Transparent; + bool stroke = OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty; + if (fill) + tmp.SetColorFill(new Color(FillColor)); + if (stroke) + { + tmp.SetLineWidth(scale.M(OutlineWidth)); + tmp.SetColorStroke(new Color(OutlineColor)); + } + tmp.Ellipse(scale.X(CX - RX), scale.Y(CY - RY), scale.X(CX + RX), scale.Y(CY + RY)); + SvgITextLibrary.StrokeAndFill(tmp, stroke, fill); + tmp.RestoreState(); + } + } + public partial class SvgGroup : SvgPartInheritance + { + public override void Draw(PdfContentByte cb, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + _SvgParts.Draw(cb, myScale, mySvg, this); + } + public override void Draw(PdfTemplate tmp, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + if (ID != "") tmp = mySvg.GetTemplate(ID, mySvg.MyContentByte); // the size will be adjusted as items are added. + SvgParts.Draw(tmp, myScale, mySvg, this); + } + } + public partial class SvgImage : SvgPart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + cb.SaveState(); + iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(ImagePath); + //Console.WriteLine("Before ScaledHeight = {0}", img.ScaledHeight); + if (Height.Value != 0) + img.ScaleAbsoluteHeight(scale.M(Height)); + if (Width.Value != 0) + img.ScaleAbsoluteWidth(scale.M(Width)); + //Console.WriteLine("After ScaledHeight = {0}", img.ScaledHeight); + img.SetAbsolutePosition(scale.X(X), scale.Y(cb, Y) - scale.M(img.ScaledHeight)); + cb.AddImage(img); + cb.RestoreState(); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + tmp.SaveState(); + iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(ImagePath); + img.ScaleAbsolute(scale.M(Width), scale.M(Height)); + img.SetAbsolutePosition(scale.X(X), scale.Y(Y) - scale.M(img.ScaledHeight)); + tmp.AddImage(img); + tmp.RestoreState(); + } + } + public partial class SvgLine : SvgLinePart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + cb.SaveState(); + bool stroke = LineColor != System.Drawing.Color.Empty; + if (stroke) + { + //cb.SetLineWidth(scale.M(LineWidth)); + cb.SetLineWidth(LineWidth.Value); + //cb.SetColorStroke(new Color(LineColor)); + cb.SetColorStroke(Color.BLACK); + } + cb.SetColorStroke(Color.GREEN); + cb.SetLineCap(PdfContentByte.LINE_CAP_ROUND); + cb.MoveTo(scale.X(X1), scale.Y(cb, Y1)); + cb.LineTo(scale.X(X2), scale.Y(cb, Y2)); + //Console.WriteLine("'CB Line',{0},{1},{2},{3},{4}", scale.X(X1), scale.Y(cb, Y1), scale.X(X2), scale.Y(cb, Y2), scale.M(LineWidth)); + cb.Stroke(); + cb.RestoreState(); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + tmp.SaveState(); + bool stroke = LineColor != System.Drawing.Color.Empty; + if (stroke) + { + tmp.SetLineWidth(scale.M(LineWidth)); + tmp.SetColorStroke(new Color(LineColor)); + } + tmp.MoveTo(scale.X(X1), scale.Y(Y1)); + tmp.LineTo(scale.X(X2), scale.Y(Y2)); + //Console.WriteLine("'Template Line',{0},{1},{2},{3},{4}", scale.X(X1), scale.Y(tmp, Y1), scale.X(X2), scale.Y(tmp, Y2), scale.M(LineWidth)); + tmp.Stroke(); + tmp.RestoreState(); + Svg.FixBoundingBox(tmp, scale.X(X1), scale.Y(Y1), scale.X(X2), scale.Y(Y2)); + } + } + public partial class SvgParts : CollectionBase + { + public void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + foreach (SvgPart svgPart in List) + svgPart.Draw(cb, scale, mySvg, myParent); + } + public void Draw(PdfTemplate tmp, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) + { + foreach (SvgPart svgPart in List) + svgPart.Draw(tmp, myScale, mySvg, myParent); + } + } + public partial class SvgRectangle : SvgShapePart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + cb.SaveState(); + bool fill = FillColor != System.Drawing.Color.Transparent; + bool stroke = OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty; + if (fill) + cb.SetColorFill(new Color(FillColor)); + if (stroke) + { + cb.SetLineWidth(scale.M(OutlineWidth)); + cb.SetColorStroke(new Color(OutlineColor)); + } + cb.Rectangle(scale.X(X), scale.Y(cb, Y), scale.M(Width), -scale.M(Height)); + SvgITextLibrary.StrokeAndFill(cb, stroke, fill); + cb.RestoreState(); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + SetupInheritance(myParent.MyInheritedSettings); + if (ID != "") tmp = mySvg.GetTemplate(ID, mySvg.MyContentByte); // the size will be adjusted as items are added. + tmp.SaveState(); + bool fill = FillColor != System.Drawing.Color.Transparent; + bool stroke = OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty; + if (fill) + tmp.SetColorFill(new Color(FillColor)); + if (stroke) + { + tmp.SetLineWidth(scale.M(OutlineWidth)); + tmp.SetColorStroke(new Color(OutlineColor)); + } + //tmp.Rectangle(scale.X(X), scale.Y(Y), scale.M(Width), -scale.M(Height)); + tmp.Rectangle(scale.X(X), scale.Y(Y), scale.M(Width), -scale.M(Height)); + SvgITextLibrary.StrokeAndFill(tmp, stroke, fill); + Svg.FixBoundingBox(tmp, scale.X(X), scale.Y(Y), scale.X(X) + scale.M(Width), scale.Y(Y) - scale.M(Height)); + tmp.RestoreState(); + } + } + public partial class SvgRtf : SvgShapePart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + ColumnText ct = new ColumnText(cb); + IRtfDocument myRtf = RtfInterpreterTool.BuildDoc(Rtf); + Rtf2iTextSharp rtf = new Rtf2iTextSharp(myRtf); + Paragraph p = rtf.Convert(); + p.SetLeading(0, 1); + float leading = 10; + ct.SetSimpleColumn(scale.X(X), scale.Y(cb, Y) + leading, scale.X(X) + scale.M(Width), scale.Y(cb, Y) - cb.PdfDocument.PageSize.Height); + ct.AddElement(p); + ct.Go(); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + ColumnText ct = new ColumnText(tmp); + IRtfDocument myRtf = RtfInterpreterTool.BuildDoc(Rtf); + Rtf2iTextSharp rtf = new Rtf2iTextSharp(myRtf); + Paragraph p = rtf.Convert(); + p.SetLeading(0, 1); + float leading = 10; + ct.SetSimpleColumn(scale.X(X), scale.Y(Y) + leading, scale.X(X) + scale.M(Width), scale.Y(Y) - tmp.PdfDocument.PageSize.Height); + ct.AddElement(p); + ct.Go(); + } + } + public partial class SvgText : SvgShapePart + { + public override void Draw(PdfContentByte cb, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + string text = mySvg.OnProcessText(Text); + SetupInheritance(myParent.MyInheritedSettings); + float yScale = scale.Y(cb, Y); + cb.SaveState(); + bool fill = FillColor != System.Drawing.Color.Transparent; + bool stroke = OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty; + //cb.MoveTo(0, scale.Y(cb, Y)); + //cb.LineTo(500, scale.Y(cb, Y)); + //cb.SetColorStroke(Color.BLUE); + //cb.Stroke(); + //cb.BeginText(); + /* + if (fill) + cb.SetColorFill(new Color(FillColor)); + if (stroke) + { + cb.SetLineWidth(scale.M(OutlineWidth)); + cb.SetColorStroke(new Color(OutlineColor)); + } + cb.SetColorFill(Color.RED); + */ + //iTextSharp.text.pdf.ColumnText ct = new ColumnText(cb); + iTextSharp.text.Font font = FontFactory.GetFont(Font.Name, scale.M(new SvgMeasurement(Font.SizeInPoints, E_MeasurementUnits.PT))); + //BaseFont baseFont = font.BaseFont; + //cb.MoveText(scale.X(X), yScale); + //cb.SetFontAndSize(baseFont, Font.SizeInPoints); + //BaseFont baseFont = BaseFont.CreateFont("Helvetica", BaseFont.CP1252, BaseFont.NOT_EMBEDDED); + //cb.SetFontAndSize(baseFont, scale.M(new SvgMeasurement(Font.SizeInPoints, E_MeasurementUnits.PT))); + //Console.WriteLine("\"FontSize\",{0},{1}", Font.SizeInPoints, scale.M(new SvgMeasurement(Font.SizeInPoints, E_MeasurementUnits.PT))); + /* + switch ((stroke ? 1 : 0) + (fill ? 2 : 0)) + { + case 0: // No Stroke or Fill + cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); + break; + case 1: // Stroke + cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE); + break; + case 2: // Fill + cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); + break; + case 3: // Stroke and Fill + cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); + break; + } + */ + ColumnText ct = new ColumnText(cb); + Chunk chk = new Chunk(text, font); + float x = scale.X(X); + float w = chk.GetWidthPoint(); + switch (Justify) + { + //case SvgJustify.Left: + //x=x; + //cb.ShowTextAlignedKerned(PdfContentByte.ALIGN_LEFT, Text, scale.X(X), yScale, 0F); + //break; + case SvgJustify.Center: + x-=w/2; + //cb.ShowTextAlignedKerned(PdfContentByte.ALIGN_CENTER, Text, scale.X(X), yScale, 0F); + break; + case SvgJustify.Right: + x-=w; + //cb.ShowTextAlignedKerned(PdfContentByte.ALIGN_RIGHT, Text, scale.X(X), yScale, 0F); + break; + } + float y = yScale; + float xRight = x + w; + //float adjustForUnderline = Font.Underline ? Font.Size * .047F / 2 : 0; + //float adjustForDescender = font.BaseFont.GetDescentPoint("Almg", font.Size); + //float width = font.BaseFont.GetWidthPoint(Text, font.Size); + //x -= isCentered ? width / 2 : 0; + //xRight = x + font.BaseFont.GetWidthPoint(Text, font.Size) + adjustForUnderline; + + float Offset = 0; + if (Font.Underline) + { + chk.SetUnderline(iTextSharp.text.Color.GREEN, 0, 0.047F, 0, -.1373F, PdfContentByte.LINE_CAP_ROUND); + } + Phrase ph = new Phrase(chk); + //Paragraph ph = new Paragraph(chk); + ct.SetSimpleColumn(x, y + ph.Leading + Offset, xRight + 1F, y + ph.Leading + Offset - 2 * font.Size); + ct.AddElement(ph); + cb.SaveState(); + //cb.SetColorStroke(iTextSharp.text.Color.RED); + cb.SetColorFill(iTextSharp.text.Color.GREEN); + ct.Go(); + cb.RestoreState(); + //Console.WriteLine("'{0}',{1},{2},{3},{4}", Text, scale.X(X), yScale, X, Y); + //cb.ShowTextKerned(Text); + //cb.EndText(); + //cb.RestoreState(); + } + public override void Draw(PdfTemplate tmp, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) + { + string text = mySvg.OnProcessText(Text); + SetupInheritance(myParent.MyInheritedSettings); + float yScale = -scale.Y(Y); + tmp.SaveState(); + bool fill = FillColor != System.Drawing.Color.Transparent; + bool stroke = OutlineWidth.Value != 0F && OutlineColor != System.Drawing.Color.Empty; + //cb.MoveTo(0, scale.Y(cb, Y)); + //cb.LineTo(500, scale.Y(cb, Y)); + //cb.SetColorStroke(Color.BLUE); + //cb.Stroke(); + tmp.BeginText(); + if (fill) + tmp.SetColorFill(new Color(FillColor)); + if (stroke) + { + tmp.SetLineWidth(scale.M(OutlineWidth)); + tmp.SetColorStroke(new Color(OutlineColor)); + } + //iTextSharp.text.pdf.ColumnText ct = new ColumnText(cb); + //Console.WriteLine("Font['{0}'].IsRegistered = {1}",Font.Name,FontFactory.IsRegistered(Font.Name)); + iTextSharp.text.Font font = Svg.GetFont(Font.Name); + BaseFont baseFont = font.BaseFont; + tmp.MoveText(scale.X(X), yScale); + //cb.SetFontAndSize(baseFont, Font.SizeInPoints); + //BaseFont baseFont = BaseFont.CreateFont("Helvetica", BaseFont.CP1252, BaseFont.NOT_EMBEDDED); + tmp.SetFontAndSize(baseFont, scale.M(new SvgMeasurement(Font.SizeInPoints, E_MeasurementUnits.PT))); + //Console.WriteLine("\"FontSize\",{0},{1}", Font.SizeInPoints, scale.M(new SvgMeasurement(Font.SizeInPoints, E_MeasurementUnits.PT))); + switch ((stroke ? 1 : 0) + (fill ? 2 : 0)) + { + case 0: // No Stroke or Fill + tmp.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); + break; + case 1: // Stroke + tmp.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE); + break; + case 2: // Fill + tmp.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); + break; + case 3: // Stroke and Fill + tmp.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); + break; + } + tmp.ShowTextKerned(text); + tmp.EndText(); + tmp.RestoreState(); + } + } + public class Rtf2iTextSharp : RtfVisualVisitorBase + { + private IRtfDocument _RtfDoc; + private Paragraph _MyParagraph = new Paragraph(); + // public Rtf2iTextSharp(IRtfDocument rtfDoc, Document doc, PdfWriter writer) + public Rtf2iTextSharp(IRtfDocument rtfDoc) + { + if (rtfDoc == null) + throw new ArgumentNullException("rtfDoc"); + _RtfDoc = rtfDoc; + } + public Paragraph Convert() + { + _MyParagraph.Clear(); + foreach (IRtfVisual visual in _RtfDoc.VisualContent) + visual.Visit(this); + //_MyParagraph.SetLeading(0, 1); + return _MyParagraph; + } + // ---------------------------------------------------------------------- + protected override void DoVisitBreak(IRtfVisualBreak visualBreak) + { + switch (visualBreak.BreakKind) + { + case RtfVisualBreakKind.Line: + break; + case RtfVisualBreakKind.Page: + break; + case RtfVisualBreakKind.Paragraph: + //_MyParagraph.Add("\r\n"); + break; + case RtfVisualBreakKind.Section: + break; + default: + break; + } + //_MyParagraph.Add(string.Format("<{0}>", visualBreak.BreakKind.ToString())); + } + protected override void DoVisitSpecial(IRtfVisualSpecialChar visualSpecialChar) + { + //_MyParagraph.Add(string.Format("", visualSpecialChar.CharKind.ToString())); + switch (visualSpecialChar.CharKind) + { + case RtfVisualSpecialCharKind.Bullet: + break; + case RtfVisualSpecialCharKind.EmDash: + break; + case RtfVisualSpecialCharKind.EmSpace: + break; + case RtfVisualSpecialCharKind.EnDash: + break; + case RtfVisualSpecialCharKind.EnSpace: + break; + case RtfVisualSpecialCharKind.LeftDoubleQuote: + break; + case RtfVisualSpecialCharKind.LeftSingleQuote: + break; + case RtfVisualSpecialCharKind.NonBreakingHyphen: + break; + case RtfVisualSpecialCharKind.NonBreakingSpace: + _MyParagraph.Add(new Chunk("\u00A0")); + break; + case RtfVisualSpecialCharKind.OptionalHyphen: + break; + case RtfVisualSpecialCharKind.ParagraphNumberBegin: + break; + case RtfVisualSpecialCharKind.ParagraphNumberEnd: + break; + case RtfVisualSpecialCharKind.QmSpace: + break; + case RtfVisualSpecialCharKind.RightDoubleQuote: + break; + case RtfVisualSpecialCharKind.RightSingleQuote: + break; + case RtfVisualSpecialCharKind.Tabulator: + break; + default: + break; + } + } + //private static Dictionary _MyBaseFonts = new Dictionary(); + //private static BaseFont GetBaseFont(string fontName) + //{ + // if (!_MyBaseFonts.ContainsKey(fontName)) + // _MyBaseFonts.Add(fontName, BaseFont.CreateFont(FontFind.FullFileName(fontName), BaseFont.IDENTITY_H, BaseFont.EMBEDDED)); + // return _MyBaseFonts[fontName]; + //} + //private static Dictionary _MyFonts = new Dictionary(); + //private static iTextSharp.text.Font GetFont(string fontName, int size, int style) + //{ + // string key = string.Format("{0}.{1}.{2}", fontName, size, style); + // if (!_MyFonts.ContainsKey(key)) + // _MyFonts.Add(key, new iTextSharp.text.Font(GetBaseFont(fontName), size / 2, style)); + // return _MyFonts[key]; + //} + //protected override void DoVisitText(IRtfVisualText visualText) + //{ + // if (visualText.Format.IsHidden) return; + // //iTextSharp.text.Font font = GetFont(visualText.Format.Font.Name, visualText.Format.FontSize, + // // (visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) + (visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0)); + // iTextSharp.text.Font font = FontFactory.GetFont(visualText.Format.Font.Name, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, + // visualText.Format.FontSize / 2F);// TODO: Don't know why this is 2.4 rather than 2. + + // font.SetStyle((visualText.Format.IsBold ? iTextSharp.text.Font.BOLD : 0) + (visualText.Format.IsItalic ? iTextSharp.text.Font.ITALIC : 0)); + // font.Color = new iTextSharp.text.Color(visualText.Format.ForegroundColor.AsDrawingColor); + // Chunk chk = new Chunk(visualText.Text, font); + // chk.SetBackground(new iTextSharp.text.Color(visualText.Format.BackgroundColor.AsDrawingColor)); + // if (visualText.Format.IsStrikeThrough) + // chk.SetUnderline(iTextSharp.text.Color.BLACK, 0, 0.05F, 0, .3F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size + // if (visualText.Format.IsUnderline) + // chk.SetUnderline(iTextSharp.text.Color.BLACK, 0, 0.05F, 0, -.09F, PdfContentByte.LINE_CAP_ROUND); // Relative Based upon font size + // if (visualText.Format.SuperScript == -1) + // chk.SetTextRise(.45F * chk.Font.Size); + // else if (visualText.Format.SuperScript == 1) + // chk.SetTextRise(-.25F * chk.Font.Size); + // //Console.WriteLine("\"RTF FontSize\",{0},{1}", visualText.Format.FontSize / 2,chk.Font.Size); + + // _MyParagraph.Add(chk); + + //}// DoVisitText + + // ---------------------------------------------------------------------- + protected override void DoVisitImage(IRtfVisualImage visualImage) + { + _MyParagraph.Add(new Chunk("")); + //WriteStartElement("rtfVisualImage"); + + //WriteElementString("format", visualImage.Format.ToString()); + //WriteElementString("width", visualImage.Width.ToString()); + //WriteElementString("height", visualImage.Height.ToString()); + //WriteElementString("desiredWidth", visualImage.DesiredWidth.ToString()); + //WriteElementString("desiredHeight", visualImage.DesiredHeight.ToString()); + //WriteElementString("scaleWidthPercent", visualImage.ScaleWidthPercent.ToString()); + //WriteElementString("scaleHeightPercent", visualImage.ScaleHeightPercent.ToString()); + //WriteElementString("alignment", visualImage.Alignment.ToString()); + + //WriteElementString("image", visualImage.ImageDataHex.ToString()); + + //WriteEndElement(); + } // DoVisitImage + + } + public class SvgPageTotal + { + public SvgPageTotal(System.Drawing.Font myFont, PdfContentByte cb) + { + _MyFont = myFont; + _MyTemplate = cb.CreateTemplate(100, 100); + _MyTemplate.BoundingBox= new Rectangle(-20, -20, 100, 100); + } + private int _PageCount=1; + //public int PageCount + //{ + // get { return _PageCount; } + // set { _PageCount = value; } + //} + private System.Drawing.Font _MyFont; + //public System.Drawing.Font MyFont + //{ + // get { return _MyFont; } + // set { _MyFont = value; } + //} + private PdfTemplate _MyTemplate; + public PdfTemplate MyTemplate + { + get { return _MyTemplate; } + } + public void Draw() + { + // Output Page Count + _MyTemplate.BeginText(); + iTextSharp.text.Font font = Svg.GetFont(_MyFont.Name); + BaseFont baseFont = font.BaseFont; + _MyTemplate.SetFontAndSize(baseFont,_MyFont.Size); + _MyTemplate.SetTextMatrix(0, 0); + _MyTemplate.ShowText(_PageCount.ToString()); + _MyTemplate.EndText(); + } + } + public class SvgPageHelper : PdfPageEventHelper + { + private Svg _MySvg; + //private Svg _MyNextSvg; + public Svg MySvg + { + //get { return _MyNextSvg; } + //set { _MyNextSvg = value; } + get { return _MySvg; } + set { _MySvg = value; } + } + private PdfLayer _PageListLayer; + private PdfLayer _WatermarkLayer; + public SvgPageHelper(Svg mySvg, PdfLayer pageListLayer, PdfLayer watermarkLayer) + { + //_MySvg = _MyNextSvg = mySvg; + _MySvg = mySvg; + _PageListLayer = pageListLayer; + _WatermarkLayer = watermarkLayer; + } + public override void OnOpenDocument(PdfWriter writer, Document document) + { + //base.OnOpenDocument(writer, document); + //tmpTotal = writer.DirectContent.CreateTemplate(100, 100); + //tmpTotal.BoundingBox = new Rectangle(-20, -20, 100, 100); + //PdfContentByte cb = writer.DirectContent; + } + public override void OnEndPage(PdfWriter writer, Document document) + { + base.OnEndPage(writer, document); + PdfContentByte cb = writer.DirectContent; + cb.SaveState(); + if (_PageListLayer != null) cb.BeginLayer(_PageListLayer); + // Do anything needed to finish off the page + _MySvg.Draw(cb); + //_MySvg = _MyNextSvg; // After doing to current page update the Svg for the next page. + if (_PageListLayer != null) cb.EndLayer(); + cb.RestoreState(); + if (_MySvg.Watermark.ToLower() != "(none)" && _MySvg.Watermark != "") + { + cb = writer.DirectContentUnder; + cb.SaveState(); + if (_WatermarkLayer != null) cb.BeginLayer(_WatermarkLayer); + SvgWatermark myWatermark = new SvgWatermark(cb, _MySvg.Watermark, System.Drawing.Color.Blue, .5F); + myWatermark.SetSquareDotPattern(.7F); + myWatermark.Draw(); + if (_WatermarkLayer != null) cb.EndLayer(); + cb.RestoreState(); + } + cb.SaveState(); + ZoomOMatic(cb, 18); + cb.RestoreState(); + } + private void ZoomOMatic(PdfContentByte cb, float size) + { + for (float xx = 0; xx < cb.PdfDocument.PageSize.Width; xx += size) + { + for (float yy = 0; yy < cb.PdfDocument.PageSize.Height; yy += size) + { + PdfDestination dest = new PdfDestination(PdfDestination.FITR, xx - 1.5F * size, yy - 1.5F * size, xx + 1.5F * size, yy + 1.5F * size); + cb.SetAction(PdfAction.GotoLocalPage(cb.PdfWriter.CurrentPageNumber, dest, cb.PdfWriter), xx - size, yy - size, xx + size, yy + size); + } + } + } + + //private void DrawRectangle(PdfContentByte cb, float x, float y, float w, float h, Color fill, Color stroke) + //{ + // cb.SaveState(); + // PdfGState gState = new PdfGState(); + // gState.StrokeOpacity = .95F; + // gState.FillOpacity = .75F; + // cb.SetGState(gState); + // cb.SetColorFill(fill); + // cb.SetLineWidth(1F); + // cb.SetColorStroke(stroke); + // cb.Rectangle(x, cb.PdfWriter.PageSize.Height - y, w, -h); + // cb.FillStroke(); + // cb.RestoreState(); + //} + public override void OnStartPage(PdfWriter writer, Document document) + { + //base.OnStartPage(writer, document); + } + public override void OnCloseDocument(PdfWriter writer, Document document) + { + // Build Templates for Page Count + } + } + public class SvgWatermark + { + private PdfContentByte _ContentByte; + private string _Text; + private Color _Color; + private PdfPatternPainter _PatternPainter; + private BaseFont _Basefont; + private float _Fontsize; + private float _XOffset; + private float _YOffset; + private float _TextAngle; + private float _Opacity; + public SvgWatermark(PdfContentByte contentByte, string text, System.Drawing.Color color, float opacity) + { + _ContentByte = contentByte; + _Text = text; + float radius = .5F; + float space = 3; + _Color = new Color(color); + _Opacity = opacity; + SetDotPattern(radius, space); + SetTextLayout(); + } + private void SetTextLayout() + { + Rectangle pageSize = _ContentByte.PdfWriter.PageSize; // Get page size + _Basefont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false); + float hgtA = _Basefont.GetAscentPoint(_Text, 12); + //float hgtD = basefont.GetDescentPoint(text,20); + float wid = _Basefont.GetWidthPointKerned(_Text, 12); + //Console.WriteLine("{0} {1} {2}",hgtA,hgtD,wid); + float rho = wid / hgtA; + float x2 = (rho * pageSize.Height - pageSize.Width) / (rho * rho - 1); + float y1 = x2 * rho; + float y2 = pageSize.Height - y1; + float x1 = pageSize.Width - x2; + _XOffset = x2 + x1 / 2; + _YOffset = y1 / 2; + _TextAngle = CalcAngle(y1, x1); + _Fontsize = (float)(10 * Math.Sqrt(x2 * x2 + y2 * y2) / hgtA); + } + public void SetDotPattern(float radius, float space) + { + _PatternPainter = _ContentByte.CreatePattern(radius * 2, radius * 2, radius * 2 + space, radius * 2 + space); + PdfGState gState = new PdfGState(); + gState.FillOpacity = _Opacity; + _PatternPainter.SetGState(gState); + _PatternPainter.SetColorFill(_Color); + _PatternPainter.Circle(radius, radius, radius); + _PatternPainter.Fill(); + } + public void SetSquareDotPattern(float radius) + { + _PatternPainter = _ContentByte.CreatePattern(radius * 4, radius * 2, radius * 4, radius * 2); + PdfGState gState = new PdfGState(); + gState.FillOpacity = _Opacity; + _PatternPainter.SetGState(gState); + _PatternPainter.SetColorFill(_Color); + _PatternPainter.Rectangle(0, 0, radius, radius); + _PatternPainter.Rectangle(radius * 2, radius, radius, radius); + _PatternPainter.Fill(); + } + public void SetHashPattern(float thickness, float size) + { + _PatternPainter = _ContentByte.CreatePattern(size, size, size, size); + PdfGState gState = new PdfGState(); + gState.FillOpacity = _Opacity; + gState.StrokeOpacity = _Opacity; + _PatternPainter.SetGState(gState); + _PatternPainter.SetLineWidth(.01F); + _PatternPainter.SetColorStroke(_Color); // Set color + _PatternPainter.MoveTo(0, 0); + _PatternPainter.LineTo(size, size); + _PatternPainter.MoveTo(size, 0); + _PatternPainter.LineTo(0, size); + _PatternPainter.Stroke(); + } + public void SetTextPattern(float fontSize, float space) + { + float hgtA = _Basefont.GetAscentPoint(_Text, fontSize) + _Basefont.GetDescentPoint(_Text, fontSize); + float wid = _Basefont.GetWidthPointKerned(_Text, fontSize); + _PatternPainter = _ContentByte.CreatePattern(wid, hgtA, wid + space, hgtA + space); + _PatternPainter.SetFontAndSize(_Basefont, fontSize); + _PatternPainter.BoundingBox = new Rectangle(-20, -20, 100, 100); + _PatternPainter.BeginText(); + PdfGState gs1 = new PdfGState(); + gs1.FillOpacity = _Opacity; + _PatternPainter.SetGState(gs1); + _PatternPainter.SetColorFill(_Color); // Set color + _PatternPainter.ShowText(_Text); + _PatternPainter.EndText(); + } + public void SetTextPattern2(float fontSize) + { + BaseFont _Basefont2 = BaseFont.CreateFont(BaseFont.HELVETICA, Encoding.ASCII.EncodingName, false); + float hgtA = _Basefont2.GetAscentPoint(_Text, fontSize); + float wid = _Basefont2.GetWidthPointKerned(_Text, fontSize); + _PatternPainter = _ContentByte.CreatePattern(wid * 2, hgtA * 2, wid * 2, hgtA * 2); + _PatternPainter.SetFontAndSize(_Basefont2, fontSize); + _PatternPainter.BoundingBox = new Rectangle(-20, -20, 100, 100); + _PatternPainter.BeginText(); + PdfGState gs1 = new PdfGState(); + gs1.FillOpacity = _Opacity; + _PatternPainter.SetGState(gs1); + _PatternPainter.SetColorFill(_Color); // Set color + _PatternPainter.ShowText(_Text); + _PatternPainter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _Text, wid, hgtA, 0); + _PatternPainter.EndText(); + } + public void Draw() + { + _ContentByte.SaveState(); + _ContentByte.BeginText(); + _ContentByte.SetPatternFill(_PatternPainter); + //_ContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); + //_ContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE); + _ContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); + _ContentByte.SetLineWidth(.5F); + _ContentByte.SetFontAndSize(_Basefont, _Fontsize);// Set font and size + _ContentByte.SetColorStroke(_Color); + PdfGState gs2 = new PdfGState(); + gs2.StrokeOpacity = _Opacity; + _ContentByte.SetGState(gs2); + _ContentByte.ShowTextAlignedKerned(PdfContentByte.ALIGN_CENTER, _Text, _XOffset, _YOffset, _TextAngle);// Draw the text + _ContentByte.EndText(); + _ContentByte.FillStroke(); // Controlled by TextRenderMode above + _ContentByte.RestoreState(); + } + private float CalcAngle(float opposite, float adjacent) + { + return (float)(Math.Atan2(opposite, adjacent) * (180 / Math.PI)); + } + + } + public enum SvgWatermarkFill + { + Dots = 1, + Text1 = 2, + Text2 = 3, + Hash = 4 + } +} \ No newline at end of file