1 /* Dia -- an diagram creation/manipulation program
2  * Copyright (C) 1998 Alexander Larsson
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 #ifndef FONT_H
19 #define FONT_H
20 
21 #include "diatypes.h"
22 #include <glib.h>
23 #include <glib-object.h>
24 #include <pango/pango.h>
25 #include "dia-enums.h"
26 #include "geometry.h"
27 
28 G_BEGIN_DECLS
29 
30 /* Do NOT put these strings through the .po mechanism. If you need to add
31    non-Roman alternatives, please insert them in the list */
32 #define BASIC_SANS_FONT "arial, helvetica, helv, sans"
33 #define BASIC_SERIF_FONT "times new roman, times, serif"
34 #define BASIC_MONOSPACE_FONT "courier new, courier, monospace, fixed"
35 
36 
37 /**
38  * In a goodly selection of fonts, 500 is very common, yet Pango doesn't name it.
39  * I am calling 500 'medium' and 600 'demibold'.
40  * We should really have a more flexible system where 300 or 400 is normal,
41  * the next one up bold, or something.  But this will do for now.
42  * We should probably store the actual weight...
43  */
44 
45 /* We are having DIA_FONT_WEIGHT_NORMAL be 0 to avoid having to do
46  * DIA_FONT_MONOSPACE | DIA_FONT_WEIGHT_NORMAL all over the place.
47  * This introduces a few hacks in font.c and widgets.c, but not too
48  * many.
49  */
50 
51 /* storing different style info like
52  * (DIA_FONT_SANS | DIA_FONT_ITALIC | DIA_FONT_BOLD)
53  */
54 typedef guint DiaFontStyle;
55 
56 typedef enum
57 {
58   DIA_FONT_FAMILY_ANY = 0,
59   DIA_FONT_SANS       = 1,
60   DIA_FONT_SERIF      = 2,
61   DIA_FONT_MONOSPACE  = 3
62 } DiaFontFamily;
63 
64 typedef enum
65 {
66   DIA_FONT_NORMAL  = (0<<2),
67   DIA_FONT_OBLIQUE = (1<<2),
68   DIA_FONT_ITALIC  = (2<<2)
69 } DiaFontSlant;
70 
71 typedef enum
72 {
73   DIA_FONT_ULTRALIGHT    = (1<<4),
74   DIA_FONT_LIGHT         = (2<<4),
75   DIA_FONT_WEIGHT_NORMAL = (0<<4), /* Deliberately 0 for DIA_FONT_NORMAL */
76   DIA_FONT_MEDIUM        = (3<<4),
77   DIA_FONT_DEMIBOLD      = (4<<4),
78   DIA_FONT_BOLD          = (5<<4),
79   DIA_FONT_ULTRABOLD     = (6<<4),
80   DIA_FONT_HEAVY         = (7<<4)
81 } DiaFontWeight;
82 
83 /* macros to get a specific style info */
84 #define DIA_FONT_STYLE_GET_FAMILY(st)    ((st) & (0x3))
85 #define DIA_FONT_STYLE_GET_SLANT(st)     ((st) & (0x3<<2))
86 #define DIA_FONT_STYLE_GET_WEIGHT(st)    ((st) & (0x7<<4))
87 
88 GType dia_font_get_type(void) G_GNUC_CONST;
89 
90 #define DIA_TYPE_FONT (dia_font_get_type())
91 
92 #define DIA_FONT(object)    (G_TYPE_CHECK_INSTANCE_CAST ((object), \
93                                                          DIA_TYPE_FONT, \
94                                                          DiaFont))
95 #define DIA_FONT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), \
96                                                         DIA_TYPE_FONT, \
97                                                         DiaFontClass))
98 
99 struct _DiaFontClass {
100     GObjectClass parent_class;
101 };
102 
103 /* Set the PangoContext used to render text.
104  */
105 void dia_font_init(PangoContext* pcontext);
106 /* Start using a new context (for AA rendering) */
107 void dia_font_push_context(PangoContext *pcontext);
108 /* Go back to using the old context */
109 void dia_font_pop_context(void);
110 /* Retrieve the current context (used for the font widget) */
111 PangoContext *dia_font_get_context(void);
112 
113     /* Get a font matching family,style,height. MUST be freed with
114        dia_font_unref(). */
115 DiaFont* dia_font_new(const char *family, DiaFontStyle style,
116                       real height);
117 
118     /* Get a font matching style. This is the preferred method to
119      * create default fonts within objects. */
120 DiaFont* dia_font_new_from_style(DiaFontStyle style, real height);
121 
122     /* Get a font from a legacy font name */
123 DiaFont* dia_font_new_from_legacy_name(const char *name);
124 
125     /* Get a simple font name from a font.
126        Name will be valid for the duration of the DiaFont* lifetime. */
127 G_CONST_RETURN char* dia_font_get_legacy_name(const DiaFont* font);
128 
129     /* Same attributes */
130 DiaFont *dia_font_copy(const DiaFont* font);
131 
132 DiaFont* dia_font_ref(DiaFont* font);
133 void dia_font_unref(DiaFont* font);
134 
135     /* Retrieves the style of the font */
136 DiaFontStyle dia_font_get_style(const DiaFont* font);
137 
138     /* Retrieves the family of the font. Caller must NOT free. */
139 G_CONST_RETURN char* dia_font_get_family(const DiaFont* font);
140 /* Acessor for the PangoFontDescription */
141 G_CONST_RETURN PangoFontDescription *dia_font_get_description (const DiaFont* font);
142 
143     /* Retrieves the height of the font */
144 real dia_font_get_height(const DiaFont* font);
145     /* Change the height inside a font record. */
146 void dia_font_set_height(DiaFont* font, real height);
147     /* Delivers the size of the font */
148 real dia_font_get_size(const DiaFont* font);
149 
150     /* Changes the slant of an existing font */
151 void dia_font_set_slant(DiaFont* font, DiaFontSlant slant);
152     /* Changes the weight of an existing font */
153 void dia_font_set_weight(DiaFont* font, DiaFontWeight weight);
154     /* Changes the family of an existing font to one of the three standard
155        families */
156 void dia_font_set_family(DiaFont* font, DiaFontFamily family);
157     /* Changes the family of an existing font to any family, but the name could
158        be system-dependent. */
159 void dia_font_set_any_family(DiaFont* font, const char* family);
160 
161     /* FIXME: what do we do with this, actually ?
162        Name lives for as long as the DiaFont lives. */
163 G_CONST_RETURN char *dia_font_get_psfontname(const DiaFont *font);
164 
165     /* returns a static string suitable for SVG */
166 G_CONST_RETURN char *dia_font_get_weight_string(const DiaFont* font);
167 
168     /* returns a static string suitable for SVG */
169 G_CONST_RETURN char *dia_font_get_slant_string(const DiaFont* font);
170 
171     /* uses an SVG style string */
172 void dia_font_set_weight_from_string(DiaFont* font, const char* weight);
173 
174     /* uses an SVG style string */
175 void dia_font_set_slant_from_string(DiaFont* font, const char* slant);
176 
177 /* -------- Font and string functions - unscaled versions.
178    Use these version in Objects, primarily. */
179 
180     /* Get the width of the string with the given font in cm */
181 real dia_font_string_width(const char* string, DiaFont* font,
182                           real height);
183     /* Get the ascent of this string in cm (a positive number). */
184 real dia_font_ascent(const char* string, DiaFont* font, real height);
185     /* Get the descent of the font in cm (a positive number) */
186 real dia_font_descent(const char* string, DiaFont* font, real height);
187 
188     /* prepares a layout of the text, in font 'font'. */
189 PangoLayout* dia_font_build_layout(const char* string, DiaFont* font,
190                                    real height);
191 /* prepares a layout of the text, in font 'font' scaled by matrix. */
192 PangoLayout* dia_font_build_layout_with_matrix(const char* string,
193 					       DiaFont* font,
194 					       real height,
195 					       PangoMatrix *matrix);
196 
197 real* dia_font_get_sizes(const char* string, DiaFont *font, real height,
198 			 real* width, real* ascent, real* descent,
199 			 int *n_offsets, PangoLayoutLine **layout_offsets);
200 
201 /* -------- Font and string functions - scaled versions.
202    Use these version in Renderers, exclusively. */
203 
204 G_END_DECLS
205 
206 #endif /* FONT_H */
207