]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/bitmap_font.cpp
* modules/gui/skins2: ignore WM_PAINT events on the vout window
[vlc] / modules / gui / skins2 / src / bitmap_font.cpp
1 /*****************************************************************************
2  * bitmap_font.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "bitmap_font.hpp"
25 #include "generic_bitmap.hpp"
26 #include "../utils/ustring.hpp"
27
28
29 BitmapFont::BitmapFont( intf_thread_t *pIntf, const GenericBitmap &rBitmap,
30                         const string &rType ):
31     GenericFont( pIntf ), m_rBitmap( rBitmap )
32 {
33     // Build the character table
34     if( rType == "digits" )
35     {
36         m_width = 9;
37         m_height = 13;
38         m_advance = 12;
39         m_skip = 6;
40         for( int i = 0; i <= 9; i++ )
41         {
42             m_table['0'+i].m_xPos = i * m_width;
43         }
44         m_table[(size_t)'-'].m_xPos = 11 * m_width;
45     }
46     else if( rType == "text" )
47     {
48         m_width = 5;
49         m_height = 6;
50         m_advance = 5;
51         m_skip = 5;
52         for( int i = 0; i < 26; i++ )
53         {
54             m_table['A'+i].m_xPos = m_table['a'+i].m_xPos = i * m_width;
55         }
56         m_table[(size_t)'"'].m_xPos = 26 * m_width;
57         m_table[(size_t)'@'].m_xPos = 27 * m_width;
58         m_table[(size_t)' '].m_xPos = 29 * m_width;
59         for( int i = 0; i <= 9; i++ )
60         {
61             m_table['0'+i].m_xPos = i * m_width;
62             m_table['0'+i].m_yPos = m_height;
63         }
64         static const char specialChars[] = {'.', ':', '(', ')', '-', '\'',
65             '!', '_', '+', '\\', '/', '[', ']', '^', '&', '%', ',', '=', '$',
66             '#'};
67         for( int i = 0; i < 19; i++ )
68         {
69             m_table[(size_t)specialChars[i]].m_xPos = (11 + i) * m_width;
70             m_table[(size_t)specialChars[i]].m_yPos = m_height;
71         }
72         m_table[(size_t)'?'].m_xPos = 4 * m_width;
73         m_table[(size_t)'*'].m_xPos = 5 * m_width;
74         m_table[(size_t)'?'].m_yPos = m_table[(size_t)'*'].m_yPos = 2 * m_height;
75     }
76 }
77
78
79 GenericBitmap *BitmapFont::drawString( const UString &rString,
80                                        uint32_t color, int maxWidth ) const
81 {
82     uint32_t *pString = (uint32_t*)rString.u_str();
83     // Compute the text width
84     int width = 0;
85     for( uint32_t *ptr = pString; *ptr; ptr++ )
86     {
87         uint32_t c = *ptr;
88         if( c < 256 && m_table[c].m_xPos != -1 )
89         {
90             width += m_advance;
91         }
92         else
93         {
94             width += m_skip;
95         }
96     }
97     // Create a bitmap
98     BitmapImpl *pBmp = new BitmapImpl( getIntf(), width, m_height );
99     int xDest = 0;
100     while( *pString )
101     {
102         uint32_t c = *(pString++);
103         if( c < 256 && m_table[c].m_xPos != -1 )
104         {
105             pBmp->drawBitmap( m_rBitmap, m_table[c].m_xPos, m_table[c].m_yPos,
106                               xDest, 0, m_width, m_height );
107             xDest += m_advance;
108         }
109         else
110         {
111             xDest += m_skip;
112         }
113     }
114     return pBmp;
115 }
116