38#include <X11/cursorfont.h>
39#include <X11/keysym.h>
50#include <X11/Xft/Xft.h>
59 TNamed(
name,
""), TRefCnt(), fXftFont(xftfont)
67 fGC = gc; fXftFont = (XftFont *)font;
70 ~TXftFontData()
override
73 if (fXftFont) XftFontClose((Display*)
gVirtualX->GetDisplay(), fXftFont);
83 TXftFontHash() { fList =
new THashTable(50); }
85 TXftFontData *FindByName(
const char *
name)
94 while (
auto d = (TXftFontData*) next()) {
95 if (
d->fXftFont == (XftFont *)font)
105 while (
auto d = (TXftFontData*) next()) {
112 void AddFont(TXftFontData *data)
119 while (
auto d = (TXftFontData*) next()) {
120 if (
d->fXftFont == data->fXftFont)
121 data->AddReference();
127 void FreeFont(TXftFontData *data)
160 SetTitle(
"ROOT interface to X11 with TrueType fonts");
166 fXftFontHash =
nullptr;
187 fXftFontHash =
nullptr;
188 XFontStruct *fs =
nullptr;
189 if (display) fs = XLoadQueryFont((Display *)display,
"-*-helvetica-*-r-*-*-14-*-*-*-*-*-*-*");
190 if (!fs)
gEnv->SetValue(
"X11.UseXft", 1);
191 if (display && fs) XFreeFont((Display *)display, fs);
192 if (
gEnv->GetValue(
"X11.UseXft", 0)) {
194 fXftFontHash =
new TXftFontHash();
211 auto source = (FT_Bitmap *) _source;
225 std::vector<RXColor> bcol(ndots);
229 for (
unsigned y = 0;
y < source->rows;
y++) {
230 for (
unsigned x = 0;
x < source->width;
x++) {
232 auto &bc = bcol[dotcnt];
233 bc.pixel = XGetPixel(xim, bx +
x, by +
y);
234 bc.flags = DoRed | DoGreen | DoBlue;
235 if (++dotcnt >= bcol.size())
break;
240 for (
auto &entry : bcol) {
245 col[0].red = (
UShort_t) (
r / bcol.size());
246 col[0].green = (
UShort_t) (
g / bcol.size());
247 col[0].blue = (
UShort_t) (
b / bcol.size());
251 col[0].flags = DoRed | DoGreen | DoBlue;
257 col[4].flags = DoRed | DoGreen | DoBlue;
262 for (
int x = 3;
x > 0;
x--) {
263 col[
x].red = (col[4].red *
x + col[0].red *(4-
x)) /4;
264 col[
x].green = (col[4].green*
x + col[0].green*(4-
x)) /4;
265 col[
x].blue = (col[4].blue *
x + col[0].blue *(4-
x)) /4;
267 Warning(
"DrawFTGlyph",
"cannot allocate smoothing color");
268 col[
x].pixel = col[
x+1].pixel;
274 for (
unsigned y = 0;
y < source->rows;
y++) {
275 for (
unsigned x = 0;
x < source->width;
x++) {
278 XPutPixel(xim, bx +
x, by +
y, col[
d].pixel);
284 for (
unsigned y = 0;
y < source->rows;
y++) {
288 for (
unsigned x = 0;
x < source->width;
x++) {
289 if (
n == 0)
d = *s++;
291 XPutPixel(xim, bx +
x, by +
y, fore);
294 row += source->pitch;
299template<
class CharType>
321 FT_Vector align_vect;
341 align_vect.x = align_vect.x >> 6;
342 align_vect.y = align_vect.y >> 6;
349 if (w <= 0 ||
h <= 0)
352 Int_t x1 =
x - Xoff - align_vect.x;
353 Int_t y1 =
y + Yoff + align_vect.y -
h;
373 depth, ZPixmap, 0,
nullptr, w,
h,
374 depth <= 8 ? 8 : (depth <= 16 ? 16 : 32), 0);
380 xim->data = (
char *)
malloc(xim->bytes_per_line *
h);
381 memset(xim->data, 0, xim->bytes_per_line *
h);
385 auto gc = (GC *)
GetGCW(wctxt, 3);
387 Error(
"DrawTextW",
"error getting Graphics Context");
390 XGetGCValues((Display*)
fDisplay, *gc, GCForeground | GCBackground, &values);
397 Error(
"DrawTextW",
"error getting background image");
402 Int_t xo = 0, yo = 0;
403 if (x1 < 0) xo = -x1;
404 if (y1 < 0) yo = -y1;
406 for (
int yp = 0; yp < (
int) bim->height; yp++) {
407 for (
int xp = 0; xp < (
int) bim->width; xp++) {
408 ULong_t pixel = XGetPixel(bim, xp, yp);
409 XPutPixel(xim, xo+xp, yo+yp, pixel);
416 XAddPixel(xim, values.background);
417 bg = values.background;
423 Int_t bx = bitmap->left + Xoff;
424 Int_t by =
h - bitmap->top - Yoff;
430 gc = (GC *)
GetGCW(wctxt, 6);
432 XPutImage((Display*)
fDisplay, cws, *gc, xim, 0, 0, x1, y1, w,
h);
476 if (
x+w > width) w = width -
x;
477 if (
y+
h > height)
h = height -
y;
495 TXftFontData *data = fXftFontHash->FindByName(font_name);
509 data =
new TXftFontData(0, xftfont, font_name);
510 fXftFontHash->AddFont(data);
525 TXftFontData *data = fXftFontHash->FindByFont(fs);
528 fXftFontHash->FreeFont(data);
541 TXftFontData *gcdata = fXftFontHash->FindByGC(gc);
542 if (gcdata) fXftFontHash->FreeFont(gcdata);
567 TXftFontData *data = fXftFontHash->FindByGC(gc);
583 TXftFontData *gcdata = fXftFontHash->FindByGC(gc);
584 TXftFontData *fontdata = fXftFontHash->FindByFont(font);
587 gcdata->fXftFont = (XftFont *)font;
590 TXftFontData *data =
new TXftFontData(gc, (XftFont *)font, fontdata->GetName());
591 fXftFontHash->AddFont(data);
604 TXftFontData *data = fXftFontHash->FindByFont(font);
608 XftFont *xftfont = data->fXftFont;
611 XGlyphInfo glyph_info;
612 XftTextExtents8((Display *)
fDisplay, xftfont, (XftChar8 *)s, len, &glyph_info);
613 return glyph_info.xOff;
628 TXftFontData *data = fXftFontHash->FindByFont(font);
635 XftFont *xftfont = data->fXftFont;
642 max_ascent = xftfont->ascent;
643 max_descent = xftfont->descent;
665 if (!
text || (len < 1) || !
text[0]) {
678 TXftFontData *data = fXftFontHash->FindByGC(gc);
686 xftfont = data->fXftFont;
697 UInt_t bwidth, width, height, depth;
701 &width, &height, &bwidth, &depth);
708 memset(&xcolor, 0,
sizeof(xcolor));
722 xftcolor.color.red = xcolor.red;
723 xftcolor.color.green = xcolor.green;
724 xftcolor.color.blue = xcolor.blue;
725 xftcolor.color.alpha = 0xffff;
728 XftDrawString8(xftdraw, &xftcolor, xftfont,
x,
y, (XftChar8 *)
text, len);
731 XftDrawDestroy(xftdraw);
Handle_t WinContext_t
Window drawing context.
const Mask_t kGCBackground
const Mask_t kGCForeground
Handle_t FontH_t
Font handle (as opposed to Font_t which is an index).
Handle_t Drawable_t
Drawable handle.
Handle_t GContext_t
Graphics context handle.
Handle_t FontStruct_t
Pointer to font structure.
Handle_t Window_t
Window handle.
unsigned short UShort_t
Unsigned Short integer 2 bytes (unsigned short).
int Int_t
Signed integer 4 bytes (int).
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char).
constexpr ULong_t kBitsPerByte
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
bool Bool_t
Boolean (0=false, 1=true) (bool).
float Float_t
Float 4 bytes (float).
static TTFX11Init gTTFX11Init
static void Activate()
Static method setting TGX11TTF as the acting gVirtualX.
void DrawTextW(WinContext_t wctxt, Int_t x, Int_t y, Float_t angle, Float_t mgn, const char *text, ETextMode mode) override
Draw text using TrueType fonts.
Bool_t Init(void *display) override
Initialize X11 system. Returns kFALSE in case of failure.
void DrawFTGlyph(void *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.
TGX11TTF(TGX11 &&org)
Create copy of TGX11 but now use TrueType fonts.
void DrawTextHelper(WinContext_t wctxt, Int_t x, Int_t y, Float_t angle, Float_t mgn, const CharType *text, ETextMode mode)
RXImage * GetBackground(WinContext_t wctxt, Int_t x, Int_t y, UInt_t w, UInt_t h)
Get the background of the current window in an XImage.
void * fDisplay
Pointer to display.
const TAttText & GetTextAttW(WinContext_t wctxt) const
Return text attributes for specified window context.
Colormap fColormap
Default colormap, 0 if b/w.
FontStruct_t LoadQueryFont(const char *font_name) override
Load font and query font.
Int_t fScreenNumber
Screen number.
void GetWindowSize(Drawable_t id, Int_t &x, Int_t &y, UInt_t &w, UInt_t &h) override
Return geometry of window (should be called GetGeometry but signature already used).
TGX11(TGX11 &&org)
Copy constructor. Currently only used by TGX11TTF.
Bool_t AllocColor(Colormap cmap, RXColor *color)
Allocate color in colormap.
void QueryColors(Colormap cmap, RXColor *colors, Int_t ncolors)
Returns the current RGB value for the pixel in the XColor structure.
void DrawTextW(WinContext_t wctxt, Int_t x, Int_t y, Float_t angle, Float_t mgn, const char *text, ETextMode mode) override
Draw a text string using current font on specified window.
Bool_t fHasXft
True when XftFonts are used.
void * GetGCW(WinContext_t wctxt, Int_t which) const
Return X11 Graphics Context for specified window context.
Int_t fDepth
Number of color planes.
Bool_t Init(void *display) override
Initialize X11 system. Returns kFALSE in case of failure.
Window_t GetWindow(WinContext_t wctxt) const
Return X11 window for specified window context.
void DeleteGC(GContext_t gc) override
Explicitly delete a graphics context.
void GetGCValues(GContext_t gc, GCValues_t &gval) override
Get current values from graphics context gc.
void DeleteFont(FontStruct_t fs) override
Explicitly delete font structure obtained with LoadQueryFont().
Int_t TextWidth(FontStruct_t font, const char *s, Int_t len) override
Return length of string in pixels. Size depends on font.
FontH_t GetFontHandle(FontStruct_t fs) override
Return handle to font described by font structure.
void DrawString(Drawable_t id, GContext_t gc, Int_t x, Int_t y, const char *s, Int_t len) override
Draw a string using a specific graphics context in position (x,y).
EAlign GetTextAlignW(WinContext_t wctxt) const
Return text align value for specified window context.
RVisual * fVisual
Pointer to visual used by all windows.
void GetFontProperties(FontStruct_t font, Int_t &max_ascent, Int_t &max_descent) override
Return some font properties.
Bool_t fHasTTFonts
True when TrueType fonts are used.
void Add(TObject *obj) override
Add object to the hash table.
TObject * Remove(TObject *obj) override
Remove object from the hashtable.
TObject * FindObject(const char *name) const override
Find object using its name.
The TNamed class is the base class for all named ROOT classes.
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
virtual void SetName(const char *name)
Set the name of the TNamed.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definitions for TRefCnt, base class for reference counted objects.
void SetRefCount(UInt_t r)
UInt_t References() const
Small utility class that takes care of switching the current gVirtualX to the new TGX11TTF class as s...
Dynamic handle to work with freetype 2 library.
Bool_t SetTextSize(Float_t textsize)
Set current text size.
void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
const FT_BBox & GetBox() const
void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
FT_BitmapGlyph GetGlyphBitmap(UInt_t n, Bool_t smooth=kFALSE)
Return bitmap for specified glyph.
UInt_t GetNumGlyphs() const
void SetTextFont(Font_t fontnumber)
Set specified font.
void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
static Bool_t GetSmoothing()
FT_Matrix * GetRotMatrix() const
static void SetSmoothing(Bool_t state)
virtual void DeleteFont(FontStruct_t fs)
Explicitly deletes the font structure "fs" obtained via LoadQueryFont().
virtual FontStruct_t GetGCFont(GContext_t gc)
Return the font associated with the graphics context gc.
virtual FontStruct_t LoadQueryFont(const char *font_name)
Provides the most common way for accessing a font: opens (loads) the specified font and returns a poi...
virtual FontH_t GetFontHandle(FontStruct_t fs)
Returns the font handle of the specified font structure "fs".
virtual void GetFontProperties(FontStruct_t font, Int_t &max_ascent, Int_t &max_descent)
Returns the font properties.
virtual void DeleteGC(GContext_t gc)
Deletes the specified GC "gc".
virtual void DrawString(Drawable_t id, GContext_t gc, Int_t x, Int_t y, const char *s, Int_t len)
Each character image, as defined by the font in the GC, is treated as an additional mask for a fill o...
virtual void MapGCFont(GContext_t, FontStruct_t)
Map the XftFont with the Graphics Context using it.
virtual Int_t TextWidth(FontStruct_t font, const char *s, Int_t len)
Return length of the string "s" in pixels. Size depends on font.
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Mask_t fMask
bit mask specifying which fields are valid
ULong_t fForeground
foreground pixel