Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Core / CSharp / System / Windows / Media / GlyphsSerializer.cs / 1 / GlyphsSerializer.cs
//------------------------------------------------------------------------------ // Microsoft Avalon // Copyright (c) Microsoft Corporation, all rights reserved // // File: GlyphsSerializer.cs // //----------------------------------------------------------------------------- using System; using System.Windows.Threading; using System.Diagnostics; using System.Collections; using System.Collections.Specialized; using System.Collections.Generic; using System.Globalization; using System.Text; using System.IO; using System.Runtime.InteropServices; using MS.Internal; using MS.Win32; using Microsoft.Win32.SafeHandles; using System.Windows; using System.Windows.Media.Media3D; using System.Windows.Media.Animation; using System.Security.Permissions; using MS.Internal.PresentationCore; namespace System.Windows.Media { ////// /// [FriendAccessAllowed] // used by System.Printing.dll internal class GlyphsSerializer { #region public methods ////// /// /// public GlyphsSerializer(GlyphRun glyphRun) { if (glyphRun == null) { throw new ArgumentNullException("glyphRun"); } _glyphTypeface = glyphRun.GlyphTypeface; _milToEm = EmScaleFactor / glyphRun.FontRenderingEmSize; _sideways = glyphRun.IsSideways; _characters = glyphRun.Characters; _caretStops = glyphRun.CaretStops; // the first value in the cluster map can be non-zero, in which case it's applied as an offset to all // subsequent entries in the cluster map _clusters = glyphRun.ClusterMap; if (_clusters != null) _glyphClusterInitialOffset = _clusters[0]; _indices = glyphRun.GlyphIndices; _advances = glyphRun.AdvanceWidths; _offsets = glyphRun.GlyphOffsets; // "100,50,,0;".Length is a capacity estimate for an individual glyph _glyphStringBuider = new StringBuilder(10); // string length * _glyphStringBuider.Capacity is an estimate for the whole string _indicesStringBuider = new StringBuilder( Math.Max( (_characters == null ? 0 : _characters.Count), _indices.Count ) * _glyphStringBuider.Capacity ); } ////// Encode glyph run glyph information into Indices, UnicodeString and CaretStops string. /// public void ComputeContentStrings(out string characters, out string indices, out string caretStops) { if (_clusters != null) { // the algorithm works by finding (n:m) clusters and appending m glyphs for each cluster int characterIndex; int glyphClusterStart = 0; int charClusterStart = 0; bool forceNewCluster = true; for (characterIndex = 0; characterIndex < _clusters.Count; ++characterIndex) { if (forceNewCluster) { glyphClusterStart = _clusters[characterIndex]; charClusterStart = characterIndex; forceNewCluster = false; continue; } if (_clusters[characterIndex] != glyphClusterStart) { // end of cluster, flush it Debug.Assert(_clusters[characterIndex] > glyphClusterStart); AddCluster(glyphClusterStart - _glyphClusterInitialOffset, _clusters[characterIndex] - _glyphClusterInitialOffset, charClusterStart, characterIndex); // start a new cluster glyphClusterStart = _clusters[characterIndex]; charClusterStart = characterIndex; } // otherwise, we are still within a cluster } // flush the last cluster Debug.Assert(_indices.Count > glyphClusterStart - _glyphClusterInitialOffset); AddCluster(glyphClusterStart - _glyphClusterInitialOffset, _indices.Count, charClusterStart, characterIndex); } else { // zero cluster map means 1:1 mapping Debug.Assert(_characters == null || _characters.Count == 0 || _indices.Count == _characters.Count); for (int i = 0; i < _indices.Count; ++i) AddCluster(i, i + 1, i, i + 1); } // remove trailing semicolons RemoveTrailingCharacters(_indicesStringBuider, GlyphSeparator); indices = _indicesStringBuider.ToString(); if (_characters == null || _characters.Count == 0) { characters = string.Empty; } else { StringBuilder builder = new StringBuilder(_characters.Count); foreach(char ch in _characters) { builder.Append(ch); } characters = builder.ToString(); } caretStops = CreateCaretStopsString(); } #endregion public methods #region private methods private void RemoveTrailingCharacters(StringBuilder sb, char trailingCharacter) { int length = sb.Length; int trailingCharIndex = length - 1; while (trailingCharIndex >= 0) { if (sb[trailingCharIndex] != trailingCharacter) break; --trailingCharIndex; } sb.Length = trailingCharIndex + 1; } private void AddGlyph(int glyph, int sourceCharacter) { Debug.Assert(_glyphStringBuider.Length == 0); // glyph index ushort fontIndex = _indices[glyph]; ushort glyphIndexFromCmap; if (sourceCharacter == -1 || !_glyphTypeface.CharacterToGlyphMap.TryGetValue(sourceCharacter, out glyphIndexFromCmap) || fontIndex != glyphIndexFromCmap) { _glyphStringBuider.Append(fontIndex.ToString(CultureInfo.InvariantCulture)); } _glyphStringBuider.Append(GlyphSubEntrySeparator); // advance width int normalizedAdvance = (int)Math.Round(_advances[glyph] * _milToEm); double fontAdvance = _sideways ? _glyphTypeface.AdvanceHeights[fontIndex] : _glyphTypeface.AdvanceWidths[fontIndex]; if (normalizedAdvance != (int)Math.Round(fontAdvance * EmScaleFactor)) { _glyphStringBuider.Append(normalizedAdvance.ToString(CultureInfo.InvariantCulture)); } _glyphStringBuider.Append(GlyphSubEntrySeparator); // u,v offset if (_offsets != null) { // u offset int offset = (int)Math.Round(_offsets[glyph].X * _milToEm); if (offset != 0) _glyphStringBuider.Append(offset.ToString(CultureInfo.InvariantCulture)); _glyphStringBuider.Append(GlyphSubEntrySeparator); // v offset offset = (int)Math.Round(_offsets[glyph].Y * _milToEm); if (offset != 0) _glyphStringBuider.Append(offset.ToString(CultureInfo.InvariantCulture)); _glyphStringBuider.Append(GlyphSubEntrySeparator); } // flags are not implemented yet // remove trailing commas RemoveTrailingCharacters(_glyphStringBuider, GlyphSubEntrySeparator); _glyphStringBuider.Append(GlyphSeparator); _indicesStringBuider.Append(_glyphStringBuider.ToString()); // reset for next glyph _glyphStringBuider.Length = 0; } private void AddCluster(int glyphClusterStart, int glyphClusterEnd, int charClusterStart, int charClusterEnd) { int charactersInCluster = charClusterEnd - charClusterStart; int glyphsInCluster = glyphClusterEnd - glyphClusterStart; // no source character to deduce glyph properties from int sourceCharacter = -1; // the format is ... [(CharacterClusterSize[:GlyphClusterSize])] GlyphIndex ... if (glyphsInCluster != 1) { _indicesStringBuider.AppendFormat(CultureInfo.InvariantCulture, "({0}:{1})", charactersInCluster, glyphsInCluster); } else { if (charactersInCluster != 1) _indicesStringBuider.AppendFormat(CultureInfo.InvariantCulture, "({0})", charactersInCluster); else { // 1:1 cluster, we can omit (n:m) specification and possibly deduce some // glyph properties from character if (_characters != null && _characters.Count != 0) sourceCharacter = _characters[charClusterStart]; } } for (int glyph = glyphClusterStart; glyph < glyphClusterEnd; ++glyph) { AddGlyph(glyph, sourceCharacter); } } private string CreateCaretStopsString() { if (_caretStops == null) return String.Empty; // Since the trailing 0xF (i.e. all true) entries in the caret stop specifications can be omitted, // we can limit the caret stop list walk until the last nibble that contains 'false'. int caretStopStringLength = 0; int lastCaretStop = 0; for (int i = _caretStops.Count - 1; i >= 0; --i) { if (!_caretStops[i]) { caretStopStringLength = (i + 4) / 4; // lastCaretStop to consider when building, the rest will correpond to 0xF entries lastCaretStop = Math.Min(i | 3, _caretStops.Count - 1); break; } } // All values are set to true, so we don't have to include caret stop string at all. if (caretStopStringLength == 0) return String.Empty; StringBuilder sb = new StringBuilder(caretStopStringLength); byte mask = 0x8; byte accumulatedValue = 0; for (int i = 0; i <= lastCaretStop; ++i) { if (_caretStops[i]) accumulatedValue |= mask; if (mask != 1) mask >>= 1; else { sb.AppendFormat("{0:x1}", accumulatedValue); accumulatedValue = 0; mask = 0x8; } } if (mask != 0x8) sb.AppendFormat("{0:x1}", accumulatedValue); Debug.Assert(caretStopStringLength == sb.ToString().Length); return sb.ToString(); } #endregion private methods #region private data private GlyphTypeface _glyphTypeface; private IList_characters; private double _milToEm; private bool _sideways; private int _glyphClusterInitialOffset; private IList _clusters; private IList _indices; private IList _advances; private IList _offsets; private IList _caretStops; private StringBuilder _indicesStringBuider; private StringBuilder _glyphStringBuider; private const char GlyphSubEntrySeparator = ','; private const char GlyphSeparator = ';'; private const double EmScaleFactor = 100.0; #endregion region private data } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //------------------------------------------------------------------------------ // Microsoft Avalon // Copyright (c) Microsoft Corporation, all rights reserved // // File: GlyphsSerializer.cs // //----------------------------------------------------------------------------- using System; using System.Windows.Threading; using System.Diagnostics; using System.Collections; using System.Collections.Specialized; using System.Collections.Generic; using System.Globalization; using System.Text; using System.IO; using System.Runtime.InteropServices; using MS.Internal; using MS.Win32; using Microsoft.Win32.SafeHandles; using System.Windows; using System.Windows.Media.Media3D; using System.Windows.Media.Animation; using System.Security.Permissions; using MS.Internal.PresentationCore; namespace System.Windows.Media { /// /// /// [FriendAccessAllowed] // used by System.Printing.dll internal class GlyphsSerializer { #region public methods ////// /// /// public GlyphsSerializer(GlyphRun glyphRun) { if (glyphRun == null) { throw new ArgumentNullException("glyphRun"); } _glyphTypeface = glyphRun.GlyphTypeface; _milToEm = EmScaleFactor / glyphRun.FontRenderingEmSize; _sideways = glyphRun.IsSideways; _characters = glyphRun.Characters; _caretStops = glyphRun.CaretStops; // the first value in the cluster map can be non-zero, in which case it's applied as an offset to all // subsequent entries in the cluster map _clusters = glyphRun.ClusterMap; if (_clusters != null) _glyphClusterInitialOffset = _clusters[0]; _indices = glyphRun.GlyphIndices; _advances = glyphRun.AdvanceWidths; _offsets = glyphRun.GlyphOffsets; // "100,50,,0;".Length is a capacity estimate for an individual glyph _glyphStringBuider = new StringBuilder(10); // string length * _glyphStringBuider.Capacity is an estimate for the whole string _indicesStringBuider = new StringBuilder( Math.Max( (_characters == null ? 0 : _characters.Count), _indices.Count ) * _glyphStringBuider.Capacity ); } ////// Encode glyph run glyph information into Indices, UnicodeString and CaretStops string. /// public void ComputeContentStrings(out string characters, out string indices, out string caretStops) { if (_clusters != null) { // the algorithm works by finding (n:m) clusters and appending m glyphs for each cluster int characterIndex; int glyphClusterStart = 0; int charClusterStart = 0; bool forceNewCluster = true; for (characterIndex = 0; characterIndex < _clusters.Count; ++characterIndex) { if (forceNewCluster) { glyphClusterStart = _clusters[characterIndex]; charClusterStart = characterIndex; forceNewCluster = false; continue; } if (_clusters[characterIndex] != glyphClusterStart) { // end of cluster, flush it Debug.Assert(_clusters[characterIndex] > glyphClusterStart); AddCluster(glyphClusterStart - _glyphClusterInitialOffset, _clusters[characterIndex] - _glyphClusterInitialOffset, charClusterStart, characterIndex); // start a new cluster glyphClusterStart = _clusters[characterIndex]; charClusterStart = characterIndex; } // otherwise, we are still within a cluster } // flush the last cluster Debug.Assert(_indices.Count > glyphClusterStart - _glyphClusterInitialOffset); AddCluster(glyphClusterStart - _glyphClusterInitialOffset, _indices.Count, charClusterStart, characterIndex); } else { // zero cluster map means 1:1 mapping Debug.Assert(_characters == null || _characters.Count == 0 || _indices.Count == _characters.Count); for (int i = 0; i < _indices.Count; ++i) AddCluster(i, i + 1, i, i + 1); } // remove trailing semicolons RemoveTrailingCharacters(_indicesStringBuider, GlyphSeparator); indices = _indicesStringBuider.ToString(); if (_characters == null || _characters.Count == 0) { characters = string.Empty; } else { StringBuilder builder = new StringBuilder(_characters.Count); foreach(char ch in _characters) { builder.Append(ch); } characters = builder.ToString(); } caretStops = CreateCaretStopsString(); } #endregion public methods #region private methods private void RemoveTrailingCharacters(StringBuilder sb, char trailingCharacter) { int length = sb.Length; int trailingCharIndex = length - 1; while (trailingCharIndex >= 0) { if (sb[trailingCharIndex] != trailingCharacter) break; --trailingCharIndex; } sb.Length = trailingCharIndex + 1; } private void AddGlyph(int glyph, int sourceCharacter) { Debug.Assert(_glyphStringBuider.Length == 0); // glyph index ushort fontIndex = _indices[glyph]; ushort glyphIndexFromCmap; if (sourceCharacter == -1 || !_glyphTypeface.CharacterToGlyphMap.TryGetValue(sourceCharacter, out glyphIndexFromCmap) || fontIndex != glyphIndexFromCmap) { _glyphStringBuider.Append(fontIndex.ToString(CultureInfo.InvariantCulture)); } _glyphStringBuider.Append(GlyphSubEntrySeparator); // advance width int normalizedAdvance = (int)Math.Round(_advances[glyph] * _milToEm); double fontAdvance = _sideways ? _glyphTypeface.AdvanceHeights[fontIndex] : _glyphTypeface.AdvanceWidths[fontIndex]; if (normalizedAdvance != (int)Math.Round(fontAdvance * EmScaleFactor)) { _glyphStringBuider.Append(normalizedAdvance.ToString(CultureInfo.InvariantCulture)); } _glyphStringBuider.Append(GlyphSubEntrySeparator); // u,v offset if (_offsets != null) { // u offset int offset = (int)Math.Round(_offsets[glyph].X * _milToEm); if (offset != 0) _glyphStringBuider.Append(offset.ToString(CultureInfo.InvariantCulture)); _glyphStringBuider.Append(GlyphSubEntrySeparator); // v offset offset = (int)Math.Round(_offsets[glyph].Y * _milToEm); if (offset != 0) _glyphStringBuider.Append(offset.ToString(CultureInfo.InvariantCulture)); _glyphStringBuider.Append(GlyphSubEntrySeparator); } // flags are not implemented yet // remove trailing commas RemoveTrailingCharacters(_glyphStringBuider, GlyphSubEntrySeparator); _glyphStringBuider.Append(GlyphSeparator); _indicesStringBuider.Append(_glyphStringBuider.ToString()); // reset for next glyph _glyphStringBuider.Length = 0; } private void AddCluster(int glyphClusterStart, int glyphClusterEnd, int charClusterStart, int charClusterEnd) { int charactersInCluster = charClusterEnd - charClusterStart; int glyphsInCluster = glyphClusterEnd - glyphClusterStart; // no source character to deduce glyph properties from int sourceCharacter = -1; // the format is ... [(CharacterClusterSize[:GlyphClusterSize])] GlyphIndex ... if (glyphsInCluster != 1) { _indicesStringBuider.AppendFormat(CultureInfo.InvariantCulture, "({0}:{1})", charactersInCluster, glyphsInCluster); } else { if (charactersInCluster != 1) _indicesStringBuider.AppendFormat(CultureInfo.InvariantCulture, "({0})", charactersInCluster); else { // 1:1 cluster, we can omit (n:m) specification and possibly deduce some // glyph properties from character if (_characters != null && _characters.Count != 0) sourceCharacter = _characters[charClusterStart]; } } for (int glyph = glyphClusterStart; glyph < glyphClusterEnd; ++glyph) { AddGlyph(glyph, sourceCharacter); } } private string CreateCaretStopsString() { if (_caretStops == null) return String.Empty; // Since the trailing 0xF (i.e. all true) entries in the caret stop specifications can be omitted, // we can limit the caret stop list walk until the last nibble that contains 'false'. int caretStopStringLength = 0; int lastCaretStop = 0; for (int i = _caretStops.Count - 1; i >= 0; --i) { if (!_caretStops[i]) { caretStopStringLength = (i + 4) / 4; // lastCaretStop to consider when building, the rest will correpond to 0xF entries lastCaretStop = Math.Min(i | 3, _caretStops.Count - 1); break; } } // All values are set to true, so we don't have to include caret stop string at all. if (caretStopStringLength == 0) return String.Empty; StringBuilder sb = new StringBuilder(caretStopStringLength); byte mask = 0x8; byte accumulatedValue = 0; for (int i = 0; i <= lastCaretStop; ++i) { if (_caretStops[i]) accumulatedValue |= mask; if (mask != 1) mask >>= 1; else { sb.AppendFormat("{0:x1}", accumulatedValue); accumulatedValue = 0; mask = 0x8; } } if (mask != 0x8) sb.AppendFormat("{0:x1}", accumulatedValue); Debug.Assert(caretStopStringLength == sb.ToString().Length); return sb.ToString(); } #endregion private methods #region private data private GlyphTypeface _glyphTypeface; private IList_characters; private double _milToEm; private bool _sideways; private int _glyphClusterInitialOffset; private IList _clusters; private IList _indices; private IList _advances; private IList _offsets; private IList _caretStops; private StringBuilder _indicesStringBuider; private StringBuilder _glyphStringBuider; private const char GlyphSubEntrySeparator = ','; private const char GlyphSeparator = ';'; private const double EmScaleFactor = 100.0; #endregion region private data } } // 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
- ExtensibleClassFactory.cs
- InputLangChangeRequestEvent.cs
- ImageAttributes.cs
- PropertyOverridesTypeEditor.cs
- MenuBase.cs
- MessageEventSubscriptionService.cs
- OleDbInfoMessageEvent.cs
- Rectangle.cs
- XmlAnyAttributeAttribute.cs
- DiscoveryInnerClientAdhocCD1.cs
- NativeObjectSecurity.cs
- Grant.cs
- CompositeDispatchFormatter.cs
- RegistrySecurity.cs
- DiscoveryExceptionDictionary.cs
- UserPreferenceChangingEventArgs.cs
- XPathExpr.cs
- StringComparer.cs
- TagNameToTypeMapper.cs
- LightweightCodeGenerator.cs
- TransformerInfoCollection.cs
- WorkflowControlClient.cs
- TransactionContext.cs
- Int16Animation.cs
- BridgeDataRecord.cs
- CheckPair.cs
- WindowsNonControl.cs
- Transform3DCollection.cs
- BindingExpression.cs
- AddInToken.cs
- ActivityDesigner.cs
- Localizer.cs
- FuncCompletionCallbackWrapper.cs
- MetadataCache.cs
- ObjectDataSourceEventArgs.cs
- MetadataSerializer.cs
- DataBinding.cs
- SortQueryOperator.cs
- ToolStripLocationCancelEventArgs.cs
- CapiHashAlgorithm.cs
- ObjectDataSourceFilteringEventArgs.cs
- BaseTemplatedMobileComponentEditor.cs
- DataGridViewAutoSizeModeEventArgs.cs
- SplitterEvent.cs
- DNS.cs
- HierarchicalDataBoundControlAdapter.cs
- PaintEvent.cs
- PageRanges.cs
- BamlResourceContent.cs
- SqlCacheDependencyDatabase.cs
- UnknownBitmapEncoder.cs
- MenuItemStyleCollection.cs
- CompileXomlTask.cs
- DesignerLoader.cs
- OdbcStatementHandle.cs
- RichTextBox.cs
- ImageFormat.cs
- VSDExceptions.cs
- SQLInt16.cs
- CreateUserErrorEventArgs.cs
- MessageFormatterConverter.cs
- SafeRegistryKey.cs
- CodeGotoStatement.cs
- QuaternionValueSerializer.cs
- AutomationPropertyInfo.cs
- LocalizedNameDescriptionPair.cs
- ReadOnlyCollection.cs
- XamlInt32CollectionSerializer.cs
- SchemaInfo.cs
- RequestReplyCorrelator.cs
- GlobalItem.cs
- GeneralTransform3DTo2D.cs
- WebControlsSection.cs
- CodeDelegateInvokeExpression.cs
- ApplicationTrust.cs
- RootBrowserWindow.cs
- BuiltInPermissionSets.cs
- NumericUpDownAccelerationCollection.cs
- WindowsIdentity.cs
- RestHandlerFactory.cs
- XmlDictionaryReader.cs
- Roles.cs
- SafeCryptoHandles.cs
- ContextStaticAttribute.cs
- ServicesUtilities.cs
- WorkflowControlEndpoint.cs
- FixedSchema.cs
- HtmlEncodedRawTextWriter.cs
- PcmConverter.cs
- MimePart.cs
- ServiceOperationParameter.cs
- HttpApplication.cs
- CheckableControlBaseAdapter.cs
- PropertyBuilder.cs
- ISAPIApplicationHost.cs
- WebPartCollection.cs
- WebRequestModulesSection.cs
- DateTimePicker.cs
- DaylightTime.cs
- LayoutEngine.cs