/// GetTextSize /// Given constraints, format flags a font and text, determine the size of the string /// employs an MRU of the last several constraints passed in via a ring-buffer of size MaxCacheSize. /// Assumes Text and TextFormatFlags are the same, if either were to change, a call to /// InvalidateCache should be made public Size GetTextSize(string text, Font font, Size proposedConstraints, TextFormatFlags flags) { if (!TextRequiresWordBreak(text, font, proposedConstraints, flags)) { // Text fits within proposed width // IF we're here, this means we've got text that can fit into the proposedConstraints // without wrapping. We've determined this because our // as a side effect of calling TextRequiresWordBreak, // unconstrainedPreferredSize is set. return(_unconstrainedPreferredSize); } else { // Text does NOT fit within proposed width - requires WordBreak // IF we're here, this means that the wrapping width is smaller // than our max width. For example: we measure the text with infinite // bounding box and we determine the width to fit all the characters // to be 200 px wide. We would come here only for proposed widths less // than 200 px. // Create our ring buffer if we dont have one if (_sizeCacheList == null) { _sizeCacheList = new PreferredSizeCache[MaxCacheSize]; } // check the existing constraints from previous calls foreach (PreferredSizeCache sizeCache in _sizeCacheList) { if (sizeCache.ConstrainingSize == proposedConstraints) { return(sizeCache.PreferredSize); } if ((sizeCache.ConstrainingSize.Width == proposedConstraints.Width) && (sizeCache.PreferredSize.Height <= proposedConstraints.Height)) { // Caching a common case where the width matches perfectly, and the stored preferred height // is smaller or equal to the constraining size. // prefSize = GetPreferredSize(w,Int32.MaxValue); // prefSize = GetPreferredSize(w,prefSize.Height); return(sizeCache.PreferredSize); } // } // if we've gotten here, it means we dont have a cache entry, therefore // we should add a new one in the next available slot. Size prefSize = TextRenderer.MeasureText(text, font, proposedConstraints, flags); _nextCacheEntry = (_nextCacheEntry + 1) % MaxCacheSize; _sizeCacheList[_nextCacheEntry] = new PreferredSizeCache(proposedConstraints, prefSize); return(prefSize); } }
public Size GetTextSize(string text, Font font, Size proposedConstraints, TextFormatFlags flags) { if (!this.TextRequiresWordBreak(text, font, proposedConstraints, flags)) { return(this.unconstrainedPreferredSize); } if (this.sizeCacheList == null) { this.sizeCacheList = new PreferredSizeCache[6]; } PreferredSizeCache[] cacheArray1 = this.sizeCacheList; for (int num1 = 0; num1 < cacheArray1.Length; num1++) { PreferredSizeCache cache1 = cacheArray1[num1]; if (cache1.ConstrainingSize == proposedConstraints) { return(cache1.PreferredSize); } if ((cache1.ConstrainingSize.Width == proposedConstraints.Width) && (cache1.PreferredSize.Height <= proposedConstraints.Height)) { return(cache1.PreferredSize); } } Size size1 = TextRenderer.MeasureText(text, font, proposedConstraints, flags); this.nextCacheEntry = (this.nextCacheEntry + 1) % 6; this.sizeCacheList[this.nextCacheEntry] = new PreferredSizeCache(proposedConstraints, size1); return(size1); }
/// GetTextSize /// Given constraints, format flags a font and text, determine the size of the string /// employs an MRU of the last several constraints passed in via a ring-buffer of size MaxCacheSize. /// Assumes Text and TextFormatFlags are the same, if either were to change, a call to /// InvalidateCache should be made public Size GetTextSize(string text, Font font, Size proposedConstraints, TextFormatFlags flags) { if (!TextRequiresWordBreak(text, font, proposedConstraints, flags)) { // Text fits within proposed width // IF we're here, this means we've got text that can fit into the proposedConstraints // without wrapping. We've determined this because our // as a side effect of calling TextRequiresWordBreak, // unconstrainedPreferredSize is set. return unconstrainedPreferredSize; } else { // Text does NOT fit within proposed width - requires WordBreak // IF we're here, this means that the wrapping width is smaller // than our max width. For example: we measure the text with infinite // bounding box and we determine the width to fit all the characters // to be 200 px wide. We would come here only for proposed widths less // than 200 px. // Create our ring buffer if we dont have one if (sizeCacheList == null) { sizeCacheList = new PreferredSizeCache[MaxCacheSize]; } // check the existing constraints from previous calls foreach (PreferredSizeCache sizeCache in sizeCacheList) { if (sizeCache.ConstrainingSize == proposedConstraints) { return sizeCache.PreferredSize; } else if ((sizeCache.ConstrainingSize.Width == proposedConstraints.Width) && (sizeCache.PreferredSize.Height <= proposedConstraints.Height)) { // Caching a common case where the width matches perfectly, and the stored preferred height // is smaller or equal to the constraining size. // prefSize = GetPreferredSize(w,Int32.MaxValue); // prefSize = GetPreferredSize(w,prefSize.Height); return sizeCache.PreferredSize; } // } // if we've gotten here, it means we dont have a cache entry, therefore // we should add a new one in the next available slot. Size prefSize = TextRenderer.MeasureText(text, font, proposedConstraints, flags); nextCacheEntry = (nextCacheEntry+1)%MaxCacheSize; sizeCacheList[nextCacheEntry] = new PreferredSizeCache(proposedConstraints, prefSize); return prefSize; } }