public static Font getFont(string text, int width, FontStyle style = FontStyle.Bold, string font = "Times new roman") { int num1 = 25; Font font1 = new Font(font, (float)num1, style); Size stringSize = PrinterTicket.getStringSize(text, font1); if (stringSize.Width > width) { int num2 = 0; Font font2; for (; stringSize.Width > width; stringSize = PrinterTicket.getStringSize(text, font2)) { num2 += 2; font2 = new Font(font, (float)(num1 - num2), style); } font1 = new Font(font, (float)(num1 - num2), style); } else if (stringSize.Width < width) { int num2 = 0; Font font2; for (; stringSize.Width < width; stringSize = PrinterTicket.getStringSize(text, font2)) { num2 += 2; font2 = new Font(font, (float)(num1 + num2), style); } font1 = new Font(font, (float)(num1 + num2), style); } return(font1); }
public static int getLastLetterByMeasuring(string text, Font font, int paperWidth) { int num = -1; for (int index = 0; index < text.Length; ++index) { if (PrinterTicket.getStringSize(text.Substring(0, index + 1), font).Width >= paperWidth) { num = index - 1 < 0 ? 0 : index - 1; break; } } return(num); }
private void phoneLbl_SizeChanged(object sender, EventArgs e) { if (this.pd == null) { return; } string text = (sender as Control).Text; Size stringSize = PrinterTicket.getStringSize(text, (sender as Control).Font); int width = this.pd.PrinterSettings.DefaultPageSettings.PaperSize.Width; string str = "\r\n"; for (; stringSize.Width > width; stringSize = PrinterTicket.getStringSize(text, (sender as Control).Font)) { int letterByMeasuring = PrinterTicket.getLastLetterByMeasuring(text, (sender as Control).Font, width); text = text.Insert(letterByMeasuring, text[letterByMeasuring] == ' ' || text[letterByMeasuring] != ' ' && text[letterByMeasuring - 1] == ' ' ? str : "-" + str); } }
public Size printCentered(Graphics g, int y, string text, Font font, int paperWidth) { string str1 = ""; string str2 = "\r\n"; int length = text.IndexOf(str2) > -1 ? text.IndexOf(str2) : text.Length; int startIndex = 0; do { string str3 = text.Substring(startIndex, length).Trim(); Size size; for (size = PrinterTicket.getStringSize(str3, font); size.Width > paperWidth; size = PrinterTicket.getStringSize(str3, font)) { int letterByMeasuring = PrinterTicket.getLastLetterByMeasuring(str3, font, paperWidth); str3 = str3.Insert(letterByMeasuring, str3[letterByMeasuring] == ' ' || str3[letterByMeasuring] != ' ' && str3[letterByMeasuring - 1] == ' ' ? str2 : "-" + str2); } if (str3.IndexOf(str2) > -1) { size = this.printCentered(g, y, str3, font, paperWidth); } else { g.DrawString(str3, font, Brushes.Black, (float)((paperWidth - size.Width) / 2), (float)y); } y += size.Height; str1 = str1 + str3 + str2; startIndex += length + str2.Length; if (startIndex >= text.Length) { length = -1; } else { while (text.Substring(startIndex).IndexOf(str2) > -1 && text.Substring(startIndex).IndexOf(str2) == 0) { startIndex += str2.Length; } string str4 = text.Substring(startIndex); string str5 = str4.IndexOf(str2) > -1 ? str4.Substring(0, str4.IndexOf(str2)) : str4.Substring(0, str4.Length); length = str5.Length > 0 ? str5.Length : -1; } }while (length > -1 && length < text.Length); return(PrinterTicket.getStringSize(str1.Trim(), font)); }
private async void getFitString(Control control, string text) { if (pd != null) { control.Text = await Task.Run <string>((Func <string>)(() => { string str1 = ""; int width = this.pd.PrinterSettings.DefaultPageSettings.PaperSize.Width; string str2 = "\r\n"; int length = text.IndexOf(str2) > -1 ? text.IndexOf(str2) : text.Length; int startIndex = 0; do { string text1 = text.Substring(startIndex, length).Trim(); for (Size stringSize = PrinterTicket.getStringSize(text1, control.Font); stringSize.Width > width; stringSize = PrinterTicket.getStringSize(text1, control.Font)) { int letterByMeasuring = PrinterTicket.getLastLetterByMeasuring(text1, control.Font, width); text1 = text1.Insert(letterByMeasuring, text1[letterByMeasuring] == ' ' || text1[letterByMeasuring] != ' ' && text1[letterByMeasuring - 1] == ' ' ? str2 : "-" + str2); } str1 = str1 + text1 + str2; startIndex += length + str2.Length; if (startIndex >= text.Length) { length = -1; } else { while (text.Substring(startIndex).IndexOf(str2) > -1 && text.Substring(startIndex).IndexOf(str2) == 0) { startIndex += str2.Length; } string str3 = text.Substring(startIndex); string str4 = str3.IndexOf(str2) > -1 ? str3.Substring(0, str3.IndexOf(str2)) : str3.Substring(0, str3.Length); length = str4.Length > 0 ? str4.Length : -1; } }while (length > -1 && length < text.Length); return(str1.Trim()); })); } }
private void centerToTicket(Control control) { Control parent = control.Parent; control.Location = new Point((parent.Width - PrinterTicket.getStringSize(control.Text, control.Font).Width) / 2, control.Location.Y); }