1 // $Id: xxPixmapGraph.cc 5749 2014-10-11 19:42:10Z flaterco $
2 
3 /*  xxPixmapGraph  Graph implemented as Pixmap.
4 
5     Copyright (C) 1998  David Flater.
6 
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "xtide.hh"
22 #include "libxtide/Graph.hh"
23 #include "libxtide/PixelatedGraph.hh"
24 #include "libxtide/RGBGraph.hh"
25 #include "xxPixmapGraph.hh"
26 
27 
xxPixmapGraph(unsigned xSize,unsigned ySize,GraphStyle style)28 xxPixmapGraph::xxPixmapGraph (unsigned xSize,
29                               unsigned ySize,
30                               GraphStyle style):
31   PixelatedGraph (xSize, ySize, style) {
32   assert (xSize >= Global::minGraphWidth && ySize >= Global::minGraphHeight);
33   pixmap = xxX::makePixmap (xSize, ySize);
34 }
35 
36 
~xxPixmapGraph()37 xxPixmapGraph::~xxPixmapGraph() {
38   XFreePixmap (xxX::display, pixmap);
39 }
40 
41 
stringWidth(const Dstr & s) const42 const unsigned xxPixmapGraph::stringWidth (const Dstr &s) const {
43   return libxtide::stringWidth (Global::graphFont, s);
44 }
45 
46 
fontHeight() const47 const unsigned xxPixmapGraph::fontHeight() const {
48   return Global::graphFont.height;
49 }
50 
51 
oughtHeight() const52 const unsigned xxPixmapGraph::oughtHeight() const {
53   return Global::graphFont.oughtHeight;
54 }
55 
56 
oughtVerticalMargin() const57 const unsigned xxPixmapGraph::oughtVerticalMargin() const {
58   return 1;
59 }
60 
61 
setPixel(int x,int y,Colors::Colorchoice c)62 void xxPixmapGraph::setPixel (int x, int y, Colors::Colorchoice c) {
63   if (x < 0 || x >= (int)_xSize || y < 0 || y >= (int)_ySize)
64     return;
65   assert (c < (int)Colors::numColors);
66   XSetForeground (xxX::display, xxX::spareGC, xxX::pixels[c]);
67   XDrawPoint (xxX::display, pixmap, xxX::spareGC, x, y);
68 }
69 
70 
drawVerticalLineP(int x,int y1,int y2,Colors::Colorchoice c,double opacity)71 void xxPixmapGraph::drawVerticalLineP (int x, int y1, int y2,
72 				       Colors::Colorchoice c,
73 				       double opacity) {
74   assert (c < (int)Colors::numColors);
75   if (opacity >= 0.5) {
76     XSetForeground (xxX::display, xxX::spareGC, xxX::pixels[c]);
77     XDrawLine (xxX::display, pixmap, xxX::spareGC, x, y1, x, y2);
78   }
79 }
80 
81 
drawHorizontalLineP(int xlo,int xhi,int y,Colors::Colorchoice c)82 void xxPixmapGraph::drawHorizontalLineP (int xlo, int xhi, int y,
83 					 Colors::Colorchoice c) {
84   assert (c < (int)Colors::numColors);
85   XSetForeground (xxX::display, xxX::spareGC, xxX::pixels[c]);
86   if (xlo <= xhi) // Constraint per Graph.hh
87     XDrawLine (xxX::display, pixmap, xxX::spareGC, xlo, y, xhi, y);
88 }
89 
90 
91 // libXft 2.2.0 exhibited background color anomalies and text alignment /
92 // typesetting anomalies on PseudoColor visuals.
93 
drawStringP(int x,int y,const Dstr & s)94 void xxPixmapGraph::drawStringP (int x, int y, const Dstr &s) {
95   for (unsigned a=0; a<s.length(); ++a) {
96     const ClientSide::Glyph &g (Global::graphFont.glyphs[(uint8_t)s[a]]);
97     for (SafeVector<ClientSide::Pixel>::const_iterator it (g.pixels.begin());
98 	 it != g.pixels.end(); ++it)
99       if (it->opacity >= 90)  // Ideal threshold depends on the font.
100         setPixel (x+it->x, y+it->y, Colors::foreground);
101     x += g.advance;
102   }
103 }
104