Module: Windows::Unicode

Included in:
File
Defined in:
lib/windows/unicode.rb

Constant Summary collapse

CP_ACP =
0
CP_OEMCP =
1
CP_MACCP =
2
CP_THREAD_ACP =
3
CP_SYMBOL =
42
CP_UTF7 =
65000
CP_UTF8 =
65001
MB_PRECOMPOSED =
0x00000001
MB_COMPOSITE =
0x00000002
MB_USEGLYPHCHARS =
0x00000004
MB_ERR_INVALID_CHARS =
0x00000008
WC_COMPOSITECHECK =
0x00000200
WC_DISCARDNS =
0x00000010
WC_SEPCHARS =
0x00000020
WC_DEFAULTCHAR =
0x00000040
WC_NO_BEST_FIT_CHARS =
0x00000400
ANSI_CHARSET =
0
DEFAULT_CHARSET =
1
SYMBOL_CHARSET =
2
SHIFTJIS_CHARSET =
128
HANGEUL_CHARSET =
129
HANGUL_CHARSET =
129
GB2312_CHARSET =
134
CHINESEBIG5_CHARSET =
136
OEM_CHARSET =
255
JOHAB_CHARSET =
130
HEBREW_CHARSET =
177
ARABIC_CHARSET =
178
GREEK_CHARSET =
161
TURKISH_CHARSET =
162
VIETNAMESE_CHARSET =
163
THAI_CHARSET =
222
EASTEUROPE_CHARSET =
238
RUSSIAN_CHARSET =
204
IS_TEXT_UNICODE_ASCII16 =
0x0001
IS_TEXT_UNICODE_REVERSE_ASCII16 =
0x0010
IS_TEXT_UNICODE_STATISTICS =
0x0002
IS_TEXT_UNICODE_REVERSE_STATISTICS =
0x0020
IS_TEXT_UNICODE_CONTROLS =
0x0004
IS_TEXT_UNICODE_REVERSE_CONTROLS =
0x0040
IS_TEXT_UNICODE_SIGNATURE =
0x0008
IS_TEXT_UNICODE_REVERSE_SIGNATURE =
0x0080
IS_TEXT_UNICODE_ILLEGAL_CHARS =
0x0100
IS_TEXT_UNICODE_ODD_LENGTH =
0x0200
IS_TEXT_UNICODE_DBCS_LEADBYTE =
0x0400
IS_TEXT_UNICODE_NULL_BYTES =
0x1000
IS_TEXT_UNICODE_UNICODE_MASK =
0x000F
IS_TEXT_UNICODE_REVERSE_MASK =
0x00F0
IS_TEXT_UNICODE_NOT_UNICODE_MASK =
0x0F00
IS_TEXT_UNICODE_NOT_ASCII_MASK =
0xF000
TCI_SRCCHARSET =
1
TCI_SRCCODEPAGE =
2
TCI_SRCFONTSIG =
3
TCI_SRCLOCALE =
0x100
GetTextCharset =
Win32API.new('gdi32', 'GetTextCharset', 'L', 'I')
GetTextCharsetInfo =
Win32API.new('gdi32', 'GetTextCharsetInfo', 'LPL', 'I')
IsDBCSLeadByte =
Win32API.new('kernel32', 'IsDBCSLeadByte', 'P', 'I')
IsDBCSLeadByteEx =
Win32API.new('kernel32', 'IsDBCSLeadByteEx', 'IP', 'I')
IsTextUnicode =
Win32API.new('advapi32', 'IsTextUnicode', 'PIP', 'I')
MultiByteToWideChar =
Win32API.new('kernel32', 'MultiByteToWideChar', 'ILPIPI', 'I')
TranslateCharsetInfo =
Win32API.new('gdi32', 'TranslateCharsetInfo', 'PPL', 'I')
WideCharToMultiByte =
Win32API.new('kernel32', 'WideCharToMultiByte', 'ILPIPIPP', 'I')

