Code

Avoid crash by uninitialized perspectives.
[inkscape.git] / src / libnrtype / FontFactory.h
1 /*
2  *  FontFactory.h
3  *  testICU
4  *
5  */
7 #ifndef my_font_factory
8 #define my_font_factory
10 //#include <glibmm/ustring.h>
12 #include <functional>
13 #include <algorithm>
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18 #ifdef _WIN32
19 #define USE_PANGO_WIN32
20 #endif
22 #include <pango/pango.h>
23 #include "nr-type-primitives.h"
24 #include "nr-type-pos-def.h"
25 #include "font-style-to-pos.h"
26 #include <libnrtype/nrtype-forward.h>
27 #include "../style.h"
29 /* Freetype */
30 #ifdef USE_PANGO_WIN32
31 #include <pango/pangowin32.h>
32 #else
33 #include <pango/pangoft2.h>
34 #include <freetype/freetype.h>
35 #endif
37 namespace Glib
38 {
39     class ustring;
40 }
42 // the font_factory keeps a hashmap of all the loaded font_instances, and uses the PangoFontDescription
43 // as index (nota: since pango already does that, using the PangoFont could work too)
44 struct font_descr_hash : public std::unary_function<PangoFontDescription*,size_t> {
45     size_t operator()(PangoFontDescription *const &x) const;
46 };
47 struct font_descr_equal : public std::binary_function<PangoFontDescription*, PangoFontDescription*, bool> {
48     bool operator()(PangoFontDescription *const &a, PangoFontDescription *const &b);
49 };
51 // Comparison functions for style names
52 int style_name_compare(char const *aa, char const *bb);
53 int family_name_compare(char const *a, char const *b);
55 // Map type for gathering UI family and style strings
56 typedef std::map<Glib::ustring, std::list<Glib::ustring> > FamilyToStylesMap;
58 class font_factory {
59 public:
60     static font_factory *lUsine; /**< The default font_factory; i cannot think of why we would
61                                   *   need more than one.
62                                   *
63                                   *   ("l'usine" is french for "the factory".)
64                                   */
66     /** A little cache for fonts, so that you don't loose your time looking up fonts in the font list
67      *  each font in the cache is refcounted once (and deref'd when removed from the cache). */
68     struct font_entry {
69         font_instance *f;
70         double age;
71     };
72     int nbEnt;   ///< Number of entries.
73     int maxEnt;  ///< Cache size.
74     font_entry *ents;
76     // Pango data.  Backend-specific structures are cast to these opaque types.
77     PangoFontMap *fontServer;
78     PangoContext *fontContext;
79 #ifdef USE_PANGO_WIN32
80     PangoWin32FontCache *pangoFontCache;
81     HDC hScreenDC;
82 #endif
83     double fontSize; /**< The huge fontsize used as workaround for hinting.
84                       *   Different between freetype and win32. */
86     font_factory();
87     virtual ~font_factory();
89     /// Returns the default font_factory.
90     static font_factory*  Default();
92     /// Constructs a pango string for use with the fontStringMap (see below)
93     Glib::ustring         ConstructFontSpecification(PangoFontDescription *font);
94     Glib::ustring         ConstructFontSpecification(font_instance *font);
96     /// Returns strings to be used in the UI for family and face (or "style" as the column is labeled)
97     Glib::ustring         GetUIFamilyString(PangoFontDescription const *fontDescr);
98     Glib::ustring         GetUIStyleString(PangoFontDescription const *fontDescr);
100     /// Modifiers for the font specification (returns new font specification)
101     Glib::ustring         ReplaceFontSpecificationFamily(const Glib::ustring & fontSpec, const Glib::ustring & newFamily);
102     Glib::ustring         FontSpecificationSetItalic(const Glib::ustring & fontSpec, bool turnOn);
103     Glib::ustring         FontSpecificationSetBold(const Glib::ustring & fontSpec, bool turnOn);
105     // Gathers all strings needed for UI while storing pango information in
106     // fontInstanceMap and fontStringMap
107     void                  GetUIFamiliesAndStyles(FamilyToStylesMap *map);
109     /// Retrieve a font_instance from a style object, first trying to use the font-specification, the CSS information
110     font_instance*        FaceFromStyle(SPStyle const *style);
112     // Various functions to get a font_instance from different descriptions.
113     font_instance*        FaceFromDescr(char const *family, char const *style);
114     font_instance*        FaceFromUIStrings(char const *uiFamily, char const *uiStyle);
115     font_instance*        FaceFromPangoString(char const *pangoString);
116     font_instance*        FaceFromFontSpecification(char const *fontSpecification);
117     font_instance*        Face(PangoFontDescription *descr, bool canFail=true);
118     font_instance*        Face(char const *family,
119                                int variant=PANGO_VARIANT_NORMAL, int style=PANGO_STYLE_NORMAL,
120                                int weight=PANGO_WEIGHT_NORMAL, int stretch=PANGO_STRETCH_NORMAL,
121                                int size=10, int spacing=0);
122     font_instance*        Face(char const *family, NRTypePosDef apos);
124     /// Semi-private: tells the font_factory taht the font_instance 'who' has died and should be removed from loadedFaces
125     void                  UnrefFace(font_instance* who);
127     // internal
128     void                  AddInCache(font_instance *who);
130 private:
131     void*                 loadedPtr;
133     // These two maps are used for translating between what's in the UI and a pango
134     // font description.  This is necessary because Pango cannot always
135     // reproduce these structures from the names it gave us in the first place.
137     // Key: A string produced by font_factory::ConstructFontSpecification
138     // Value: The associated PangoFontDescription
139     typedef std::map<Glib::ustring, PangoFontDescription *> PangoStringToDescrMap;
140     PangoStringToDescrMap fontInstanceMap;
142     // Key: Family name in UI + Style name in UI
143     // Value: The associated string that should be produced with font_factory::ConstructFontSpecification
144     typedef std::map<Glib::ustring, Glib::ustring> UIStringToPangoStringMap;
145     UIStringToPangoStringMap fontStringMap;
146 };
149 #endif /* my_font_factory */
152 /*
153   Local Variables:
154   mode:c++
155   c-file-style:"stroustrup"
156   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
157   indent-tabs-mode:nil
158   fill-column:99
159   End:
160 */
161 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :