~linaro-graphics-wg/compiz-plugins-main/oneiric-gles2

« back to all changes in this revision

Viewing changes to text/src/text.cpp

  • Committer: Sam Spilsbury
  • Date: 2011-09-20 07:43:55 UTC
  • Revision ID: sam.spilsbury@canonical.com-20110920074355-puzdutejjwsu3ta2
Sync - Remove Plugins

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Compiz text plugin
3
 
 * Description: Adds text to pixmap support to Compiz.
4
 
 *
5
 
 * text.c
6
 
 *
7
 
 * Copyright: (C) 2006-2007 Patrick Niklaus, Danny Baumann, Dennis Kasprzyk
8
 
 * Authors: Patrick Niklaus <marex@opencompsiting.org>
9
 
 *          Danny Baumann   <maniac@opencompositing.org>
10
 
 *          Dennis Kasprzyk <onestone@opencompositing.org>
11
 
 *
12
 
 * This program is free software; you can redistribute it and/or
13
 
 * modify it under the terms of the GNU General Public License
14
 
 * as published by the Free Software Foundation; either version 2
15
 
 * of the License, or (at your option) any later version.
16
 
 *
17
 
 * This program is distributed in the hope that it will be useful,
18
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
 * GNU General Public License for more details.
21
 
 *
22
 
 * You should have received a copy of the GNU General Public License
23
 
 * along with this program; if not, write to the Free Software
24
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25
 
 *
26
 
 */
27
 
 
28
 
#include "private.h"
29
 
 
30
 
#define PI 3.14159265359f
31
 
 
32
 
COMPIZ_PLUGIN_20090315 (text, TextPluginVTable);
33
 
 
34
 
CompString
35
 
PrivateTextScreen::getUtf8Property (Window id,
36
 
                                    Atom   atom)
37
 
{
38
 
    Atom          type;
39
 
    int           result, format;
40
 
    unsigned long nItems, bytesAfter;
41
 
    char          *val;
42
 
    CompString    retval;
43
 
 
44
 
    result = XGetWindowProperty (screen->dpy (), id, atom, 0L, 65536, False,
45
 
                                 utf8StringAtom, &type, &format, &nItems,
46
 
                                 &bytesAfter, (unsigned char **) &val);
47
 
 
48
 
    if (result != Success)
49
 
        return retval;
50
 
 
51
 
    if (type == utf8StringAtom && format == 8 && val && nItems > 0)
52
 
    {
53
 
        char valueString[nItems + 1];
54
 
 
55
 
        strncpy (valueString, val, nItems);
56
 
        valueString[nItems] = 0;
57
 
 
58
 
        retval = valueString;
59
 
    }
60
 
 
61
 
    if (val)
62
 
        XFree (val);
63
 
 
64
 
    return retval;
65
 
}
66
 
 
67
 
CompString
68
 
PrivateTextScreen::getTextProperty (Window id,
69
 
                                    Atom   atom)
70
 
{
71
 
    XTextProperty text;
72
 
    CompString    retval;
73
 
 
74
 
    text.nitems = 0;
75
 
    if (XGetTextProperty (screen->dpy (), id, &text, atom))
76
 
    {
77
 
        if (text.value)
78
 
        {
79
 
            char valueString[text.nitems + 1];
80
 
 
81
 
            strncpy (valueString, (char *) text.value, text.nitems);
82
 
            valueString[text.nitems] = 0;
83
 
 
84
 
            retval = valueString;
85
 
 
86
 
            XFree (text.value);
87
 
        }
88
 
    }
89
 
 
90
 
    return retval;
91
 
}
92
 
 
93
 
CompString
94
 
PrivateTextScreen::getWindowName (Window id)
95
 
{
96
 
    CompString name;
97
 
 
98
 
    name = getUtf8Property (id, visibleNameAtom);
99
 
 
100
 
    if (name.empty ())
101
 
        name = getUtf8Property (id, wmNameAtom);
102
 
 
103
 
    if (name.empty ())
104
 
        name = getTextProperty (id, XA_WM_NAME);
105
 
 
106
 
    return name;
107
 
}
108
 
 
109
 
/* Actual text rendering functions */
110
 
 
111
 
