ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QuartzLine.mm
Go to the documentation of this file.
1 // @(#)root/graf2d:$Id$
2 // Author: Olivier Couet, 24/01/2012
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include <cassert>
13 #include <vector>
14 
15 #include "CocoaUtils.h"
16 #include "TObjString.h"
17 #include "QuartzLine.h"
18 #include "RStipples.h"
19 #include "TObjArray.h"
20 #include "TString.h"
21 #include "TColor.h"
22 #include "TStyle.h"
23 #include "TROOT.h"
24 
25 namespace ROOT {
26 namespace Quartz {
27 
28 //______________________________________________________________________________
29 Bool_t SetLineColor(CGContextRef ctx, Color_t colorIndex)
30 {
31  assert(ctx != 0 && "SetLineColor, ctx parameter is null");
32 
33  const TColor *color = gROOT->GetColor(colorIndex);
34  //Do as TGX11 does.
35  if (!color)
36  color = gROOT->GetColor(kWhite);
37 
38  if (!color)
39  return kFALSE;
40 
41  const CGFloat alpha = color->GetAlpha();
42  Float_t rgb[3] = {};
43  color->GetRGB(rgb[0], rgb[1], rgb[2]);
44  CGContextSetRGBStrokeColor(ctx, rgb[0], rgb[1], rgb[2], alpha);
45 
46  return kTRUE;
47 }
48 
49 //______________________________________________________________________________
50 void SetLineType(CGContextRef ctx, Int_t n, Int_t *dash)
51 {
52  // Set the line type in the context ctx.
53  //
54  // n - length of the dash list
55  // n <= 0 use solid lines
56  // n > 0 use dashed lines described by dash(n)
57  // e.g. n = 4,dash = (6,3,1,3) gives a dashed-dotted line
58  // with dash length 6 and a gap of 7 between dashes
59  // dash(n) - dash segment lengths
60 
61  assert(ctx != 0 && "SetLineType, ctx parameter is null");
62 
63  if (n) {
64  CGFloat lengths[n];
65  for (int i = 0; i < n; i++)
66  lengths[i] = dash[i];
67  CGContextSetLineDash(ctx, 0, lengths, n);
68  } else {
69  CGContextSetLineDash(ctx, 0, NULL, 0);
70  }
71 }
72 
73 //______________________________________________________________________________
74 void SetLineStyle(CGContextRef ctx, Int_t lstyle)
75 {
76  // Set current line style in the context ctx.
77  assert(ctx != 0 && "SetLineStyle, ctx parameter is null");
78 
79  static Int_t dashed[2] = {3, 3};
80  static Int_t dotted[2] = {1, 2};
81  static Int_t dasheddotted[4] = {3, 4, 1, 4};
82 
83  if (lstyle <= 1 ) {
84  SetLineType(ctx, 0, 0);
85  } else if (lstyle == 2) {
86  SetLineType(ctx, 2, dashed);
87  } else if (lstyle == 3) {
88  SetLineType(ctx, 2,dotted);
89  } else if (lstyle == 4) {
90  SetLineType(ctx, 4, dasheddotted);
91  } else {
93  TObjArray *tokens = st.Tokenize(" ");
94  Int_t nt;
95  nt = tokens->GetEntries();
96  std::vector<Int_t> linestyle(nt);
97  for (Int_t j = 0; j<nt; j++) {
98  Int_t it;
99  sscanf(((TObjString*)tokens->At(j))->GetName(), "%d", &it);
100  linestyle[j] = (Int_t)(it/4);
101  }
102  SetLineType(ctx, nt, &linestyle[0]);
103  delete tokens;
104  }
105 }
106 
107 //______________________________________________________________________________
108 void SetLineWidth(CGContextRef ctx, Int_t width)
109 {
110  // Set the line width in the context ctx.
111  //
112  // width - the line width in pixels
113  assert(ctx != 0 && "SetLineWidth, ctx parameter is null");
114 
115 
116  if (width < 0)
117  return;
118 
119  CGContextSetLineWidth(ctx, width ? width : 1);
120 }
121 
122 //______________________________________________________________________________
123 void DrawLine(CGContextRef ctx, Int_t x1, Int_t y1, Int_t x2, Int_t y2)
124 {
125  assert(ctx != 0 && "DrawLine, ctx parameter is null");
126 
127  CGContextBeginPath(ctx);
128  CGContextMoveToPoint(ctx, x1, y1);
129  CGContextAddLineToPoint(ctx, x2, y2);
130  CGContextStrokePath(ctx);
131 }
132 
133 
134 //______________________________________________________________________________
135 void DrawPolyLine(CGContextRef ctx, Int_t n, TPoint * xy)
136 {
137  // Draw a line through all points.
138  // n : number of points
139  // xy : list of points
140 
141  assert(ctx != 0 && "DrawPolyLine, ctx parameter is null");
142  assert(xy != 0 && "DrawPolyLine, xy parameter is null");
143 
144  CGContextBeginPath(ctx);
145  CGContextMoveToPoint(ctx, xy[0].fX, xy[0].fY);
146  for (Int_t i = 1; i < n; ++i)
147  CGContextAddLineToPoint(ctx, xy[i].fX, xy[i].fY);
148 
149  if (xy[n - 1].fX == xy[0].fX && xy[n - 1].fY == xy[0].fY)
150  CGContextClosePath(ctx);
151 
152  CGContextStrokePath(ctx);
153 }
154 
155 }//namespace Quartz
156 }//namespace ROOT
const char * GetLineStyleString(Int_t i=1) const
Return line style string (used by PostScript).
Definition: TStyle.cxx:787
An array of TObjects.
Definition: TObjArray.h:39
Collectable string class.
Definition: TObjString.h:32
float Float_t
Definition: RtypesCore.h:53
#define assert(cond)
Definition: unittest.h:542
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
#define gROOT
Definition: TROOT.h:344
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
static const double x2[5]
void SetLineType(CGContextRef ctx, Int_t n, Int_t *dash)
Definition: QuartzLine.mm:50
short Color_t
Definition: RtypesCore.h:79
void DrawPolyLine(CGContextRef ctx, Int_t n, TPoint *xy)
Definition: QuartzLine.mm:135
Definition: TPoint.h:33
Definition: Rtypes.h:60
void SetLineWidth(CGContextRef ctx, Int_t width)
Definition: QuartzLine.mm:108
XPoint xy[kMAXMK]
Definition: TGX11.cxx:122
virtual void GetRGB(Float_t &r, Float_t &g, Float_t &b) const
Definition: TColor.h:79
Bool_t SetLineColor(CGContextRef ctx, Color_t colorIndex)
Definition: QuartzLine.mm:29
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2227
void SetLineStyle(CGContextRef ctx, Int_t lstyle)
Definition: QuartzLine.mm:74
static const double x1[5]
void DrawLine(CGContextRef ctx, Int_t x1, Int_t y1, Int_t x2, Int_t y2)
Definition: QuartzLine.mm:123
The color creation and management class.
Definition: TColor.h:47
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
#define NULL
Definition: Rtypes.h:82
Float_t GetAlpha() const
Definition: TColor.h:91
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
const Bool_t kTRUE
Definition: Rtypes.h:91
const Int_t n
Definition: legend1.C:16