NCBI C++ ToolKit
pdf_element.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: pdf_element.cpp 29243 2013-11-18 19:56:58Z falkrb $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software / database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software / database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Peter Meric
27  *
28  * File Description:
29  * CPdfElement - Stream for output of Adobe PDF objects
30  */
31 
32 #include <ncbi_pch.hpp>
34 #include <gui/print/pdf_object.hpp>
35 #include "pdf_object_writer.hpp"
36 #include <math.h>
37 
38 
40 
41 
42 
43 CPdfElement::CPdfElement(const string& str) : m_Str(str)
44 {
45 }
46 
47 
49 {
50 }
51 
52 
54 {
55 }
56 
57 
59 {
60  stream << m_Str;
61 }
62 
63 
64 string CPdfElement::GetValue(void) const
65 {
66  return m_Str;
67 }
68 
69 
71  : m_Num(i)
72  , m_Precision(precision)
73 {
74 }
75 
76 
78  : m_Num(i)
79  , m_Precision(precision)
80 {
81 }
82 
83 
85  : m_Num(d)
86  , m_Precision(precision)
87 {
88 }
89 
90 
92 {
93 }
94 
95 
96 void CPdfNumber::PrintTo(CNcbiOstream& stream) const
97 {
98  char buf[256];
99 
100  // Precision not specified, use the default (6) which is pretty high.
101  // We don't pick a smaller one since the needed precision varies
102  // based on viewing matrices.
103  if (m_Precision == -1) {
104  ::sprintf(buf, "%f", m_Num);
105  }
106  else {
107  // But if user asks for a precision, use it.
108  ::sprintf(buf, "%.*f", m_Precision, m_Num);
109  }
110 
111  // Remove any trailing 0's since they waste space in the file, e.g
112  // 3.950=> 3.95, 6.000 => 6, 90=>90 and 0.=>0
113  if (strchr(buf, '.') != NULL) {
114  char *pos;
115  int len = (int)strlen(buf);
116 
117  pos = buf + len - 1;
118 
119  while(*pos == '0') {
120  *pos-- = '\0';
121  }
122 
123  if(*pos == '.') {
124  *pos = '\0';
125  }
126  }
127 
128  stream << buf;
129 }
130 
131 
132 CPdfRotate::CPdfRotate(double angle) : m_Rot(angle)
133 {
134 }
135 
136 
138 {
139 }
140 
141 
143 {
144  const double rad = m_Rot * 3.1415926535 / 180;
145  const double c = cos(rad);
146  const double s = sin(rad);
147  stream << setiosflags(ios::fixed) << CPdfNumber(c) <<
148  ' ' << CPdfNumber(s) << ' ' << CPdfNumber(-s) << ' ' << CPdfNumber(c) << " 0 0 cm" << pdfeol;
149 }
150 
151 
152 CPdfTranslate::CPdfTranslate(double xoff, double yoff)
153  : m_XOff(xoff),
154  m_YOff(yoff)
155 {
156 }
157 
158 
159 CPdfTranslate::CPdfTranslate(unsigned int xoff, unsigned int yoff)
160  : m_XOff(xoff),
161  m_YOff(yoff)
162 {
163 }
164 
165 
166 CPdfTranslate::CPdfTranslate(int xoff, int yoff)
167  : m_XOff(xoff),
168  m_YOff(yoff)
169 {
170 }
171 
172 
174 {
175 }
176 
177 
179 {
180  stream << "1 0 0 1 " << setiosflags(ios::fixed)
181  << CPdfNumber(m_XOff) << ' ' << CPdfNumber(m_YOff) << " cm" << pdfeol;
182 }
183 
185 : m_Mat(m)
186 {
187 }
188 
190 {
191  stream << setiosflags(ios::fixed);
192 
193  // Matrices need a little extra precision than other numbers (can get errors if viewing
194  // area is very large)
195  for (int c=0; c<3; ++c)
196  for (int r=0; r<2; ++r)
197  stream << CPdfNumber(m_Mat(r,c), 12) << " ";
198 
199  stream << " cm" << pdfeol;
200 }
201 
202 
204 : m_Mat(m)
205 {
206 }
207 
209 {
210  stream << setiosflags(ios::fixed);
211 
212  for (int c=0; c<3; ++c)
213  for (int r=0; r<2; ++r) {
214  stream << CPdfNumber(m_Mat(r,c), 12) << " ";
215  }
216 
217  stream << " Tm" << pdfeol;
218 }
219 
220 
221 
223 {
224 }
225 
226 
228 {
229 }
230 
231 
233 {
234  stream << '(' << m_Str << ')';
235 }
236 
237 
239 {
240 }
241 
242 
244 {
245 }
246 
247 
248 void CPdfName::PrintTo(CNcbiOstream& stream) const
249 {
250  stream << '/' << m_Str;
251 }
252 
253 
255 {
256 }
257 
258 
260 {
261 }
262 
263 
265 {
266  stream << m_Obj->GetObjNum() << ' ' << m_Obj->GetGeneration() << " R";
267 }
268 
269 
271 {
272  return m_Array;
273 }
274 
275 
276 void CPdfArray::PrintTo(CNcbiOstream& stream) const
277 {
278  if (m_Array.size() == 0) {
279  return;
280  }
281 
282  stream << '[';
283 
284  bool first = true;
285  ITERATE(vector<TPdfEltRef>, it, m_Array) {
286  if (first) {
287  first = false;
288  } else {
289  stream << ' ';
290  }
291 
292  stream << **it;
293  }
294 
295  stream << ']' << pdfeol;
296 }
297 
298 
300 {
301  m_Array.insert(m_Array.end(),
302  array->m_Array.begin(), array->m_Array.end());
303 }
304 
305 
307 {
308  return m_Dict[key];
309 }
310 
311 
313 {
314  if (m_Dict.size() == 0) {
315  return;
316  }
317 
318  stream << "<<";
319 
320  ITERATE(TDict, it, m_Dict) {
321  const CPdfElement* val = it->second.GetPointer();
322 
323  bool insert_space = false;
324  if (dynamic_cast < const CPdfIndirectObj*>(val) ||
325  dynamic_cast < const CPdfNumber*>(val)) {
326  insert_space = true;
327  }
328 
329  stream << '/' << it->first;
330  if (insert_space) {
331  stream << ' ';
332  }
333  stream << *val;
334  }
335 
336  stream << ">>" << pdfeol;
337 }
338 
339 
341 {
342  //
343  // std::map::insert(beg, end) doesn't seem to work on MSVC++ 6.0.
344  // Iterate "manually" and copy each element instead.
345  //
346 
347  ITERATE(TDict, it, dict->m_Dict) {
348  m_Dict.insert(*it);
349  }
350 }
351 
353 {
354  return "sh" + NStr::IntToString(m_MaxShaderID++);
355 }
356 
357 
358 
virtual void PrintTo(CNcbiOstream &stream) const
void Add(const CRef< CPdfArray > &arr)
TArray & GetArray(void)
vector< TPdfEltRef > TArray
TArray m_Array
string GenShaderID()
Get a new (unique) shader ID of form sh##.
void Add(const CRef< CPdfDictionary > &dict)
TPdfEltRef & operator[](const string &key)
virtual void PrintTo(CNcbiOstream &stream) const
int m_MaxShaderID
Shaders need unique ID strings which will use this #.
string m_Str
Definition: pdf_element.hpp:63
virtual ~CPdfElement()
Definition: pdf_element.cpp:53
virtual void PrintTo(CNcbiOstream &stream) const
Definition: pdf_element.cpp:58
virtual string GetValue(void) const
Definition: pdf_element.cpp:64
virtual void PrintTo(CNcbiOstream &stream) const
CPdfIndirectObj(const CRef< CPdfObject > &obj)
virtual ~CPdfIndirectObj()
CRef< CPdfObject > m_Obj
virtual void PrintTo(CNcbiOstream &stream) const
CPdfName(const string &str)
virtual ~CPdfName()
double m_Num
virtual void PrintTo(CNcbiOstream &stream) const
Definition: pdf_element.cpp:96
virtual ~CPdfNumber()
Definition: pdf_element.cpp:91
CPdfNumber(double d, int precision=-1)
Definition: pdf_element.cpp:84
unsigned int GetObjNum(void) const
Definition: pdf_object.cpp:478
unsigned int GetGeneration(void) const
Definition: pdf_object.cpp:484
virtual void PrintTo(CNcbiOstream &stream) const
virtual ~CPdfRotate()
double m_Rot
Definition: pdf_element.hpp:83
CPdfRotate(double angle)
CPdfString(const string &str)
virtual ~CPdfString()
virtual void PrintTo(CNcbiOstream &stream) const
virtual void PrintTo(CNcbiOstream &stream) const
CPdfTextTransform(const CMatrix3< double > &m)
CMatrix3< double > m_Mat
CMatrix3< double > m_Mat
virtual void PrintTo(CNcbiOstream &stream) const
CPdfTransform(const CMatrix3< double > &m)
virtual void PrintTo(CNcbiOstream &stream) const
virtual ~CPdfTranslate()
CPdfTranslate(double xoff, double yoff)
size_type size() const
Definition: map.hpp:148
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
static char precision
Definition: genparams.c:28
static const char * str(char *buf, int n)
Definition: stats.c:84
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
char * buf
int i
int len
const struct ncbi::grid::netcache::search::fields::KEY key
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
CNcbiOstream & pdfeol(CNcbiOstream &strm)
Modified on Mon Jun 17 05:10:51 2024 by modify_doxy.py rev. 669887