/*
112
 
 * Draw a rounded rectangle path
113
 
 */
114
 
void
115
 
TextSurface::drawBackground (int     x,
116
 
                             int     y,
117
 
                             int     width,
118
 
                             int     height,
119
 
                             int     radius)
120
 
{
121
 
    int x0, y0, x1, y1;
122
 
 
123
 
    x0 = x;
124
 
    y0 = y;
125
 
    x1 = x + width;
126
 
    y1 = y + height;
127
 
 
128
 
    cairo_new_path (cr);
129
 
    cairo_arc (cr, x0 + radius, y1 - radius, radius, PI / 2, PI);
130
 
    cairo_line_to (cr, x0, y0 + radius);
131
 
    cairo_arc (cr, x0 + radius, y0 + radius, radius, PI, 3 * PI / 2);
132
 
    cairo_line_to (cr, x1 - radius, y0);
133
 
    cairo_arc (cr, x1 - radius, y0 + radius, radius, 3 * PI / 2, 2 * PI);
134
 
    cairo_line_to (cr, x1, y1 - radius);
135
 
    cairo_arc (cr, x1 - radius, y1 - radius, radius, 0, PI / 2);
136
 
    cairo_close_path (cr);
137
 
}
138
 
 
139
 
bool
140
 
TextSurface::initCairo (int width,
141
 
                        int height)
142
 
{
143
 
    Display *dpy = screen->dpy ();
144
 
 
145
 
    mPixmap = None;
146
 
    if (width > 0 && height > 0)
147
 
        mPixmap = XCreatePixmap (dpy, screen->root (), width, height, 32);
148
 
 
149
 
    mWidth  = width;
150
 
    mHeight = height;
151
 
 
152
 
    if (!mPixmap)
153
 
    {
154
 
        compLogMessage ("text", CompLogLevelError,
155
 
                        "Couldn't create %d x %d pixmap.", width, height);
156
 
        return false;
157
 
    }
158
 
 
159
 
    surface = cairo_xlib_surface_create_with_xrender_format (dpy,
160
 
                                                             mPixmap,
161
 
                                                             scrn,
162
 
                                                             format,
163
 
                                                             width,
164
 
                                                             height);
165
 
 
166
 
    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
167
 
    {
168
 
        compLogMessage ("text", CompLogLevelError, "Couldn't create surface.");
169
 
        return false;
170
 
    }
171
 
 
172
 
    cr = cairo_create (surface);
173
 
    if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
174
 
    {
175
 
        compLogMessage ("text", CompLogLevelError,
176
 
                        "Couldn't create cairo context.");
177
 
        return false;
178
 
    }
179
 
 
180
 
    return true;
181
 
}
182
 
 
183
 
bool
184
 
TextSurface::update (int width,
185
 
                     int height)
186
 
{
187
 
    Display *dpy = screen->dpy ();
188
 
 
189
 
    cairo_surface_destroy (surface);
190
 
    surface = NULL;
191
 
 
192
 
    cairo_destroy (cr);
193
 
    cr = NULL;
194
 
 
195
 
    XFreePixmap (dpy, mPixmap);
196
 
    mPixmap = None;
197
 
 
198
 
    return initCairo (width, height);
199
 
}
200
 
 
201
 
bool
202
 
TextSurface::render (const CompText::Attrib &attrib,
203
 
                     const CompString       &text)
204
 
