From 4a0c34d751dbf5b6f17c0ff5f4334b74ae1e6f9c Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Thu, 2 May 2013 14:51:06 +1000 Subject: Replacing raw pointers and allocations with std::vector and std::map. --- src/ViewStyle.cxx | 200 +++++++++++++++++++++--------------------------------- 1 file changed, 79 insertions(+), 121 deletions(-) (limited to 'src/ViewStyle.cxx') diff --git a/src/ViewStyle.cxx b/src/ViewStyle.cxx index 9181d9b24..9991aa02f 100644 --- a/src/ViewStyle.cxx +++ b/src/ViewStyle.cxx @@ -33,100 +33,52 @@ MarginStyle::MarginStyle() : // A list of the fontnames - avoids wasting space in each style FontNames::FontNames() { - size = 8; - names = new char *[size]; - max = 0; } FontNames::~FontNames() { Clear(); - delete []names; - names = 0; } void FontNames::Clear() { - for (int i=0; i= size) { - // Grow array - int sizeNew = size * 2; - char **namesNew = new char *[sizeNew]; - for (int j=0; j::const_iterator it=names.begin(); it != names.end(); ++it) { + if (strcmp(*it, name) == 0) { + return *it; } - delete []names; - names = namesNew; - size = sizeNew; } - names[max] = new char[strlen(name) + 1]; - strcpy(names[max], name); - max++; - return names[max-1]; + char *nameSave = new char[strlen(name) + 1]; + strcpy(nameSave, name); + names.push_back(nameSave); + return nameSave; } -FontRealised::FontRealised(const FontSpecification &fs) { - frNext = NULL; - (FontSpecification &)(*this) = fs; +FontRealised::FontRealised() { } FontRealised::~FontRealised() { font.Release(); - delete frNext; - frNext = 0; } -void FontRealised::Realise(Surface &surface, int zoomLevel, int technology) { - PLATFORM_ASSERT(fontName); - sizeZoomed = size + zoomLevel * SC_FONT_SIZE_MULTIPLIER; +void FontRealised::Realise(Surface &surface, int zoomLevel, int technology, const FontSpecification &fs) { + PLATFORM_ASSERT(fs.fontName); + sizeZoomed = fs.size + zoomLevel * SC_FONT_SIZE_MULTIPLIER; if (sizeZoomed <= 2 * SC_FONT_SIZE_MULTIPLIER) // Hangs if sizeZoomed <= 1 sizeZoomed = 2 * SC_FONT_SIZE_MULTIPLIER; float deviceHeight = surface.DeviceHeightFont(sizeZoomed); - FontParameters fp(fontName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, weight, italic, extraFontFlag, technology, characterSet); + FontParameters fp(fs.fontName, deviceHeight / SC_FONT_SIZE_MULTIPLIER, fs.weight, fs.italic, fs.extraFontFlag, technology, fs.characterSet); font.Create(fp); ascent = surface.Ascent(font); descent = surface.Descent(font); aveCharWidth = surface.AverageCharWidth(font); spaceWidth = surface.WidthChar(font, ' '); - if (frNext) { - frNext->Realise(surface, zoomLevel, technology); - } -} - -FontRealised *FontRealised::Find(const FontSpecification &fs) { - if (!fs.fontName) - return this; - FontRealised *fr = this; - while (fr) { - if (fr->EqualTo(fs)) - return fr; - fr = fr->frNext; - } - return 0; -} - -void FontRealised::FindMaxAscentDescent(unsigned int &maxAscent, unsigned int &maxDescent) { - FontRealised *fr = this; - while (fr) { - if (maxAscent < fr->ascent) - maxAscent = fr->ascent; - if (maxDescent < fr->descent) - maxDescent = fr->descent; - fr = fr->frNext; - } } ViewStyle::ViewStyle() { @@ -134,9 +86,8 @@ ViewStyle::ViewStyle() { } ViewStyle::ViewStyle(const ViewStyle &source) { - frFirst = NULL; - Init(source.stylesSize); - for (unsigned int sty=0; stysecond; + } + fonts.clear(); } void ViewStyle::Init(size_t stylesSize_) { - frFirst = NULL; - stylesSize = 0; - styles = NULL; AllocStyles(stylesSize_); nextExtendedStyle = 256; fontNames.Clear(); @@ -334,52 +283,42 @@ void ViewStyle::Init(size_t stylesSize_) { braceBadLightIndicator = 0; } -void ViewStyle::CreateFont(const FontSpecification &fs) { - if (fs.fontName) { - for (FontRealised *cur=frFirst; cur; cur=cur->frNext) { - if (cur->EqualTo(fs)) - return; - if (!cur->frNext) { - cur->frNext = new FontRealised(fs); - return; - } - } - frFirst = new FontRealised(fs); +void ViewStyle::Refresh(Surface &surface) { + for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) { + delete it->second; } -} + fonts.clear(); -void ViewStyle::Refresh(Surface &surface) { - delete frFirst; - frFirst = NULL; selbar = Platform::Chrome(); selbarlight = Platform::ChromeHighlight(); - for (unsigned int i=0; iRealise(surface, zoomLevel, technology); + for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) { + it->second->Realise(surface, zoomLevel, technology, it->first); + } - for (unsigned int k=0; kFind(styles[k]); + for (unsigned int k=0; kfont, *fr); } maxAscent = 1; maxDescent = 1; - frFirst->FindMaxAscentDescent(maxAscent, maxDescent); + FindMaxAscentDescent(maxAscent, maxDescent); maxAscent += extraAscent; maxDescent += extraDescent; lineHeight = maxAscent + maxDescent; someStylesProtected = false; someStylesForceCase = false; - for (unsigned int l=0; l STYLE_DEFAULT) { - for (; i= stylesSize) { - size_t sizeNew = stylesSize * 2; - while (sizeNew <= index) - sizeNew *= 2; - AllocStyles(sizeNew); + if (index >= styles.size()) { + AllocStyles(index+1); } } @@ -449,7 +366,7 @@ void ViewStyle::ResetDefaultStyle() { void ViewStyle::ClearStyles() { // Reset all styles to be like the default style - for (unsigned int i=0; i STYLE_DEFAULT) { + for (; isecond; + FontMap::iterator it = fonts.find(fs); + if (it != fonts.end()) { + // Should always reach here since map was just set for all styles + return it->second; + } + return 0; +} + +void ViewStyle::FindMaxAscentDescent(unsigned int &maxAscent, unsigned int &maxDescent) { + for (FontMap::const_iterator it = fonts.begin(); it != fonts.end(); ++it) { + if (maxAscent < it->second->ascent) + maxAscent = it->second->ascent; + if (maxDescent < it->second->descent) + maxDescent = it->second->descent; + } +} -- cgit v1.2.3