Update language encoding after certain calls.

This commit is contained in:
Eugene Wang
2023-04-05 22:46:50 -04:00
parent ab2947d03b
commit 13521c71d6
5 changed files with 70 additions and 10 deletions

View File

@@ -41,6 +41,8 @@ namespace WinFormSample
var caps = twain.GetAllCaps(); var caps = twain.GetAllCaps();
foreach (var c in caps) foreach (var c in caps)
listCaps.Items.Add(c); listCaps.Items.Add(c);
var sts = twain.GetCapLabel(CAP.ICAP_XRESOLUTION, out string? test);
} }
else else
{ {

View File

@@ -776,9 +776,9 @@ namespace NTwain.Data
/// The normal get... /// The normal get...
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public string Get() public string Get(Encoding? encoding = null)
{ {
return (GetValue(true)); return (GetValue(true, encoding));
} }
/// <summary> /// <summary>
@@ -786,16 +786,16 @@ namespace NTwain.Data
/// that doesn't include the prefix byte... /// that doesn't include the prefix byte...
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public string GetNoPrefix() public string GetNoPrefix(Encoding? encoding = null)
{ {
return (GetValue(false)); return (GetValue(false, encoding));
} }
/// <summary> /// <summary>
/// Get our value... /// Get our value...
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private string GetValue(bool a_blMayHavePrefix) private string GetValue(bool a_blMayHavePrefix, Encoding? encoding = null)
{ {
// convert what we have into a byte array // convert what we have into a byte array
byte[] abyItem = new byte[256]; byte[] abyItem = new byte[256];
@@ -879,7 +879,7 @@ namespace NTwain.Data
} }
// change encoding of byte array, then convert the bytes array to a string // change encoding of byte array, then convert the bytes array to a string
string sz = Encoding.Unicode.GetString(Encoding.Convert(Language.GetEncoding(), Encoding.Unicode, abyItem)); string sz = Encoding.Unicode.GetString(Encoding.Convert(encoding ?? Language.GetEncoding(), Encoding.Unicode, abyItem));
// If the first character is a NUL, then return the empty string... // If the first character is a NUL, then return the empty string...
if (sz[0] == '\0') if (sz[0] == '\0')
@@ -2482,7 +2482,7 @@ namespace NTwain.Data
/// Describes the status of a source. /// Describes the status of a source.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
public struct TW_STATUS public partial struct TW_STATUS
{ {
public TWCC ConditionCode; public TWCC ConditionCode;
public ushort Data; public ushort Data;

View File

@@ -234,6 +234,14 @@ namespace NTwain.Data
//} //}
} }
partial struct TW_STATUS
{
public override string ToString()
{
return ConditionCode.ToString();
}
}
/// <summary> /// <summary>
/// A more dotnet-friendly representation of <see cref="TW_ENUMERATION"/>. /// A more dotnet-friendly representation of <see cref="TW_ENUMERATION"/>.
/// </summary> /// </summary>

View File

@@ -2,6 +2,8 @@
using NTwain.Triplets; using NTwain.Triplets;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NTwain namespace NTwain
{ {
@@ -81,6 +83,7 @@ namespace NTwain
/// <summary> /// <summary>
/// Gets a CAP's help text (description). /// Gets a CAP's help text (description).
/// This is not implemented.
/// </summary> /// </summary>
/// <param name="cap"></param> /// <param name="cap"></param>
/// <param name="help"></param> /// <param name="help"></param>
@@ -90,12 +93,18 @@ namespace NTwain
help = null; help = null;
var value = new TW_CAPABILITY(cap); var value = new TW_CAPABILITY(cap);
var rc = DGControl.Capability.GetHelp(ref _appIdentity, ref _currentDS, ref value); var rc = DGControl.Capability.GetHelp(ref _appIdentity, ref _currentDS, ref value);
if (rc == TWRC.SUCCESS)
{
// how to determine the length of this thing???
var data = value.ReadOneValue<IntPtr>(this, false);
}
value.Free(this); value.Free(this);
return WrapInSTS(rc); return WrapInSTS(rc);
} }
/// <summary> /// <summary>
/// Gets a CAP's text name label. /// Gets a CAP's text name label.
/// This is not implemented.
/// </summary> /// </summary>
/// <param name="cap"></param> /// <param name="cap"></param>
/// <param name="label"></param> /// <param name="label"></param>
@@ -103,8 +112,13 @@ namespace NTwain
public STS GetCapLabel(CAP cap, out string? label) public STS GetCapLabel(CAP cap, out string? label)
{ {
label = null; label = null;
var value = new TW_CAPABILITY(cap); var value = new TW_CAPABILITY(cap) { ConType = TWON.ONEVALUE };
var rc = DGControl.Capability.GetLabel(ref _appIdentity, ref _currentDS, ref value); var rc = DGControl.Capability.GetLabel(ref _appIdentity, ref _currentDS, ref value);
if (rc == TWRC.SUCCESS)
{
// how to determine the length of this thing???
var data = value.ReadOneValue<IntPtr>(this, false);
}
value.Free(this); value.Free(this);
return WrapInSTS(rc); return WrapInSTS(rc);
} }
@@ -120,6 +134,11 @@ namespace NTwain
labels = null; labels = null;
var value = new TW_CAPABILITY(cap); var value = new TW_CAPABILITY(cap);
var rc = DGControl.Capability.GetLabelEnum(ref _appIdentity, ref _currentDS, ref value); var rc = DGControl.Capability.GetLabelEnum(ref _appIdentity, ref _currentDS, ref value);
if (rc == TWRC.SUCCESS)
{
// spec says they're utf8
labels = value.ReadArray<TW_STR255>(this, false).Select(t => t.Get(Encoding.UTF8)).ToArray();
}
value.Free(this); value.Free(this);
return WrapInSTS(rc); return WrapInSTS(rc);
} }
@@ -133,6 +152,12 @@ namespace NTwain
{ {
var rc = DGControl.Capability.Set(ref _appIdentity, ref _currentDS, ref value); var rc = DGControl.Capability.Set(ref _appIdentity, ref _currentDS, ref value);
value.Free(this); value.Free(this);
if (value.Cap == CAP.CAP_LANGUAGE && rc == TWRC.SUCCESS)
{
RefreshCapLanguage();
}
return WrapInSTS(rc); return WrapInSTS(rc);
} }
@@ -158,7 +183,14 @@ namespace NTwain
public STS ResetCap(CAP cap, out TW_CAPABILITY value) public STS ResetCap(CAP cap, out TW_CAPABILITY value)
{ {
value = new TW_CAPABILITY(cap); value = new TW_CAPABILITY(cap);
return WrapInSTS(DGControl.Capability.Reset(ref _appIdentity, ref _currentDS, ref value)); var rc = DGControl.Capability.Reset(ref _appIdentity, ref _currentDS, ref value);
if (value.Cap == CAP.CAP_LANGUAGE && rc == TWRC.SUCCESS)
{
RefreshCapLanguage();
}
return WrapInSTS(rc);
} }
/// <summary> /// <summary>
@@ -168,7 +200,24 @@ namespace NTwain
public STS ResetAllCaps() public STS ResetAllCaps()
{ {
var value = new TW_CAPABILITY(CAP.CAP_SUPPORTEDCAPS); var value = new TW_CAPABILITY(CAP.CAP_SUPPORTEDCAPS);
return WrapInSTS(DGControl.Capability.ResetAll(ref _appIdentity, ref _currentDS, ref value)); var rc = DGControl.Capability.ResetAll(ref _appIdentity, ref _currentDS, ref value);
if (rc == TWRC.SUCCESS)
{
RefreshCapLanguage();
}
return WrapInSTS(rc);
}
private void RefreshCapLanguage()
{
var rc2 = GetCapCurrent(CAP.CAP_LANGUAGE, out TW_CAPABILITY curCap);
if (rc2.RC == TWRC.SUCCESS)
{
var lang = curCap.ReadOneValue<TWLG>(this);
Language.Set(lang);
}
} }
} }
} }

View File

@@ -49,6 +49,7 @@ namespace NTwain
{ {
State = STATE.S4; State = STATE.S4;
RegisterCallback(); RegisterCallback();
Language.Set(source.Version.Language);
CurrentSource = source; CurrentSource = source;
} }
return WrapInSTS(rc); return WrapInSTS(rc);