1//============================================================================
2//
3// SSSS tt lll lll
4// SS SS tt ll ll
5// SS tttttt eeee ll ll aaaa
6// SSSS tt ee ee ll ll aa
7// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
8// SS SS tt ee ll ll aa aa
9// SSSS ttt eeeee llll llll aaaaa
10//
11// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
12// and the Stella Team
13//
14// See the file "License.txt" for information on usage and redistribution of
15// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16//
17// Based on code from ScummVM - Scumm Interpreter
18// Copyright (C) 2002-2004 The ScummVM project
19//============================================================================
20
21#include "Font.hxx"
22
23namespace GUI {
24
25// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
26Font::Font(const FontDesc& desc)
27 : myFontDesc(desc)
28{
29}
30
31// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32int Font::getCharWidth(uInt8 chr) const
33{
34 // If no width table is specified, return the maximum width
35 if(!myFontDesc.width)
36 return myFontDesc.maxwidth;
37
38 // If this character is not included in the font, use the default char.
39 if(chr < myFontDesc.firstchar || myFontDesc.firstchar + myFontDesc.size < chr)
40 {
41 if(chr == ' ')
42 return myFontDesc.maxwidth / 2;
43 chr = myFontDesc.defaultchar;
44 }
45
46 return myFontDesc.width[chr - myFontDesc.firstchar];
47}
48
49// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50int Font::getStringWidth(const string& str) const
51{
52 // If no width table is specified, use the maximum width
53 if(!myFontDesc.width)
54 return myFontDesc.maxwidth * int(str.size());
55 else
56 {
57 int space = 0;
58 for(auto c: str)
59 space += getCharWidth(c);
60
61 return space;
62 }
63}
64
65} // namespace GUI
66