jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//============================================================================
//Gios Pdf.NET - A library for exporting Pdf Documents in C#
//Copyright (C) 2005  Paolo Gios - www.paologios.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//=============================================================================
using System;
using System.Drawing;
using System.Collections;
using System.Text;
using HH.WMS.Utils.ZipLib.Zip.Compression.Streams;
 
namespace HH.WMS.Utils.Gios.Pdf
{
    internal class Utility
    {
        internal static int[] TimesNewRomanRegular=new int[]{481,481,481,481,481,481,481,481,481,0,0,481,481,0,481,481,481,481,481,481,481,481,481,481,481,481,481,481,0,0,0,0,155,206,253,310,310,516,481,112,206,206,309,349,155,207,155,173,309,310,309,309,310,310,310,310,310,310,172,173,349,349,349,275,570,447,413,412,447,379,344,447,447,207,241,446,379,550,447,447,344,447,413,344,379,447,447,584,447,447,378,206,172,206,291,309,206,274,310,275,310,275,206,310,310,172,172,310,172,482,310,310,309,310,206,241,173,310,310,446,310,310,275,297,124,297,335,481,481,481,481,481,0,0,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,481,155,206,310,310,309,310,124,309,206,470,171,310,349,207,470,309,248,340,186,186,206,357,281,155,206,186,192,310,464,464,464,275,447,447,447,447,447,447,550,412,379,379,379,379,207,207,207,207,447,447,447,447,447,447,447,349,447,447,447,447,447,447,344,309,274,274,274,274,274,274,413,275,275,275,275,275,172,172,172,172,310,310,310,310,310,310,310,339,310,310,310,310,310,310};
        internal static int[] TimesNewRomanBold=new int[]{493,493,493,493,493,493,493,493,493,0,0,493,493,0,493,493,493,493,493,493,493,493,493,493,493,493,493,493,0,0,0,0,154,206,342,309,309,618,515,171,206,205,308,352,154,206,154,171,308,309,309,308,309,309,309,309,309,309,205,206,352,352,352,308,575,446,412,446,446,412,377,481,481,241,308,480,412,583,446,481,378,481,446,343,411,446,446,618,446,446,412,206,171,205,359,309,205,309,344,274,343,274,205,309,343,172,205,343,172,514,343,308,343,343,274,240,205,344,309,446,309,309,274,244,136,243,321,493,493,493,493,493,0,0,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,493,154,205,309,309,309,309,136,309,205,462,185,308,352,206,462,309,247,339,184,185,206,356,334,154,205,185,203,308,463,463,463,308,446,446,446,446,446,446,618,446,412,412,412,412,241,241,241,241,446,446,481,481,481,481,481,352,481,446,446,446,446,446,378,343,309,309,309,309,309,309,446,274,274,274,274,274,171,172,171,172,308,343,308,308,308,308,308,339,308,344,344,344,344,309};
        internal static int[] HelveticaRegular=new int[]{464,464,464,464,464,464,464,464,464,0,0,464,464,0,464,464,464,464,464,464,464,464,464,464,464,464,464,464,0,0,0,0,172,173,220,344,344,550,413,119,207,206,241,362,172,206,173,172,344,344,345,344,344,344,344,344,345,344,172,172,362,362,362,344,628,412,413,447,446,413,378,481,447,172,310,413,344,515,446,481,412,481,447,413,378,447,412,584,413,412,378,172,172,172,291,344,206,344,344,310,344,344,172,344,345,137,137,309,138,516,345,344,345,344,206,309,172,344,310,447,309,309,310,207,161,207,361,464,464,464,464,464,0,0,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,464,172,206,344,345,344,344,161,344,206,455,229,344,362,206,455,342,248,340,206,206,207,356,333,172,206,206,226,344,515,515,516,378,412,412,412,412,412,412,618,447,413,413,413,413,172,172,173,172,447,446,481,481,481,481,481,361,481,447,447,447,447,412,412,378,344,344,344,344,344,344,550,310,344,344,344,344,172,172,173,0,345,345,344,344,344,344,344,340,378,344,344,344,344,309};
        internal static int[] HelveticaBold=new int[]{476,476,476,476,476,476,476,476,476,0,0,476,476,0,476,476,476,476,476,476,476,476,476,476,476,476,476,476,0,0,0,0,172,207,294,344,345,550,447,148,206,207,241,361,172,206,172,173,344,344,344,344,344,345,344,344,345,344,206,206,361,361,361,378,604,447,447,447,447,413,378,481,447,172,344,447,378,516,447,481,413,481,447,413,378,447,413,584,413,413,379,206,173,206,362,345,206,345,379,344,378,344,206,378,378,172,172,344,172,550,379,379,378,379,242,345,207,378,344,482,344,344,309,241,174,241,361,476,476,476,476,476,0,0,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,476,172,207,344,344,344,344,174,344,206,456,229,344,361,206,456,342,248,340,206,206,206,357,344,172,206,207,226,345,516,516,516,379,447,447,447,447,447,447,618,447,413,413,413,413,172,173,172,172,447,447,481,481,481,481,481,362,481,447,447,447,447,413,413,378,345,345,345,345,345,345,550,344,344,344,344,344,172,173,172,172,379,379,379,379,379,379,379,340,378,378,378,378,378,344};
        
