21 #include <Cocoa/Cocoa.h> 23 # include <ft2build.h> 24 # include FT_FREETYPE_H 58 void ConvertPointsROOTToCocoa(
Int_t nPoints,
const TPoint *
xy, std::vector<TPoint> &dst,
59 NSObject<X11Drawable> *drawable)
61 assert(nPoints != 0 &&
"ConvertPointsROOTToCocoa, nPoints parameter is 0");
62 assert(xy != 0 &&
"ConvertPointsROOTToCocoa, xy parameter is null");
63 assert(drawable != 0 &&
"ConvertPointsROOTToCocoa, drawable parameter is null");
65 const auto scaleFactor = drawable.fScaleFactor;
68 for (
Int_t i = 0; i < nPoints; ++i) {
69 dst[i].fX =
SCoord_t(xy[i].fX * scaleFactor);
78 : fUseAA(true), fUseFAAA(false)
88 Error(
"TGQuartz",
"TTF::Init() failed");
108 Error(
"TGQuartz",
"TTF::Init() failed");
131 CGContextRef ctx = drawable.fContext;
144 polygon[0].
fX =
x1, polygon[0].
fY = y1;
145 polygon[1].
fX =
x2, polygon[1].
fY = y1;
146 polygon[2].
fX =
x2, polygon[2].
fY = y2;
147 polygon[3].
fX =
x1, polygon[3].
fY = y2;
156 unsigned patternIndex = 0;
164 Error(
"DrawBox",
"SetFillAreaParameters failed");
192 NSObject<X11Drawable> *
const drawable =
198 CGContextRef ctx = drawable.fContext;
207 if (drawable.fScaleFactor > 1.) {
209 CGContextScaleCTM(ctx, 1. / drawable.fScaleFactor, 1. / drawable.fScaleFactor);
218 if (
const TColorGradient *
const gradient = dynamic_cast<const TColorGradient *>(fillColor)) {
222 unsigned patternIndex = 0;
224 Error(
"DrawFillArea",
"SetFillAreaParameters failed");
256 NSObject<X11Drawable> *
const drawable =
261 CGContextRef ctx = drawable.fContext;
292 NSObject<X11Drawable> *
const drawable =
297 CGContextRef ctx = drawable.fContext;
313 if (drawable.fScaleFactor > 1.)
314 CGContextScaleCTM(ctx, 1. / drawable.fScaleFactor, 1. / drawable.fScaleFactor);
335 NSObject<X11Drawable> *
const drawable =
340 CGContextRef ctx = drawable.fContext;
356 if (drawable.fScaleFactor > 1.)
357 CGContextScaleCTM(ctx, 1. / drawable.fScaleFactor, 1. / drawable.fScaleFactor);
370 if (!text || !text[0])
376 NSObject<X11Drawable> *
const drawable =
381 CGContextRef ctx = drawable.fContext;
385 CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
390 if (fontIndex == 12 || fontIndex == 15) {
396 typedef std::vector<UniChar>::size_type size_type;
398 std::vector<UniChar> unichars(std::strlen(text));
399 for (size_type i = 0, len = unichars.size(); i < len; ++i)
400 unichars[i] = 0xF000 + (
unsigned char)text[i];
409 }
catch (
const std::exception &
e) {
410 Error(
"DrawText",
"Exception from Quartz::TextLine: %s", e.what());
417 if (!text || !text[0])
421 Error(
"DrawText",
"wchar_t string to draw, but TTF initialization failed");
451 if (!text || !text[0]) {
459 if (fontIndex == 12 || fontIndex == 15) {
460 typedef std::vector<UniChar>::size_type size_type;
462 std::vector<UniChar> unichars(std::strlen(text));
463 for (size_type i = 0, len = unichars.size(); i < len; ++i)
464 unichars[i] = 0xF000 + (
unsigned char)text[i];
466 fPimpl->fFontManager.GetTextBounds(w, h, unichars);
468 fPimpl->fFontManager.GetTextBounds(w, h, text);
493 if (!text || !text[0])
498 if (fontIndex == 12 || fontIndex == 15) {
500 typedef std::vector<UniChar>::size_type size_type;
502 std::vector<UniChar> unichars(std::strlen(text));
503 for (size_type i = 0, len = unichars.size(); i < len; ++i)
504 unichars[i] = 0xF000 + (
unsigned char)text[i];
506 return Int_t(
fPimpl->fFontManager.GetAscent(unichars));
508 return Int_t(
fPimpl->fFontManager.GetAscent(text));
534 if (!text || !text[0])
539 if (fontIndex == 12 || fontIndex == 15) {
541 typedef std::vector<UniChar>::size_type size_type;
543 std::vector<UniChar> unichars(std::strlen(text));
544 for (size_type i = 0, len = unichars.size(); i < len; ++i)
545 unichars[i] = 0xF000 + (
unsigned char)text[i];
547 return Int_t(
fPimpl->fFontManager.GetDescent(unichars));
549 return Int_t(
fPimpl->fFontManager.GetDescent(text));
660 Error(
"SetTextFont",
"TTF is not initialized");
673 Error(
"SetTextFont",
"TTF is not initialized");
688 Error(
"SetTextSize",
"TTF is not initialized");
767 if (x +
int(w) <= 0 || x >=
int(width))
770 if (y +
int(h) <= 0 || y >=
int(height))
792 dstPixmap = (QuartzPixmap *)drawable;
793 else if ([drawable isKindOfClass : [
QuartzView class]] || [drawable isKindOfClass : [
QuartzWindow class]])
794 dstPixmap = ((NSObject<X11Window> *)drawable).fBackBuffer;
798 Error(
"DrawText",
"fSelectedDrawable is neither QuartzPixmap nor a double buffered window");
817 Util::NSScopeGuard<QuartzPixmap> pixmap([[QuartzPixmap alloc] initWithW : w
H : h scaleFactor : 1.f]);
819 Error(
"DrawText",
"pixmap creation failed");
823 const unsigned char defaultBackgroundPixel[] = {255, 255, 255, 255};
824 Util::ScopedArray<unsigned char> arrayGuard;
833 arrayGuard.Reset([dstPixmap readColorBits : bbox]);
835 if (!arrayGuard.Get()) {
836 Error(
"DrawText",
"problem with reading background pixels");
841 const Int_t xo = x1 < 0 ? -
x1 : 0;
842 const Int_t yo = y1 < 0 ? -y1 : 0;
844 for (
int yp = 0; yp < int(bbox.fHeight) && yo + yp <
h; ++yp) {
845 const unsigned char *srcBase = arrayGuard.Get() + bbox.fWidth * yp * 4;
846 for (
int xp = 0; xp < int(bbox.fWidth) && xo + xp < w; ++xp) {
847 const unsigned char *
const pixel = srcBase + xp * 4;
848 [pixmap.Get() putPixel : pixel X : xo + xp Y : yo + yp];
853 [pixmap.Get() addPixel : defaultBackgroundPixel];
856 CGContextRef ctx = drawable.fContext;
859 CGContextSetRGBStrokeColor(ctx, 0., 0., 1., 1.);
863 if (FT_Glyph_To_Bitmap(&glyph->
fImage,
TTF::GetSmoothing() ? ft_render_mode_normal : ft_render_mode_mono, 0, 1 ))
866 FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph->
fImage;
867 FT_Bitmap *source = &bitmap->bitmap;
868 const Int_t bx = bitmap->left + xOff;
869 const Int_t by = h - bitmap->top - yOff;
877 [dstPixmap copy : pixmap.Get() area : copyArea withMask : nil clipOrigin :
X11::Point() toPoint : dstPoint];
890 assert(pixmap != nil &&
"DrawFTGlyphIntoPixmap, pixmap parameter is nil");
891 assert(source != 0 &&
"DrawFTGlyphIntoPixmap, source parameter is null");
897 if (back ==
ULong_t(-1) && source->width) {
898 const int maxDots = 50000;
899 int dots =
Int_t(source->width * source->rows);
907 for (
int y = 0, dotCnt = 0;
y < int(source->rows);
y++) {
908 for (
int x = 0;
x < int(source->width);
x++) {
910 const unsigned char *
const pixels = pixmap.
fData + (
y + by) * pixmap.
fWidth * 4 + (
x + bx) * 4;
911 r +=
UShort_t(pixels[0] / 255. * 0xffff);
912 g +=
UShort_t(pixels[1] / 255. * 0xffff);
913 b +=
UShort_t(pixels[2] / 255. * 0xffff);
916 if (++dotCnt >= maxDots)
927 if (col[0].fRed == r && col[0].fGreen ==
g && col[0].fBlue ==
b) {
940 if (fore != col[4].fPixel || back != col[0].fPixel) {
949 for (
int x = 3;
x > 0; --
x) {
959 const unsigned char *
s = source->buffer;
960 for (
int y = 0;
y < (int) source->rows; ++
y) {
961 for (
int x = 0;
x < (int) source->width; ++
x) {
962 unsigned char d = *s++ & 0xff;
963 d = ((d + 10) * 5) / 256;
966 if (d &&
x < (
int) source->width) {
967 const UChar_t pixel[] = {
UChar_t(
double(col[d].fRed) / 0xffff * 255),
968 UChar_t(
double(col[d].fGreen) / 0xffff * 255),
969 UChar_t(
double(col[d].fBlue) / 0xffff * 255), 255};
970 [pixmap putPixel : pixel X : bx +
x Y : by +
y];
976 unsigned char rgba[4] = {};
981 const unsigned char *row = source->buffer;
982 for (
int y = 0;
y < int(source->rows); ++
y) {
984 const unsigned char *
s = row;
985 for (
int x = 0;
x < int(source->width); ++
x) {
990 [pixmap putPixel : rgba X : bx +
x Y : by +
y];
996 row += source->pitch;
1008 if (value ==
"auto") {
1009 [[NSScreen mainScreen] backingScaleFactor] > 1. ?
fUseAA = true :
fUseAA =
false;
1010 }
else if (value ==
"no")
1013 assert(value ==
"yes" &&
"SetAA, value must be 'yes', 'no' or 'auto'");
1017 if (valuefa ==
"auto") {
1018 [[NSScreen mainScreen] backingScaleFactor] > 1. ?
fUseFAAA = true :
fUseFAAA =
false;
1019 }
else if (valuefa ==
"no")
1022 assert(valuefa ==
"yes" &&
"SetAA, value must be 'yes', 'no' or 'auto'");
1031 assert(calledFrom != 0 &&
"GetSelectedDrawableChecked, calledFrom parameter is null");
1032 assert(
fSelectedDrawable >
fPimpl->GetRootWindowID() &&
"GetSelectedDrawableChecked, bad drawable is selected");
1035 if (!drawable.fIsPixmap) {
1037 if ([drawable isKindOfClass : [
QuartzView class]]) {
1038 QuartzView *view = (QuartzView *)drawable;
1040 Error(calledFrom,
"Selected window is not double buffered");
1046 Error(calledFrom,
"Selected drawable is neither a pixmap, nor a double buffered window");
1051 if (!drawable.fContext) {
1052 Error(calledFrom,
"Context is null");
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
static void Init()
Initialise the TrueType fonts interface.
virtual void SetMarkerColor(Color_t cindex)
Sets color index "cindex" for markers.
std::unique_ptr< ROOT::MacOSX::Details::CocoaPrivate > fPimpl
virtual Float_t GetTextMagnitude()
Returns the current font magnification factor.
std::vector< unsigned char > fData
Bool_t SetFillAreaParameters(CGContextRef ctx, unsigned *patternIndex)
virtual void DrawBox(Int_t x1, Int_t y1, Int_t x2, Int_t y2, EBoxMode mode)
Draws a box between [x1,y1] and [x2,y2] according to the "mode".
virtual Color_t GetTextColor() const
Return the text color.
virtual void DrawText(Int_t x, Int_t y, Float_t angle, Float_t mgn, const char *text, ETextMode mode)
Draws a text string using current font.
virtual void SetTextColor(Color_t cindex)
Sets the color index "cindex" for text.
static Bool_t IsInitialized()
FT_Glyph fImage
glyph image
bool AdjustCropArea(const Rectangle &srcRect, Rectangle &cropArea)
virtual void DrawFillArea(Int_t n, TPoint *xy)
Fills area described by the polygon.
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
QuartzPixmap * fBackBuffer
virtual void QueryColor(Colormap_t cmap, ColorStruct_t &color)
Returns the current RGB value for the pixel in the "color" structure.
std::vector< TPoint > fConvertedPoints
virtual Width_t GetLineWidth() const
Return the line width.
virtual void DrawCellArray(Int_t x1, Int_t y1, Int_t x2, Int_t y2, Int_t nx, Int_t ny, Int_t *ic)
Draws a cell array.
virtual void SetLineWidth(Width_t width)
Sets the line width.
virtual Style_t GetMarkerStyle() const
Return the marker style.
virtual void DrawPolyMarker(Int_t n, TPoint *xy)
Draws "n" markers with the current attributes at position [x,y].
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
virtual Style_t GetLineStyle() const
Return the line style.
static const double x2[5]
virtual void SetLineColor(Color_t cindex)
Sets color index "cindex" for drawing lines.
static void LayoutGlyphs()
Compute the glyphs positions, fgAscent and fgWidth (needed for alignment).
static void SetTextFont(Font_t fontnumber)
Set specified font.
virtual Float_t GetTextSize() const
Return the text size.
static void PrepareString(const char *string)
Put the characters in "string" in the "glyphs" array.
virtual void DrawLine(Int_t x1, Int_t y1, Int_t x2, Int_t y2)
Draws a line.
virtual Int_t GetFontDescent() const
Returns the descent of the current font (in pixels.
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
virtual Size_t GetMarkerSize() const
Return the marker size.
static TTGlyph * GetGlyphs()
virtual Bool_t AllocColor(Colormap_t cmap, ColorStruct_t &color)
Allocates a read-only colormap entry corresponding to the closest RGB value supported by the hardware...
virtual void SetTextSize(Float_t textsize)
Sets the current text size to "textsize".
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
void DrawPolyLine(CGContextRef ctx, Int_t n, TPoint *xy)
static void SetSmoothing(Bool_t state)
Set smoothing (anti-aliasing) flag.
Drawable_t fSelectedDrawable
virtual void SetLineColor(Color_t lcolor)
Set the line color.
void DrawFillArea(CGContextRef ctx, Int_t n, TPoint *xy, Bool_t drawShadow)
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
virtual Font_t GetTextFont() const
Return the text font.
virtual void SetMarkerSize(Float_t markersize)
Sets marker size index.
static FT_Matrix * GetRotMatrix()
TTF helper class containing glyphs description.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
void DrawPolyMarker(CGContextRef ctx, const std::vector< TPoint > &marker, Size_t markerSize, Style_t markerStyle)
void DrawFTGlyphIntoPixmap(void *pixmap, FT_Bitmap *source, ULong_t fore, ULong_t back, Int_t bx, Int_t by)
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
char * Strip(const char *str, char c=' ')
Strip leading and trailing c (blanks by default) from a string.
void DrawPolygonWithGradientFill(CGContextRef ctx, const TColorGradient *extendedColor, const CGSize &sizeOfDrawable, Int_t nPoints, const TPoint *xy, Bool_t drawShadow)
virtual void SetFillColor(Color_t cindex)
Sets color index "cindex" for fill areas.
void PixelToRGB(Pixel_t pixelColor, CGFloat *rgb)
virtual void SetMarkerStyle(Style_t markerstyle)
Sets marker style.
lv DrawLine(0.33, 0.0, 0.33, 1.0)
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
void * GetSelectedDrawableChecked(const char *calledFrom) const
int LocalYROOTToCocoa(NSView< X11Window > *parentView, CGFloat yROOT)
virtual Window_t GetCurrentWindow() const
pointer to the current internal window used in canvas graphics
virtual Color_t GetLineColor() const
Return the line color.
static const double x1[5]
virtual ULong_t GetPixel(Color_t cindex)
Returns pixel value associated to specified ROOT color number "cindex".
virtual Int_t GetFontAscent() const
Returns the ascent of the current font (in pixels).
virtual Color_t GetFillColor() const
Return the fill area color.
static constexpr double s
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
The color creation and management class.
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
virtual void SetTextAlign(Short_t talign=11)
Sets the text alignment.
virtual void GetWindowSize(Drawable_t wid, Int_t &x, Int_t &y, UInt_t &w, UInt_t &h)
Returns the location and the size of window "id".
virtual void SetTextFont(Font_t fontnumber)
Sets the current text font number.
static void SetRotationMatrix(Float_t angle)
Set the rotation matrix used to rotate the font outlines.
typedef void((*Func_t)())
void RenderTTFString(Int_t x, Int_t y, ETextMode mode)
Bool_t IsTTFStringVisible(Int_t x, Int_t y, UInt_t w, UInt_t h)
This class implements TVirtualX interface for MacOS X, using Cocoa and Quartz 2D. ...
void DrawBox(CGContextRef ctx, Int_t x1, Int_t y1, Int_t x2, Int_t y2, bool hollow)
virtual void SetOpacity(Int_t percent)
Sets opacity of the current window.
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
virtual void GetTextExtent(UInt_t &w, UInt_t &h, char *text)
Returns the size of the specified character string "mess".
virtual void DrawPolyLine(Int_t n, TPoint *xy)
Draws a line through all points in the list.
const size_t kBitsPerByte
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
static void SetTextSize(Float_t textsize)
Set current text size.
void DrawLine(CGContextRef ctx) const
virtual Color_t GetMarkerColor() const
Return the marker color.
virtual Style_t GetFillStyle() const
Return the fill area style.
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
virtual void SetLineStyle(Style_t linestyle)
Sets the line style.
static Int_t GetNumGlyphs()
TColorGradient extends basic TColor.
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
virtual void SetFillStyle(Style_t style)
Sets fill area style.
This is non-GUI part of TVirtualX interface, implemented for MacOS X, using CoreGraphics (Quartz)...
static const FT_BBox & GetBox()
static Bool_t GetSmoothing()
Short_t fTextAlign
Text alignment.
static constexpr double g