Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Media / FamilyTypefaceCollection.cs / 1305600 / FamilyTypefaceCollection.cs
//------------------------------------------------------------------------ // // Microsoft Windows Client Platform // Copyright (C) Microsoft Corporation, 2002 // // File: FamilyTypefaceCollection.cs // // Contents: FamilyTypefaceCollection // // Created: 2-5-05 Niklas Borson (niklasb) // //----------------------------------------------------------------------- using System; using System.Globalization; using SC=System.Collections; using System.Collections.Generic; using MS.Internal.FontFace; using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; namespace System.Windows.Media { ////// List of FamilyTypeface objects in a FontFamily, in lookup order. /// public sealed class FamilyTypefaceCollection : IList, SC.IList { private const int InitialCapacity = 2; private ICollection _innerList; private FamilyTypeface[] _items; private int _count; /// /// Constructs a read-write list of FamilyTypeface objects. /// internal FamilyTypefaceCollection() { _innerList = null; _items = null; _count = 0; } ////// Constructes a read-only list that wraps an ICollection. /// internal FamilyTypefaceCollection(ICollectioninnerList) { _innerList = innerList; _items = null; _count = innerList.Count; } #region IEnumerable members /// /// Returns an enumerator for iterating through the list. /// public IEnumeratorGetEnumerator() { return new Enumerator(this); } SC.IEnumerator SC.IEnumerable.GetEnumerator() { return new Enumerator(this); } #endregion #region ICollection methods /// /// Adds a FamilyTypeface to the font family. /// public void Add(FamilyTypeface item) { InsertItem(_count, item); } ////// Removes all FamilyTypeface objects from the FontFamily. /// public void Clear() { ClearItems(); } ////// Determines whether the FontFamily contains the specified FamilyTypeface. /// public bool Contains(FamilyTypeface item) { return FindItem(item) >= 0; } ////// Copies the contents of the list to the specified array. /// public void CopyTo(FamilyTypeface[] array, int index) { CopyItems(array, index); } void SC.ICollection.CopyTo(Array array, int index) { CopyItems(array, index); } bool SC.ICollection.IsSynchronized { get { return false; } } object SC.ICollection.SyncRoot { get { return this; } } ////// Removes the specified FamilyTypeface. /// public bool Remove(FamilyTypeface item) { VerifyChangeable(); int i = FindItem(item); if (i >= 0) { RemoveAt(i); return true; } return false; } ////// Gets the number of items in the list. /// public int Count { get { return _count; } } ////// Gets a value indicating whether the FamilyTypeface list can be changed. /// public bool IsReadOnly { get { return _innerList != null; } } #endregion #region IList members ////// Gets the index of the specified FamilyTypeface. /// public int IndexOf(FamilyTypeface item) { return FindItem(item); } ////// Inserts a FamilyTypeface into the list. /// public void Insert(int index, FamilyTypeface item) { InsertItem(index, item); } ////// Removes the FamilyTypeface at the specified index. /// public void RemoveAt(int index) { RemoveItem(index); } ////// Gets or sets the FamilyTypeface at the specified index. /// public FamilyTypeface this[int index] { get { return GetItem(index); } set { SetItem(index, value); } } int SC.IList.Add(object value) { return InsertItem(_count, ConvertValue(value)); } bool SC.IList.Contains(object value) { return FindItem(value as FamilyTypeface) >= 0; } int SC.IList.IndexOf(object value) { return FindItem(value as FamilyTypeface); } void SC.IList.Insert(int index, object item) { InsertItem(index, ConvertValue(item)); } void SC.IList.Remove(object value) { VerifyChangeable(); int i = FindItem(value as FamilyTypeface); if (i >= 0) RemoveItem(i); } bool SC.IList.IsFixedSize { get { return IsReadOnly; } } object SC.IList.this[int index] { get { return GetItem(index); } set { SetItem(index, ConvertValue(value)); } } #endregion #region Internal implementation private int InsertItem(int index, FamilyTypeface item) { if (item == null) throw new ArgumentNullException("item"); VerifyChangeable(); // Validate the index. if (index < 0 || index > Count) throw new ArgumentOutOfRangeException("index"); // We can't have two items with same style, weight, stretch. if (FindItem(item) >= 0) throw new ArgumentException(SR.Get(SRID.CompositeFont_DuplicateTypeface)); // Make room for the new item. if (_items == null) { _items = new FamilyTypeface[InitialCapacity]; } else if (_count == _items.Length) { FamilyTypeface[] items = new FamilyTypeface[_count * 2]; for (int i = 0; i < index; ++i) items[i] = _items[i]; for (int i = index; i < _count; ++i) items[i + 1] = _items[i]; _items = items; } else if (index < _count) { for (int i = _count - 1; i >= index; --i) _items[i + 1] = _items[i]; } // Add the item. _items[index] = item; _count++; return index; } private void InitializeItemsFromInnerList() { if (_innerList != null && _items == null) { // Create the array. FamilyTypeface[] items = new FamilyTypeface[_count]; // Create a FamilyTypeface for each Typeface in the inner list. int i = 0; foreach (Typeface face in _innerList) { items[i++] = new FamilyTypeface(face); } // For thread-safety, set _items to the fully-initialized array at the end. _items = items; } } private FamilyTypeface GetItem(int index) { RangeCheck(index); InitializeItemsFromInnerList(); return _items[index]; } private void SetItem(int index, FamilyTypeface item) { if (item == null) throw new ArgumentNullException("item"); VerifyChangeable(); RangeCheck(index); _items[index] = item; } private void ClearItems() { VerifyChangeable(); _count = 0; _items = null; } private void RemoveItem(int index) { VerifyChangeable(); RangeCheck(index); _count--; for (int i = index; i < _count; ++i) { _items[i] = _items[i + 1]; } _items[_count] = null; } private int FindItem(FamilyTypeface item) { InitializeItemsFromInnerList(); if (_count != 0 && item != null) { for (int i = 0; i < _count; ++i) { if (GetItem(i).Equals(item)) return i; } } return -1; } private void RangeCheck(int index) { if (index < 0 || index >= _count) throw new ArgumentOutOfRangeException("index"); } private void VerifyChangeable() { if (_innerList != null) throw new NotSupportedException(SR.Get(SRID.General_ObjectIsReadOnly)); } private FamilyTypeface ConvertValue(object obj) { if (obj == null) throw new ArgumentNullException("obj"); FamilyTypeface familyTypeface = obj as FamilyTypeface; if (familyTypeface == null) throw new ArgumentException(SR.Get(SRID.CannotConvertType, obj.GetType(), typeof(FamilyTypeface))); return familyTypeface; } private void CopyItems(Array array, int index) { if (array == null) throw new ArgumentNullException("array"); if (array.Rank != 1) throw new ArgumentException(SR.Get(SRID.Collection_CopyTo_ArrayCannotBeMultidimensional)); Type elementType = array.GetType().GetElementType(); if (!elementType.IsAssignableFrom(typeof(FamilyTypeface))) throw new ArgumentException(SR.Get(SRID.CannotConvertType, typeof(FamilyTypeface[]), elementType)); if (index >= array.Length) throw new ArgumentException(SR.Get(SRID.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array")); if (_count > array.Length - index) throw new ArgumentException(SR.Get(SRID.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, "array")); if (_count != 0) { InitializeItemsFromInnerList(); Array.Copy(_items, 0, array, index, _count); } } private class Enumerator : IEnumerator, SC.IEnumerator { FamilyTypefaceCollection _list; int _index; FamilyTypeface _current; internal Enumerator(FamilyTypefaceCollection list) { _list = list; _index = -1; _current = null; } public bool MoveNext() { int count = _list.Count; if (_index < count) { _index++; if (_index < count) { _current = _list[_index]; return true; } } _current = null; return false; } void SC.IEnumerator.Reset() { _index = -1; } public FamilyTypeface Current { get { return _current; } } object SC.IEnumerator.Current { get { // If there is no current item a non-generic IEnumerator should throw an exception, // but a generic IEnumerator is not required to. if (_current == null) throw new InvalidOperationException(SR.Get(SRID.Enumerator_VerifyContext)); return _current; } } public void Dispose() { } } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- GeneralTransform.cs
- DateTimeOffsetStorage.cs
- TextDecoration.cs
- NativeMethods.cs
- StylusPointPropertyUnit.cs
- XPathNavigatorKeyComparer.cs
- XPathAncestorIterator.cs
- FileDialogCustomPlacesCollection.cs
- JsonServiceDocumentSerializer.cs
- IndexedString.cs
- DataBindingCollectionConverter.cs
- AlphabetConverter.cs
- XmlExceptionHelper.cs
- XmlUtil.cs
- EventData.cs
- FormViewPagerRow.cs
- Models.cs
- GatewayDefinition.cs
- HMACSHA384.cs
- SizeConverter.cs
- DataGridColumnHeader.cs
- WebScriptMetadataMessageEncoderFactory.cs
- BrowserCapabilitiesFactory.cs
- ErrorBehavior.cs
- UnitControl.cs
- DispatcherFrame.cs
- HtmlTitle.cs
- TextParagraphView.cs
- AudioFormatConverter.cs
- Authorization.cs
- CustomErrorCollection.cs
- ObjectAssociationEndMapping.cs
- StoreContentChangedEventArgs.cs
- TreeViewItemAutomationPeer.cs
- XmlCustomFormatter.cs
- SpeechRecognitionEngine.cs
- LocalizationParserHooks.cs
- HtmlTitle.cs
- VisualBasicImportReference.cs
- Unit.cs
- TimerEventSubscription.cs
- InstanceDescriptor.cs
- Mutex.cs
- DataGridHeaderBorder.cs
- TimeSpan.cs
- ContentWrapperAttribute.cs
- WindowProviderWrapper.cs
- KeyboardEventArgs.cs
- SQLDateTime.cs
- UnsafeMethods.cs
- WebPart.cs
- ColumnResizeUndoUnit.cs
- IfJoinedCondition.cs
- WebBrowser.cs
- NavigationProperty.cs
- assertwrapper.cs
- DataGridViewColumnConverter.cs
- Accessible.cs
- DynamicRendererThreadManager.cs
- CustomError.cs
- XmlDataSourceView.cs
- IDReferencePropertyAttribute.cs
- ReachSerializableProperties.cs
- HMACSHA384.cs
- RegularExpressionValidator.cs
- DataSourceXmlElementAttribute.cs
- QueryResults.cs
- TableCell.cs
- HostingEnvironmentException.cs
- TemplateField.cs
- SoapIncludeAttribute.cs
- InheritanceContextChangedEventManager.cs
- DataTableClearEvent.cs
- DataObject.cs
- DeviceSpecificDesigner.cs
- ProfilePropertySettingsCollection.cs
- Expression.cs
- RootCodeDomSerializer.cs
- PropertyIDSet.cs
- AssemblyUtil.cs
- FileBasedResourceGroveler.cs
- ToolBar.cs
- MarkupCompilePass2.cs
- CompilationLock.cs
- SupportsEventValidationAttribute.cs
- SmiRecordBuffer.cs
- MDIWindowDialog.cs
- SID.cs
- Accessible.cs
- DataGridViewAccessibleObject.cs
- HotSpotCollectionEditor.cs
- infer.cs
- BamlLocalizationDictionary.cs
- XamlFigureLengthSerializer.cs
- RouteItem.cs
- TimerElapsedEvenArgs.cs
- LambdaCompiler.Expressions.cs
- ViewKeyConstraint.cs
- PathSegment.cs
- EntityDataSourceViewSchema.cs