summaryrefslogtreecommitdiff
path: root/iup/src/gtk/iupgtk_font.c
blob: 1139f621958c5070686ea206ba4b830378fea0cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/** \file
 * \brief GTK Font mapping
 *
 * See Copyright Notice in "iup.h"
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <gtk/gtk.h>

#include "iup.h"

#include "iup_str.h"
#include "iup_attrib.h"
#include "iup_array.h"
#include "iup_object.h"
#include "iup_drv.h"
#include "iup_drvfont.h"
#include "iup_assert.h"

#include "iupgtk_drv.h"


typedef struct _IgtkFont
{
  char standardfont[200];
  PangoFontDescription* fontdesc;
  PangoAttribute* strikethrough;
  PangoAttribute* underline;
  PangoLayout* layout;
  int charwidth, charheight;
} IgtkFont;

static Iarray* gtk_fonts = NULL;
static PangoContext *gtk_fonts_context = NULL;

static void gtkFontUpdate(IgtkFont* gtkfont)
{
  PangoAttrList *attrs;

  pango_layout_set_font_description(gtkfont->layout, gtkfont->fontdesc);

  attrs = pango_layout_get_attributes(gtkfont->layout);
  if (!attrs) 
  {
    attrs = pango_attr_list_new();
    pango_attr_list_insert(attrs, pango_attribute_copy(gtkfont->strikethrough));
    pango_attr_list_insert(attrs, pango_attribute_copy(gtkfont->underline));
    pango_layout_set_attributes(gtkfont->layout, attrs);
  }
  else
  {
    pango_attr_list_change(attrs, pango_attribute_copy(gtkfont->strikethrough));
    pango_attr_list_change(attrs, pango_attribute_copy(gtkfont->underline));
  }
}

static IgtkFont* gtkFindFont(const char *standardfont)
{
  PangoFontMetrics* metrics;
  PangoFontDescription* fontdesc;
  int i, 
      is_underline = 0,
      is_strikeout = 0,
      count = iupArrayCount(gtk_fonts);

  IgtkFont* fonts = (IgtkFont*)iupArrayGetData(gtk_fonts);

  /* Check if the standardfont already exists in cache */
  for (i = 0; i < count; i++)
  {
    if (iupStrEqualNoCase(standardfont, fonts[i].standardfont))
      return &fonts[i];
  }

  /* not found, create a new one */
  {
    int size = 0, is_pango = 0;
    int is_bold = 0,
      is_italic = 0;
    char typeface[1024];
    const char* mapped_name;

    /* parse the old Windows format first */
    if (!iupFontParseWin(standardfont, typeface, &size, &is_bold, &is_italic, &is_underline, &is_strikeout))
    {
      if (!iupFontParseX(standardfont, typeface, &size, &is_bold, &is_italic, &is_underline, &is_strikeout))
      {
        if (!iupFontParsePango(standardfont, typeface, &size, &is_bold, &is_italic, &is_underline, &is_strikeout))
          return NULL;
        else
          is_pango = 1;
      }
    }

    mapped_name = iupFontGetPangoName(typeface);
    if (mapped_name)
      strcpy(typeface, mapped_name);

    if (is_pango && !is_underline && !is_strikeout && size>0)
      fontdesc = pango_font_description_from_string(standardfont);
    else
    {
      char new_standardfont[200];
      if (size<0)
      {
        double res = ((double)gdk_screen_get_width(gdk_screen_get_default()) / (double)gdk_screen_get_width_mm(gdk_screen_get_default())); /* pixels/mm */
        /* 1 point = 1/72 inch     1 inch = 25.4 mm */
        /* pixel = ((point/72)*25.4)*pixel/mm */
        size = (int)((-size/res)*2.83464567 + 0.5); /* from pixels to points */
      }

      sprintf(new_standardfont, "%s, %s%s%d", typeface, is_bold?"Bold ":"", is_italic?"Italic ":"", size);

      fontdesc = pango_font_description_from_string(new_standardfont);
    }
  }

  if (!fontdesc) 
    return NULL;

  /* create room in the array */
  fonts = (IgtkFont*)iupArrayInc(gtk_fonts);

  strcpy(fonts[i].standardfont, standardfont);
  fonts[i].fontdesc = fontdesc;
  fonts[i].strikethrough = pango_attr_strikethrough_new(is_strikeout? TRUE: FALSE);
  fonts[i].underline = pango_attr_underline_new(is_underline? PANGO_UNDERLINE_SINGLE: PANGO_UNDERLINE_NONE);
  fonts[i].layout = pango_layout_new(gtk_fonts_context);

  metrics = pango_context_get_metrics(gtk_fonts_context, fontdesc, pango_context_get_language(gtk_fonts_context));
  fonts[i].charheight = pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics);
  fonts[i].charheight = iupGTK_PANGOUNITS2PIXELS(fonts[i].charheight);
  fonts[i].charwidth = pango_font_metrics_get_approximate_char_width(metrics);
  fonts[i].charwidth = iupGTK_PANGOUNITS2PIXELS(fonts[i].charwidth);
  pango_font_metrics_unref(metrics); 

  gtkFontUpdate(&(fonts[i]));

  return &fonts[i];
}

