internal float RunTextCalcHeight(Report rpt, Graphics g, Row row, PageTextHtml pth) { // normally only called when CanGrow is true Size s = Size.Empty; if (IsHidden(rpt, row)) { return(0); } object o = _Value.Evaluate(rpt, row); TypeCode tc = _Value.GetTypeCode(); int width = this.WidthCalc(rpt, g); if (this.Style != null) { width -= (Style.EvalPaddingLeftPx(rpt, row) + Style.EvalPaddingRightPx(rpt, row)); if (this.IsHtml(rpt, row)) { if (pth == null) { pth = new PageTextHtml(o == null? "": o.ToString()); SetPagePositionAndStyle(rpt, pth, row); } pth.Build(g); s.Height = RSize.PixelsFromPoints(pth.TotalHeight); } else { s = Style.MeasureString(rpt, g, o, tc, row, width); } } else // call the class static method { s = Style.MeasureStringDefaults(rpt, g, o, tc, row, width); } TextboxRuntime tbr = TextboxRuntime.GetTextboxRuntime(rpt, this); tbr.RunHeight = RSize.PointsFromPixels(g, s.Height); if (Style != null) { tbr.RunHeight += (Style.EvalPaddingBottom(rpt, row) + Style.EvalPaddingTop(rpt, row)); } return(tbr.RunHeight); }
/// <summary> /// Measures the location of words within a string; limited by .Net 1.1 to 32 words /// MEASUREMAX is a constant that defines that limit /// </summary> private static WordStartFinish[] MeasureString32(string s, Graphics g, System.Drawing.Font drawFont, StringFormat drawFormat, CharacterRange[] cra) { if (s == null || s.Length == 0) { return(null); } drawFormat.SetMeasurableCharacterRanges(cra); Region[] rs = new Region[cra.Length]; rs = g.MeasureCharacterRanges(s, drawFont, new RectangleF(0, 0, float.MaxValue, float.MaxValue), drawFormat); WordStartFinish[] sz = new WordStartFinish[cra.Length]; int isz = 0; foreach (Region r in rs) { RectangleF mr = r.GetBounds(g); sz[isz].start = RSize.PointsFromPixels(g, mr.Left); sz[isz].end = RSize.PointsFromPixels(g, mr.Right); isz++; } return(sz); }
private int GetTwipsFromPixels(int pixels) { return((int)Math.Round(RSize.PointsFromPixels(GetGraphics, pixels) * 20, 0)); }
private SizeF MeasureString(string s, StyleInfo si, Graphics g, out float descent) { Font drawFont = null; StringFormat drawFormat = null; SizeF ms = SizeF.Empty; descent = 0; if (s == null || s.Length == 0) { return(ms); } try { // STYLE System.Drawing.FontStyle fs = 0; if (si.FontStyle == FontStyleEnum.Italic) { fs |= System.Drawing.FontStyle.Italic; } // WEIGHT switch (si.FontWeight) { case FontWeightEnum.Bold: case FontWeightEnum.Bolder: case FontWeightEnum.W500: case FontWeightEnum.W600: case FontWeightEnum.W700: case FontWeightEnum.W800: case FontWeightEnum.W900: fs |= System.Drawing.FontStyle.Bold; break; default: break; } try { FontFamily ff = si.GetFontFamily(); drawFont = new Font(ff, si.FontSize, fs); // following algorithm comes from the C# Font Metrics documentation float descentPixel = si.FontSize * ff.GetCellDescent(fs) / ff.GetEmHeight(fs); descent = RSize.PointsFromPixels(g, descentPixel); } catch { drawFont = new Font("Arial", si.FontSize, fs); // usually because font not found descent = 0; } drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Near; CharacterRange[] cr = { new CharacterRange(0, s.Length) }; drawFormat.SetMeasurableCharacterRanges(cr); Region[] rs = new Region[1]; rs = g.MeasureCharacterRanges(s, drawFont, new RectangleF(0, 0, float.MaxValue, float.MaxValue), drawFormat); RectangleF mr = rs[0].GetBounds(g); ms.Height = RSize.PointsFromPixels(g, mr.Height); // convert to points from pixels ms.Width = RSize.PointsFromPixels(g, mr.Width); // convert to points from pixels return(ms); } finally { if (drawFont != null) { drawFont.Dispose(); } if (drawFormat != null) { drawFont.Dispose(); } } }
private PageImage BuildImage(Graphics g, string token, StyleInfo oldsi, PageText model) { PageTextHtmlCmdLexer hc = new PageTextHtmlCmdLexer(token.Substring(4)); Hashtable ht = hc.Lex(); string src = (string)ht["src"]; if (src == null || src.Length < 1) { return(null); } string alt = (string)ht["alt"]; string height = (string)ht["height"]; string width = (string)ht["width"]; string align = (string)ht["align"]; Stream strm = null; System.Drawing.Image im = null; PageImage pi = null; try { // Obtain the image stream if (src.StartsWith("http:") || src.StartsWith("file:") || src.StartsWith("https:")) { WebRequest wreq = WebRequest.Create(src); WebResponse wres = wreq.GetResponse(); strm = wres.GetResponseStream(); } else { strm = new FileStream(src, System.IO.FileMode.Open, FileAccess.Read); } im = System.Drawing.Image.FromStream(strm); int h = im.Height; int w = im.Width; MemoryStream ostrm = new MemoryStream(); ImageFormat imf; imf = ImageFormat.Jpeg; im.Save(ostrm, imf); byte[] ba = ostrm.ToArray(); ostrm.Close(); pi = new PageImage(imf, ba, w, h); pi.AllowSelect = false; pi.Page = this.Page; pi.HyperLink = model.HyperLink; pi.Tooltip = alt == null ? model.Tooltip : alt; pi.X = 0; pi.Y = 0; pi.W = RSize.PointsFromPixels(g, width != null? Convert.ToInt32(width): w); pi.H = RSize.PointsFromPixels(g, height != null? Convert.ToInt32(height): h); pi.SI = new StyleInfo(); } catch { pi = null; } finally { if (strm != null) { strm.Close(); } if (im != null) { im.Dispose(); } } return(pi); }
public static string[] MeasureString(PageText pt, Graphics g, out float[] width) { StyleInfo si = pt.SI; string s = pt.Text; System.Drawing.Font drawFont = null; StringFormat drawFormat = null; SizeF ms; string[] sa = null; width = null; try { // STYLE System.Drawing.FontStyle fs = 0; if (si.FontStyle == FontStyleEnum.Italic) { fs |= System.Drawing.FontStyle.Italic; } // WEIGHT switch (si.FontWeight) { case FontWeightEnum.Bold: case FontWeightEnum.Bolder: case FontWeightEnum.W500: case FontWeightEnum.W600: case FontWeightEnum.W700: case FontWeightEnum.W800: case FontWeightEnum.W900: fs |= System.Drawing.FontStyle.Bold; break; default: break; } drawFont = new System.Drawing.Font(StyleInfo.GetFontFamily(si.FontFamilyFull), si.FontSize, fs); drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Near; // Measure string // pt.NoClip indicates that this was generated by PageTextHtml Build. It has already word wrapped. if (pt.NoClip || pt.SI.WritingMode == WritingModeEnum.tb_rl) // TODO: support multiple lines for vertical text { ms = MeasureString(s, g, drawFont, drawFormat); width = new float[1]; width[0] = RSize.PointsFromPixels(g, ms.Width); // convert to points from pixels sa = new string[1]; sa[0] = s; return(sa); } // handle multiple lines; // 1) split the string into the forced line breaks (ie "\n and \r") // 2) foreach of the forced line breaks; break these into words and recombine s = s.Replace("\r\n", "\n"); // don't want this to result in double lines string[] flines = s.Split(lineBreak); List <string> lines = new List <string>(); List <float> lineWidths = new List <float>(); // remove the size reserved for left and right padding float ptWidth = pt.W - pt.SI.PaddingLeft - pt.SI.PaddingRight; if (ptWidth <= 0) { ptWidth = 1; } foreach (string tfl in flines) { string fl; if (tfl.Length > 0 && tfl[tfl.Length - 1] == ' ') { fl = tfl.TrimEnd(' '); } else { fl = tfl; } // Check if entire string fits into a line ms = MeasureString(fl, g, drawFont, drawFormat); float tw = RSize.PointsFromPixels(g, ms.Width); if (tw <= ptWidth) { // line fits don't need to break it down further lines.Add(fl); lineWidths.Add(tw); continue; } // Line too long; need to break into multiple lines // 1) break line into parts; then build up again keeping track of word positions string[] parts = fl.Split(wordBreak); // this is the maximum split of lines StringBuilder sb = new StringBuilder(fl.Length); CharacterRange[] cra = new CharacterRange[parts.Length]; for (int i = 0; i < parts.Length; i++) { int sc = sb.Length; // starting character sb.Append(parts[i]); // endding character if (i != parts.Length - 1) // last item doesn't need blank { sb.Append(" "); } int ec = sb.Length; CharacterRange cr = new CharacterRange(sc, ec - sc); cra[i] = cr; // add to character array } // 2) Measure the word locations within the line string wfl = sb.ToString(); WordStartFinish[] wordLocations = MeasureString(wfl, g, drawFont, drawFormat, cra); if (wordLocations == null) { continue; } // 3) Loop thru creating new lines as needed int startLoc = 0; CharacterRange crs = cra[startLoc]; CharacterRange cre = cra[startLoc]; float cwidth = wordLocations[0].end; // length of the first float bwidth = wordLocations[0].start; // characters need a little extra on start string ts; bool bLine = true; for (int i = 1; i < cra.Length; i++) { cwidth = wordLocations[i].end - wordLocations[startLoc].start + bwidth; if (cwidth > ptWidth) { // time for a new line cre = cra[i - 1]; ts = wfl.Substring(crs.First, cre.First + cre.Length - crs.First); lines.Add(ts); lineWidths.Add(wordLocations[i - 1].end - wordLocations[startLoc].start + bwidth); // Find the first non-blank character of the next line while (i < cra.Length && cra[i].Length == 1 && fl[cra[i].First] == ' ') { i++; } if (i < cra.Length) // any lines left? { // yes, continue on startLoc = i; crs = cre = cra[startLoc]; cwidth = wordLocations[i].end - wordLocations[startLoc].start + bwidth; } else // no, we can stop { bLine = false; } // bwidth = wordLocations[startLoc].start - wordLocations[startLoc - 1].end; } else { cre = cra[i]; } } if (bLine) { ts = fl.Substring(crs.First, cre.First + cre.Length - crs.First); lines.Add(ts); lineWidths.Add(cwidth); } } // create the final array from the Lists string[] la = lines.ToArray(); width = lineWidths.ToArray(); return(la); } finally { if (drawFont != null) { drawFont.Dispose(); } if (drawFormat != null) { drawFont.Dispose(); } } }
/// <summary> /// Render all the objects in a page /// </summary> private void processPage(Pages pages, IEnumerable page) { //loop thru the items in the page foreach (PageItem pageItem in page) { if (pageItem.SI.BackgroundImage != null) { //put out any background image PageImage backgroundImage = pageItem.SI.BackgroundImage; float imageWidth = RSize.PointsFromPixels(pages.G, backgroundImage.SamplesW); float imageHeight = RSize.PointsFromPixels(pages.G, backgroundImage.SamplesH); int repeatX = 0; int repeatY = 0; float itemWidth = pageItem.W - (pageItem.SI.PaddingLeft + pageItem.SI.PaddingRight); float itemHeight = pageItem.H - (pageItem.SI.PaddingTop + pageItem.SI.PaddingBottom); switch (backgroundImage.Repeat) { case ImageRepeat.Repeat: repeatX = (int)Math.Floor(itemWidth / imageWidth); repeatY = (int)Math.Floor(itemHeight / imageHeight); break; case ImageRepeat.RepeatX: repeatX = (int)Math.Floor(itemWidth / imageWidth); repeatY = 1; break; case ImageRepeat.RepeatY: repeatY = (int)Math.Floor(itemHeight / imageHeight); repeatX = 1; break; case ImageRepeat.NoRepeat: default: repeatX = repeatY = 1; break; } //make sure the image is drawn at least 1 times repeatX = Math.Max(repeatX, 1); repeatY = Math.Max(repeatY, 1); float currX = pageItem.X + pageItem.SI.PaddingLeft; float currY = pageItem.Y + pageItem.SI.PaddingTop; float startX = currX; float startY = currY; for (int i = 0; i < repeatX; i++) { for (int j = 0; j < repeatY; j++) { currX = startX + i * imageWidth; currY = startY + j * imageHeight; addImage(backgroundImage.SI, currX, currY, imageWidth, imageHeight, RectangleF.Empty, backgroundImage.ImageData, null, pageItem.Tooltip); } } } else if (pageItem is PageTextHtml) { PageTextHtml pageTextHtml = pageItem as PageTextHtml; pageTextHtml.Build(pages.G); processPage(pages, pageTextHtml); continue; } else if (pageItem is PageText) { PageText pageText = pageItem as PageText; float[] textwidth; string[] measureStrings = RenderUtility.MeasureString(pageText, pages.G, out textwidth); addText(pageText.X, pageText.Y, pageText.W, pageText.H, measureStrings, pageText.SI, textwidth, pageText.CanGrow, pageText.HyperLink, pageText.NoClip, pageText.Tooltip); continue; } else if (pageItem is PageLine) { PageLine pageLine = pageItem as PageLine; addLine(pageLine.X, pageLine.Y, pageLine.X2, pageLine.Y2, pageLine.SI); continue; } else if (pageItem is PageEllipse) { PageEllipse pageEllipse = pageItem as PageEllipse; addEllipse(pageEllipse.X, pageEllipse.Y, pageEllipse.W, pageEllipse.H, pageEllipse.SI, pageEllipse.HyperLink); continue; } else if (pageItem is PageImage) { PageImage pageImage = pageItem as PageImage; //Duc Phan added 20 Dec, 2007 to support sized image RectangleF r2 = new RectangleF(pageImage.X + pageImage.SI.PaddingLeft, pageImage.Y + pageImage.SI.PaddingTop, pageImage.W - pageImage.SI.PaddingLeft - pageImage.SI.PaddingRight, pageImage.H - pageImage.SI.PaddingTop - pageImage.SI.PaddingBottom); //work rectangle RectangleF adjustedRect; RectangleF clipRect = RectangleF.Empty; switch (pageImage.Sizing) { case ImageSizingEnum.AutoSize: adjustedRect = new RectangleF(r2.Left, r2.Top, r2.Width, r2.Height); break; case ImageSizingEnum.Clip: adjustedRect = new RectangleF(r2.Left, r2.Top, RSize.PointsFromPixels(pages.G, pageImage.SamplesW), RSize.PointsFromPixels(pages.G, pageImage.SamplesH)); clipRect = new RectangleF(r2.Left, r2.Top, r2.Width, r2.Height); break; case ImageSizingEnum.FitProportional: float height; float width; float ratioIm = (float)pageImage.SamplesH / pageImage.SamplesW; float ratioR = r2.Height / r2.Width; height = r2.Height; width = r2.Width; if (ratioIm > ratioR) { //this means the rectangle width must be corrected width = height * (1 / ratioIm); } else if (ratioIm < ratioR) { //this means the rectangle height must be corrected height = width * ratioIm; } adjustedRect = new RectangleF(r2.X, r2.Y, width, height); break; case ImageSizingEnum.Fit: default: adjustedRect = r2; break; } if (pageImage.ImgFormat != System.Drawing.Imaging.ImageFormat.Wmf && pageImage.ImgFormat != System.Drawing.Imaging.ImageFormat.Emf) { addImage(pageImage.SI, adjustedRect.X, adjustedRect.Y, adjustedRect.Width, adjustedRect.Height, clipRect, pageImage.ImageData, pageImage.HyperLink, pageImage.Tooltip); } continue; } else if (pageItem is PageRectangle) { PageRectangle pageRectangle = pageItem as PageRectangle; addRectangle(pageRectangle.X, pageRectangle.Y, pageRectangle.W, pageRectangle.H, pageItem.SI, pageItem.HyperLink, pageItem.Tooltip); continue; } else if (pageItem is PagePie) { PagePie pagePie = pageItem as PagePie; addPie(pagePie.X, pagePie.Y, pagePie.W, pagePie.H, pageItem.SI, pageItem.HyperLink, pageItem.Tooltip); continue; } else if (pageItem is PagePolygon) { PagePolygon pagePolygon = pageItem as PagePolygon; addPolygon(pagePolygon.Points, pageItem.SI, pageItem.HyperLink); continue; } else if (pageItem is PageCurve) { PageCurve pageCurve = pageItem as PageCurve; addCurve(pageCurve.Points, pageItem.SI); continue; } } }