Instance Method Summary collapse

Instance Method Details

#GetTextCharset(hdc) ⇒ Object



74
75
76
# File 'lib/windows/unicode.rb', line 74

def GetTextCharset(hdc)
   GetTextCharset.call(hdc)
end

#GetTextCharsetInfo(hdc, sig, flags) ⇒ Object



78
79
80
# File 'lib/windows/unicode.rb', line 78

def GetTextCharsetInfo(hdc, sig, flags)
   GetTextCharsetInfo.call(hdc, sig, flags)
end

#IsDBCSLeadByte(char) ⇒ Object



82
83
84
# File 'lib/windows/unicode.rb', line 82

def IsDBCSLeadByte(char)
   IsDBCSLeadByte.call(char) != 0
end

#IsDBCSLeadByteEx(code_page, char) ⇒ Object



86
87
88
# File 'lib/windows/unicode.rb', line 86

def IsDBCSLeadByteEx(code_page, char)
   IsDBCSLeadByteEx.call(code_pag, char) != 0
end

#IsTextUnicode(buf, size = buf.size, options = 0) ⇒ Object



90
91
92
# File 'lib/windows/unicode.rb', line 90

def IsTextUnicode(buf, size = buf.size, options = 0)
   IsTextUnicode.call(buf, size, options) != 0
end

#multi_to_wide(string, encoding = nil) ⇒ Object

Maps a string to a wide (unicode) string using encoding. If no encoding is specified, then CP_UTF8 is used if $KCODE is set to UTF8. Otherwise, CP_ACP is used.

If the function fails it simply returns the string as-is.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/windows/unicode.rb', line 114

def multi_to_wide(string, encoding=nil)
   unless encoding
      encoding = ($KCODE == 'UTF8') ? CP_UTF8 : CP_ACP
   end
   
   buf = 0.chr * string.size * 2 # sizeof(WCHAR)

   int = MultiByteToWideChar(
      encoding,
      0,
      string,
      string.size,
      buf,
      buf.size
   )
   
   if int > 0
      buf[0, int*2]
   else
      string
   end
end

#MultiByteToWideChar(page, flags, str, str_size, buf, buf_size) ⇒ Object



94
95
96
# File 'lib/windows/unicode.rb', line 94

def MultiByteToWideChar(page, flags, str, str_size, buf, buf_size)
   MultiByteToWideChar.call(page, flags, str, str_size, buf, buf_size)
end

#TranslateCharsetInfo(src, cs, flags) ⇒ Object



98
99
100
# File 'lib/windows/unicode.rb', line 98

def TranslateCharsetInfo(src, cs, flags)
   TranslateCharsetInfo.call(src, cs, flags) != 0
end

#wide_to_multi(string, encoding = nil) ⇒ Object

Maps a wide character string to a new character string using the specified encoding. If no encoding is specified, then CP_UTF8 iss used if $KCODE is set to UTF8. Otherwise, CP_ACP is used.

If the function fails it simply returns the string as-is.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/windows/unicode.rb', line 143

def wide_to_multi(string, encoding=nil)
   unless encoding
      encoding = ($KCODE == 'UTF8') ? CP_UTF8 : CP_ACP
   end
   
   buf = 0.chr * string.size
   
   int = WideCharToMultiByte(
      encoding,
      0,
      string,
      string.size/2,
      buf,
      buf.size,
      0,
      0
   )

   if int > 0
      buf[0, int]
   else
      string
   end
end

#WideCharToMultiByte(page, flags, str, str_size, buf, buf_size, defchar, used_def) ⇒ Object



102
103
104
# File 'lib/windows/unicode.rb', line 102

def WideCharToMultiByte(page, flags, str, str_size, buf, buf_size, defchar, used_def)
   WideCharToMultiByte.call(page, flags, str, str_size, buf, buf_size, defchar, used_def)
end