26 # include <ft2build.h> 27 # include FT_FREETYPE_H 34 #include <X11/Xutil.h> 35 #include <X11/Xatom.h> 36 #include <X11/cursorfont.h> 37 #include <X11/keysym.h> 40 struct RXColor:XColor{};
41 struct RVisual:Visual{};
42 struct RXImage:XImage{};
48 #include <X11/Xft/Xft.h> 65 fGC = gc; fXftFont = (XftFont *)font;
70 if (References() == 1) {
71 if (fXftFont) XftFontClose((Display*)
gVirtualX->GetDisplay(), fXftFont);
83 TXftFontData *FindByName(
const char *
name)
93 while ((d = (TXftFontData*) next())) {
94 if (d->fXftFont == (XftFont *)font) {
106 while ((d = (TXftFontData*) next())) {
114 void AddFont(TXftFontData *
data)
122 while ((d = (TXftFontData*) next())) {
123 if (d->fXftFont == data->fXftFont) {
124 data->AddReference();
131 void FreeFont(TXftFontData *data)
162 SetTitle(
"ROOT interface to X11 with TrueType fonts");
196 if (display) fs = XLoadQueryFont((Display *)display,
"-*-helvetica-*-r-*-*-14-*-*-*-*-*-*-*");
198 if (display && fs) XFreeFont((Display *)display, fs);
201 fXftFontHash =
new TXftFontHash();
225 if (align == kTLeft || align == kTCenter || align == kTRight) {
227 }
else if (align == kMLeft || align == kMCenter || align == kMRight) {
234 if (align == kTRight || align == kMRight || align == kBRight) {
236 }
else if (align == kTCenter || align == kMCenter || align == kBCenter) {
243 fAlign.x = fAlign.x >> 6;
244 fAlign.y = fAlign.y >> 6;
254 UChar_t d = 0, *s = source->buffer;
258 static RXColor col[5];
268 const Int_t maxdots = 50000;
270 dots =
Int_t(source->width * source->rows);
271 dots = dots > maxdots ? maxdots : dots;
272 bcol =
new RXColor[dots];
276 for (y = 0; y < (int) source->rows; y++) {
277 for (x = 0; x < (int) source->width; x++, bc++) {
279 bc->pixel = XGetPixel(xim, bx + x, by + y);
280 bc->flags = DoRed | DoGreen | DoBlue;
281 if (++dotcnt >= maxdots)
break;
284 QueryColors(fColormap, bcol, dots);
288 for (y = 0; y < (int) source->rows; y++) {
289 for (x = 0; x < (int) source->width; x++, bc++) {
293 if (++dotcnt >= maxdots)
break;
302 if (bc->red == r && bc->green == g && bc->blue == b)
316 if (fore != col[4].pixel || back != col[0].pixel) {
318 col[4].flags = DoRed|DoGreen|DoBlue;
321 col[3].flags = DoRed | DoGreen | DoBlue;
322 QueryColors(fColormap, &col[3], 2);
325 QueryColors(fColormap, &col[4], 1);
329 for (x = 3; x > 0; x--) {
330 col[
x].red = (col[4].red *x + col[0].red *(4-
x)) /4;
331 col[
x].green = (col[4].green*x + col[0].green*(4-
x)) /4;
332 col[
x].blue = (col[4].blue *x + col[0].blue *(4-
x)) /4;
333 if (!AllocColor(fColormap, &col[x])) {
334 Warning(
"DrawImage",
"cannot allocate smoothing color");
335 col[
x].pixel = col[x+1].pixel;
342 for (y = 0; y < (int) source->rows; y++) {
343 for (x = 0; x < (int) source->width; x++) {
345 d = ((d + 10) * 5) / 256;
347 if (d && x < (
int) source->width) {
349 XPutPixel(xim, bx + x, by + y, p);
356 for (
int y = 0;
y < (int) source->rows;
y++) {
359 for (
int x = 0;
x < (int) source->width;
x++) {
360 if (n == 0) d = *s++;
362 XPutPixel(xim, bx +
x, by +
y, fore);
365 row += source->pitch;
385 RenderString(x, y, mode);
404 RenderString(x, y, mode);
417 gVirtualX->GetWindowSize(cws, xy, xy, width, height);
428 if (x+w > width) w = width -
x;
429 if (y+h > height) h = height -
y;
431 return (RXImage*)XGetImage((Display*)fDisplay, cws, x, y, w, h, AllPlanes, ZPixmap);
443 gVirtualX->GetWindowSize(cws, xy, xy, width, height);
446 if ((
int)w == 0 || (
int)h == 0)
return kFALSE;
449 if (x + (
int)w <= 0 || x >= (
int)width)
return kFALSE;
450 if (y + (
int)h <= 0 || y >= (
int)height)
return kFALSE;
455 if (w > 10*width)
return kFALSE;
456 if (h > 10*height)
return kFALSE;
476 Int_t y1 = y+Yoff+fAlign.y-
h;
478 if (!IsVisible(x1, y1, w, h))
return;
483 xim = XCreateImage((Display*)fDisplay, fVisual,
484 depth, ZPixmap, 0, 0, w, h,
485 depth == 24 ? 32 : (depth==15?16:depth), 0);
489 xim->data = (
char *)
malloc(xim->bytes_per_line * h);
490 memset(xim->data, 0, xim->bytes_per_line * h);
496 Error(
"DrawText",
"error getting Graphics Context");
499 XGetGCValues((Display*)fDisplay, *gc, GCForeground | GCBackground, &values);
502 if (mode == kClear) {
504 XImage *bim = GetBackground(x1, y1, w, h);
506 Error(
"DrawText",
"error getting background image");
511 Int_t xo = 0, yo = 0;
512 if (x1 < 0) xo = -
x1;
513 if (y1 < 0) yo = -y1;
515 for (
int yp = 0; yp < (int) bim->height; yp++) {
516 for (
int xp = 0; xp < (int) bim->width; xp++) {
517 ULong_t pixel = XGetPixel(bim, xp, yp);
518 XPutPixel(xim, xo+xp, yo+yp, pixel);
525 XAddPixel(xim, values.background);
526 bg = values.background;
532 if (FT_Glyph_To_Bitmap(&glyph->
fImage,
534 : ft_render_mode_mono,
536 FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph->
fImage;
537 FT_Bitmap* source = &bitmap->bitmap;
540 bx = bitmap->left+Xoff;
541 by = h - bitmap->top-Yoff;
542 DrawImage(source, values.foreground, bg, (RXImage*)xim, bx, by);
548 if (gc) XPutImage((Display*)fDisplay, cws, *gc, xim, 0, 0, x1, y1, w, h);
557 fTextFont = fontnumber;
589 fTextSize = textsize;
609 TXftFontData *data = fXftFontHash->FindByName(font_name);
616 XftFont *xftfont = XftFontOpenXlfd((Display*)fDisplay, fScreenNumber, font_name);
618 data =
new TXftFontData(0, xftfont, font_name);
619 fXftFontHash->AddFont(data);
634 TXftFontData *data = fXftFontHash->FindByFont(fs);
637 fXftFontHash->FreeFont(data);
650 TXftFontData *gcdata = fXftFontHash->FindByGC(gc);
651 if (gcdata) fXftFontHash->FreeFont(gcdata);
676 TXftFontData *data = fXftFontHash->FindByGC(gc);
692 TXftFontData *gcdata = fXftFontHash->FindByGC(gc);
693 TXftFontData *fontdata = fXftFontHash->FindByFont(font);
696 gcdata->fXftFont = (XftFont *)font;
699 TXftFontData *data =
new TXftFontData(gc, (XftFont *)font, fontdata->GetName());
700 fXftFontHash->AddFont(data);
713 TXftFontData *data = fXftFontHash->FindByFont(font);
717 XftFont *xftfont = data->fXftFont;
720 XGlyphInfo glyph_info;
721 XftTextExtents8((Display *)fDisplay, xftfont, (XftChar8 *)s, len, &glyph_info);
722 return glyph_info.xOff;
737 TXftFontData *data = fXftFontHash->FindByFont(font);
744 XftFont *xftfont = data->fXftFont;
751 max_ascent = xftfont->ascent;
752 max_descent = xftfont->descent;
774 if (!text || (len < 1) || !text[0]) {
785 GetGCValues(gc, gval);
787 TXftFontData *data = fXftFontHash->FindByGC(gc);
795 xftfont = data->fXftFont;
806 UInt_t bwidth, width, height, depth;
809 XGetGeometry((Display*)fDisplay, (
Drawable)xwindow, &droot, &dx, &dy,
810 &width, &height, &bwidth, &depth);
817 memset(&xcolor, 0,
sizeof(xcolor));
820 XQueryColor((Display*)fDisplay, fColormap, &xcolor);
823 xftdraw = XftDrawCreate((Display*)fDisplay, (
Drawable)xwindow, fVisual, fColormap);
831 xftcolor.color.red = xcolor.red;
832 xftcolor.color.green = xcolor.green;
833 xftcolor.color.blue = xcolor.blue;
834 xftcolor.color.alpha = 0xffff;
837 XftDrawString8(xftdraw, &xftcolor, xftfont, x, y, (XftChar8 *)text, len);
840 XftDrawDestroy(xftdraw);
static void Activate()
Static method setting TGX11TTF as the acting gVirtualX.
static void Init()
Initialise the TrueType fonts interface.
void DeleteFont(FontStruct_t fs)
Explicitly delete font structure obtained with LoadQueryFont().
static FT_Matrix * fgRotMatrix
rotation matrix
Bool_t Init(void *display)
Initialize X11 system. Returns kFALSE in case of failure.
FT_Glyph fImage
glyph image
Definitions for TRefCnt, base class for reference counted objects.
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.
Interface to low level X11 (Xlib).
static Int_t fgAscent
string ascent, used to compute Y alignment
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=0)
Set the value of a resource or create a new resource.
virtual Int_t SetTextFont(char *fontname, ETextSetMode mode)
Set text font to specified name.
THashTable implements a hash table to store TObject's.
static TTF::TTGlyph fgGlyphs[kMaxGlyphs]
glyphs
static void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
static void SetTextFont(Font_t fontnumber)
Set specified font.
The TNamed class is the base class for all named ROOT classes.
static void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
void Error(const char *location, const char *msgfmt,...)
static void SetSmoothing(Bool_t state)
Set smoothing (anti-aliasing) flag.
void GetFontProperties(FontStruct_t font, Int_t &max_ascent, Int_t &max_descent)
Return some font properties.
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.
void SetTextSize(Float_t textsize)
Set current text size.
FontH_t GetFontHandle(FontStruct_t fs)
Return handle to font described by font structure.
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
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).
static Bool_t fgInit
true if the Init has been called
void DrawText(Int_t x, Int_t y, Float_t angle, Float_t mgn, const char *text, ETextMode mode)
Draw text using TrueType fonts.
FontStruct_t LoadQueryFont(const char *font_name)
Load font and query font.
TTF helper class containing glyphs description.
virtual FontStruct_t GetGCFont(GContext_t gc)
Return the font associated with the graphics context gc.
void SetTextFont(Font_t fontnumber)
Set specified font.
void Warning(const char *location, const char *msgfmt,...)
This class is the basic interface to the X11 (Xlib) graphics system.
void RenderString(Int_t x, Int_t y, ETextMode mode)
Perform the string rendering in the pad.
virtual void SetTextSize(Float_t textsize)
Set current text size.
TObject * FindObject(const char *name) const
Find object using its name.
static const double x1[5]
void Add(TObject *obj)
Add object to the hash table.
static TTFX11Init gTTFX11Init
const Mask_t kGCForeground
void Align(void)
Compute alignment variables.
void DeleteGC(GContext_t gc)
Explicitly delete a graphics context.
#define org(otri, vertexptr)
static Bool_t fgSmoothing
use anti-aliasing (true when >8 planes, false otherwise)
static void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
const Mask_t kGCBackground
virtual void MapGCFont(GContext_t, FontStruct_t)
Map the XftFont with the Graphics Context using it.
Bool_t Init(void *display)
Initialize X11 system. Returns kFALSE in case of failure.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
const size_t kBitsPerByte
Bool_t IsVisible(Int_t x, Int_t y, UInt_t w, UInt_t h)
Test if there is really something to render.
static Int_t fgNumGlyphs
number of glyphs in the string
static void SetTextSize(Float_t textsize)
Set current text size.
RXImage * GetBackground(Int_t x, Int_t y, UInt_t w, UInt_t h)
Get the background of the current window in an XImage.
Int_t TextWidth(FontStruct_t font, const char *s, Int_t len)
Return length of string in pixels. Size depends on font.
static Int_t fgWidth
string width, used to compute X alignment
static const FT_BBox & GetBox()
TObject * Remove(TObject *obj)
Remove object from the hashtable.