Logo ROOT  
Reference Guide
TGX11TTF.cxx
Go to the documentation of this file.
1 // @(#)root/x11ttf:$Id: 80028b538e60290371c1c5d73728f78b1c32f09a $
2 // Author: Valeriy Onuchin (Xft support) 02/10/07
3 // Author: Olivier Couet 01/10/02
4 // Author: Fons Rademakers 21/11/98
5 
6 /*************************************************************************
7  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
8  * All rights reserved. *
9  * *
10  * For the licensing terms see $ROOTSYS/LICENSE. *
11  * For the list of contributors see $ROOTSYS/README/CREDITS. *
12  *************************************************************************/
13 
14 /** \class TGX11TTF
15 \ingroup x11
16 
17 Interface to low level X11 (Xlib). This class gives access to basic
18 X11 graphics via the parent class TGX11. However, all text and font
19 handling is done via the Freetype TrueType library. When the
20 shared library containing this class is loaded the global gVirtualX
21 is redirected to point to this class.
22 */
23 
24 #include <cstdlib>
25 
26 #include <ft2build.h>
27 #include FT_FREETYPE_H
28 #include FT_GLYPH_H
29 #include "TGX11TTF.h"
30 #include "TEnv.h"
31 
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
34 #include <X11/Xatom.h>
35 #include <X11/cursorfont.h>
36 #include <X11/keysym.h>
37 #include <X11/xpm.h>
38 
39 struct RXColor:XColor{};
40 struct RVisual:Visual{};
41 struct RXImage:XImage{};
42 
43 #ifdef R__HAS_XFT
44 
45 #include "THashTable.h"
46 #include "TRefCnt.h"
47 #include <X11/Xft/Xft.h>
48 
49 ///////////////////////// xft font data //////////////////////////////////////
50 class TXftFontData : public TNamed, public TRefCnt {
51 public:
52  GContext_t fGC; // graphics context
53  XftFont *fXftFont; // xft font
54 
55  TXftFontData(GContext_t gc, XftFont *xftfont, const char *name) :
56  TNamed(name, ""), TRefCnt(), fXftFont(xftfont)
57  {
58  SetRefCount(1);
59  fGC = gc;
60  }
61 
62  void MapGCFont(GContext_t gc, FontStruct_t font)
63  {
64  fGC = gc; fXftFont = (XftFont *)font;
65  }
66 
67  ~TXftFontData()
68  {
69  if (References() == 1) {
70  if (fXftFont) XftFontClose((Display*)gVirtualX->GetDisplay(), fXftFont);
71  }
72  }
73 };
74 
75 /////////////////// hash table //////////////////////////////////////////////
76 class TXftFontHash {
77 public:
78  THashTable *fList; // hash table
79 
80  TXftFontHash() { fList = new THashTable(50); }
81 
82  TXftFontData *FindByName(const char *name)
83  {
84  return (TXftFontData*)fList->FindObject(name);
85  }
86 
87  TXftFontData *FindByFont(FontStruct_t font)
88  {
89  TIter next(fList);
90  TXftFontData *d = 0;
91 
92  while ((d = (TXftFontData*) next())) {
93  if (d->fXftFont == (XftFont *)font) {
94  return d;
95  }
96  }
97  return 0;
98  }
99 
100  TXftFontData *FindByGC(GContext_t gc)
101  {
102  TIter next(fList);
103  TXftFontData *d = 0;
104 
105  while ((d = (TXftFontData*) next())) {
106  if (d->fGC == gc) {
107  return d;
108  }
109  }
110  return 0;
111  }
112 
113  void AddFont(TXftFontData *data)
114  {
115  // Loop over all existing TXftFontData, if we already have one with the same
116  // font data, set the reference counter of this one beyond 1 so it does
117  // delete the font pointer
118  TIter next(fList);
119  TXftFontData *d = 0;
120 
121  while ((d = (TXftFontData*) next())) {
122  if (d->fXftFont == data->fXftFont) {
123  data->AddReference();
124  }
125  }
126 
127  fList->Add(data);
128  }
129 
130  void FreeFont(TXftFontData *data)
131  {
132  fList->Remove(data);
133  delete data;
134  }
135 };
136 #endif // R__HAS_XFT
137 
138 /** \class TTFX11Init
139 \ingroup GraphicsBackends
140 
141 Small utility class that takes care of switching the current
142 gVirtualX to the new TGX11TTF class as soon as the shared library
143 containing this class is loaded.
144 */
145 
146 class TTFX11Init {
147 public:
148  TTFX11Init() { TGX11TTF::Activate(); }
149 };
150 static TTFX11Init gTTFX11Init;
151 
152 
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Create copy of TGX11 but now use TrueType fonts.
157 
159 {
160  SetName("X11TTF");
161  SetTitle("ROOT interface to X11 with TrueType fonts");
162 
163  if (!TTF::fgInit) TTF::Init();
164 
165  fHasTTFonts = kTRUE;
166  fHasXft = kFALSE;
167  fAlign.x = 0;
168  fAlign.y = 0;
169 
170 #ifdef R__HAS_XFT
171  fXftFontHash = 0;
172 #endif
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 /// Static method setting TGX11TTF as the acting gVirtualX.
177 
179 {
180  if (gVirtualX && dynamic_cast<TGX11*>(gVirtualX)) {
181  TGX11 *oldg = (TGX11 *) gVirtualX;
182  gVirtualX = new TGX11TTF(*oldg);
183  delete oldg;
184  }
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Initialize X11 system. Returns kFALSE in case of failure.
189 
190 Bool_t TGX11TTF::Init(void *display)
191 {
192 #ifdef R__HAS_XFT
193  fXftFontHash = 0;
194  XFontStruct *fs = 0;
195  if (display) fs = XLoadQueryFont((Display *)display, "-*-helvetica-*-r-*-*-14-*-*-*-*-*-*-*");
196  if (!fs) gEnv->SetValue("X11.UseXft", 1);
197  if (display && fs) XFreeFont((Display *)display, fs);
198  if (gEnv->GetValue("X11.UseXft", 0)) {
199  fHasXft = kTRUE;
200  fXftFontHash = new TXftFontHash();
201  }
202 #endif
203  Bool_t r = TGX11::Init(display);
204 
205  if (fDepth > 8) {
207  } else {
209  }
210 
211  return r;
212 }
213 
214 ////////////////////////////////////////////////////////////////////////////////
215 /// Compute alignment variables. The alignment is done on the horizontal string
216 /// then the rotation is applied on the alignment variables.
217 /// SetRotation and LayoutGlyphs should have been called before.
218 
219 void TGX11TTF::Align(void)
220 {
221  EAlign align = (EAlign) fTextAlign;
222 
223  // vertical alignment
224  if (align == kTLeft || align == kTCenter || align == kTRight) {
225  fAlign.y = TTF::fgAscent;
226  } else if (align == kMLeft || align == kMCenter || align == kMRight) {
227  fAlign.y = TTF::fgAscent/2;
228  } else {
229  fAlign.y = 0;
230  }
231 
232  // horizontal alignment
233  if (align == kTRight || align == kMRight || align == kBRight) {
234  fAlign.x = TTF::fgWidth;
235  } else if (align == kTCenter || align == kMCenter || align == kBCenter) {
236  fAlign.x = TTF::fgWidth/2;
237  } else {
238  fAlign.x = 0;
239  }
240 
241  FT_Vector_Transform(&fAlign, TTF::fgRotMatrix);
242  fAlign.x = fAlign.x >> 6;
243  fAlign.y = fAlign.y >> 6;
244 }
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 /// Draw FT_Bitmap bitmap to xim image at position bx,by using specified
248 /// foreground color.
249 
250 void TGX11TTF::DrawImage(FT_Bitmap *source, ULong_t fore, ULong_t back,
251  RXImage *xim, Int_t bx, Int_t by)
252 {
253  UChar_t d = 0, *s = source->buffer;
254 
255  if (TTF::fgSmoothing) {
256 
257  static RXColor col[5];
258  RXColor *bcol = 0;
259  XColor *bc;
260  Int_t x, y;
261 
262  // background kClear, i.e. transparent, we take as background color
263  // the average of the rgb values of all pixels covered by this character
264  if (back == (ULong_t) -1 && (UInt_t)source->width) {
265  ULong_t r, g, b;
266  Int_t dots, dotcnt;
267  const Int_t maxdots = 50000;
268 
269  dots = Int_t(source->width * source->rows);
270  dots = dots > maxdots ? maxdots : dots;
271  bcol = new RXColor[dots];
272  if (!bcol) return;
273  bc = bcol;
274  dotcnt = 0;
275  for (y = 0; y < (int) source->rows; y++) {
276  for (x = 0; x < (int) source->width; x++, bc++) {
277 /// bc->pixel = XGetPixel(xim, bx + x, by - c->TTF::fgAscent + y);
278  bc->pixel = XGetPixel(xim, bx + x, by + y);
279  bc->flags = DoRed | DoGreen | DoBlue;
280  if (++dotcnt >= maxdots) break;
281  }
282  }
283  QueryColors(fColormap, bcol, dots);
284  r = g = b = 0;
285  bc = bcol;
286  dotcnt = 0;
287  for (y = 0; y < (int) source->rows; y++) {
288  for (x = 0; x < (int) source->width; x++, bc++) {
289  r += bc->red;
290  g += bc->green;
291  b += bc->blue;
292  if (++dotcnt >= maxdots) break;
293  }
294  }
295  if (dots != 0) {
296  r /= dots;
297  g /= dots;
298  b /= dots;
299  }
300  bc = &col[0];
301  if (bc->red == r && bc->green == g && bc->blue == b)
302  bc->pixel = back;
303  else {
304  bc->pixel = ~back;
305  bc->red = (UShort_t) r;
306  bc->green = (UShort_t) g;
307  bc->blue = (UShort_t) b;
308  }
309  }
310  delete [] bcol;
311 
312  // if fore or background have changed from previous character
313  // recalculate the 3 smoothing colors (interpolation between fore-
314  // and background colors)
315  if (fore != col[4].pixel || back != col[0].pixel) {
316  col[4].pixel = fore;
317  col[4].flags = DoRed|DoGreen|DoBlue;
318  if (back != (ULong_t) -1) {
319  col[3].pixel = back;
320  col[3].flags = DoRed | DoGreen | DoBlue;
321  QueryColors(fColormap, &col[3], 2);
322  col[0] = col[3];
323  } else {
324  QueryColors(fColormap, &col[4], 1);
325  }
326 
327  // interpolate between fore and background colors
328  for (x = 3; x > 0; x--) {
329  col[x].red = (col[4].red *x + col[0].red *(4-x)) /4;
330  col[x].green = (col[4].green*x + col[0].green*(4-x)) /4;
331  col[x].blue = (col[4].blue *x + col[0].blue *(4-x)) /4;
332  if (!AllocColor(fColormap, &col[x])) {
333  Warning("DrawImage", "cannot allocate smoothing color");
334  col[x].pixel = col[x+1].pixel;
335  }
336  }
337  }
338 
339  // put smoothed character, character pixmap values are an index
340  // into the 5 colors used for aliasing (4 = foreground, 0 = background)
341  for (y = 0; y < (int) source->rows; y++) {
342  for (x = 0; x < (int) source->width; x++) {
343  d = *s++ & 0xff;
344  d = ((d + 10) * 5) / 256;
345  if (d > 4) d = 4;
346  if (d && x < (int) source->width) {
347  ULong_t p = col[d].pixel;
348  XPutPixel(xim, bx + x, by + y, p);
349  }
350  }
351  }
352  } else {
353  // no smoothing, just put character using foreground color
354  UChar_t* row=s;
355  for (int y = 0; y < (int) source->rows; y++) {
356  int n = 0;
357  s = row;
358  for (int x = 0; x < (int) source->width; x++) {
359  if (n == 0) d = *s++;
360  if (TESTBIT(d,7-n))
361  XPutPixel(xim, bx + x, by + y, fore);
362  if (++n == (int) kBitsPerByte) n = 0;
363  }
364  row += source->pitch;
365  }
366  }
367 }
368 
369 ////////////////////////////////////////////////////////////////////////////////
370 /// Draw text using TrueType fonts. If TrueType fonts are not available the
371 /// text is drawn with TGX11::DrawText.
372 
374  const char *text, ETextMode mode)
375 {
376  if (!fHasTTFonts) {
377  TGX11::DrawText(x, y, angle, mgn, text, mode);
378  } else {
379  if (!TTF::fgInit) TTF::Init();
380  TTF::SetRotationMatrix(angle);
383  Align();
384  RenderString(x, y, mode);
385  }
386 }
387 
388 ////////////////////////////////////////////////////////////////////////////////
389 /// Draw text using TrueType fonts. If TrueType fonts are not available the
390 /// text is drawn with TGX11::DrawText.
391 
393  const wchar_t *text, ETextMode mode)
394 {
395  if (!fHasTTFonts) {
396  TGX11::DrawText(x, y, angle, mgn, text, mode);
397  } else {
398  if (!TTF::fgInit) TTF::Init();
399  TTF::SetRotationMatrix(angle);
402  Align();
403  RenderString(x, y, mode);
404  }
405 }
406 
407 ////////////////////////////////////////////////////////////////////////////////
408 /// Get the background of the current window in an XImage.
409 
411 {
412  Window_t cws = GetCurrentWindow();
413  UInt_t width;
414  UInt_t height;
415  Int_t xy;
416  gVirtualX->GetWindowSize(cws, xy, xy, width, height);
417 
418  if (x < 0) {
419  w += x;
420  x = 0;
421  }
422  if (y < 0) {
423  h += y;
424  y = 0;
425  }
426 
427  if (x+w > width) w = width - x;
428  if (y+h > height) h = height - y;
429 
430  return (RXImage*)XGetImage((Display*)fDisplay, cws, x, y, w, h, AllPlanes, ZPixmap);
431 }
432 
433 ////////////////////////////////////////////////////////////////////////////////
434 /// Test if there is really something to render.
435 
437 {
438  Window_t cws = GetCurrentWindow();
439  UInt_t width;
440  UInt_t height;
441  Int_t xy;
442  gVirtualX->GetWindowSize(cws, xy, xy, width, height);
443 
444  // If w or h is 0, very likely the string is only blank characters
445  if ((int)w == 0 || (int)h == 0) return kFALSE;
446 
447  // If string falls outside window, there is probably no need to draw it.
448  if (x + (int)w <= 0 || x >= (int)width) return kFALSE;
449  if (y + (int)h <= 0 || y >= (int)height) return kFALSE;
450 
451  // If w or h are much larger than the window size, there is probably no need
452  // to draw it. Moreover a to large text size may produce a Seg Fault in
453  // malloc in RenderString.
454  if (w > 10*width) return kFALSE;
455  if (h > 10*height) return kFALSE;
456 
457  return kTRUE;
458 }
459 
460 ////////////////////////////////////////////////////////////////////////////////
461 /// Perform the string rendering in the pad.
462 /// LayoutGlyphs should have been called before.
463 
465 {
466  TTF::TTGlyph* glyph = TTF::fgGlyphs;
467  GC *gc;
468 
469  // compute the size and position of the XImage that will contain the text
470  Int_t Xoff = 0; if (TTF::GetBox().xMin < 0) Xoff = -TTF::GetBox().xMin;
471  Int_t Yoff = 0; if (TTF::GetBox().yMin < 0) Yoff = -TTF::GetBox().yMin;
472  Int_t w = TTF::GetBox().xMax + Xoff;
473  Int_t h = TTF::GetBox().yMax + Yoff;
474  Int_t x1 = x-Xoff-fAlign.x;
475  Int_t y1 = y+Yoff+fAlign.y-h;
476 
477  if (!IsVisible(x1, y1, w, h)) return;
478 
479  // create the XImage that will contain the text
480  UInt_t depth = fDepth;
481  XImage *xim = 0;
482  xim = XCreateImage((Display*)fDisplay, fVisual,
483  depth, ZPixmap, 0, 0, w, h,
484  depth == 24 ? 32 : (depth==15?16:depth), 0);
485  if (!xim) return;
486 
487  // use malloc since Xlib will use free() in XDestroyImage
488  xim->data = (char *) malloc(xim->bytes_per_line * h);
489  memset(xim->data, 0, xim->bytes_per_line * h);
490 
491  ULong_t bg;
492  XGCValues values;
493  gc = (GC*)GetGC(3);
494  if (!gc) {
495  Error("DrawText", "error getting Graphics Context");
496  return;
497  }
498  XGetGCValues((Display*)fDisplay, *gc, GCForeground | GCBackground, &values);
499 
500  // get the background
501  if (mode == kClear) {
502  // if mode == kClear we need to get an image of the background
503  XImage *bim = GetBackground(x1, y1, w, h);
504  if (!bim) {
505  Error("DrawText", "error getting background image");
506  return;
507  }
508 
509  // and copy it into the text image
510  Int_t xo = 0, yo = 0;
511  if (x1 < 0) xo = -x1;
512  if (y1 < 0) yo = -y1;
513 
514  for (int yp = 0; yp < (int) bim->height; yp++) {
515  for (int xp = 0; xp < (int) bim->width; xp++) {
516  ULong_t pixel = XGetPixel(bim, xp, yp);
517  XPutPixel(xim, xo+xp, yo+yp, pixel);
518  }
519  }
520  XDestroyImage(bim);
521  bg = (ULong_t) -1;
522  } else {
523  // if mode == kOpaque its simple, we just draw the background
524  XAddPixel(xim, values.background);
525  bg = values.background;
526  }
527 
528  // paint the glyphs in the XImage
529  glyph = TTF::fgGlyphs;
530  for (int n = 0; n < TTF::fgNumGlyphs; n++, glyph++) {
531  if (FT_Glyph_To_Bitmap(&glyph->fImage,
532  TTF::fgSmoothing ? ft_render_mode_normal
533  : ft_render_mode_mono,
534  0, 1 )) continue;
535  FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph->fImage;
536  FT_Bitmap* source = &bitmap->bitmap;
537  Int_t bx, by;
538 
539  bx = bitmap->left+Xoff;
540  by = h - bitmap->top-Yoff;
541  DrawImage(source, values.foreground, bg, (RXImage*)xim, bx, by);
542  }
543 
544  // put the Ximage on the screen
545  Window_t cws = GetCurrentWindow();
546  gc = (GC*)GetGC(6);
547  if (gc) XPutImage((Display*)fDisplay, cws, *gc, xim, 0, 0, x1, y1, w, h);
548  XDestroyImage(xim);
549 }
550 
551 ////////////////////////////////////////////////////////////////////////////////
552 /// Set specified font.
553 
555 {
556  fTextFont = fontnumber;
557  if (!fHasTTFonts) {
558  TGX11::SetTextFont(fontnumber);
559  } else {
560  TTF::SetTextFont(fontnumber);
561  }
562 }
563 
564 ////////////////////////////////////////////////////////////////////////////////
565 /// Set text font to specified name.
566 /// mode : loading flag
567 /// mode=0 : search if the font exist (kCheck)
568 /// mode=1 : search the font and load it if it exists (kLoad)
569 /// font : font name
570 ///
571 /// Set text font to specified name. This function returns 0 if
572 /// the specified font is found, 1 if not.
573 
575 {
576  if (!fHasTTFonts) {
577  return TGX11::SetTextFont(fontname, mode);
578  } else {
579  return TTF::SetTextFont(fontname);
580  }
581 }
582 
583 ////////////////////////////////////////////////////////////////////////////////
584 /// Set current text size.
585 
587 {
588  fTextSize = textsize;
589  if (!fHasTTFonts) {
590  TGX11::SetTextSize(textsize);
591  } else {
592  TTF::SetTextSize(textsize);
593  }
594 }
595 
596 #ifdef R__HAS_XFT
597 
598 ///////////////////////////// Xft font methods /////////////////////////////////
599 ////////////////////////////////////////////////////////////////////////////////
600 /// Parses an XLFD name and opens a font.
601 
602 FontStruct_t TGX11TTF::LoadQueryFont(const char *font_name)
603 {
604  if (!fXftFontHash) {
605  return TGX11::LoadQueryFont(font_name);
606  }
607 
608  TXftFontData *data = fXftFontHash->FindByName(font_name);
609 
610  // already loaded
611  if (data) {
612  return (FontStruct_t)data->fXftFont;
613  }
614 
615  XftFont *xftfont = XftFontOpenXlfd((Display*)fDisplay, fScreenNumber, font_name);
616 
617  data = new TXftFontData(0, xftfont, font_name);
618  fXftFontHash->AddFont(data);
619 
620  return (FontStruct_t)xftfont;
621 }
622 
623 ////////////////////////////////////////////////////////////////////////////////
624 /// Explicitly delete font structure obtained with LoadQueryFont().
625 
627 {
628  if (!fXftFontHash) {
629  TGX11::DeleteFont(fs);
630  return;
631  }
632 
633  TXftFontData *data = fXftFontHash->FindByFont(fs);
634 
635  if (data)
636  fXftFontHash->FreeFont(data);
637 }
638 
639 ////////////////////////////////////////////////////////////////////////////////
640 /// Explicitly delete a graphics context.
641 
643 {
644  if (!fXftFontHash) {
645  TGX11::DeleteGC(gc);
646  return;
647  }
648 
649  TXftFontData *gcdata = fXftFontHash->FindByGC(gc);
650  if (gcdata) fXftFontHash->FreeFont(gcdata);
651  TGX11::DeleteGC(gc);
652 }
653 
654 ////////////////////////////////////////////////////////////////////////////////
655 /// Return handle to font described by font structure.
656 
658 {
659  if (!fXftFontHash) {
660  return TGX11::GetFontHandle(fs);
661  }
662 
663  return (FontH_t)fs;
664 }
665 
666 ////////////////////////////////////////////////////////////////////////////////
667 /// Return the font associated with the graphics context gc
668 
670 {
671  if (!fXftFontHash) {
672  return 0;
673  }
674 
675  TXftFontData *data = fXftFontHash->FindByGC(gc);
676 
677  // no XftFont data
678  if (!data) return 0;
679 
680  return (FontStruct_t)data->fXftFont;
681 }
682 
683 ////////////////////////////////////////////////////////////////////////////////
684 /// Map the XftFont with the Graphics Context using it.
685 
687 {
688  if (!fXftFontHash)
689  return;
690 
691  TXftFontData *gcdata = fXftFontHash->FindByGC(gc);
692  TXftFontData *fontdata = fXftFontHash->FindByFont(font);
693 
694  if (gcdata) { // && (gcdata->fXftFont == 0)) {
695  gcdata->fXftFont = (XftFont *)font;
696  }
697  else if (fontdata) {
698  TXftFontData *data = new TXftFontData(gc, (XftFont *)font, fontdata->GetName());
699  fXftFontHash->AddFont(data);
700  }
701 }
702 
703 ////////////////////////////////////////////////////////////////////////////////
704 /// Return length of string in pixels. Size depends on font
705 
706 Int_t TGX11TTF::TextWidth(FontStruct_t font, const char *s, Int_t len)
707 {
708  if (!fXftFontHash) {
709  return TGX11::TextWidth(font, s, len);
710  }
711 
712  TXftFontData *data = fXftFontHash->FindByFont(font);
713 
714  if (!data) return 0;
715 
716  XftFont *xftfont = data->fXftFont;
717 
718  if (xftfont) {
719  XGlyphInfo glyph_info;
720  XftTextExtents8((Display *)fDisplay, xftfont, (XftChar8 *)s, len, &glyph_info);
721  return glyph_info.xOff;
722  }
723  return 0;
724 }
725 
726 ////////////////////////////////////////////////////////////////////////////////
727 /// Return some font properties
728 
729 void TGX11TTF::GetFontProperties(FontStruct_t font, Int_t &max_ascent, Int_t &max_descent)
730 {
731  if (!fXftFontHash) {
732  TGX11::GetFontProperties(font, max_ascent, max_descent);
733  return;
734  }
735 
736  TXftFontData *data = fXftFontHash->FindByFont(font);
737 
738  if (!data) {
739  TGX11::GetFontProperties(font, max_ascent, max_descent);
740  return;
741  }
742 
743  XftFont *xftfont = data->fXftFont;
744 
745  if (!xftfont) {
746  TGX11::GetFontProperties(font, max_ascent, max_descent);
747  return;
748  }
749 
750  max_ascent = xftfont->ascent;
751  max_descent = xftfont->descent;
752 }
753 
754 ////////////////////////////////////////////////////////////////////////////////
755 /// Draw text string
756 
758  const char *text, Int_t len)
759 {
760  XftDraw *xftdraw;
761  XftColor xftcolor;
762  XColor xcolor;
763  XftFont *xftfont;
764 
765  if (!xwindow) {
766  return;
767  }
768 
769  if (!gc) {
770  return;
771  }
772 
773  if (!text || (len < 1) || !text[0]) {
774  return;
775  }
776 
777  if (!fXftFontHash) {
778  TGX11::DrawString(xwindow, gc, x, y, text, len);
779  return;
780  }
781 
782  GCValues_t gval;
783  gval.fMask = kGCForeground | kGCBackground; // retrieve GC values
784  GetGCValues(gc, gval);
785 
786  TXftFontData *data = fXftFontHash->FindByGC(gc);
787 
788  // no XftFont data
789  if (!data) {
790  TGX11::DrawString(xwindow, gc, x, y, text, len);
791  return;
792  }
793 
794  xftfont = data->fXftFont;
795 
796  // no Xft font
797  if (!xftfont) {
798  TGX11::DrawString(xwindow, gc, x, y, text, len);
799  return;
800  }
801 
802  // dummies
803  Window droot;
804  Int_t dx,dy;
805  UInt_t bwidth, width, height, depth;
806 
807  // check if drawable is bitmap
808  XGetGeometry((Display*)fDisplay, (Drawable)xwindow, &droot, &dx, &dy,
809  &width, &height, &bwidth, &depth);
810 
811  if (depth <= 1) {
812  TGX11::DrawString(xwindow, gc, x, y, text, len);
813  return;
814  }
815 
816  memset(&xcolor, 0, sizeof(xcolor));
817  xcolor.pixel = gval.fForeground;
818 
819  XQueryColor((Display*)fDisplay, fColormap, &xcolor);
820 
821  // create XftDraw
822  xftdraw = XftDrawCreate((Display*)fDisplay, (Drawable)xwindow, fVisual, fColormap);
823 
824  if (!xftdraw) {
825  //Warning("could not create an XftDraw");
826  TGX11::DrawString(xwindow, gc, x, y, text, len);
827  return;
828  }
829 
830  xftcolor.color.red = xcolor.red;
831  xftcolor.color.green = xcolor.green;
832  xftcolor.color.blue = xcolor.blue;
833  xftcolor.color.alpha = 0xffff;
834  xftcolor.pixel = gval.fForeground;
835 
836  XftDrawString8(xftdraw, &xftcolor, xftfont, x, y, (XftChar8 *)text, len);
837 
838  // cleanup
839  XftDrawDestroy(xftdraw);
840 }
841 
842 #endif // R__HAS_XFT
843 
TGX11TTF::kTCenter
@ kTCenter
Definition: TGX11TTF.h:30
n
const Int_t n
Definition: legend1.C:16
TGX11TTF::RenderString
void RenderString(Int_t x, Int_t y, ETextMode mode)
Perform the string rendering in the pad.
Definition: TGX11TTF.cxx:464
Drawable
XID Drawable
Definition: TGX11.h:36
TGX11TTF::DrawText
void DrawText(Int_t x, Int_t y, Float_t angle, Float_t mgn, const char *text, ETextMode mode)
Draw text using TrueType fonts.
Definition: TGX11TTF.cxx:373
TTF::TTGlyph
TTF helper class containing glyphs description.
Definition: TTF.h:65
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:100
TGX11TTF::Activate
static void Activate()
Static method setting TGX11TTF as the acting gVirtualX.
Definition: TGX11TTF.cxx:178
TEnv::SetValue
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
Set the value of a resource or create a new resource.
Definition: TEnv.cxx:736
TTF::fgInit
static Bool_t fgInit
true if the Init has been called
Definition: TTF.h:85
TGX11::Init
Bool_t Init(void *display)
Initialize X11 system. Returns kFALSE in case of failure.
Definition: TGX11.cxx:342
TGX11::fScreenNumber
Int_t fScreenNumber
Screen number.
Definition: TGX11.h:135
TTF::LayoutGlyphs
static void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
Definition: TTF.cxx:181
TTF::Init
static void Init()
Initialise the TrueType fonts interface.
Definition: TTF.cxx:65
TRefCnt::SetRefCount
void SetRefCount(UInt_t r)
Definition: TRefCnt.h:39
TNamed::SetName
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
TRefCnt::References
UInt_t References() const
Definition: TRefCnt.h:38
TGX11::TextWidth
Int_t TextWidth(FontStruct_t font, const char *s, Int_t len)
Return length of string in pixels. Size depends on font.
Definition: GX11Gui.cxx:2081
TTF::TTGlyph::fImage
FT_Glyph fImage
glyph image
Definition: TTF.h:69
TTF::fgSmoothing
static Bool_t fgSmoothing
use anti-aliasing (true when >8 planes, false otherwise)
Definition: TTF.h:90
THashTable::Add
void Add(TObject *obj)
Add object to the hash table.
Definition: THashTable.cxx:92
TGX11TTF::DrawImage
void DrawImage(FT_Bitmap *source, ULong_t fore, ULong_t back, RXImage *xim, Int_t bx, Int_t by)
Draw FT_Bitmap bitmap to xim image at position bx,by using specified foreground color.
Definition: TGX11TTF.cxx:250
TGX11TTF::kBCenter
@ kBCenter
Definition: TGX11TTF.h:31
UShort_t
unsigned short UShort_t
Definition: RtypesCore.h:40
gEnv
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
gVirtualX
#define gVirtualX
Definition: TVirtualX.h:338
TGX11TTF::SetTextFont
void SetTextFont(Font_t fontnumber)
Set specified font.
Definition: TGX11TTF.cxx:554
FontH_t
Handle_t FontH_t
Font handle (as opposed to Font_t which is an index)
Definition: GuiTypes.h:35
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TVirtualX::MapGCFont
virtual void MapGCFont(GContext_t, FontStruct_t)
Map the XftFont with the Graphics Context using it.
Definition: TVirtualX.cxx:2505
TTF::SetSmoothing
static void SetSmoothing(Bool_t state)
Set smoothing (anti-aliasing) flag.
Definition: TTF.cxx:365
TGX11
This class is the basic interface to the X11 (Xlib) graphics system.
Definition: TGX11.h:83
FontStruct_t
Handle_t FontStruct_t
Pointer to font structure.
Definition: GuiTypes.h:39
r
ROOT::R::TRInterface & r
Definition: Object.C:4
TGX11TTF::TGX11TTF
TGX11TTF(const TGX11 &org)
Create copy of TGX11 but now use TrueType fonts.
Definition: TGX11TTF.cxx:158
TGX11TTF.h
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
TTF::SetTextSize
static void SetTextSize(Float_t textsize)
Set current text size.
Definition: TTF.cxx:562
TVirtualX::ETextSetMode
ETextSetMode
Definition: TVirtualX.h:52
GCValues_t
Graphics context structure.
Definition: GuiTypes.h:224
TGX11::DeleteFont
void DeleteFont(FontStruct_t fs)
Explicitly delete font structure obtained with LoadQueryFont().
Definition: GX11Gui.cxx:977
TRefCnt.h
Float_t
float Float_t
Definition: RtypesCore.h:57
width
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
Int_t
int Int_t
Definition: RtypesCore.h:45
TTF::SetTextFont
static void SetTextFont(Font_t fontnumber)
Set specified font.
Definition: TTF.cxx:491
Font_t
short Font_t
Definition: RtypesCore.h:88
TGX11::fColormap
Colormap fColormap
Default colormap, 0 if b/w.
Definition: TGX11.h:132
gTTFX11Init
static TTFX11Init gTTFX11Init
Definition: TGX11TTF.cxx:150
x
Double_t x[n]
Definition: legend1.C:17
TGX11TTF::Init
Bool_t Init(void *display)
Initialize X11 system. Returns kFALSE in case of failure.
Definition: TGX11TTF.cxx:190
TGX11TTF::kTRight
@ kTRight
Definition: TGX11TTF.h:30
TAttText::fTextSize
Float_t fTextSize
Text size.
Definition: TAttText.h:22
TEnv::GetValue
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition: TEnv.cxx:491
TGX11::fHasXft
Bool_t fHasXft
True when XftFonts are used.
Definition: TGX11.h:150
TEnv.h
TESTBIT
#define TESTBIT(n, i)
Definition: Rtypes.h:88
TGX11TTF::Align
void Align(void)
Compute alignment variables.
Definition: TGX11TTF.cxx:219
text
TText * text
Definition: entrylist_figure1.C:10
b
#define b(i)
Definition: RSha256.hxx:100
TVirtualX::GetGCFont
virtual FontStruct_t GetGCFont(GContext_t gc)
Return the font associated with the graphics context gc.
Definition: TVirtualX.cxx:1844
bool
TGX11TTF::kBRight
@ kBRight
Definition: TGX11TTF.h:31
x1
static const double x1[5]
Definition: RooGaussKronrodIntegrator1D.cxx:346
Drawable_t
Handle_t Drawable_t
Drawable handle.
Definition: GuiTypes.h:31
xy
XPoint xy[kMAXMK]
Definition: TGX11.cxx:123
TGX11::GetCurrentWindow
Window_t GetCurrentWindow() const
Return current window pointer. Protected method used by TGX11TTF.
Definition: TGX11.cxx:922
GContext_t
Handle_t GContext_t
Graphics context handle.
Definition: GuiTypes.h:38
TTF::fgGlyphs
static TTF::TTGlyph fgGlyphs[kMaxGlyphs]
glyphs
Definition: TTF.h:83
TGX11::DrawString
void DrawString(Drawable_t id, GContext_t gc, Int_t x, Int_t y, const char *s, Int_t len)
Draw a string using a specific graphics context in position (x,y).
Definition: GX11Gui.cxx:2070
TGX11::fTextAlign
Int_t fTextAlign
Text alignment (set in SetTextAlign)
Definition: TGX11.h:138
TGX11TTF::kMLeft
@ kMLeft
Definition: TGX11TTF.h:30
TGX11::LoadQueryFont
FontStruct_t LoadQueryFont(const char *font_name)
Load font and query font.
Definition: GX11Gui.cxx:956
TTF::fgRotMatrix
static FT_Matrix * fgRotMatrix
rotation matrix
Definition: TTF.h:89
TTF::fgWidth
static Int_t fgWidth
string width, used to compute X alignment
Definition: TTF.h:92
TGX11::SetTextFont
virtual Int_t SetTextFont(char *fontname, ETextSetMode mode)
Set text font to specified name.
Definition: TGX11.cxx:3154
malloc
#define malloc
Definition: civetweb.c:1536
TGX11TTF::EAlign
EAlign
Definition: TGX11TTF.h:30
THashTable
THashTable implements a hash table to store TObject's.
Definition: THashTable.h:35
h
#define h(i)
Definition: RSha256.hxx:106
TGX11::fDepth
Int_t fDepth
Number of color planes.
Definition: TGX11.h:142
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
THashTable::FindObject
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:238
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:101
TGX11::fHasTTFonts
Bool_t fHasTTFonts
True when TrueType fonts are used.
Definition: TGX11.h:149
kGCForeground
const Mask_t kGCForeground
Definition: GuiTypes.h:288
TGX11::QueryColors
void QueryColors(Colormap cmap, RXColor *colors, Int_t ncolors)
Returns the current RGB value for the pixel in the XColor structure.
Definition: TGX11.cxx:383
TGX11::GetFontHandle
FontH_t GetFontHandle(FontStruct_t fs)
Return handle to font described by font structure.
Definition: GX11Gui.cxx:965
Window_t
Handle_t Window_t
Window handle.
Definition: GuiTypes.h:29
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TGX11TTF::GetBackground
RXImage * GetBackground(Int_t x, Int_t y, UInt_t w, UInt_t h)
Get the background of the current window in an XImage.
Definition: TGX11TTF.cxx:410
y
Double_t y[n]
Definition: legend1.C:17
org
#define org(otri, vertexptr)
Definition: triangle.c:1037
ULong_t
unsigned long ULong_t
Definition: RtypesCore.h:55
TTF::fgNumGlyphs
static Int_t fgNumGlyphs
number of glyphs in the string
Definition: TTF.h:88
TGX11::DeleteGC
void DeleteGC(GContext_t gc)
Explicitly delete a graphics context.
Definition: GX11Gui.cxx:1047
TGX11::SetTextSize
virtual void SetTextSize(Float_t textsize)
Set current text size.
Definition: TGX11.cxx:3203
kGCBackground
const Mask_t kGCBackground
Definition: GuiTypes.h:289
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:879
unsigned int
TGX11::GetFontProperties
void GetFontProperties(FontStruct_t font, Int_t &max_ascent, Int_t &max_descent)
Return some font properties.
Definition: GX11Gui.cxx:2089
TGX11::DrawText
virtual void DrawText(Int_t x, Int_t y, Float_t angle, Float_t mgn, const char *text, ETextMode mode)
Draw a text string using current font.
Definition: TGX11.cxx:751
TGX11TTF::SetTextSize
void SetTextSize(Float_t textsize)
Set current text size.
Definition: TGX11TTF.cxx:586
THashTable::Remove
TObject * Remove(TObject *obj)
Remove object from the hashtable.
Definition: THashTable.cxx:417
TGX11TTF::kTLeft
@ kTLeft
Definition: TGX11TTF.h:30
GCValues_t::fMask
Mask_t fMask
bit mask specifying which fields are valid
Definition: GuiTypes.h:251
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
TGX11::fVisual
RVisual * fVisual
Pointer to visual used by all windows.
Definition: TGX11.h:129
TGX11::AllocColor
Bool_t AllocColor(Colormap cmap, RXColor *color)
Allocate color in colormap.
Definition: TGX11.cxx:366
TGX11TTF::kMCenter
@ kMCenter
Definition: TGX11TTF.h:30
TGX11::GetGC
void * GetGC(Int_t which) const
Return desired Graphics Context ("which" maps directly on gGCList[]).
Definition: TGX11.cxx:931
TGX11TTF::kMRight
@ kMRight
Definition: TGX11TTF.h:30
name
char name[80]
Definition: TGX11.cxx:110
GCValues_t::fForeground
ULong_t fForeground
foreground pixel
Definition: GuiTypes.h:227
TGX11TTF
Interface to low level X11 (Xlib).
Definition: TGX11TTF.h:27
TAttText::fTextFont
Font_t fTextFont
Text font.
Definition: TAttText.h:25
d
#define d(i)
Definition: RSha256.hxx:102
TTF::fgAscent
static Int_t fgAscent
string ascent, used to compute Y alignment
Definition: TTF.h:75
TRefCnt
Definitions for TRefCnt, base class for reference counted objects.
Definition: TRefCnt.h:27
TIter
Definition: TCollection.h:233
Window
XID Window
Definition: TGX11.h:39
kBitsPerByte
const ULong_t kBitsPerByte
Definition: RtypesCore.h:123
TGX11TTF::fAlign
FT_Vector fAlign
alignment vector
Definition: TGX11TTF.h:33
TTF::PrepareString
static void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
Definition: TTF.cxx:250
TTF::SetRotationMatrix
static void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
Definition: TTF.cxx:342
TTF::GetBox
static const FT_BBox & GetBox()
Definition: TTF.cxx:643
TGX11::GetGCValues
void GetGCValues(GContext_t gc, GCValues_t &gval)
Get current values from graphics context gc.
Definition: GX11Gui.cxx:2102
TGX11::fDisplay
void * fDisplay
Pointer to display.
Definition: TGX11.h:128
TVirtualX::ETextMode
ETextMode
Definition: TVirtualX.h:51
int
THashTable.h
TVirtualX::kClear
@ kClear
Definition: TVirtualX.h:51
TGX11TTF::IsVisible
Bool_t IsVisible(Int_t x, Int_t y, UInt_t w, UInt_t h)
Test if there is really something to render.
Definition: TGX11TTF.cxx:436
g
#define g(i)
Definition: RSha256.hxx:105