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

Go to the SVN repository for this file.

1 /* $Id: phylo_pdf_grabber.cpp 32681 2015-04-10 15:14:16Z 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: Bob Falk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
35 #include <gui/opengl/glresmgr.hpp>
37 #include <gui/print/pdf.hpp>
39 
40 #include <util/image/image.hpp>
41 
42 #include <corelib/ncbifile.hpp>
43 
44 
45 
47 
49 : m_Pane(pane)
50 , m_SaveTooltips(false)
51 {
53 }
54 
55 
58 {
59  // progress was more for the tiled image grabber so you could see images as they were genereated
60  // on the dialog. For PDF we wuld just use the pane to set the context.
61  if (progress != NULL)
62  progress->SetGLContext();
63  else
65 
66  x_BeginCaptures(tex_size);
67 
68  string pdfname = CDir::ConcatPath(m_Directory, m_BaseImageName + "." + m_ImageFormat);
69  CNcbiOfstream pstrm(pdfname.c_str(), ios::out | ios::binary);
70  m_Pdf->SetOutputStream(&pstrm);
71 
72 
73  //***************************************************
74 
76  m_Pdf->BeginPage();
78  m_Pdf->EndPage();
79 
80  m_Pdf->EndDocument();
81  x_EndCaptures();
82 
83  // Show on dialog widget that the image has been saved
84  if (progress != NULL)
85  progress->ImageSaved(0,0);
86 
88 
89  return eSuccess;
90 }
91 
92 void CPhyloPdfGrabber::x_BeginCaptures(int /*buffer_size*/)
93 {
94  // Save some current rendering values we want to change
98 
99  int viewport_x, viewport_y;
100 
101  m_Pdf.Reset(new CPdf());
102 
103  CPrintOptions po;
105  string pdfname = CDir::ConcatPath(m_Directory, m_BaseImageName + "." + m_ImageFormat);
106  po.SetFilename(pdfname.c_str());
107 
108  // Set margins for pdf to 0 since our own rendered image will have its own margins
109  // as specified in CPhyloTreeScheme.
114  po.SetPagesWide(1);
115  po.SetPagesTall(1);
116 
119  TVPRect viewport = m_Pane.GetPane().GetViewport();
120 
121  // Use the ratio of visible area to total area to determine
122  // the level of zoom, then expand the viewport to accomodate
123  // the total view
124  double ratiox = vis_rect.Width()/full_rect.Width();
125  double ratioy = vis_rect.Height()/full_rect.Height();
126  viewport_x = (1.0/ratiox)*double(viewport.Width());
127  viewport_y = (1.0/ratioy)*double(viewport.Height());
128 
129  float maxdim = std::max(viewport_x, viewport_y);
130  if (maxdim > 14000.0f) {
131  float scaler = 14000.0f/maxdim;
132  float user_unit = 1.0f/scaler;
133  po.SetUserUnit(user_unit);
134  m_Pane.GetPane().SetOutputUnitScaler(user_unit);
135 
136  viewport_x *= scaler;
137  viewport_x = std::max(viewport_x, 100);
138 
139  viewport_y *= scaler;
140  viewport_y = std::max(viewport_y, 100);
141  }
142 
143  CMedia m(m_OutputFormat, viewport_x, viewport_y, CUnit::ePdfUnit);
144  po.SetMedia(m);
145 
146  CGlRect<int> vp(0, 0, viewport_x, viewport_y);
147  m_Pane.GetPane().SetViewport(vp);
148 
150  po.SetPagesTall(1);
151  po.SetPagesWide(1);
152  m_Pdf->SetOptions(po);
153 
155 }
156 
158 {
161 }
162 
163 
164 
#define false
Definition: bool.h:36
Definition: pdf.hpp:62
virtual void BeginPage(void)
Definition: pdf.cpp:204
virtual void EndPage(void)
Definition: pdf.cpp:208
virtual void BeginDocument(void)
Definition: pdf.cpp:88
virtual void SetOptions(const CPrintOptions &options)
Definition: pdf.cpp:74
virtual void SetOutputStream(CNcbiOstream *ostream)
Definition: pdf.cpp:81
virtual void EndDocument(void)
Definition: pdf.cpp:1471
CPhyloTreeScheme::TLabelTruncation m_LabelsTruncated
void x_EndCaptures()
Called to restore values (in pane) when capturing stops.
virtual eCaptureResult GrabImages(int tex_size, IImageGrabberProgress *p=NULL)
Grab the requested images.
void x_BeginCaptures(int buffer_size)
Called to setup values before image capturing starts.
CGlPane m_PreviousGlPane
We change some rendering options.
CPhyloTreePane & m_Pane
Pane display canvas for the tree we are saving.
bool m_SaveTooltips
If true, tooltips will be generated for the pdf as well.
CPhyloPdfGrabber(CPhyloTreePane &pane)
class CPhyloTreePane
virtual void SetContext(void)
CGlPane & GetPane()
void RenderPdf(CRef< CPdf > pdf, bool render_tooltips)
IPhyloTreeRender * GetCurrRenderer(void)
TLabelTruncation & GetLabelTruncation()
void SetLabelTruncation(TLabelTruncation lt)
CUnit::TPdfUnit TPdfUnit
void SetUserUnit(TPdfUnit userunit)
void SetPagesTall(unsigned int w)
void SetFilename(const string &filename)
void SetMedia(const CMedia &media)
void SetMarginLeft(TPdfUnit h)
void SetOutputFormat(TOutputFormat fmt)
void SetPagesWide(unsigned int w)
void SetPageOrientation(TPageOrientation orient)
void SetMarginTop(TPdfUnit h)
void SetMarginRight(TPdfUnit w)
void SetMarginBottom(TPdfUnit w)
@ ePdfUnit
Definition: print_utils.hpp:52
virtual void ImageSaved(int, int)
virtual void SetGLContext()
std::string m_BaseImageName
Image name without its appended number (e.g. img for img1, img2...)
std::string m_Directory
Target directory for saved images.
std::string m_ImageFormat
Image output format (e.g. jpeg, png...)
string m_OutputFormat
Name of output format, e.g. A4, US Letter..
CPhyloTreeScheme & GetScheme(void)
std::ofstream out("events_result.xml")
main entry point for tests
#define NULL
Definition: ncbistd.hpp:225
static string ConcatPath(const string &first, const string &second)
Concatenate two parts of the path for the current OS.
Definition: ncbifile.cpp:776
T Height() const
Definition: glrect.hpp:90
void SetViewport(const TVPRect &R)
Definition: glpane.cpp:96
T Width() const
Definition: glrect.hpp:86
TVPRect & GetViewport(void)
Definition: glpane.hpp:332
TModelRect & GetModelLimitsRect(void)
Definition: glpane.hpp:347
TModelRect & GetVisibleRect(void)
Definition: glpane.hpp:357
void SetOutputUnitScaler(TModelUnit u)
Set/get output scaler.
Definition: glpane.hpp:236
TObjectType * ReleaseOrNull(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:816
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#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::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
T max(T x_, T y_)
Modified on Sat Dec 02 09:22:21 2023 by modify_doxy.py rev. 669887