~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/source_exporter/wxPdfDocument/samples/minimal/labels.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
///////////////////////////////////////////////////////////////////////////////
 
2
// Name:        labels.cpp
 
3
// Purpose:     Example of label printing
 
4
// Author:      Ulrich Telle
 
5
// Modified by:
 
6
// Created:     2005-11-26
 
7
// Copyright:   (c) Ulrich Telle
 
8
// Licence:     wxWindows licence
 
9
///////////////////////////////////////////////////////////////////////////////
 
10
 
 
11
// For compilers that support precompilation, includes "wx/wx.h".
 
12
#include "wx/wxprec.h"
 
13
 
 
14
#ifdef __BORLANDC__
 
15
#pragma hdrstop
 
16
#endif
 
17
 
 
18
#ifndef WX_PRECOMP
 
19
#include "wx/wx.h"
 
20
#endif
 
21
 
 
22
#include "wx/pdfdoc.h"
 
23
 
 
24
/**
 
25
* Labels
 
26
 
27
* This example demonstrates the PDF label printing.
 
28
*
 
29
* The code is based on the PHP script PDF_Label of Laurent PASSEBECQ.
 
30
*
 
31
* Following are the comments from the original PHP code:
 
32
*
 
33
* ////////////////////////////////////////////////////
 
34
* // PDF_Label 
 
35
* //
 
36
* // Class to print labels in Avery or custom formats
 
37
* //
 
38
* // Copyright (C) 2003 Laurent PASSEBECQ (LPA)
 
39
* // Based on code by Steve Dillon : steved@mad.scientist.com
 
40
* //
 
41
* //-------------------------------------------------------------------
 
42
* // VERSIONS :
 
43
* // 1.0  : Initial release
 
44
* // 1.1  : + : Added unit in the constructor
 
45
* //        + : Now Positions start @ (1,1).. then the first image @top-left of a page is (1,1)
 
46
* //        + : Added in the description of a label : 
 
47
* //        font-size  : defaut char size (can be changed by calling Set_Char_Size(xx);
 
48
* //        paper-size  : Size of the paper for this sheet (thanx to Al Canton)
 
49
* //        metric    : type of unit used in this description
 
50
* //                You can define your label properties in inches by setting metric to 'in'
 
51
* //                and printing in millimiter by setting unit to 'mm' in constructor.
 
52
* //        Added some labels :
 
53
* //        5160, 5161, 5162, 5163,5164 : thanx to Al Canton : acanton@adams-blake.com
 
54
* //        8600             : thanx to Kunal Walia : kunal@u.washington.edu
 
55
* //        + : Added 3mm to the position of labels to avoid errors 
 
56
* // 1.2  : + : Added Set_Font_Name method
 
57
* //        = : Bug of positioning
 
58
* //        = : Set_Font_Size modified -> Now, just modify the size of the font
 
59
* //        = : Set_Char_Size renamed to Set_Font_Size
 
60
* ////////////////////////////////////////////////////
 
61
*/
 
62
 
 
63
typedef struct tPdfLabelFormat
 
64
{
 
65
  const wxChar* name;
 
66
  wxPaperSize   paperSize;
 
67
  const wxChar* metric;
 
68
  double        marginLeft;
 
69
  double        marginTop;
 
70
  int           nx;
 
71
  int           ny;
 
72
  double        xSpace;
 
73
  double        ySpace;
 
74
  double        width;
 
75
  double        height;
 
76
  double        fontSize;
 
77
} PdfLabelFormat;
 
78
 
 
79
static PdfLabelFormat s_averyLabels[] =
 
80
{
 
81
  {  wxT("5160"),  wxPAPER_LETTER, wxT("mm"), 1.762, 10.7,    3, 10,  3.175,  0.,   66.675, 25.4,   8 },
 
82
  {  wxT("5161"),  wxPAPER_LETTER, wxT("mm"), 0.967, 10.7,    2, 10,  3.967,  0.,  101.6,   25.4,   8 },
 
83
  {  wxT("5162"),  wxPAPER_LETTER, wxT("mm"), 0.97,  20.224,  2,  7,  4.762,  0.,  100.807, 35.72,  8 },
 
84
  {  wxT("5163"),  wxPAPER_LETTER, wxT("mm"), 1.762, 10.7,   2,  5,  3.175,  0.,  101.6,   50.8,   8 },
 
85
  {  wxT("5164"),  wxPAPER_LETTER, wxT("in"), 0.148,  0.5,    2,  3,  0.2031, 0.,    4.0,    3.33, 12 },
 
86
  {  wxT("8600"),  wxPAPER_LETTER, wxT("mm"), 7.1,   19,     3, 10,  9.5,    3.1,  66.6,   25.4,   8 },
 
87
  {  wxT("L7163"), wxPAPER_A4,     wxT("mm"), 5.0,   15,     2,  7, 25.0,    0.,   99.1,   38.1,   9 },
 
88
  // Last sentinel entry (do not delete)
 
89
  { wxT(""),      wxPAPER_NONE,   wxT("mm"), 0.0,    0,     0,  0,  0.0,    0.,    0.0,    0.0,   0 },
 
90
};
 
