/// <summary> /// Parses FillItem /// </summary> /// <param name="padding">FillItem</param> /// <param name="space">space for FllItem (in dots)</param> /// <param name="font"></param> /// <param name="fillWidth">returns width (in dots) of in fact connected fillitems</param> /// <returns>printer command for FillItem</returns> private StringBuilder ParseFillItem(FillItem padding, int space, Font font, out int fillWidth) { StringBuilder fillText = new StringBuilder(); int i = 0; if (padding.Value == null) { padding.Value = " "; } int patternWidth = padding.Value.Length * printer.GetCharWidth(font); fillText.Append(printer.CharacterFont(font)); for (i = 0; i < space / patternWidth; i++) { fillText.Append(padding.Value); } fillWidth = i * patternWidth; //space - i*patternWidth == remaining space return(fillText); }
/// <summary> /// parses a Line, Lien can consist of (one or more) TextItem, FillItem, BarcodeItem or ImageItem /// </summary> /// <param name="line"></param> /// <returns>printer command for Line-part of SPF-object</returns> private StringBuilder ParseLine(Line line) { if (line.Items == null || line.Items.Length <= 0) { return(null); } StringBuilder lineData = new StringBuilder(); List <object> textAndFillItem = new List <object>(); List <int> textWidth = new List <int>(); int txtWidthSum = 0; int numFillItems = 0; int recurrence = 0; bool underline = false; if (line.RecurrenceSpecified) { recurrence = line.Recurrence; } if (line.UnderlineSpecified) { underline = line.Underline; } lineData.Append(printer.LineFeed()); if (line.LineSpacingSpecified) { lineData.Append(printer.LineSpacing(line.LineSpacing)); } else { lineData.Append(printer.LineSpacingDefault()); } if (underline) { lineData.Append(printer.UnderlineOn()); } foreach (Object item in line.Items) { if (item.GetType() == typeof(ImageItem)) { ImageItem image = item as ImageItem; lineData.Append(ParseImageItem(image)); } if (item.GetType() == typeof(BarCodeItem)) { BarCodeItem bc = item as BarCodeItem; lineData.Append(ParseBarCodeItem(bc)); lineData.Append(printer.AbsoluteHorizontalPosition(0)); } if (item.GetType() == typeof(TextItem)) { TextItem txt = item as TextItem; ParseTextItem(txt); //set some properties oft txt TextItem textAndFillItem.Add(txt); //textWidth.Add(txt.TextWidth); txtWidthSum += txt.TextWidth; //lineData.Append(ParseTextItem(txt)); } if (item.GetType() == typeof(FillItem)) { FillItem fill = item as FillItem; textAndFillItem.Add(fill); numFillItems++; //lineData.Append(ParseFillItem(fill)); } } #region Append printer command for text and fill items (stored in textAndFillItem List). // quite simple when there are no fill items (else condition). Mixed lists will be parsed until the first // textItem with special position (indent, align center|right), appended and this is repeated with the remaining list items. if (numFillItems > 0) { Font fillFont = printer.DefaultFont; //fill font will be font of previous text item or defaultfont int i = 0, old_i = 0, remain = textAndFillItem.Count; int leftPosAfter = 0; //!? double fullSpace = 0.0; int specialTextPosInd; int fillWidth = 0; int unusedWidth = 0; //should always be 0 except when mixing fonts or fontB with Indent do { //try to arragne mixture fo text and fill items specialTextPosInd = textAndFillItem.GetRange(old_i, remain).FindIndex(IsSpecialPosTextItem) + old_i; if (specialTextPosInd < 0) { //no special positioned text in line specialTextPosInd = textAndFillItem.Count; //parse all fullSpace = printer.DotsPerLine - leftPosAfter; } else { int leftPosTmp = ((TextItem)textAndFillItem[specialTextPosInd]).LeftPos; fullSpace = leftPosTmp - leftPosAfter; leftPosAfter = leftPosTmp + ((TextItem)textAndFillItem[specialTextPosInd]).TextWidth; txtWidthSum = 0; foreach (object item in textAndFillItem.GetRange(old_i, specialTextPosInd - old_i).FindAll(IsTextItem)) { txtWidthSum += ((TextItem)item).TextWidth; } numFillItems = textAndFillItem.GetRange(old_i, specialTextPosInd - old_i).FindAll(delegate(object o) { return(o.GetType() == typeof(FillItem)); }).Count; } float fillSpace = ((float)fullSpace - txtWidthSum) / numFillItems + unusedWidth;//standard fill //foreach (object item in textAndFillItem) { for (i = old_i; i <= specialTextPosInd && i < textAndFillItem.Count; i++) { if (textAndFillItem[i].GetType() == typeof(TextItem)) { TextItem txtItem = textAndFillItem[i] as TextItem; lineData.Append(txtItem.TextPrinterData); if (txtItem.FontSpecified) { fillFont = txtItem.Font; } } else if (textAndFillItem[i].GetType() == typeof(FillItem)) { lineData.Append(ParseFillItem((FillItem)textAndFillItem[i], (int)fillSpace, fillFont, out fillWidth)); unusedWidth = (int)fillSpace - fillWidth; } } //if fillWidth < fillSpace (due to FontB in special cases) filling is not 100% excat old_i = specialTextPosInd + 1; //first not printed item remain = textAndFillItem.Count - i; } while (specialTextPosInd < textAndFillItem.Count && remain > 0); } else { //no fillitems if (textAndFillItem != null) { int leftPos = 0; foreach (Object item in textAndFillItem) { if (item.GetType() == typeof(TextItem)) { //must be TextItem txtItem = item as TextItem; if (txtItem.LeftPos > 0) { txtItem.LeftPos = LeftPosText(leftPos, txtItem); lineData.Append(printer.AbsoluteHorizontalPosition(txtItem.LeftPos)); } lineData.Append(txtItem.TextPrinterData); leftPos += txtItem.LeftPos + txtItem.TextWidth; } } } if (underline) { //only fillitem free lines have to be underlined 'manually' lineData.Append(printer.AbsoluteHorizontalPosition(0)); lineData.Append(printer.CharacterFont(printer.DefaultFont)); for (int i = 0; i < printer.MaxTab - 1; i++) { lineData.Append(" "); } } } #endregion if (underline) { lineData.Append(printer.UnderlineOff()); } if (recurrence > 0) { StringBuilder manyLines = new StringBuilder(lineData.Length * recurrence); for (int j = 0; j < recurrence; j++) { manyLines.Append(lineData); } lineData.Append(manyLines); } return(lineData); }