]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/bitmap_font.cpp
Use pl_Locked and pl_Unlocked
[vlc] / modules / gui / skins2 / src / bitmap_font.cpp
1 /*****************************************************************************
2  * bitmap_font.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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     int i;
34
35     // Build the character table
36     if( rType == "digits" )
37     {
38         m_width = 9;
39         m_height = 13;
40         m_advance = 12;
41         m_skip = 6;
42         for( i = 0; i <= 9; i++ )
43         {
44             m_table['0'+i].m_xPos = i * m_width;
45         }
46         m_table[(size_t)' '].m_xPos = 10 * m_width;
47         m_table[(size_t)'-'].m_xPos = 11 * m_width;
48     }
49     else if( rType == "text" )
50     {
51         m_width = 5;
52         m_height = 6;
53         m_advance = 5;
54         m_skip = 5;
55         for( i = 0; i < 26; i++ )
56         {
57             m_table['A'+i].m_xPos = m_table['a'+i].m_xPos = i * m_width;
58         }
59         m_table[(size_t)'"'].m_xPos = 26 * m_width;
60         m_table[(size_t)'@'].m_xPos = 27 * m_width;
61         m_table[(size_t)' '].m_xPos = 29 * m_width;
62         for( i = 0; i <= 9; i++ )
63         {
64             m_table['0'+i].m_xPos = i * m_width;
65             m_table['0'+i].m_yPos = m_height;
66         }
67         static const char specialChars[] = {'.', ':', '(', ')', '-', '\'',
68             '!', '_', '+', '\\', '/', '[', ']', '^', '&', '%', ',', '=', '$',
69             '#'};
70         for( i = 0; i < 19; i++ )
71         {
72             m_table[(size_t)specialChars[i]].m_xPos = (11 + i) * m_width;
73             m_table[(size_t)specialChars[i]].m_yPos = m_height;
74         }
75         m_table[(size_t)'?'].m_xPos = 4 * m_width;
76         m_table[(size_t)'*'].m_xPos = 5 * m_width;
77         m_table[(size_t)'?'].m_yPos = m_table[(size_t)'*'].m_yPos = 2 * m_height;
78     }
79 }
80
81
82 GenericBitmap *BitmapFont::drawString( const UString &rString,
83                                        uint32_t color, int maxWidth ) const
84 {
85     uint32_t *pString = (uint32_t*)rString.u_str();
86     // Compute the text width
87     int width = 0;
88     for( uint32_t *ptr = pString; *ptr; ptr++ )
89     {
90         uint32_t c = *ptr;
91         if( c < 256 && m_table[c].m_xPos != -1 )
92         {
93             width += m_advance;
94         }
95         else
96         {
97             width += m_skip;
98         }
99     }
100     // Create a bitmap
101     BitmapImpl *pBmp = new BitmapImpl( getIntf(), width, m_height );
102     int xDest = 0;
103     while( *pString )
104     {
105         uint32_t c = *(pString++);
106         if( c < 256 && m_table[c].m_xPos != -1 )
107         {
108             bool res = pBmp->drawBitmap( m_rBitmap, m_table[c].m_xPos,
109                                          m_table[c].m_yPos, xDest, 0,
110                                          m_width, m_height );
111             if ( !res )
112                 msg_Warn( getIntf(), "BitmapFont::drawString: ignoring char" );
113             xDest += m_advance;
114         }
115         else
116         {
117             xDest += m_skip;
118         }
119     }
120     return pBmp;
121 }
122