PageRenderTime 73ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ITextSharp.Silverlight/iTextSharp/text/pdf/PdfTextArray.cs

#
C# | 129 lines | 53 code | 12 blank | 64 comment | 11 complexity | 22071e08139a6563bccef7e9c80b6fe7 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. /*
  4. * This file is part of the iText project.
  5. * Copyright (c) 1998-2009 1T3XT BVBA
  6. * Authors: Bruno Lowagie, Paulo Soares, et al.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3
  10. * as published by the Free Software Foundation with the addition of the
  11. * following permission added to Section 15 as permitted in Section 7(a):
  12. * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
  13. * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE.
  18. * See the GNU Affero General Public License for more details.
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program; if not, see http://www.gnu.org/licenses or write to
  21. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA, 02110-1301 USA, or download the license from the following URL:
  23. * http://itextpdf.com/terms-of-use/
  24. *
  25. * The interactive user interfaces in modified source and object code versions
  26. * of this program must display Appropriate Legal Notices, as required under
  27. * Section 5 of the GNU Affero General Public License.
  28. *
  29. * In accordance with Section 7(b) of the GNU Affero General Public License,
  30. * you must retain the producer line in every PDF that is created or manipulated
  31. * using iText.
  32. *
  33. * You can be released from the requirements of the license by purchasing
  34. * a commercial license. Buying such a license is mandatory as soon as you
  35. * develop commercial activities involving the iText software without
  36. * disclosing the source code of your own applications.
  37. * These activities include: offering paid services to customers as an ASP,
  38. * serving PDFs on the fly in a web application, shipping iText with a closed
  39. * source product.
  40. *
  41. * For more information, please contact iText Software Corp. at this
  42. * address: sales@itextpdf.com
  43. */
  44. namespace iTextSharp.text.pdf {
  45. /**
  46. * <CODE>PdfTextArray</CODE> defines an array with displacements and <CODE>PdfString</CODE>-objects.
  47. * <P>
  48. * A <CODE>TextArray</CODE> is used with the operator <VAR>TJ</VAR> in <CODE>PdfText</CODE>.
  49. * The first object in this array has to be a <CODE>PdfString</CODE>;
  50. * see reference manual version 1.3 section 8.7.5, pages 346-347.
  51. * OR
  52. * see reference manual version 1.6 section 5.3.2, pages 378-379.
  53. */
  54. public class PdfTextArray{
  55. List<Object> arrayList = new List<Object>();
  56. // To emit a more efficient array, we consolidate
  57. // repeated numbers or strings into single array entries.
  58. // "add( 50 ); Add( -50 );" will REMOVE the combined zero from the array.
  59. // the alternative (leaving a zero in there) was Just Weird.
  60. // --Mark Storer, May 12, 2008
  61. private String lastStr;
  62. private float lastNum = float.NaN;
  63. // constructors
  64. public PdfTextArray(String str) {
  65. Add(str);
  66. }
  67. public PdfTextArray() {
  68. }
  69. /**
  70. * Adds a <CODE>PdfNumber</CODE> to the <CODE>PdfArray</CODE>.
  71. *
  72. * @param number displacement of the string
  73. */
  74. public void Add(PdfNumber number) {
  75. Add((float)number.DoubleValue);
  76. }
  77. public void Add(float number) {
  78. if (number != 0) {
  79. if (!float.IsNaN(lastNum)) {
  80. lastNum += number;
  81. if (lastNum != 0) {
  82. ReplaceLast(lastNum);
  83. } else {
  84. arrayList.RemoveAt(arrayList.Count - 1);
  85. }
  86. } else {
  87. lastNum = number;
  88. arrayList.Add(lastNum);
  89. }
  90. lastStr = null;
  91. }
  92. // adding zero doesn't modify the TextArray at all
  93. }
  94. public void Add(String str) {
  95. if (str.Length > 0) {
  96. if (lastStr != null) {
  97. lastStr = lastStr + str;
  98. ReplaceLast(lastStr);
  99. } else {
  100. lastStr = str;
  101. arrayList.Add(lastStr);
  102. }
  103. lastNum = float.NaN;
  104. }
  105. // adding an empty string doesn't modify the TextArray at all
  106. }
  107. internal List<Object> ArrayList {
  108. get {
  109. return arrayList;
  110. }
  111. }
  112. private void ReplaceLast(Object obj) {
  113. // deliberately throw the IndexOutOfBoundsException if we screw up.
  114. arrayList[arrayList.Count - 1] = obj;
  115. }
  116. }
  117. }