Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Core / System / Windows / Media / Parsers.cs / 1 / Parsers.cs
//------------------------------------------------------------------------------ // Microsoft Avalon // Copyright (c) Microsoft Corporation, 2001 // // File: Parsers.cs // Synopsis: Implements class Parsers for internal use of type converters // Created: 06/13/2002 // //----------------------------------------------------------------------------- using System; using System.Collections; using System.Collections.Specialized; using System.Diagnostics; using MS.Internal; using System.ComponentModel; using System.Globalization; using System.Windows.Media; using System.Windows.Media.Media3D; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows; using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; namespace MS.Internal { internal static partial class Parsers { private const int s_zeroChar = (int) '0'; private const int s_aLower = (int) 'a'; private const int s_aUpper = (int) 'A'; static private int ParseHexChar(char c ) { int intChar = (int) c; if ((intChar >= s_zeroChar) && (intChar <= (s_zeroChar+9))) { return (intChar-s_zeroChar); } if ((intChar >= s_aLower) && (intChar <= (s_aLower+5))) { return (intChar-s_aLower + 10); } if ((intChar >= s_aUpper) && (intChar <= (s_aUpper+5))) { return (intChar-s_aUpper + 10); } throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } static private Color ParseHexColor(string trimmedColor) { int a,r,g,b; a = 255; if ( trimmedColor.Length > 7 ) { a = ParseHexChar(trimmedColor[1]) * 16 + ParseHexChar(trimmedColor[2]); r = ParseHexChar(trimmedColor[3]) * 16 + ParseHexChar(trimmedColor[4]); g = ParseHexChar(trimmedColor[5]) * 16 + ParseHexChar(trimmedColor[6]); b = ParseHexChar(trimmedColor[7]) * 16 + ParseHexChar(trimmedColor[8]); } else if ( trimmedColor.Length > 5) { r = ParseHexChar(trimmedColor[1]) * 16 + ParseHexChar(trimmedColor[2]); g = ParseHexChar(trimmedColor[3]) * 16 + ParseHexChar(trimmedColor[4]); b = ParseHexChar(trimmedColor[5]) * 16 + ParseHexChar(trimmedColor[6]); } else if (trimmedColor.Length > 4) { a = ParseHexChar(trimmedColor[1]); a = a + a*16; r = ParseHexChar(trimmedColor[2]); r = r + r*16; g = ParseHexChar(trimmedColor[3]); g = g + g*16; b = ParseHexChar(trimmedColor[4]); b = b + b*16; } else { r = ParseHexChar(trimmedColor[1]); r = r + r*16; g = ParseHexChar(trimmedColor[2]); g = g + g*16; b = ParseHexChar(trimmedColor[3]); b = b + b*16; } return ( Color.FromArgb ((byte)a, (byte)r, (byte)g, (byte)b) ); } internal const string s_ContextColor = "ContextColor "; internal const string s_ContextColorNoSpace = "ContextColor"; static private Color ParseContextColor(string trimmedColor, IFormatProvider formatProvider, ITypeDescriptorContext context) { if (!trimmedColor.StartsWith(s_ContextColor, StringComparison.OrdinalIgnoreCase)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(s_ContextColor.Length); tokens = tokens.Trim(); string[] preSplit = tokens.Split(new Char[] { ' ' }); if (preSplit.GetLength(0)< 2) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } tokens = tokens.Substring(preSplit[0].Length); TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); string[] split = tokens.Split(new Char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); int numTokens = split.GetLength(0); float alpha = Convert.ToSingle(th.NextTokenRequired(), formatProvider); float[] values = new float[numTokens - 1]; for (int i = 0; i < numTokens - 1; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } string profileString = preSplit[0]; UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context,profileString); Uri profileUri; if (uriHolder.BaseUri != null) { profileUri = new Uri(uriHolder.BaseUri, uriHolder.OriginalUri); } else { profileUri = uriHolder.OriginalUri; } Color result = Color.FromAValues(alpha, values, profileUri); // If the number of color values found does not match the number of channels in the profile, we must throw if (result.ColorContext.NumChannels != values.Length) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return result; } static private Color ParseScRgbColor(string trimmedColor, IFormatProvider formatProvider) { if (!trimmedColor.StartsWith("sc#", StringComparison.Ordinal)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(3, trimmedColor.Length - 3); // The tokenizer helper will tokenize a list based on the IFormatProvider. TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); float[] values = new float[4]; for (int i = 0; i < 3; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } if (th.NextToken()) { values[3] = Convert.ToSingle(th.GetCurrentToken(), formatProvider); // We should be out of tokens at this point if (th.NextToken()) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return Color.FromScRgb(values[0], values[1], values[2], values[3]); } else { return Color.FromScRgb(1.0f, values[0], values[1], values[2]); } } ////// ParseColor /// string with color description /// IFormatProvider for processing string /// internal static Color ParseColor(string color, IFormatProvider formatProvider) { return ParseColor(color, formatProvider, null); } ////// ParseColor /// string with color description /// IFormatProvider for processing string /// ITypeDescriptorContext /// internal static Color ParseColor(string color, IFormatProvider formatProvider, ITypeDescriptorContext context) { bool isPossibleKnowColor; bool isNumericColor; bool isScRgbColor; bool isContextColor; string trimmedColor = KnownColors.MatchColor(color, out isPossibleKnowColor, out isNumericColor, out isContextColor, out isScRgbColor); if ((isPossibleKnowColor == false) && (isNumericColor == false) && (isScRgbColor == false) && (isContextColor== false)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } //Is it a number? if (isNumericColor) { return ParseHexColor(trimmedColor); } else if (isContextColor) { return ParseContextColor(trimmedColor, formatProvider, context); } else if (isScRgbColor) { return ParseScRgbColor(trimmedColor, formatProvider); } else { KnownColor kc = KnownColors.ColorStringToKnownColor(trimmedColor); if (kc == KnownColor.UnknownColor) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return Color.FromUInt32((uint)kc); } } ////// ParseBrush /// string with brush description /// IFormatProvider for processing string /// ITypeDescriptorContext /// internal static Brush ParseBrush(string brush, IFormatProvider formatProvider, ITypeDescriptorContext context) { bool isPossibleKnownColor; bool isNumericColor; bool isScRgbColor; bool isContextColor; string trimmedColor = KnownColors.MatchColor(brush, out isPossibleKnownColor, out isNumericColor, out isContextColor, out isScRgbColor); if (trimmedColor.Length == 0) { throw new FormatException(SR.Get(SRID.Parser_Empty)); } // Note that because trimmedColor is exactly brush.Trim() we don't have to worry about // extra tokens as we do with TokenizerHelper. If we return one of the solid color // brushes then the ParseColor routine (or ColorStringToKnownColor) matched the entire // input. if (isNumericColor) { return (new SolidColorBrush(ParseHexColor(trimmedColor))); } if (isContextColor) { return (new SolidColorBrush(ParseContextColor(trimmedColor, formatProvider, context))); } if (isScRgbColor) { return (new SolidColorBrush(ParseScRgbColor(trimmedColor, formatProvider))); } if (isPossibleKnownColor) { SolidColorBrush scp = KnownColors.ColorStringToKnownBrush(trimmedColor); if (scp != null) { return scp; } } // If it's not a color, so the content is illegal. throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } ////// ParseTransform - parse a Transform from a string /// internal static Transform ParseTransform( string transformString, IFormatProvider formatProvider) { Matrix matrix = Matrix.Parse(transformString); return new MatrixTransform(matrix); } ////// Parse a PathFigureCollection string. /// internal static PathFigureCollection ParsePathFigureCollection( string pathString, IFormatProvider formatProvider) { PathStreamGeometryContext context = new PathStreamGeometryContext(); AbbreviatedGeometryParser parser = new AbbreviatedGeometryParser(); parser.ParseToGeometryContext(context, pathString, 0 /* curIndex */); PathGeometry pathGeometry = context.GetPathGeometry(); return pathGeometry.Figures; } } } // 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
- ZipIOCentralDirectoryBlock.cs
- ZipIOEndOfCentralDirectoryBlock.cs
- HtmlDocument.cs
- DataColumnSelectionConverter.cs
- StringFreezingAttribute.cs
- XmlJsonReader.cs
- EventTrigger.cs
- RelationshipEnd.cs
- OdbcFactory.cs
- EntityWithChangeTrackerStrategy.cs
- HtmlPanelAdapter.cs
- CLRBindingWorker.cs
- SapiGrammar.cs
- ContainerTracking.cs
- Win32SafeHandles.cs
- IIS7UserPrincipal.cs
- DirtyTextRange.cs
- PointAnimationClockResource.cs
- MessageBox.cs
- HostingEnvironment.cs
- FilteredAttributeCollection.cs
- ReachFixedDocumentSerializer.cs
- SQLDecimalStorage.cs
- Vertex.cs
- HostedElements.cs
- ToolBar.cs
- SchemaElementDecl.cs
- COAUTHIDENTITY.cs
- ToolboxItemAttribute.cs
- FixedHyperLink.cs
- LogManagementAsyncResult.cs
- HtmlTextArea.cs
- FrameSecurityDescriptor.cs
- LambdaCompiler.Unary.cs
- DynamicRenderer.cs
- WebPartEventArgs.cs
- XmlSerializerFactory.cs
- InternalUserCancelledException.cs
- FileSystemWatcher.cs
- PathSegmentCollection.cs
- RadioButtonFlatAdapter.cs
- ThicknessConverter.cs
- TypeNameConverter.cs
- NCryptSafeHandles.cs
- remotingproxy.cs
- TlsSspiNegotiation.cs
- GridItem.cs
- SendParametersContent.cs
- IdentitySection.cs
- TcpHostedTransportConfiguration.cs
- DbConnectionClosed.cs
- ButtonColumn.cs
- mda.cs
- ArithmeticException.cs
- ResXDataNode.cs
- HttpRuntime.cs
- DriveInfo.cs
- PlacementWorkspace.cs
- ActivityDesignerAccessibleObject.cs
- BypassElementCollection.cs
- DataGridColumnEventArgs.cs
- ExpressionBinding.cs
- VoiceChangeEventArgs.cs
- PointConverter.cs
- SQLInt32.cs
- Int32AnimationBase.cs
- LinkedResource.cs
- ToolStripDesigner.cs
- BitmapEffectInput.cs
- XmlILIndex.cs
- Stream.cs
- ExpressionDumper.cs
- MemoryFailPoint.cs
- RealizationDrawingContextWalker.cs
- Panel.cs
- MethodRental.cs
- MethodBuilderInstantiation.cs
- PageCatalogPart.cs
- BooleanKeyFrameCollection.cs
- FormView.cs
- BamlLocalizabilityResolver.cs
- PageClientProxyGenerator.cs
- CodePageUtils.cs
- ScrollEvent.cs
- RepeaterItemCollection.cs
- ScrollChangedEventArgs.cs
- RequestDescription.cs
- SelectionPatternIdentifiers.cs
- RowParagraph.cs
- ConfigViewGenerator.cs
- QueryOperatorEnumerator.cs
- RichTextBox.cs
- CacheDependency.cs
- InternalPolicyElement.cs
- ItemContainerProviderWrapper.cs
- CapabilitiesSection.cs
- _UriTypeConverter.cs
- RelationshipEndMember.cs
- ControlValuePropertyAttribute.cs
- ChildDocumentBlock.cs