        internal static Hashtable wordWidthsCache=new Hashtable();
        
        internal static string BCR(Color Color)
        {
            return (((double)Color.R)/255).ToString("0.##");
        }
        internal static string BCG(Color Color)
        {
            return (((double)Color.G)/255).ToString("0.##");
        
        }
        internal static string BCB(Color Color)
        {
            return (((double)Color.B)/255).ToString("0.##");
        }
        internal static string ColorRGLine(Color Color)
        {
            System.Text.StringBuilder sb=new StringBuilder();
            sb.Append(BCR(Color));
            sb.Append(" ");
            sb.Append(BCG(Color));
            sb.Append(" ");
            sb.Append(BCB(Color));
            sb.Append(" RG\n");
            return sb.Replace(",",".").ToString();
        }
        internal static string ColorrgLine(Color Color)
        {
            return ColorRGLine(Color).Replace("RG","rg");
        }
        internal static Byte[] Deflate(string text)
        {
            Byte[] b=System.Text.ASCIIEncoding.ASCII.GetBytes(text);
            return Utility.Deflate(b);
        }
        internal static Byte[] Deflate(Byte[] b)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            DeflaterOutputStream outStream =new DeflaterOutputStream( ms);
            
            outStream.Write(b, 0, b.Length);
            outStream.Flush();
            outStream.Finish();
            
            Byte[] result=ms.ToArray();
            outStream.Close();
            ms.Close();
            return result;
        }
        internal static string xRefFormatting(long xValue)
        {
            string strMsg =xValue.ToString();
            int iLen=strMsg.Length;
            if (iLen<10)
            {
                StringBuilder s=new StringBuilder();
                int i=10-iLen;
                s.Append('0',i);
                strMsg=s.ToString() + strMsg;
            }
            return strMsg;
        }
        internal static double Measure(Font Font,string text)
        {
            string key=text+Font.Name.ToString()+Font.Size.ToString()
                +Font.Bold.ToString()+Font.Italic.ToString();
            object o=wordWidthsCache[key];
            if (o==null)
            {
                double dbl=-Utility.Difference(Font,0,text);
                wordWidthsCache.Add(key,dbl);
                return dbl;
            }
            return (double)o;
        }
        private static double Difference(System.Drawing.Font font,double Width,string text)
        {
            text=System.Text.RegularExpressions.Regex.Replace(text,@"\\\d\d\d","A");
            text=text.Replace(@"\\",@"\");
 
            switch (font.Name)
            {
                case "Courier New":
                    return Width-(text.Length*font.Size)/1.66666;
                    
                case "Times New Roman":
                    if (font.Bold) 
                    {
                        double dim=0;
                        int l=text.Length;
                        for (int index=0;index<l;index++)
                        {
                            char c=text[index];
                            if (c<255)    dim+=Utility.TimesNewRomanBold[c];
                            else
                            {
                                dim+=Utility.TimesNewRomanBold['A'];
                            }
                        }
                        return Width-(dim*font.Size)/618;
                    }
                    else
                    {
                        double dim=0;
                        int l=text.Length;
                        for (int index=0;index<l;index++)
                        {
                            char c=text[index];
                            if (c<255)    dim+=Utility.TimesNewRomanRegular[c];
                            else
                            {
                                dim+=Utility.TimesNewRomanRegular['A'];
                            }
                        }
                        return Width-(dim*font.Size)/618;
                    }
                default:
                    if (font.Bold) 
                    {
                        double dim=0;
                        int l=text.Length;
                        for (int index=0;index<l;index++)
                        {
                            char c=text[index];
                            if (c<255)    dim+=Utility.HelveticaBold[c];
                            else
                            {
                                dim+=Utility.HelveticaBold['A'];
                            }
                        }
                        return Width-(dim*font.Size)/618;
                    }
                    else
                    {
                        double dim=0;
                        int l=text.Length;
                        for (int index=0;index<l;index++)
                        {
                            char c=text[index];
                            if (c<255) dim+=Utility.HelveticaRegular[c];
                            else
                            {
                                dim+=Utility.HelveticaBold['A']; 
                            }
                        }
                        return Width-(dim*font.Size)/618;
                    }
                    
            }
        }
 
        internal static string TextEncode(string text)
        {
            text=text.Replace("(","\\050").Replace(")","\\051");
            text=text.Replace("?","\\350").Replace("?","\\322").Replace("?","\\323");
            text=text.Replace("?","\\300").Replace("?","\\301").Replace("?","\\307");
            text=text.Replace("?","\\311").Replace("?","\\310").Replace("?","\\314").Replace("?","\\315");
            text=text.Replace("?","\\351").Replace("?","\\362").Replace("?","\\371");
            text=text.Replace("?","\\331").Replace("?","\\332");
            text=text.Replace("?","\\354").Replace("?","\\340").Replace("€","\\200").Replace("?","\\243");
            text=text.Replace("?","\\347").Replace("?","\\272").Replace("?","\\247");
            return text;
        }
        internal static string FontToFontLine(Font Font)
        {
            return "/"+PdfFont.FontToPdfType(Font)+" "+Font.Size.ToString()+" Tf\n";
        }
    
    }
}