static PangoLayout* gtkFontGetWidgetPangoLayout(Ihandle *ih)
{
  int inherit;
  char *def_value;
  /* only check the  native implementation */
  return (PangoLayout*)iupClassObjectGetAttribute(ih, "PANGOLAYOUT", &def_value, &inherit);
}

static IgtkFont* gtkFontCreateNativeFont(Ihandle* ih, const char* value)
{
  IgtkFont *gtkfont = gtkFindFont(value);
  if (!gtkfont)
  {
    iupERROR1("Failed to create Font: %s", value); 
    return NULL;
  }

  iupAttribSetStr(ih, "_IUP_GTKFONT", (char*)gtkfont);
  return gtkfont;
}

static IgtkFont* gtkFontGet(Ihandle *ih)
{
  IgtkFont* gtkfont = (IgtkFont*)iupAttribGet(ih, "_IUP_GTKFONT");
  if (!gtkfont)
    gtkfont = gtkFontCreateNativeFont(ih, iupGetFontAttrib(ih));
  return gtkfont;
}

void iupgtkFontUpdatePangoLayout(Ihandle* ih, PangoLayout* layout)
{
  IgtkFont* gtkfont;
  PangoAttrList *attrs;

  if (!layout)
    return;

  gtkfont = gtkFontGet(ih);
  if (!gtkfont)
    return;

  attrs = pango_layout_get_attributes(layout);
  if (!attrs) 
  {
    attrs = pango_attr_list_new();
    pango_attr_list_insert(attrs, pango_attribute_copy(gtkfont->strikethrough));
    pango_attr_list_insert(attrs, pango_attribute_copy(gtkfont->underline));
    pango_layout_set_attributes(layout, attrs);
  }
  else
  {
    pango_attr_list_change(attrs, pango_attribute_copy(gtkfont->strikethrough));
    pango_attr_list_change(attrs, pango_attribute_copy(gtkfont->underline));
  }
}

void iupgtkFontUpdateObjectPangoLayout(Ihandle* ih, gpointer object)
{
  PangoAttrList *attrs;

  IgtkFont* gtkfont = gtkFontGet(ih);
  if (!gtkfont)
    return;

  g_object_get(object, "attributes", &attrs, NULL);
  if (!attrs) 
  {
    attrs = pango_attr_list_new();
    pango_attr_list_insert(attrs, pango_attribute_copy(gtkfont->strikethrough));
    pango_attr_list_insert(attrs, pango_attribute_copy(gtkfont->underline));
    g_object_set(object, "attributes", attrs, NULL);
  }
  else
  {
    pango_attr_list_change(attrs, pango_attribute_copy(gtkfont->strikethrough));
    pango_attr_list_change(attrs, pango_attribute_copy(gtkfont->underline));
  }
}

char* iupdrvGetSystemFont(void)
{
  static char systemfont[200] = "";
  GtkStyle* style;
  GtkWidget* widget = gtk_invisible_new();
  gtk_widget_realize(widget);
  style = gtk_widget_get_style(widget);
  if (!style || !style->font_desc)
    strcpy(systemfont, "Sans, 10");
  else
  {
    char* desc = pango_font_description_to_string(style->font_desc);
    strcpy(systemfont, desc);
    g_free(desc);
  }
  gtk_widget_unrealize(widget);
  gtk_widget_destroy(widget);
  return systemfont;
}

char* iupgtkFindPangoFontDesc(PangoFontDescription* fontdesc)
{
  int i, count = iupArrayCount(gtk_fonts);
  IgtkFont* fonts = (IgtkFont*)iupArrayGetData(gtk_fonts);

  /* Check if the standardfont already exists in cache */
  for (i = 0; i < count; i++)
  {
    if (pango_font_description_equal(fontdesc, fonts[i].fontdesc))
      return fonts[i].standardfont;
  }

  return NULL;
}

PangoFontDescription* iupgtkGetPangoFontDesc(const char* value)
{
  IgtkFont *gtkfont = gtkFindFont(value);
  if (!gtkfont)
  {
    iupERROR1("Failed to create Font: %s", value); 
    return NULL;
  }
  return gtkfont->fontdesc;
}

char* iupgtkGetPangoFontDescAttrib(Ihandle *ih)
{
  IgtkFont* gtkfont = gtkFontGet(ih);
  if (!gtkfont)
    return NULL;
  else
    return (char*)gtkfont->fontdesc;
}

char* iupgtkGetFontIdAttrib(Ihandle *ih)
{
  IgtkFont* gtkfont = gtkFontGet(ih);
  if (!gtkfont)
    return NULL;
  else
  {
    GdkFont* gdk_font = gdk_font_from_description(gtkfont->fontdesc);
    return (char*)gdk_font_id(gdk_font);  /* In UNIX will return an X Font ID, in Win32 will return an HFONT */
  }
}

int iupdrvSetStandardFontAttrib(Ihandle* ih, const char* value)
{
  IgtkFont* gtkfont = gtkFontCreateNativeFont(ih, value);
  if (!gtkfont)
    return 1;

  /* If FONT is changed, must update the SIZE attribute */
  iupBaseUpdateSizeFromFont(ih);

  /* FONT attribute must be able to be set before mapping, 
    so the font is enable for size calculation. */
  if (ih->handle && (ih->iclass->nativetype != IUP_TYPEVOID))
  {
    gtk_widget_modify_font(ih->handle, gtkfont->fontdesc);
    iupgtkFontUpdatePangoLayout(ih, gtkFontGetWidgetPangoLayout(ih));
  }

  return 1;
}

void iupdrvFontGetMultiLineStringSize(Ihandle* ih, const char* str, int *w, int *h)
{
  int max_w;

  IgtkFont* gtkfont = gtkFontGet(ih);
  if (!gtkfont)
  {
    if (w) *w = 0;
    if (h) *h = 0;
    return;
  }

  if (!str)
  {
    if (w) *w = 0;
    if (h) *h = gtkfont->charheight * 1;
    return;
  }

  max_w = 0;
  if (str[0])
  {
    int dummy_h;

    pango_layout_set_attributes(gtkfont->layout, NULL);

    if (iupAttribGetBoolean(ih, "MARKUP"))
      pango_layout_set_markup(gtkfont->layout, iupgtkStrConvertToUTF8(str), -1);
    else
      pango_layout_set_text(gtkfont->layout, iupgtkStrConvertToUTF8(str), -1);

    pango_layout_get_pixel_size(gtkfont->layout, &max_w, &dummy_h);
  }

  if (w) *w = max_w;
  if (h) *h = gtkfont->charheight * iupStrLineCount(str);
}

int iupdrvFontGetStringWidth(Ihandle* ih, const char* str)
{
  IgtkFont* gtkfont;
  int len, w;
  char* line_end;

  if (!str || str[0]==0)
    return 0;

  gtkfont = gtkFontGet(ih);
  if (!gtkfont)
    return 0;

  line_end = strchr(str, '\n');
  if (line_end)
    len = line_end-str;
  else
    len = strlen(str);

  if (iupAttribGetBoolean(ih, "MARKUP"))
    pango_layout_set_markup(gtkfont->layout, iupgtkStrConvertToUTF8(str), len);
  else
    pango_layout_set_text(gtkfont->layout, iupgtkStrConvertToUTF8(str), len);

  pango_layout_get_pixel_size(gtkfont->layout, &w, NULL);
  return w;
}

void iupdrvFontGetCharSize(Ihandle* ih, int *charwidth, int *charheight)
{
  IgtkFont* gtkfont = gtkFontGet(ih);
  if (!gtkfont)
  {
    if (charwidth)  *charwidth = 0;
    if (charheight) *charheight = 0;
    return;
  }

  if (charheight) 
    *charheight = gtkfont->charheight;

  if (charwidth)
    *charwidth = gtkfont->charwidth;
}

void iupdrvFontInit(void)
{
  gtk_fonts = iupArrayCreate(50, sizeof(IgtkFont));
  gtk_fonts_context = gdk_pango_context_get();
  pango_context_set_language(gtk_fonts_context, gtk_get_default_language());
}

void iupdrvFontFinish(void)
{
  int i, count = iupArrayCount(gtk_fonts);
  IgtkFont* fonts = (IgtkFont*)iupArrayGetData(gtk_fonts);
  for (i = 0; i < count; i++)
  {
    pango_font_description_free(fonts[i].fontdesc);
    pango_attribute_destroy(fonts[i].strikethrough);
    pango_attribute_destroy(fonts[i].underline);
  }
  iupArrayDestroy(gtk_fonts);
  g_object_unref(gtk_fonts_context);
}