{
205
 
    int width, height, layoutWidth;
206
 
 
207
 
    if (!valid ())
208
 
        return false;
209
 
 
210
 
    pango_font_description_set_family (font, attrib.family);
211
 
    pango_font_description_set_absolute_size (font,
212
 
                                              attrib.size * PANGO_SCALE);
213
 
    pango_font_description_set_style (font, PANGO_STYLE_NORMAL);
214
 
 
215
 
    if (attrib.flags & CompText::StyleBold)
216
 
        pango_font_description_set_weight (font, PANGO_WEIGHT_BOLD);
217
 
 
218
 
    if (attrib.flags & CompText::StyleItalic)
219
 
        pango_font_description_set_style (font, PANGO_STYLE_ITALIC);
220
 
 
221
 
    pango_layout_set_font_description (layout, font);
222
 
 
223
 
    if (attrib.flags & CompText::Ellipsized)
224
 
        pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
225
 
 
226
 
    pango_layout_set_auto_dir (layout, false);
227
 
    pango_layout_set_text (layout, text.c_str (), -1);
228
 
 
229
 
    pango_layout_get_pixel_size (layout, &width, &height);
230
 
 
231
 
    if (attrib.flags & CompText::WithBackground)
232
 
    {
233
 
        width  += 2 * attrib.bgHMargin;
234
 
        height += 2 * attrib.bgVMargin;
235
 
    }
236
 
 
237
 
    width  = MIN (attrib.maxWidth, width);
238
 
    height = MIN (attrib.maxHeight, height);
239
 
 
240
 
    /* update the size of the pango layout */
241
 
    layoutWidth = attrib.maxWidth;
242
 
    if (attrib.flags & CompText::WithBackground)
243
 
        layoutWidth -= 2 * attrib.bgHMargin;
244
 
 
245
 
    pango_layout_set_width (layout, layoutWidth * PANGO_SCALE);
246
 
 
247
 
    if (!update (width, height))
248
 
        return false;
249
 
 
250
 
    pango_cairo_update_layout (cr, layout);
251
 
 
252
 
    cairo_save (cr);
253
 
    cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
254
 
    cairo_paint (cr);
255
 
    cairo_restore (cr);
256
 
 
257
 
    cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
258
 
 
259
 
    if (attrib.flags & CompText::WithBackground)
260
 
    {
261
 
        drawBackground (0, 0, width, height,
262
 
                        MIN (attrib.bgHMargin, attrib.bgVMargin));
263
 
        cairo_set_source_rgba (cr,
264
 
                               attrib.bgColor[0] / 65535.0,
265
 
                               attrib.bgColor[1] / 65535.0,
266
 
                               attrib.bgColor[2] / 65535.0,
267
 
                               attrib.bgColor[3] / 65535.0);
268
 
        cairo_fill (cr);
269
 
        cairo_move_to (cr, attrib.bgHMargin, attrib.bgVMargin);
270
 
    }
271
 
 
272
 
    cairo_set_source_rgba (cr,
273
 
                           attrib.color[0] / 65535.0,
274
 
                           attrib.color[1] / 65535.0,
275
 
                           attrib.color[2] / 65535.0,
276
 
                           attrib.color[3] / 65535.0);
277
 
 
278
 
    pango_cairo_show_layout (cr, layout);
279
 
 
280
 
    return true;
281
 
}
282
 
 
283
 
bool
284
 
TextSurface::valid () const
285
 
{
286
 
    return scrn && format && layout && font &&
287
 
           cr && cairo_status (cr) == CAIRO_STATUS_SUCCESS &&
288
 
           surface && cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS;
289
 
}
290
 
 
291
 
TextSurface::TextSurface () :
292
 
    mWidth  (0),
293
 
    mHeight (0),
294
 
    mPixmap (None),
295
 
    cr (NULL),
296
 
    surface (NULL),
297
 
    layout (NULL),
298
 
    format (NULL),
299
 
    font (NULL),
300
 
    scrn (NULL)
301
 
{
302
 
    Display *dpy = screen->dpy ();
303
 
 
304
 
    scrn = ScreenOfDisplay (dpy, screen->screenNum ());
305
 
 
306
 
    if (!scrn)
307
 
    {
308
 
        compLogMessage ("text", CompLogLevelError,
309
 
                        "Couldn't get screen for %d.", screen->screenNum ());
310
 
        return;
311
 
    }
312
 
 
313
 
    format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
314
 
    if (!format)
315
 
    {
316
 
        compLogMessage ("text", CompLogLevelError, "Couldn't get format.");
317
 
        return;
318
 
    }
319
 
 
320
 
    if (!initCairo (1, 1))
321
 
        return;
322
 
 
323
 
    /* init pango */
324
 
    layout = pango_cairo_create_layout (cr);
325
 
    if (!layout)
326
 
    {
327
 
        compLogMessage ("text", CompLogLevelError,
328
 
                        "Couldn't create pango layout.");
329
 
        return;
330
 
    }
331
 
 
332
 
    font = pango_font_description_new ();
333
 
    if (!font)
334
 
    {
335
 
        compLogMessage ("text", CompLogLevelError,
336
 
                        "Couldn't create font description.");
337
 
        return;
338
 
    }
339
 
}
340
 
 
341
 
