diff options
Diffstat (limited to 'src/ViewStyle.cxx')
| -rw-r--r-- | src/ViewStyle.cxx | 200 | 
1 files changed, 79 insertions, 121 deletions
| 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<max; i++) { -		delete []names[i]; -	} -	max = 0; +	names.clear();  }  const char *FontNames::Save(const char *name) {  	if (!name)  		return 0; -	for (int i=0; i<max; i++) { -		if (strcmp(names[i], name) == 0) { -			return names[i]; -		} -	} -	if (max >= size) { -		// Grow array -		int sizeNew = size * 2; -		char **namesNew = new char *[sizeNew]; -		for (int j=0; j<max; j++) { -			namesNew[j] = names[j]; + +	for (std::vector<char *>::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; sty<source.stylesSize; sty++) { +	Init(source.styles.size()); +	for (unsigned int sty=0; sty<source.styles.size(); sty++) {  		styles[sty] = source.styles[sty];  		// Can't just copy fontname as its lifetime is relative to its owning ViewStyle  		styles[sty].fontName = fontNames.Save(source.styles[sty].fontName); @@ -218,16 +169,14 @@ ViewStyle::ViewStyle(const ViewStyle &source) {  }  ViewStyle::~ViewStyle() { -	delete []styles; -	styles = NULL; -	delete frFirst; -	frFirst = NULL; +	styles.clear(); +	for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) { +		delete it->second; +	} +	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; i<stylesSize; i++) { +	for (unsigned int i=0; i<styles.size(); i++) {  		styles[i].extraFontFlag = extraFontFlag;  	}  	CreateFont(styles[STYLE_DEFAULT]); -	for (unsigned int j=0; j<stylesSize; j++) { +	for (unsigned int j=0; j<styles.size(); j++) {  		CreateFont(styles[j]);  	} -	assert(frFirst); -	frFirst->Realise(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; k<stylesSize; k++) { -		FontRealised *fr = frFirst->Find(styles[k]); +	for (unsigned int k=0; k<styles.size(); k++) { +		FontRealised *fr = Find(styles[k]);  		styles[k].Copy(fr->font, *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<stylesSize; l++) { +	for (unsigned int l=0; l<styles.size(); l++) {  		if (styles[l].IsProtected()) {  			someStylesProtected = true;  		} @@ -401,25 +340,6 @@ void ViewStyle::Refresh(Surface &surface) {  	textStart = marginInside ? fixedColumnWidth : leftMarginWidth;  } -void ViewStyle::AllocStyles(size_t sizeNew) { -	Style *stylesNew = new Style[sizeNew]; -	size_t i=0; -	for (; i<stylesSize; i++) { -		stylesNew[i] = styles[i]; -		stylesNew[i].fontName = styles[i].fontName; -	} -	if (stylesSize > STYLE_DEFAULT) { -		for (; i<sizeNew; i++) { -			if (i != STYLE_DEFAULT) { -				stylesNew[i].ClearTo(styles[STYLE_DEFAULT]); -			} -		} -	} -	delete []styles; -	styles = stylesNew; -	stylesSize = sizeNew; -} -  void ViewStyle::ReleaseAllExtendedStyles() {  	nextExtendedStyle = 256;  } @@ -431,11 +351,8 @@ int ViewStyle::AllocateExtendedStyles(int numberStyles) {  }  void ViewStyle::EnsureStyle(size_t index) { -	if (index >= 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<stylesSize; i++) { +	for (unsigned int i=0; i<styles.size(); i++) {  		if (i != STYLE_DEFAULT) {  			styles[i].ClearTo(styles[STYLE_DEFAULT]);  		} @@ -470,7 +387,7 @@ bool ViewStyle::ProtectionActive() const {  }  bool ViewStyle::ValidStyle(size_t styleIndex) const { -	return styleIndex < stylesSize; +	return styleIndex < styles.size();  }  void ViewStyle::CalcLargestMarkerHeight() { @@ -488,3 +405,44 @@ void ViewStyle::CalcLargestMarkerHeight() {  		}  	}  } + +void ViewStyle::AllocStyles(size_t sizeNew) { +	size_t i=styles.size(); +	styles.resize(sizeNew); +	if (styles.size() > STYLE_DEFAULT) { +		for (; i<sizeNew; i++) { +			if (i != STYLE_DEFAULT) { +				styles[i].ClearTo(styles[STYLE_DEFAULT]); +			} +		} +	} +} + +void ViewStyle::CreateFont(const FontSpecification &fs) { +	if (fs.fontName) { +		FontMap::iterator it = fonts.find(fs); +		if (it == fonts.end()) { +			fonts[fs] = new FontRealised(); +		} +	} +} + +FontRealised *ViewStyle::Find(const FontSpecification &fs) { +	if (!fs.fontName)	// Invalid specification so return arbitrary object +		return fonts.begin()->second; +	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; +	} +} | 
