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