TextSurface::~TextSurface ()
342
 
{
343
 
    if (layout)
344
 
        g_object_unref (layout);
345
 
    if (surface)
346
 
        cairo_surface_destroy (surface);
347
 
    if (cr)
348
 
        cairo_destroy (cr);
349
 
    if (font)
350
 
        pango_font_description_free (font);
351
 
}
352
 
 
353
 
void
354
 
CompText::clear ()
355
 
{
356
 
    if (pixmap)
357
 
        XFreePixmap (screen->dpy (), pixmap);
358
 
 
359
 
    width  = 0;
360
 
    height = 0;
361
 
}
362
 
 
363
 
bool
364
 
CompText::renderText (CompString   text,
365
 
                      const Attrib &attrib)
366
 
{
367
 
    TextSurface surface;
368
 
    bool        retval = false;
369
 
 
370
 
    TEXT_SCREEN (screen);
371
 
 
372
 
    if (!ts)
373
 
        return false;
374
 
 
375
 
    if (!surface.valid ())
376
 
        return false;
377
 
 
378
 
    if (!(attrib.flags & NoAutoBinding) && !ts->gScreen)
379
 
        return false;
380
 
 
381
 
    if (surface.render (attrib, text))
382
 
    {
383
 
        if (!(attrib.flags & NoAutoBinding))
384
 
        {
385
 
            texture = GLTexture::bindPixmapToTexture (surface.mPixmap,
386
 
                                                      surface.mWidth,
387
 
                                                      surface.mHeight,
388
 
                                                      32);
389
 
            retval  = !texture.empty ();
390
 
        }
391
 
        else
392
 
        {
393
 
            retval = true;
394
 
        }
395
 
    }
396
 
 
397
 
    if (!retval && surface.mPixmap)
398
 
    {
399
 
        XFreePixmap (screen->dpy (), surface.mPixmap);
400
 
        return retval;
401
 
    }
402
 
 
403
 
    clear ();
404
 
 
405
 
    pixmap = surface.mPixmap;
406
 
    width  = surface.mWidth;
407
 
    height = surface.mHeight;
408
 
 
409
 
    return retval;
410
 
}
411
 
 
412
 
bool
413
 
CompText::renderWindowTitle (Window               window,
414
 
                             bool                 withViewportNumber,
415
 
                             const CompText::Attrib &attrib)
416
 
{
417
 
    CompString text;
418
 
 
419
 
    TEXT_SCREEN (screen);
420
 
 
421
 
    if (!ts)
422
 
        return false;
423
 
 
424
 
    if (withViewportNumber)
425
 
    {
426
 
        CompString title;
427
 
        CompPoint  winViewport;
428
 
        CompSize   viewportSize;
429
 
 
430
 
        title = ts->getWindowName (window);
431
 
        if (!title.empty ())
432
 
        {
433
 
            CompWindow *w;
434
 
 
435
 
            w = screen->findWindow (window);
436
 
            if (w)
437
 
            {
438
 
                int viewport;
439
 
 
440
 
                winViewport  = w->defaultViewport ();
441
 
                viewportSize = screen->vpSize ();
442
 
                viewport = winViewport.y () * viewportSize.width () +
443
 
                           winViewport.x () + 1;
444
 
                text = compPrintf ("%s -[%d]-", title.c_str (), viewport);
445
 
            }
446
 
            else
447
 
            {
448
 
                text = title;
449
 
            }
450
 
        }
451
 
    }
452
 
    else
453
 
    {
454
 
        text = ts->getWindowName (window);
455
 
    }
456
 
 
457
 
    if (text.empty ())
458
 
        return false;
459
 
 
460
 
    return renderText (text, attrib);
461
 
}
462
 
 
463
 
Pixmap
464
 
CompText::getPixmap ()
465
 
{
466
 
    Pixmap retval = None;
467
 
 
468
 
    if (texture.empty ())
469
 
    {
470
 
        retval = pixmap;
471
 
        pixmap = None;
472
 
    }
473
 
 
474
 
    return retval;
475
 
}
476
 
 
477
 
