public override void Put(PagesContext context) { if (context == null) { throw new ArgumentNullException("context"); } Graphics graphics = context.UnitGraphics; if (graphics == null) { throw new Exception("Context already disposed!"); } if (context.Pages.Length <= context.CurrentIndex) // 页用完了。 { // 页索引超出范围,表示页不够用了。 return; } Page page = context.Pages[context.CurrentIndex]; float height = context.MaxLineHeight; PointF leftTop = context.CurrentLeftTop; var width = 0.0f; if (page.Height < leftTop.Y + context.MaxLineHeight + height) // 要换页了。 { context.CurrentIndex++; if (context.Pages.Length <= context.CurrentIndex) // 页用完了。 { this.PageIndex = context.CurrentIndex; } else // 用下一页。 { this.LeftTop = new PointF(0, 0); this.TheSize = new SizeF(width, height); this.PageIndex = context.CurrentIndex; context.CurrentLeftTop = new Point(0, 0); //context.MaxLineHeight = 0; } } else // 仅仅换行,不换页。 { leftTop = new PointF(0, leftTop.Y + context.MaxLineHeight); this.LeftTop = leftTop; this.TheSize = new SizeF(width, height); this.PageIndex = context.CurrentIndex; context.CurrentLeftTop = new PointF(leftTop.X + width, leftTop.Y); //context.MaxLineHeight = 0; } }
private static PagesContext GetPagesContext(int width, int height, int x, int y, float maxWidth, float maxHeight) { var page0 = new Page(width - x * 2 - (float)Math.Ceiling(maxWidth) * 2 - x * 2, (float)Math.Ceiling(maxHeight)); page0.Left = x * 2 + (float)Math.Ceiling(maxWidth) * 2; page0.Top = y * 2; var page1 = new Page(width - x * 2, height - y * 2 - (float)Math.Ceiling(maxHeight)); page1.Left = x * 2; page1.Top = y * 2 + (float)Math.Ceiling(maxHeight); var context = new PagesContext(page0, page1); return(context); }
private static void Draw(List <ChunkBase> chunkList, PagesContext context, Graphics graphics, Brush brush, bool showRectangle) { foreach (var item in chunkList) { SizeF bigSize = graphics.MeasureString("丨" + item.Text + "丨", item.TheFont); Bitmap bigImage = new Bitmap((int)bigSize.Width, (int)bigSize.Height); if (item.Tag is IngredientCategory) { using (var g = Graphics.FromImage(bigImage)) { g.DrawString("丨" + item.Text + "丨", item.TheFont, brush, 0, 0); } } else if (item.Tag is WeightedIngredient) { var weighted = item.Tag as WeightedIngredient; var ingredientBrush = new SolidBrush(weighted.Ingredient.ForeColor); using (var g = Graphics.FromImage(bigImage)) { g.DrawString("丨" + item.Text + "丨", item.TheFont, ingredientBrush, 0, 0); } ingredientBrush.Dispose(); } graphics.DrawImage(bigImage, // 把带有左右多余内容的bigImage的中间部分画到目标上。 new RectangleF( item.LeftTop.X + context.Pages[item.PageIndex].Left, item.LeftTop.Y + context.Pages[item.PageIndex].Top, item.TheSize.Width, item.TheSize.Height), new RectangleF( (bigImage.Width - item.TheSize.Width) / 2, 0, item.TheSize.Width, item.TheSize.Height), GraphicsUnit.Pixel); if (showRectangle) // 用矩形框起来,方便调试。 { graphics.DrawRectangle(rectPen, new Rectangle( (int)(item.LeftTop.X + context.Pages[item.PageIndex].Left), (int)(item.LeftTop.Y + context.Pages[item.PageIndex].Top), (int)(item.TheSize.Width), (int)(item.TheSize.Height))); } bigImage.Dispose(); } }
/// <summary> /// /// </summary> /// <param name="project"></param> /// <param name="font"></param> /// <param name="brush"></param> /// <returns></returns> public static Bitmap DumpBitmap(this PartyProject project, int tableCount, Font font, Pen pen, Brush brush, bool showRectangle) { if (project == null) { return(null); } const int width = 827, height = 1169;// A4 const int x = 10, y = 10; var bitmap = new Bitmap(width, height); using (var graphics = Graphics.FromImage(bitmap)) { graphics.PageUnit = GraphicsUnit.Pixel; // 边线 DrawBounds(pen, width, height, x, y, graphics); // 左右两列菜品 float maxWidth, maxHeight; DrawDishes(project, font, pen, brush, x, y, graphics, out maxWidth, out maxHeight); // 所有的食材 List <WeightedIngredient> list = GetIngredientList(project); // 转化为待处理的Chunk List <ChunkBase> chunkList = GetChunkList(list, tableCount, font); // 页集合的上下文。 PagesContext context = GetPagesContext(width, height, x, y, maxWidth, maxHeight); // 处理:将Chunk放到正确的位置。 foreach (var item in chunkList) { item.Put(context); } // 根据Chunk记录的位置信息,画图。 Draw(chunkList, context, graphics, brush, showRectangle); } return(bitmap); }
/// <summary> /// 把这个单元安排到合适的地方去。 /// </summary> /// <param name="context"></param> public abstract void Put(PagesContext context);
public override void Put(PagesContext context) { if (context == null) { throw new ArgumentNullException("context"); } Graphics graphics = context.UnitGraphics; if (graphics == null) { throw new Exception("Context already disposed!"); } if (context.Pages.Length <= context.CurrentIndex) // 页用完了。 { this.PageIndex = context.CurrentIndex; // 页索引超出范围,表示页不够用了。 return; } Page page = context.Pages[context.CurrentIndex]; PointF leftTop = context.CurrentLeftTop; SizeF bigSize = graphics.MeasureString("丨" + this.Text + "丨", this.TheFont); SizeF emptySize = graphics.MeasureString("丨丨", this.TheFont); var width = (bigSize.Width - emptySize.Width); var height = bigSize.Height; if (page.Width < leftTop.X + width) // 该换行了。 { if (page.Height < leftTop.Y + context.MaxLineHeight + height) // 要换页了。 { context.CurrentIndex++; if (context.Pages.Length <= context.CurrentIndex) // 页用完了。 { this.PageIndex = context.CurrentIndex; } else // 用下一页。 { this.LeftTop = new PointF(0, 0); this.TheSize = new SizeF(width, height); this.PageIndex = context.CurrentIndex; context.CurrentLeftTop = new Point(0, 0); //context.MaxLineHeight = 0; } } else // 仅仅换行,不换页。 { leftTop = new PointF(0, leftTop.Y + context.MaxLineHeight); this.LeftTop = leftTop; this.TheSize = new SizeF(width, height); this.PageIndex = context.CurrentIndex; context.CurrentLeftTop = new PointF(leftTop.X + width, leftTop.Y); //context.MaxLineHeight = 0; } } else // 当前行还可以放下此chunk。 { this.LeftTop = leftTop; this.TheSize = new SizeF(width, height); this.PageIndex = context.CurrentIndex; context.CurrentLeftTop = new PointF(leftTop.X + width, leftTop.Y); if (leftTop.X == 0 || // 第一个字符(串) context.MaxLineHeight < (int)height) { context.MaxLineHeight = (int)height; } } }