91
 
 
92
class PdfLabel : public wxPdfDocument
 
93
{
 
94
public:
 
95
  /// Constructor
 
96
  PdfLabel (const PdfLabelFormat& format, const wxString& unit = wxT("mm"), int posX = 1, int  posY = 1)
 
97
    : wxPdfDocument(wxPORTRAIT, format.metric, format.paperSize)
 
98
  {
 
99
    m_charSize   = 10;
 
100
    m_lineHeight = 10;
 
101
    m_xCount     = 1;
 
102
    m_yCount     = 1;
 
103
    
 
104
    SetFormat(format);
 
105
    SetFontName(wxT("Helvetica"));
 
106
    SetMargins(0,0); 
 
107
    SetAutoPageBreak(false); 
 
108
 
 
109
    m_metricDoc = unit;
 
110
    // Start at the given label position
 
111
    m_xCount = (posX >= m_xNumber) ? m_xNumber-1 : ((posX > 1) ? posX-1 : 0);
 
112
    m_yCount = (posY >= m_yNumber) ? m_yNumber-1 : ((posY > 1) ? posY-1 : 0);
 
113
  }
 
114
 
 
115
  /// Print a label
 
116
  void AddLabel(const wxString& texte)
 
117
  {
 
118
    // We are in a new page, then we must add a page
 
119
    if ((m_xCount == 0) && (m_yCount == 0))
 
120
    {
 
121
      AddPage();
 
122
    }
 
123
 
 
124
    double posX = m_marginLeft+(m_xCount*(m_width+m_xSpace));
 
125
    double posY = m_marginTop+(m_yCount*(m_height+m_ySpace));
 
126
    SetXY(posX+3, posY+3);
 
127
    MultiCell(m_width, m_lineHeight, texte);
 
128
 
 
129
    m_yCount++;
 
130
 
 
131
    if (m_yCount == m_yNumber)
 
132
    {
 
133
      // End of column reached, we start a new one
 
134
      m_xCount++;
 
135
      m_yCount = 0;
 
136
    }
 
137
 
 
138
    if (m_xCount == m_xNumber)
 
139
    {
 
140
      // Page full, we start a new one
 
141
      m_xCount = 0;
 
142
      m_yCount = 0;
 
143
    }
 
144
  }
 
145
 
 
146
  /// Changes the font
 
147
  void SetFontName(const wxString& fontname)
 
148
  {
 
149
    if (fontname != wxT(""))
 
150
    {
 
151
      m_fontName = fontname;
 
152
      SetFont(m_fontName);
 
153
    }
 
154
  }
 
155
 
 
156
  /// Sets the font size
 
157
  /**
 
158
  * This changes the line height too
 
159
  */
 
160
  void SetFontSize(double pt)
 
161
  {
 
162
    if (pt > 3)
 
163
    {
 
164
      m_charSize = pt;
 
165
      m_lineHeight = GetHeightChars(pt);
 
166
      wxPdfDocument::SetFontSize(m_charSize);
 
167
    }
 
168
  }
 
169
 
 
170
  /// Finds a predefined label format by name
 
171
  static const PdfLabelFormat& FindFormat(const wxString& format)
 
172
  {
 
173
    PdfLabelFormat* tFormat = &s_averyLabels[0];
 
174
    int j = 0;
 
175
    for (j = 0; wxString(wxT("")).Cmp(s_averyLabels[j].name) != 0; j++)
 
176
    {
 
177
      if (format == s_averyLabels[j].name)
 
178
      {
 
179
        tFormat = &s_averyLabels[j];
 
180
        break;
 
181
      }
 
182
    }
 
183
    return *tFormat;
 
184
  }
 
185
 
 
186
protected:
 
187
  /// Convert units (in to mm, mm to in)
 
188
  /**
 
189
  * src and dest must be 'in' or 'mm'
 
190
  */
 
191
  double ConvertMetric (double value, const wxString& src, const wxString& dest)
 
192
  {
 
193
    double retValue = value;
 
194
    if (src == wxT("in") && dest == wxT("mm"))
 
195
    {
 
196
      retValue *= (1000./ 39.37008);
 
197
    }
 
198
    else if (src == wxT("mm") && dest == wxT("in"))
 
199
    {
 
200
      retValue *= (39.37008 / 1000.);
 
201
    }
 
202
    return retValue;
 
203
  }
 
204
 
 
205
  /// Gives the height for a char size given.
 
206
  double GetHeightChars(double pt)
 
207
  {
 
208
    double height = 100;
 
209
    // Array matching character sizes and line heights
 
210
    static const int nTable = 10;
 
211
    static const double cTable[nTable] = { 6, 7,   8, 9, 10, 11, 12, 13, 14, 15 };
 
212
    static const double hTable[nTable] = { 2, 2.5, 3, 4,  5,  6,  7,  8,  9, 10 };
 
213
    int i;
 
214
    for (i = 0; i < nTable; i++)
 
215
    {
 
216
      if (pt == cTable[i])
 
217
      {
 
218
        height = hTable[i];
 
219
        break;
 
220
      }
 
221
    }
 
222
    return height;
 
223
  }
 
224
 
 
225
  /// Sets the label format
 
226
  void SetFormat(const PdfLabelFormat& format)
 
227
  {
 
228
    m_metric     = format.metric;
 
229
    m_averyName  = format.name;
 
230
    m_marginLeft = ConvertMetric(format.marginLeft, m_metric, m_metricDoc);
 
231
    m_marginTop  = ConvertMetric(format.marginTop,  m_metric, m_metricDoc);
 
232
    m_xSpace     = ConvertMetric(format.xSpace,     m_metric, m_metricDoc);
 
233
    m_ySpace     = ConvertMetric(format.ySpace,     m_metric, m_metricDoc);
 
234
    m_xNumber    = format.nx;
 
235
    m_yNumber    = format.ny;
 
236
    m_width      = ConvertMetric(format.width,      m_metric, m_metricDoc);
 
237
    m_height     = ConvertMetric(format.height,     m_metric, m_metricDoc);
 
238
    SetFontSize(format.fontSize);
 
239
  }
 
240
 
 
241
private:
 
242
  wxString m_averyName;   //< Name of format
 
243
  double   m_marginLeft;  //< Left margin of labels
 
244
  double   m_marginTop;   //< Top margin of labels
 
245
  double   m_xSpace;      //< Horizontal space between 2 labels
 
246
  double   m_ySpace;      //< Vertical space between 2 labels
 
247
  int      m_xNumber;     //< Number of labels horizontally
 
248
  int      m_yNumber;     //< Number of labels vertically
 
249
  double   m_width;       //< Width of label
 
250
  double   m_height;      //< Height of label
 
251
  double   m_charSize;    //< Character size
 
252
  double   m_lineHeight;  //< Default line height
 
253
  wxString m_metric;      //< Type of metric for labels.. Will help to calculate good values
 
254
  wxString m_metricDoc;   //< Type of metric for the document
 
255
  wxString m_fontName;    //< Name of the font
 
256
 
 
257
  int      m_xCount;
 
258
  int      m_yCount;
 
259
};
 
260
 
 
261
void
 
262
labels()
 
263
{
 
264
  // To create the object, 2 possibilities:
 
265
  // either pass a custom format via an array
 
266
  // or use a built-in AVERY name
 
267
  //
 
268
  // Example of custom format; we start at the second column
 
269
  // PdfLabelFormat perso1 = { wxT("perso1"), wxPAPER_A4, wxT("mm"), 1.,         1.,        2,  7,  0.,     0.,     99.1,  38.1,   14 },
 
270
  // PdfLabel pdf(perso1, wxT("mm"), 1, 2);
 
271
  //
 
272
  // Standard format
 
273
  PdfLabel pdf(PdfLabel::FindFormat(wxT("L7163")), wxT("mm"), 1, 2);
 
274
  pdf.AddPage();
 
275
 
 
276
  // Print labels
 
277
  int i;
 
278
  for (i = 1; i <= 40; i++)
 
279
  {
 
280
    pdf.AddLabel(wxString::Format(wxT("Laurent %d\nImmeuble Titi\nav. fragonard\n06000, NICE, FRANCE"), i));
 
281
  }
 
282
  pdf.SaveAsFile(wxT("labels.pdf"));
 
283
}
 
284