int
478
 
CompText::getWidth () const
479
 
{
480
 
    return width;
481
 
}
482
 
 
483
 
int
484
 
CompText::getHeight () const
485
 
{
486
 
    return height;
487
 
}
488
 
 
489
 
void
490
 
CompText::draw (float x,
491
 
                float y,
492
 
                float alpha) const
493
 
{
494
 
    GLboolean  wasBlend;
495
 
    GLint      oldBlendSrc, oldBlendDst;
496
 
 
497
 
    if (texture.empty ())
498
 
        return;
499
 
 
500
 
    glGetIntegerv (GL_BLEND_SRC, &oldBlendSrc);
501
 
    glGetIntegerv (GL_BLEND_DST, &oldBlendDst);
502
 
 
503
 
    wasBlend = glIsEnabled (GL_BLEND);
504
 
    if (!wasBlend)
505
 
        glEnable (GL_BLEND);
506
 
 
507
 
    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
508
 
 
509
 
    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
510
 
    glColor4f (alpha, alpha, alpha, alpha);
511
 
 
512
 
    for (unsigned int i = 0; i < texture.size (); i++)
513
 
    {
514
 
        GLTexture         *tex = texture[i];
515
 
        GLTexture::Matrix m = tex->matrix ();
516
 
 
517
 
        tex->enable (GLTexture::Good);
518
 
 
519
 
        glBegin (GL_QUADS);
520
 
 
521
 
        glTexCoord2f (COMP_TEX_COORD_X (m, 0), COMP_TEX_COORD_Y (m, 0));
522
 
        glVertex2f (x, y - height);
523
 
        glTexCoord2f (COMP_TEX_COORD_X (m, 0), COMP_TEX_COORD_Y (m, height));
524
 
        glVertex2f (x, y);
525
 
        glTexCoord2f (COMP_TEX_COORD_X (m, width), COMP_TEX_COORD_Y (m, height));
526
 
        glVertex2f (x + width, y);
527
 
        glTexCoord2f (COMP_TEX_COORD_X (m, width), COMP_TEX_COORD_Y (m, 0));
528
 
        glVertex2f (x + width, y - height);
529
 
 
530
 
        glEnd ();
531
 
 
532
 
        tex->disable ();
533
 
    }
534
 
 
535
 
    glColor4usv (defaultColor);
536
 
 
537
 
    if (!wasBlend)
538
 
        glDisable (GL_BLEND);
539
 
    glBlendFunc (oldBlendSrc, oldBlendDst);
540
 
}
541
 
 
542
 
CompText::CompText () :
543
 
    width (0),
544
 
    height (0),
545
 
    pixmap (None)
546
 
{
547
 
}
548
 
 
549
 
CompText::~CompText ()
550
 
{
551
 
    if (pixmap)
552
 
        XFreePixmap (screen->dpy (), pixmap);
553
 
}
554
 
 
555
 
PrivateTextScreen::PrivateTextScreen (CompScreen *screen) :
556
 
    PluginClassHandler <PrivateTextScreen, CompScreen, COMPIZ_TEXT_ABI> (screen),
557
 
    gScreen (GLScreen::get (screen))
558
 
{
559
 
    visibleNameAtom = XInternAtom (screen->dpy (), "_NET_WM_VISIBLE_NAME", 0);
560
 
    utf8StringAtom = XInternAtom (screen->dpy (), "UTF8_STRING", 0);
561
 
    wmNameAtom = XInternAtom (screen->dpy (), "_NET_WM_NAME", 0);
562
 
}
563
 
 
564
 
PrivateTextScreen::~PrivateTextScreen ()
565
 
{
566
 
}
567
 
 
568
 
bool
569
 
TextPluginVTable::init ()
570
 
{
571
 
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION))
572
 
         return false;
573
 
 
574
 
    CompPrivate p;
575
 
    p.uval = COMPIZ_TEXT_ABI;
576
 
    screen->storeValue ("text_ABI", p);
577
 
 
578
 
    return true;
579
 
}
580
 
 
581
 
void
582
 
TextPluginVTable::fini ()
583
 
{
584
 
    screen->eraseValue ("text_ABI");
585
 
}