Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Framework / System / Windows / Documents / NaturalLanguageHyphenator.cs / 1 / NaturalLanguageHyphenator.cs
//----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Description:
// Implementation of TextLexicalService abstract class used by TextFormatter for
// document layout. This implementation is based on the hyphenation service in
// NaturalLanguage6.dll - the component owned by the Natural Language Team.
//
// History:
// 06/28/2005 : Worachai Chaoweeraprasit (Wchao) - created
//
//---------------------------------------------------------------------------
using System.Security;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Media.TextFormatting;
using MS.Win32;
using MS.Internal;
namespace System.Windows.Documents
{
///
/// The NLG hyphenation-based implementation of TextLexicalService used by TextFormatter
/// for line-breaking purpose.
///
internal class NaturalLanguageHyphenator : TextLexicalService, IDisposable
{
///
/// Critical: Holds a COM component instance that has unmanged code elevations
///
[SecurityCritical]
private IntPtr _hyphenatorResource;
private bool _disposed;
///
/// Construct an NLG-based hyphenator
///
///
/// Critical: This code calls into NlCreateHyphenator, which elevates unmanaged code permission.
/// TreatAsSafe: This function call takes no input parameters
///
[SecurityCritical, SecurityTreatAsSafe]
internal NaturalLanguageHyphenator()
{
try
{
_hyphenatorResource = UnsafeNativeMethods.NlCreateHyphenator();
}
catch (DllNotFoundException)
{
}
catch (EntryPointNotFoundException)
{
}
}
///
/// Finalize hyphenator's unmanaged resource
///
~NaturalLanguageHyphenator()
{
CleanupInternal(true);
}
///
/// Dispose hyphenator's unmanaged resource
///
void IDisposable.Dispose()
{
GC.SuppressFinalize(this);
CleanupInternal(false);
}
///
/// Internal clean-up routine
///
///
/// Critical: This code calls into NlDestroyHyphenator, which elevates unmanaged code permission.
/// TreatAsSafe: This function call takes no input memory block
///
[SecurityCritical, SecurityTreatAsSafe]
private void CleanupInternal(bool finalizing)
{
if (!_disposed && _hyphenatorResource != IntPtr.Zero)
{
UnsafeNativeMethods.NlDestroyHyphenator(ref _hyphenatorResource);
_disposed = true;
}
}
///
/// TextFormatter to query whether the lexical services component could provides
/// analysis for the specified culture.
///
/// Culture whose text is to be analyzed
/// Boolean value indicates whether the specified culture is supported
public override bool IsCultureSupported(CultureInfo culture)
{
// Accept all cultures for the time being. Ideally NL6 should provide a way for the client
// to test supported culture.
return true;
}
///
/// TextFormatter to get the lexical breaks of the specified raw text
///
/// character array
/// number of character in the character array to analyze
/// culture of the specified character source
/// lexical breaks of the text
///
/// Critical: This code calls NlHyphenate which is critical.
/// TreatAsSafe: This code accepts a buffer that is length checked and returns
/// data that is ok to return.
///
[SecurityCritical, SecurityTreatAsSafe]
public override TextLexicalBreaks AnalyzeText(
char[] characterSource,
int length,
CultureInfo textCulture
)
{
Invariant.Assert(
characterSource != null
&& characterSource.Length > 0
&& length > 0
&& length <= characterSource.Length
);
if (_hyphenatorResource == IntPtr.Zero)
{
// No hyphenator available, no service delivered
return null;
}
if (_disposed)
{
throw new ObjectDisposedException(SR.Get(SRID.HyphenatorDisposed));
}
byte[] isHyphenPositions = new byte[(length + 7) / 8];
UnsafeNativeMethods.NlHyphenate(
_hyphenatorResource,
characterSource,
length,
((textCulture != null && textCulture != CultureInfo.InvariantCulture) ? textCulture.LCID : 0),
isHyphenPositions,
isHyphenPositions.Length
);
return new HyphenBreaks(isHyphenPositions, length);
}
///
/// Private implementation of TextLexicalBreaks that encapsulates hyphen opportunities within
/// a character string.
///
private class HyphenBreaks : TextLexicalBreaks
{
private byte[] _isHyphenPositions;
private int _numPositions;
internal HyphenBreaks(byte[] isHyphenPositions, int numPositions)
{
_isHyphenPositions = isHyphenPositions;
_numPositions = numPositions;
}
///
/// Indexer for the value at the nth break index (bit nth of the logical bit array)
///
private bool this[int index]
{
get { return (_isHyphenPositions[index / 8] & (1 << index % 8)) != 0; }
}
public override int Length
{
get
{
return _numPositions;
}
}
public override int GetNextBreak(int currentIndex)
{
if (_isHyphenPositions != null && currentIndex >= 0)
{
int ich = currentIndex + 1;
while (ich < _numPositions && !this[ich])
ich++;
if (ich < _numPositions)
return ich;
}
// return negative value when break is not found.
return -1;
}
public override int GetPreviousBreak(int currentIndex)
{
if (_isHyphenPositions != null && currentIndex < _numPositions)
{
int ich = currentIndex;
while (ich > 0 && !this[ich])
ich--;
if (ich > 0)
return ich;
}
// return negative value when break is not found.
return -1;
}
}
private static class UnsafeNativeMethods
{
///
/// Critical: This elevates to unmanaged code permission
///
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[DllImport(Microsoft.Internal.DllImport.PresentationNative, PreserveSig = false)]
internal static extern IntPtr NlCreateHyphenator();
///
/// Critical: This elevates to unmanaged code permission
///
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[DllImport(Microsoft.Internal.DllImport.PresentationNative, PreserveSig = false)]
internal static extern void NlDestroyHyphenator(ref IntPtr hyphenator);
///
/// Critical: This elevates to unmanaged code permission
///
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[DllImport(Microsoft.Internal.DllImport.PresentationNative, PreserveSig = false)]
internal static extern void NlHyphenate(
IntPtr hyphenator,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U2, SizeParamIndex = 2)]
char[] inputText,
int textLength,
int localeID,
[In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5)]
byte[] hyphenBreaks,
int numPositions
);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Description:
// Implementation of TextLexicalService abstract class used by TextFormatter for
// document layout. This implementation is based on the hyphenation service in
// NaturalLanguage6.dll - the component owned by the Natural Language Team.
//
// History:
// 06/28/2005 : Worachai Chaoweeraprasit (Wchao) - created
//
//---------------------------------------------------------------------------
using System.Security;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Media.TextFormatting;
using MS.Win32;
using MS.Internal;
namespace System.Windows.Documents
{
///
/// The NLG hyphenation-based implementation of TextLexicalService used by TextFormatter
/// for line-breaking purpose.
///
internal class NaturalLanguageHyphenator : TextLexicalService, IDisposable
{
///
/// Critical: Holds a COM component instance that has unmanged code elevations
///
[SecurityCritical]
private IntPtr _hyphenatorResource;
private bool _disposed;
///
/// Construct an NLG-based hyphenator
///
///
/// Critical: This code calls into NlCreateHyphenator, which elevates unmanaged code permission.
/// TreatAsSafe: This function call takes no input parameters
///
[SecurityCritical, SecurityTreatAsSafe]
internal NaturalLanguageHyphenator()
{
try
{
_hyphenatorResource = UnsafeNativeMethods.NlCreateHyphenator();
}
catch (DllNotFoundException)
{
}
catch (EntryPointNotFoundException)
{
}
}
///
/// Finalize hyphenator's unmanaged resource
///
~NaturalLanguageHyphenator()
{
CleanupInternal(true);
}
///
/// Dispose hyphenator's unmanaged resource
///
void IDisposable.Dispose()
{
GC.SuppressFinalize(this);
CleanupInternal(false);
}
///
/// Internal clean-up routine
///
///
/// Critical: This code calls into NlDestroyHyphenator, which elevates unmanaged code permission.
/// TreatAsSafe: This function call takes no input memory block
///
[SecurityCritical, SecurityTreatAsSafe]
private void CleanupInternal(bool finalizing)
{
if (!_disposed && _hyphenatorResource != IntPtr.Zero)
{
UnsafeNativeMethods.NlDestroyHyphenator(ref _hyphenatorResource);
_disposed = true;
}
}
///
/// TextFormatter to query whether the lexical services component could provides
/// analysis for the specified culture.
///
/// Culture whose text is to be analyzed
/// Boolean value indicates whether the specified culture is supported
public override bool IsCultureSupported(CultureInfo culture)
{
// Accept all cultures for the time being. Ideally NL6 should provide a way for the client
// to test supported culture.
return true;
}
///
/// TextFormatter to get the lexical breaks of the specified raw text
///
/// character array
/// number of character in the character array to analyze
/// culture of the specified character source
/// lexical breaks of the text
///
/// Critical: This code calls NlHyphenate which is critical.
/// TreatAsSafe: This code accepts a buffer that is length checked and returns
/// data that is ok to return.
///
[SecurityCritical, SecurityTreatAsSafe]
public override TextLexicalBreaks AnalyzeText(
char[] characterSource,
int length,
CultureInfo textCulture
)
{
Invariant.Assert(
characterSource != null
&& characterSource.Length > 0
&& length > 0
&& length <= characterSource.Length
);
if (_hyphenatorResource == IntPtr.Zero)
{
// No hyphenator available, no service delivered
return null;
}
if (_disposed)
{
throw new ObjectDisposedException(SR.Get(SRID.HyphenatorDisposed));
}
byte[] isHyphenPositions = new byte[(length + 7) / 8];
UnsafeNativeMethods.NlHyphenate(
_hyphenatorResource,
characterSource,
length,
((textCulture != null && textCulture != CultureInfo.InvariantCulture) ? textCulture.LCID : 0),
isHyphenPositions,
isHyphenPositions.Length
);
return new HyphenBreaks(isHyphenPositions, length);
}
///
/// Private implementation of TextLexicalBreaks that encapsulates hyphen opportunities within
/// a character string.
///
private class HyphenBreaks : TextLexicalBreaks
{
private byte[] _isHyphenPositions;
private int _numPositions;
internal HyphenBreaks(byte[] isHyphenPositions, int numPositions)
{
_isHyphenPositions = isHyphenPositions;
_numPositions = numPositions;
}
///
/// Indexer for the value at the nth break index (bit nth of the logical bit array)
///
private bool this[int index]
{
get { return (_isHyphenPositions[index / 8] & (1 << index % 8)) != 0; }
}
public override int Length
{
get
{
return _numPositions;
}
}
public override int GetNextBreak(int currentIndex)
{
if (_isHyphenPositions != null && currentIndex >= 0)
{
int ich = currentIndex + 1;
while (ich < _numPositions && !this[ich])
ich++;
if (ich < _numPositions)
return ich;
}
// return negative value when break is not found.
return -1;
}
public override int GetPreviousBreak(int currentIndex)
{
if (_isHyphenPositions != null && currentIndex < _numPositions)
{
int ich = currentIndex;
while (ich > 0 && !this[ich])
ich--;
if (ich > 0)
return ich;
}
// return negative value when break is not found.
return -1;
}
}
private static class UnsafeNativeMethods
{
///
/// Critical: This elevates to unmanaged code permission
///
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[DllImport(Microsoft.Internal.DllImport.PresentationNative, PreserveSig = false)]
internal static extern IntPtr NlCreateHyphenator();
///
/// Critical: This elevates to unmanaged code permission
///
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[DllImport(Microsoft.Internal.DllImport.PresentationNative, PreserveSig = false)]
internal static extern void NlDestroyHyphenator(ref IntPtr hyphenator);
///
/// Critical: This elevates to unmanaged code permission
///
[SecurityCritical, SuppressUnmanagedCodeSecurity]
[DllImport(Microsoft.Internal.DllImport.PresentationNative, PreserveSig = false)]
internal static extern void NlHyphenate(
IntPtr hyphenator,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U2, SizeParamIndex = 2)]
char[] inputText,
int textLength,
int localeID,
[In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5)]
byte[] hyphenBreaks,
int numPositions
);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TreeWalker.cs
- TableStyle.cs
- CodeAttributeDeclaration.cs
- DataControlCommands.cs
- ExpressionBuilderCollection.cs
- ConfigurationLocationCollection.cs
- MissingFieldException.cs
- ZipIOBlockManager.cs
- MenuAutomationPeer.cs
- DocumentGridContextMenu.cs
- CompilerHelpers.cs
- ApplicationBuildProvider.cs
- PointKeyFrameCollection.cs
- DataContractSerializerFaultFormatter.cs
- ListViewTableRow.cs
- DataGridViewColumnEventArgs.cs
- DateTimeConstantAttribute.cs
- UInt64.cs
- MostlySingletonList.cs
- MsmqPoisonMessageException.cs
- TransformerConfigurationWizardBase.cs
- ComponentSerializationService.cs
- HttpCachePolicyWrapper.cs
- UIElementCollection.cs
- ServiceBehaviorAttribute.cs
- SocketElement.cs
- TiffBitmapEncoder.cs
- XmlMemberMapping.cs
- Quad.cs
- FontCacheLogic.cs
- MsmqUri.cs
- TextRangeBase.cs
- QilTernary.cs
- RowTypePropertyElement.cs
- DataGridViewButtonCell.cs
- GridViewCancelEditEventArgs.cs
- DefaultValueAttribute.cs
- OrderByBuilder.cs
- embossbitmapeffect.cs
- SourceFilter.cs
- RightsManagementUser.cs
- RuleSettingsCollection.cs
- CachingHintValidation.cs
- MiniMapControl.xaml.cs
- _NegoState.cs
- VersionPair.cs
- SynchronizedChannelCollection.cs
- AttachedPropertyBrowsableForTypeAttribute.cs
- DataListItemCollection.cs
- NonSerializedAttribute.cs
- RuleInfoComparer.cs
- EncryptedPackage.cs
- SystemResourceHost.cs
- RegexTree.cs
- AncillaryOps.cs
- FormViewUpdatedEventArgs.cs
- DecoderFallbackWithFailureFlag.cs
- TreeIterator.cs
- ComponentEditorForm.cs
- ObjectPersistData.cs
- ColorEditor.cs
- EnumType.cs
- InvalidContentTypeException.cs
- ActivityMetadata.cs
- CommandManager.cs
- ClonableStack.cs
- LinkedResource.cs
- CapabilitiesState.cs
- RegexRunnerFactory.cs
- IconConverter.cs
- XmlReader.cs
- OracleInfoMessageEventArgs.cs
- StaticResourceExtension.cs
- SQLDecimal.cs
- ModifierKeysValueSerializer.cs
- SafeNativeMethods.cs
- FixedSOMTableCell.cs
- MobileControlDesigner.cs
- XpsSerializationException.cs
- Converter.cs
- IUnknownConstantAttribute.cs
- XmlSchemaAll.cs
- LineSegment.cs
- ComponentCache.cs
- WSFederationHttpSecurityElement.cs
- PtsHelper.cs
- FormsAuthentication.cs
- RuntimeIdentifierPropertyAttribute.cs
- UnauthorizedWebPart.cs
- StackOverflowException.cs
- EmptyStringExpandableObjectConverter.cs
- Button.cs
- Compilation.cs
- QilInvoke.cs
- ElasticEase.cs
- TableItemStyle.cs
- X509CertificateInitiatorClientCredential.cs
- TransformConverter.cs
- SafeProcessHandle.cs
- Rules.cs