diff options
Diffstat (limited to 'src/ViewStyle.cxx')
-rw-r--r-- | src/ViewStyle.cxx | 79 |
1 files changed, 56 insertions, 23 deletions
diff --git a/src/ViewStyle.cxx b/src/ViewStyle.cxx index 8f51fd5b9..f73c15ed1 100644 --- a/src/ViewStyle.cxx +++ b/src/ViewStyle.cxx @@ -29,15 +29,15 @@ MarginStyle::MarginStyle() : // A list of the fontnames - avoids wasting space in each style FontNames::FontNames() { - size = 8; - names = new char *[size]; + size = 8; + names = new char *[size]; max = 0; } FontNames::~FontNames() { Clear(); - delete []names; - names = 0; + delete []names; + names = 0; } void FontNames::Clear() { @@ -55,17 +55,17 @@ const char *FontNames::Save(const char *name) { 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]; - } - delete []names; - names = namesNew; - size = sizeNew; - } + 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]; + } + delete []names; + names = namesNew; + size = sizeNew; + } names[max] = new char[strlen(name) + 1]; strcpy(names[max], name); max++; @@ -77,8 +77,8 @@ ViewStyle::ViewStyle() { } ViewStyle::ViewStyle(const ViewStyle &source) { - Init(); - for (unsigned int sty=0;sty<(sizeof(styles)/sizeof(styles[0]));sty++) { + Init(source.stylesSize); + for (unsigned int sty=0;sty<source.stylesSize;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); @@ -142,9 +142,14 @@ ViewStyle::ViewStyle(const ViewStyle &source) { } ViewStyle::~ViewStyle() { + delete []styles; + styles = NULL; } -void ViewStyle::Init() { +void ViewStyle::Init(size_t stylesSize_) { + stylesSize = 0; + styles = NULL; + AllocStyles(stylesSize_); fontNames.Clear(); ResetDefaultStyle(); @@ -232,7 +237,7 @@ void ViewStyle::Init() { void ViewStyle::RefreshColourPalette(Palette &pal, bool want) { unsigned int i; - for (i=0;i<(sizeof(styles)/sizeof(styles[0]));i++) { + for (i=0;i<stylesSize;i++) { pal.WantFind(styles[i].fore, want); pal.WantFind(styles[i].back, want); } @@ -267,7 +272,7 @@ void ViewStyle::Refresh(Surface &surface) { maxAscent = styles[STYLE_DEFAULT].ascent; maxDescent = styles[STYLE_DEFAULT].descent; someStylesProtected = false; - for (unsigned int i=0;i<(sizeof(styles)/sizeof(styles[0]));i++) { + for (unsigned int i=0; i<stylesSize; i++) { if (i != STYLE_DEFAULT) { styles[i].Realise(surface, zoomLevel, &styles[STYLE_DEFAULT], extraFontFlag); if (maxAscent < styles[i].ascent) @@ -295,17 +300,45 @@ void ViewStyle::Refresh(Surface &surface) { } } +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::EnsureStyle(size_t index) { + if (index >= stylesSize) { + size_t sizeNew = stylesSize * 2; + while (sizeNew < index) + sizeNew *= 2; + AllocStyles(sizeNew); + } +} + void ViewStyle::ResetDefaultStyle() { styles[STYLE_DEFAULT].Clear(ColourDesired(0,0,0), ColourDesired(0xff,0xff,0xff), - Platform::DefaultFontSize(), fontNames.Save(Platform::DefaultFont()), + Platform::DefaultFontSize(), fontNames.Save(Platform::DefaultFont()), SC_CHARSET_DEFAULT, false, false, false, false, Style::caseMixed, true, true, false); } void ViewStyle::ClearStyles() { // Reset all styles to be like the default style - for (unsigned int i=0;i<(sizeof(styles)/sizeof(styles[0]));i++) { + for (unsigned int i=0; i<stylesSize; i++) { if (i != STYLE_DEFAULT) { styles[i].ClearTo(styles[STYLE_DEFAULT]); } @@ -322,5 +355,5 @@ void ViewStyle::SetStyleFontName(int styleIndex, const char *name) { } bool ViewStyle::ProtectionActive() const { - return someStylesProtected; + return someStylesProtected; } |