Code

ec58ce1d783abe8f88d3a333a4b9510e43ba56c3
[inkscape.git] / font-instance.h
1 #ifndef SEEN_LIBNRTYPE_FONT_INSTANCE_H
2 #define SEEN_LIBNRTYPE_FONT_INSTANCE_H
4 #include <ext/hash_map>
5 #include <map>
6 #include <pango/pango-types.h>
7 #include <pango/pango-font.h>
8 #include <require-config.h>
9 #include "FontFactory.h"
11 #include <libnr/nr-forward.h>
12 #include <libnrtype/nrtype-forward.h>
13 #include <libnrtype/font-style.h>
14 #include <livarot/livarot-forward.h>
16 // the font_instance are the template of several raster_font; they provide metrics and outlines
17 // that are drawn by the raster_font, so the raster_font needs info relative to the way the 
18 // font need to be drawn. note that fontsize is a scale factor in the transform matrix
19 // of the style
20 // the various raster_font in use at a given time are held in a hash_map whose indices are the
21 // styles, hence the 2 following 'classes'
22 struct font_style_hash : public std::unary_function<font_style, size_t> {
23     size_t operator()(font_style const &x) const;
24 };
26 struct font_style_equal : public std::binary_function<font_style, font_style, bool> {
27     bool operator()(font_style const &a, font_style const &b);
28 };
30 class font_instance {
31 public:
32         // hashmap to get the raster_font for a given style
33     __gnu_cxx::hash_map<font_style, raster_font*, font_style_hash, font_style_equal>     loadedStyles;
34         // the real source of the font
35     PangoFont*            pFont;
36                 // depending on the rendering backend, different temporary data
38                 // that's the font's fingerprint; this particular PangoFontDescription gives the entry at which this font_instance
39                 // resides in the font_factory loadedFaces hash_map
40     PangoFontDescription* descr;
41                 // refcount
42     int                   refCount;
43                 // font_factory owning this font_instance
44     font_factory*         daddy;
46     // common glyph definitions for all the rasterfonts
47     std::map<int, int>    id_to_no;
48     int                   nbGlyph, maxGlyph;
49     font_glyph*           glyphs;
51     font_instance(void);
52     ~font_instance(void);
54     void                 Ref(void);
55     void                 Unref(void);
57     bool                 IsOutlineFont(void); // utility
58     void                 InstallFace(PangoFont* iFace); // utility; should reset the pFont field if loading failed
59                                         // in case the PangoFont is a bitmap font, for example. that way, the calling function 
60                                         // will be able to check the validity of the font before installing it in loadedFaces
61     void                 InitTheFace();
63     int                  MapUnicodeChar(gunichar c); // calls the relevant unicode->glyph index function
64     void                 LoadGlyph(int glyph_id);    // the main backend-dependent function
65                                         // loads the given glyph's info
66                 
67                 // nota: all coordinates returned by these functions are on a [0..1] scale; you need to multiply 
68                 // by the fontsize to get the real sizes
69     Path*                Outline(int glyph_id, Path *copyInto=NULL);
70                                         // queries the outline of the glyph (in livarot Path form), and copies it into copyInto instead
71                                         // of allocating a new Path if copyInto != NULL
72     void*                ArtBPath(int glyph_id);
73                                         // returns the artbpath for this glyph. no refcounting needed, it's deallocated when the
74                                                                                                                 // font_instance dies
75     double               Advance(int glyph_id, bool vertical);
76                                         // nominal advance of the font.
77     bool                 FontMetrics(double &ascent, double &descent, double &leading);
78     bool                 FontSlope(double &run, double &rise);
79                                 // for generating slanted cursors for oblique fonts
80     NR::Rect             BBox(int glyph_id);
82                 // creates a rasterfont for the given style
83     raster_font*         RasterFont(NR::Matrix const &trs, double stroke_width,
84                                     bool vertical = false, JoinType stroke_join = join_straight,
85                                     ButtType stroke_cap = butt_straight, float miter_limit = 4.0);
86                 // the dashes array in iStyle is copied
87     raster_font*         RasterFont(font_style const &iStyle);
88                 // private use: tells the font_instance that the raster_font 'who' has died
89     void                 RemoveRasterFont(raster_font *who);
91                 // attribute queries
92     unsigned             Name(gchar *str, unsigned size);
93     unsigned             PSName(gchar *str, unsigned size);
94     unsigned             Family(gchar *str, unsigned size);
95     unsigned             Attribute(gchar const *key, gchar *str, unsigned size);
97 private:
98     void                 FreeTheFace();
100 #ifdef USE_PANGO_WIN32
101     HFONT                 theFace;
102 #else
103     FT_Face               theFace; 
104                 // it's a pointer in fact; no worries to ref/unref it, pango does its magic
105                 // as long as pFont is valid, theFace is too
106 #endif
108 };
111 #endif /* !SEEN_LIBNRTYPE_FONT_INSTANCE_H */
113 /*
114   Local Variables:
115   mode:c++
116   c-file-style:"stroustrup"
117   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
118   indent-tabs-mode:nil
119   fill-column:99
120   End:
121 */
122 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :