gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
gmshPopplerWrapper.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include "gmshPopplerWrapper.h"
7 
8 #if defined(HAVE_POPPLER)
9 
10 #include "GmshMessage.h"
11 #include "FunctionManager.h"
12 #include <poppler/cpp/poppler-image.h>
13 #include <poppler/cpp/poppler-page-renderer.h>
14 #if defined(HAVE_FLTK)
15 #include <FL/Fl_JPEG_Image.H>
16 #endif
17 
18 gmshPopplerWrapper *gmshPopplerWrapper::_instance = 0;
19 std::map<int, std::pair<GModel *, std::string> > gmshPopplerWrapper::_macros;
20 poppler::document *gmshPopplerWrapper::_currentDoc = 0;
21 #if defined(HAVE_OPENGL)
22 std::map<int, GLuint> gmshPopplerWrapper::_pages2textures;
23 int gmshPopplerWrapper::_w = -1;
24 int gmshPopplerWrapper::_h = -1;
25 int gmshPopplerWrapper::_currentPage = 0;
26 #endif
27 
28 gmshPopplerWrapper *gmshPopplerWrapper::instance()
29 {
30  if(!_instance) _instance = new gmshPopplerWrapper;
31  return _instance;
32 }
33 
34 int gmshPopplerWrapper::loadFromFile(const std::string &fileName,
35  const std::string &ownerPassword,
36  const std::string &userPassword)
37 {
38  if(_currentDoc) delete _currentDoc;
39 
40  Msg::Info("Loading PDF file `%s'...", fileName.c_str());
41  _currentDoc =
42  poppler::document::load_from_file(fileName, ownerPassword, userPassword);
43  if(!_currentDoc) return 0;
44 
45  Msg::Info("Loaded PDF file `%s'", fileName.c_str());
46  // createBitmap(1,72.,72.,-1,-1,-1,-1);
47  return 1;
48 }
49 
50 int gmshPopplerWrapper::getNumPages()
51 {
52  if(!_currentDoc) return 0;
53  return _currentDoc->pages();
54 }
55 
56 #if defined(HAVE_OPENGL)
57 GLuint gmshPopplerWrapper::getTextureForPage(double xres, double yres)
58 {
59  int iPage = _currentPage;
60  int numPages = getNumPages();
61  if(iPage < 0) iPage = 0;
62  if(iPage > numPages - 1) iPage = numPages - 1;
63  auto it = _pages2textures.find(iPage);
64  if(it != _pages2textures.end()) return it->second;
65  if(!_currentDoc) return 0;
66 
67  poppler::page *page = _currentDoc->create_page(iPage);
68  poppler::page_renderer pr;
69  pr.set_render_hint(poppler::page_renderer::text_antialiasing, true);
70  pr.set_render_hint(poppler::page_renderer::antialiasing, true);
71  poppler::image im = pr.render_page(page, xres, yres, -1, -1, -1);
72  im.save("hop.jpeg", "jpeg");
73  Fl_RGB_Image *img = new Fl_JPEG_Image("hop.jpeg");
74  // Fl_RGB_Image *img2 = (Fl_RGB_Image*)img->copy(2048, 2048);
75  glPixelStorei(GL_UNPACK_ROW_LENGTH, img->w());
76  GLuint texture;
77  glGenTextures(1, &texture);
78  glBindTexture(GL_TEXTURE_2D, texture);
79  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->w(), img->h(), 0,
82  (img->d() == 4) ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,
83  img->array);
84  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
85 
86  _w = img->w();
87  _h = img->h();
88 
89  delete img;
90  // delete img2;
91  _pages2textures[iPage] = texture;
92  return texture;
93 }
94 #endif
95 
96 #endif
Msg::Info
static void Info(const char *fmt,...)
Definition: GmshMessage.cpp:587
gmshPopplerWrapper.h
FunctionManager.h
GmshMessage.h