public static GraphicsMx OpenMetafile( string metaFileName, ResultsFormat rf) { GraphicsMx g = new GraphicsMx(); g.MetaFileName = metaFileName; g.G = MetaFileMx.Open(metaFileName, rf.PageBounds); if (g.G == null) { return(null); } g.PageWidth = rf.PageWidth; g.PageHeight = rf.PageHeight; g.PageMargins = rf.PageMargins.Clone(); g.SetFont(rf.FontName, rf.FontSize); return(g); }
/// <summary> /// Wrap text into specified width in pixels /// </summary> /// <param name="text">Text to wrap</param> /// <param name="wrapWidth">In pixels</param> /// <returns></returns> public static string WrapText( string text, int wrapWidth) { string s, txt; StringBuilder sb = new StringBuilder(); char c; int p, l, width, lineCount, i1; GraphicsMx graphics = new GraphicsMx(); graphics.SetFont("Arial", 9); s = text.Replace('\t', ' '); // convert tabs to spaces p = 0; lineCount = 0; while (true) { width = 0; for (l = 0; p + l < s.Length; l++) { c = s[p + l]; if (c == '\n' || c == '\r') { break; } width += graphics.CharWidth(c); } if (width <= wrapWidth) { goto LocatedBreak; } // Backscan looking for a space, dash or comma break character while (true) { for (l = l - 1; l > 0; l--) { c = s[p + l - 1]; if (c == ' ' || c == '-' || c == ',') { break; } } if (l == 0) { break; // break out if no luck } width = 0; int p2 = p + l - 1; if (p2 >= s.Length) { ClientLog.Message("p2 too big: " + p + " " + p2 + " " + s); // debug info p2 = s.Length - 1; } for (i1 = p; i1 <= p2; i1++) { width += graphics.CharWidth(s[i1]); } if (width <= wrapWidth) { goto LocatedBreak; } } // break within a word l = 0; width = 0; while (true) { i1 = graphics.CharWidth(s[p + l]); if (l > 0 && width + i1 > wrapWidth) { break; } width += i1; l++; } LocatedBreak: i1 = 0; // left justified txt = s.Substring(p, l); p += l; while (p < s.Length - 1 && (s[p] == '\n' || s[p] == '\r')) { p++; } if (lineCount > 0) { sb.Append("\r\n"); } sb.Append(txt); lineCount++; if (p >= s.Length) { break; } } // end of text loop return(sb.ToString()); }