Cadabra
Computer algebra system for field theory problems
TeXEngine.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <stdexcept>
5 #include <vector>
6 #include <set>
7 #include <string>
8 #include <memory>
9 
10 namespace cadabra {
11 
22 
23  class TeXEngine {
24  public:
25  class TeXException : public std::logic_error {
26  public:
27  TeXException(const std::string&);
28  };
29 
30  class TeXRequest {
31  public:
32  TeXRequest();
33  friend class TeXEngine;
34 
35  unsigned width() const;
36  unsigned height() const;
37  const std::vector<unsigned char>& image() const;
38 
40  std::string latex() const;
41 
42  private:
43  std::string latex_string;
44  std::string start_wrap, end_wrap;
46  bool has_error; // currently set, but never re-set, and unused.
47  std::vector<unsigned char> image_;
48  unsigned width_, height_;
49  };
50 
51  TeXEngine();
52  ~TeXEngine();
53 
54  // Set the width and font size for all images to be generated.
55  void set_geometry(int horizontal_pixels);
56 
57  // Get the width.
58  int get_geometry() const;
59 
60  // Set the scale factor for generating bitmaps. The total scale is
61  // the product of HiDPI scale and any text-scaling factor on top of
62  // that, so that it represents the total scale at which text renders.
63  // The device_scale is just the HiDPI factor; we need to generate
64  // bitmaps at the width times this size.
65  void set_scale(double total_scale, double device_scale);
66  double get_scale() const;
67  double get_device_scale() const;
68  void set_font_size(int font_size);
69  int get_font_size() const;
70  std::vector<std::string> latex_packages;
71 
72  // All checkin/checkout conversion routines. TeXEngine keeps
73  // track of all TeXRequests in order to be able to convert
74  // all of them in one shot (with one LaTeX run), which you can
75  // do with 'convert_all'.
76 
77  // You can share the result in a TeXRequest in multiple
78  // widgets, but you need to call checkout if a widget no
79  // longer needs it. TeXEngine will then run a cleanup on all
80  // TeXRequests that are no longer referenced except by
81  // itself.
82 
83  std::shared_ptr<TeXRequest> checkin(const std::string&,
84  const std::string& startwrap, const std::string& endwrap);
85  std::shared_ptr<TeXRequest> modify(std::shared_ptr<TeXRequest>, const std::string&);
86 
88  void convert_all();
89 
92  void invalidate_all();
93 
95  void checkout(std::shared_ptr<TeXRequest>);
96  void checkout_all();
97 
98  private:
99  std::string convert_unicode_to_tex(const std::string&) const;
100 
101  static double millimeter_per_inch;
102 
103  std::set<std::shared_ptr<TeXRequest> > requests;
104 
105  std::string preamble_string;
109 
110  void erase_file(const std::string&) const;
111  void convert_one(std::shared_ptr<TeXRequest>);
112  void convert_set(std::set<std::shared_ptr<TeXRequest> >&);
113 
114  std::string handle_latex_errors(const std::string&, int exit_code) const;
115  };
116 
117  }
Definition: TeXEngine.hh:25
TeXException(const std::string &)
Definition: TeXEngine.cc:38
Definition: TeXEngine.hh:30
unsigned width_
Definition: TeXEngine.hh:48
std::string latex() const
Get the original LaTeX string.
Definition: TeXEngine.cc:118
bool needs_generating
Definition: TeXEngine.hh:45
std::string latex_string
Definition: TeXEngine.hh:43
const std::vector< unsigned char > & image() const
Definition: TeXEngine.cc:123
bool has_error
Definition: TeXEngine.hh:46
unsigned width() const
Definition: TeXEngine.cc:108
TeXRequest()
Definition: TeXEngine.cc:267
unsigned height_
Definition: TeXEngine.hh:48
unsigned height() const
Definition: TeXEngine.cc:113
std::string end_wrap
Definition: TeXEngine.hh:44
std::vector< unsigned char > image_
Definition: TeXEngine.hh:47
std::string start_wrap
Definition: TeXEngine.hh:44
TeXEngine is used to convert LaTeX strings into PNG images.
Definition: TeXEngine.hh:23
TeXEngine()
Definition: TeXEngine.cc:188
int get_font_size() const
Definition: TeXEngine.cc:238
double get_device_scale() const
Definition: TeXEngine.cc:262
std::set< std::shared_ptr< TeXRequest > > requests
Definition: TeXEngine.hh:103
~TeXEngine()
Definition: TeXEngine.cc:184
std::string handle_latex_errors(const std::string &, int exit_code) const
Definition: TeXEngine.cc:133
void set_scale(double total_scale, double device_scale)
Definition: TeXEngine.cc:243
std::string preamble_string
Definition: TeXEngine.hh:105
void invalidate_all()
Mark all TeXRequests as needing re-generating.
Definition: TeXEngine.cc:296
void erase_file(const std::string &) const
Definition: TeXEngine.cc:128
double total_scale_
Definition: TeXEngine.hh:108
void convert_set(std::set< std::shared_ptr< TeXRequest > > &)
Definition: TeXEngine.cc:355
void convert_one(std::shared_ptr< TeXRequest >)
Definition: TeXEngine.cc:347
void convert_all()
Generate images for all TeXRequests which are labelled as needing conversion.
Definition: TeXEngine.cc:312
void checkout_all()
Definition: TeXEngine.cc:291
int horizontal_pixels_
Definition: TeXEngine.hh:106
std::vector< std::string > latex_packages
Definition: TeXEngine.hh:70
double get_scale() const
Definition: TeXEngine.cc:257
void set_font_size(int font_size)
Definition: TeXEngine.cc:224
void checkout(std::shared_ptr< TeXRequest >)
Mark a TeXRequest as no longer being needed.
Definition: TeXEngine.cc:284
int get_geometry() const
Definition: TeXEngine.cc:219
static double millimeter_per_inch
Definition: TeXEngine.hh:101
std::string convert_unicode_to_tex(const std::string &) const
Definition: TeXEngine.cc:43
std::shared_ptr< TeXRequest > checkin(const std::string &, const std::string &startwrap, const std::string &endwrap)
Definition: TeXEngine.cc:272
int font_size_
Definition: TeXEngine.hh:107
void set_geometry(int horizontal_pixels)
Definition: TeXEngine.cc:206
double device_scale_
Definition: TeXEngine.hh:108
std::shared_ptr< TeXRequest > modify(std::shared_ptr< TeXRequest >, const std::string &)
Definition: TeXEngine.cc:305
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:83
void set(rset_t::iterator &num, multiplier_t fac)
Definition: Storage.cc:1044