Commit 71758aa9 authored by Davis King's avatar Davis King

Merging in the unicode branch. The change log for that branch is as follows:

	------------------------------------------------------------------------
	r2331 | davisking | 2008-06-15 15:03:05 -0400 (Sun, 15 Jun 2008) | 1 line
	Changed paths:
	   M /branches/unicode/dlib/CMakeLists.txt

	Added another link library to the windows part of the cmake file
	------------------------------------------------------------------------
	r2330 | davisking | 2008-06-15 14:11:47 -0400 (Sun, 15 Jun 2008) | 3 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.h
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.h
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_abstract.h
	   M /branches/unicode/dlib/gui_widgets/base_widgets.cpp
	   M /branches/unicode/dlib/gui_widgets/base_widgets.h
	   M /branches/unicode/dlib/gui_widgets/base_widgets_abstract.h
	   M /branches/unicode/dlib/gui_widgets/drawable.cpp
	   M /branches/unicode/dlib/gui_widgets/drawable.h
	   M /branches/unicode/dlib/gui_widgets/fonts.cpp
	   M /branches/unicode/dlib/gui_widgets/fonts.h
	   M /branches/unicode/dlib/gui_widgets/widgets.cpp
	   M /branches/unicode/dlib/gui_widgets/widgets.h
	   M /branches/unicode/dlib/gui_widgets/widgets_abstract.h

	Added Keita Mochizuki's name to a bunch of files.  Also added a missing
	prototype for the get_native_font() function in fonts.h

	------------------------------------------------------------------------
	r2329 | davisking | 2008-06-15 14:05:17 -0400 (Sun, 15 Jun 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_widgets/fonts.cpp
	   M /branches/unicode/dlib/gui_widgets/fonts_abstract.h
	   A /branches/unicode/dlib/gui_widgets/nativefont.h

	Added the nativefont object from Keita Mochizuki

	------------------------------------------------------------------------
	r2328 | davisking | 2008-06-15 14:04:44 -0400 (Sun, 15 Jun 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.cpp

	Removed an unused variable

	------------------------------------------------------------------------
	r2327 | davisking | 2008-06-15 13:33:31 -0400 (Sun, 15 Jun 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.h
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.h
	   M /branches/unicode/dlib/gui_widgets/base_widgets.h
	   M /branches/unicode/dlib/gui_widgets/base_widgets_abstract.h
	   M /branches/unicode/dlib/gui_widgets/drawable.cpp
	   M /branches/unicode/dlib/gui_widgets/drawable.h
	   M /branches/unicode/dlib/gui_widgets/drawable_abstract.h
	   M /branches/unicode/dlib/gui_widgets/widgets.cpp
	   M /branches/unicode/dlib/gui_widgets/widgets.h

	Converted tabs to spaces

	------------------------------------------------------------------------
	r2326 | davisking | 2008-06-15 13:31:32 -0400 (Sun, 15 Jun 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.h
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.h
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_abstract.h
	   M /branches/unicode/dlib/gui_widgets/drawable.cpp
	   M /branches/unicode/dlib/gui_widgets/drawable.h
	   M /branches/unicode/dlib/gui_widgets/drawable_abstract.h
	   M /branches/unicode/dlib/gui_widgets/widgets.cpp
	   M /branches/unicode/dlib/gui_widgets/widgets.h

	Checking in the on_string_put() event patch from Keita Mochizuki.

	------------------------------------------------------------------------
	r2325 | davisking | 2008-06-15 13:13:49 -0400 (Sun, 15 Jun 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_widgets/base_widgets.h
	   M /branches/unicode/dlib/gui_widgets/base_widgets_abstract.h
	   M /branches/unicode/dlib/gui_widgets/style.cpp
	   M /branches/unicode/dlib/gui_widgets/style.h
	   M /branches/unicode/dlib/gui_widgets/style_abstract.h
	   M /branches/unicode/dlib/gui_widgets/widgets.cpp
	   M /branches/unicode/dlib/gui_widgets/widgets.h
	   M /branches/unicode/dlib/gui_widgets/widgets_abstract.h
	   M /branches/unicode/dlib/serialize.h

	Added the first part of the UNICODE patch from Keita Mochizuki

	------------------------------------------------------------------------
	r2324 | davisking | 2008-06-15 13:05:17 -0400 (Sun, 15 Jun 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_widgets/fonts_abstract.h

	Fixed a typo in a comment

	------------------------------------------------------------------------
	r2244 | davisking | 2008-05-15 17:40:47 -0400 (Thu, 15 May 2008) | 3 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.cpp
	   M /branches/unicode/dlib/unicode/unicode.cpp
	   M /branches/unicode/dlib/unicode/unicode.h
	   M /branches/unicode/dlib/unicode/unicode_abstract.h

	Put the convert_wstring_to_mbstring() function back in but in a way that
	works with mingw.

	------------------------------------------------------------------------
	r2219 | davisking | 2008-05-04 11:13:23 -0400 (Sun, 04 May 2008) | 1 line
	Changed paths:
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.cpp

	changed to be the same as the windows version
	------------------------------------------------------------------------
	r2218 | davisking | 2008-05-04 10:42:28 -0400 (Sun, 04 May 2008) | 4 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.cpp
	   M /branches/unicode/dlib/unicode/unicode.cpp
	   M /branches/unicode/dlib/unicode/unicode.h
	   M /branches/unicode/dlib/unicode/unicode_abstract.h

	Moved some more stuff around and cleaned up the code.  The wcstombs got
	moved into the gui_core_kernel_2.cpp file since I removed it from the
	unicode.cpp file.  This was because mingw doesn't seem to support it.

	------------------------------------------------------------------------
	r2216 | davisking | 2008-05-03 16:03:40 -0400 (Sat, 03 May 2008) | 1 line
	Changed paths:
	   M /branches/unicode/dlib/algs.h
	   M /branches/unicode/dlib/string/string.h
	   M /branches/unicode/dlib/unicode/unicode.h

	Changed code to avoid compiler trouble in mingw and inane warnings in
	visual studio.
	------------------------------------------------------------------------
	r2215 | davisking | 2008-05-03 10:22:35 -0400 (Sat, 03 May 2008) | 3 lines
	Changed paths:
	   M /branches/unicode/dlib/unicode/unicode.h

	Added a char_traits for dlib::unichar since gcc 3.3 (and probably others)
	won't compile without it.

	------------------------------------------------------------------------
	r2214 | davisking | 2008-05-03 10:21:32 -0400 (Sat, 03 May 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_widgets/style.h

	Added a missing #include

	------------------------------------------------------------------------
	r2213 | davisking | 2008-05-02 15:11:23 -0400 (Fri, 02 May 2008) | 3 lines
	Changed paths:
	   M /branches/unicode/dlib/string/string.h
	   M /branches/unicode/dlib/unicode/unicode.cpp

	mingw doesn't have a std::wcstombs so I replaced calls to it with calls to
	dlib::narrow.  Also changed dlib::narrow so it works in mingw as well since
	the previous version ended up calling something that mingw doesn't have as
	well.
	------------------------------------------------------------------------
	r2212 | davisking | 2008-05-02 11:23:41 -0400 (Fri, 02 May 2008) | 3 lines
	Changed paths:
	   M /branches/unicode/dlib/all/source.cpp

	Applying more of Keita Mochizuki's unicode patch.


	------------------------------------------------------------------------
	r2211 | davisking | 2008-05-02 11:22:58 -0400 (Fri, 02 May 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/unicode/unicode.h
	   M /branches/unicode/dlib/unicode/unicode_abstract.h

	updated specs a little

	------------------------------------------------------------------------
	r2210 | davisking | 2008-05-02 11:16:33 -0400 (Fri, 02 May 2008) | 2 lines
	Changed paths:
	   A /branches/unicode/dlib/unicode/unicode.cpp
	   M /branches/unicode/dlib/unicode/unicode.h

	Applying more of the unicode patch from Keita Mochizuki

	------------------------------------------------------------------------
	r2209 | davisking | 2008-05-02 11:10:19 -0400 (Fri, 02 May 2008) | 2 lines
	Changed paths:
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_1.h
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.cpp
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_2.h
	   M /branches/unicode/dlib/gui_core/gui_core_kernel_abstract.h

	Applying the first part of the unicode patch from Keita Mochizuki

	------------------------------------------------------------------------
	r2208 | davisking | 2008-05-02 10:49:15 -0400 (Fri, 02 May 2008) | 3 lines
	Changed paths:
	   A /branches/unicode (from /trunk:2207)

	Adding a branch to deal with the unicode patch from Keita Mochizuki.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402333
parent 8dda77b5
......@@ -51,7 +51,7 @@ IF (NOT DLIB_ISO_CPP_ONLY)
if (DLIB_NO_GUI_SUPPORT)
set (dlib_needed_libraries ws2_32)
else()
set (dlib_needed_libraries ws2_32 comctl32 gdi32)
set (dlib_needed_libraries ws2_32 comctl32 gdi32 imm32)
endif()
ELSEIF(APPLE) ############################################################################
FIND_LIBRARY(pthreadlib pthread)
......
......@@ -6,19 +6,6 @@
// this file contains miscellaneous stuff
#include <algorithm> // for std::swap
#include <new> // for std::bad_alloc
#include <string> // for the exceptions
#include <cstdlib>
#include "platform.h"
#include "assert.h"
#include "error.h"
#include "noncopyable.h"
#include "enable_if.h"
#include "uintn.h"
#ifdef _MSC_VER
// Disable the following warnings for Visual Studio
......@@ -56,6 +43,19 @@
#endif
#include <algorithm> // for std::swap
#include <new> // for std::bad_alloc
#include <string> // for the exceptions
#include <cstdlib>
#include "platform.h"
#include "assert.h"
#include "error.h"
#include "noncopyable.h"
#include "enable_if.h"
#include "uintn.h"
// ----------------------------------------------------------------------------------------
/*!A _dT !*/
......
......@@ -15,6 +15,7 @@
#include "../md5/md5_kernel_1.cpp"
#include "../pixel.cpp"
#include "../tokenizer/tokenizer_kernel_1.cpp"
#include "../unicode/unicode.cpp"
#ifndef DLIB_ISO_CPP_ONLY
// Code that depends on OS specific APIs
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_GUI_CORE_KERNEL_1_CPp_
#define DLIB_GUI_CORE_KERNEL_1_CPp_
......@@ -14,6 +14,7 @@
#pragma comment (lib, "gdi32.lib")
#pragma comment (lib, "comctl32.lib")
#pragma comment (lib, "user32.lib")
#pragma comment (lib, "imm32.lib")
#endif
......@@ -44,7 +45,7 @@ namespace dlib
static bool core_has_been_initialized = false;
static bool quit_windows_loop = false;
static bool set_window_title_done = true;
static std::string window_title;
static std::wstring window_title;
static bool move_window_done = true;
static HWND move_window_hwnd = NULL;
static int move_window_width = 0;
......@@ -54,6 +55,7 @@ namespace dlib
static bool request_new_window = false;
static DWORD dwStyle;
static HWND new_window = NULL;
static bool in_ime_composition = false;
// the window_table.get_mutex() mutex locks the above 11 variables
......@@ -549,7 +551,7 @@ namespace dlib
case WM_USER+CALL_SET_WINDOW_TITLE:
if (hwnd == helper_window)
{
SetWindowText((HWND)wParam,window_title.c_str());
SetWindowTextW((HWND)wParam,window_title.c_str());
set_window_title_done = true;
et_signaler.broadcast();
}
......@@ -588,6 +590,8 @@ namespace dlib
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
{
if (in_ime_composition) break;
base_window** win_ = window_table[hwnd];
base_window* win;
if (win_)
......@@ -1109,6 +1113,39 @@ namespace dlib
}
return DefWindowProc (hwnd, message, wParam, lParam);
case WM_IME_STARTCOMPOSITION:
in_ime_composition = true;
break;
case WM_IME_COMPOSITION:
{
in_ime_composition = false;
base_window** win_ = window_table[hwnd];
base_window* win;
if (win_)
win = *win_;
else
break;
HIMC hImc = ImmGetContext(hwnd);
if (lParam & GCS_RESULTSTR){
WCHAR wc;
LONG bufbyte = ImmGetCompositionStringW(hImc, GCS_RESULTSTR, &wc, 0);
if (bufbyte != IMM_ERROR_NODATA && bufbyte != IMM_ERROR_GENERAL){
bufbyte += sizeof(WCHAR);
WCHAR *buf = new WCHAR[bufbyte / sizeof(WCHAR)];
ImmGetCompositionStringW(hImc, GCS_RESULTSTR, buf, bufbyte);
buf[bufbyte / sizeof(WCHAR) - 1] = L'\0';
// signal the putstring event
win->on_string_put(std::wstring(buf));
delete [] buf;
}
}
ImmReleaseContext(hwnd, hImc);
}
break;
default:
break;
......@@ -1578,7 +1615,23 @@ namespace dlib
void base_window::
set_title (
const std::string& title
const std::string &title
)
{
set_title(convert_mbstring_to_wstring(title));
}
void base_window::
set_title (
const ustring &title
)
{
set_title(convert_utf32_to_wstring(title));
}
void base_window::
set_title (
const std::wstring& title
)
{
DLIB_ASSERT(is_closed() == false,
......@@ -1596,7 +1649,7 @@ namespace dlib
if (get_thread_id() == gui_core_kernel_1_globals::event_thread_id)
{
SetWindowText(hwnd,title.c_str());
SetWindowTextW(hwnd,title.c_str());
}
else
{
......@@ -1902,6 +1955,20 @@ namespace dlib
void put_on_clipboard (
const std::string& str
)
{
put_on_clipboard(convert_mbstring_to_wstring(str));
}
void put_on_clipboard (
const dlib::ustring& str
)
{
put_on_clipboard(convert_utf32_to_wstring(str));
}
void put_on_clipboard (
const std::wstring& str
)
{
using namespace gui_core_kernel_1_globals;
using namespace std;
......@@ -1913,34 +1980,34 @@ namespace dlib
EmptyClipboard();
auto_mutex M(window_table.get_mutex());
const unsigned long newlines = count(str.begin(),str.end(),'\n');
const unsigned long newlines = count(str.begin(),str.end(),L'\n');
HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE,str.size()+newlines+1);
HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE,(str.size()+newlines+1)*sizeof(wchar_t));
if (mem != NULL)
{
char* buf = reinterpret_cast<char*>(GlobalLock(mem));
wchar_t* buf = reinterpret_cast<wchar_t*>(GlobalLock(mem));
if (buf != NULL)
{
// copy str into buf while also replacing all the \n with \r\n
for (string::size_type i = 0; i < str.size(); ++i)
for (wstring::size_type i = 0; i < str.size(); ++i)
{
if (str[i] != '\n')
if (str[i] != L'\n')
{
*buf = str[i];
++buf;
}
else
{
*buf = '\r';
*buf = L'\r';
++buf;
*buf = '\n';
*buf = L'\n';
++buf;
}
}
*buf = '\0';
*buf = L'\0';
GlobalUnlock(mem);
SetClipboardData(CF_TEXT,mem);
SetClipboardData(CF_UNICODETEXT,mem);
}
}
CloseClipboard();
......@@ -1952,6 +2019,24 @@ namespace dlib
void get_from_clipboard (
std::string& str
)
{
std::wstring wstr;
get_from_clipboard(wstr);
str = convert_wstring_to_mbstring(wstr);
}
void get_from_clipboard (
dlib::ustring& str
)
{
std::wstring wstr;
get_from_clipboard(wstr);
str = convert_wstring_to_utf32(wstr);
}
void get_from_clipboard (
std::wstring& str
)
{
using namespace gui_core_kernel_1_globals;
using namespace std;
......@@ -1962,19 +2047,19 @@ namespace dlib
if (OpenClipboard(helper_window))
{
HANDLE data = GetClipboardData(CF_TEXT);
HANDLE data = GetClipboardData(CF_UNICODETEXT);
if (data != NULL)
{
char* buf = reinterpret_cast<char*>(GlobalLock(data));
wchar_t* buf = reinterpret_cast<wchar_t*>(GlobalLock(data));
if (buf != 0)
{
str.clear();
// copy the data from buf into str while also removing any '\r'
// characters.
while (*buf != '\0')
while (*buf != L'\0')
{
if (*buf != '\r')
if (*buf != L'\r')
str += *buf;
++buf;
}
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_GUI_CORE_KERNEl_1_
#define DLIB_GUI_CORE_KERNEl_1_
......@@ -38,6 +38,7 @@
#include "../assert.h"
#include "../queue.h"
#include "../pixel.h"
#include "../unicode.h"
namespace dlib
......@@ -136,12 +137,28 @@ namespace dlib
const std::string& str
);
void put_on_clipboard (
const std::wstring& str
);
void put_on_clipboard (
const dlib::ustring& str
);
// ----------------------------------------------------------------------------------------
void get_from_clipboard (
std::string& str
);
void get_from_clipboard (
std::wstring& str
);
void get_from_clipboard (
dlib::ustring& str
);
// ----------------------------------------------------------------------------------------
class base_window
......@@ -186,6 +203,14 @@ namespace dlib
const std::string& title
);
void set_title (
const std::wstring& title
);
void set_title (
const ustring& title
);
virtual void show (
);
......@@ -353,6 +378,10 @@ namespace dlib
bool is_printable,
unsigned long state
){}
virtual void on_string_put (
const std::wstring &str
){}
private:
......
This diff is collapsed.
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_GUI_CORE_KERNEl_2_
#define DLIB_GUI_CORE_KERNEl_2_
......@@ -21,6 +21,7 @@
#include "../binary_search_tree.h"
#include <string.h>
#include "../pixel.h"
#include "../unicode.h"
namespace dlib
......@@ -46,12 +47,28 @@ namespace dlib
const std::string& str
);
void put_on_clipboard (
const std::wstring& str
);
void put_on_clipboard (
const dlib::ustring& str
);
// ----------------------------------------------------------------------------------------
void get_from_clipboard (
std::string& str
);
void get_from_clipboard (
std::wstring& str
);
void get_from_clipboard (
dlib::ustring& str
);
// ----------------------------------------------------------------------------------------
class canvas : public rectangle
......@@ -252,6 +269,14 @@ namespace dlib
const std::string& title_
);
void set_title (
const std::wstring& title_
);
void set_title (
const dlib::ustring& title_
);
virtual void show (
);
......@@ -352,6 +377,10 @@ namespace dlib
unsigned long state
){}
virtual void on_string_put (
const std::wstring &str
){}
private:
virtual void paint (
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_GUI_CORE_KERNEl_ABSTRACT_
#ifdef DLIB_GUI_CORE_KERNEl_ABSTRACT_
......@@ -6,6 +6,7 @@
#include <string>
#include "../algs.h"
#include "../geometry/rectangle_abstract.h"
#include "../unicode/unicode_abstract.h"
namespace dlib
{
......@@ -61,6 +62,10 @@ namespace dlib
- dlib::thread_error
!*/
// overloads for wide character strings
void put_on_clipboard (const std::wstring& str);
void put_on_clipboard (const dlib::ustring& str);
// ----------------------------------------------------------------------------------------
void get_from_clipboard (
......@@ -78,6 +83,10 @@ namespace dlib
- dlib::thread_error
!*/
// overloads for wide character strings
void get_from_clipboard (std::wtring& str);
void get_from_clipboard (dlib::utring& str);
// ----------------------------------------------------------------------------------------
......@@ -356,6 +365,26 @@ namespace dlib
- sets the title of the window
!*/
void set_title (
const std::wstring& title
);
/*!
requires
- is_closed() == false
ensures
- sets the title of the window
!*/
void set_title (
const dlib::ustring& title
);
/*!
requires
- is_closed() == false
ensures
- sets the title of the window
!*/
virtual void show (
);
/*!
......@@ -708,6 +737,20 @@ namespace dlib
- does not change the state of mutex wm
!*/
virtual void on_string_put (
const std::wstring &str
){}
/*!
requires
- is_closed() == false
- mutex wm is locked
- is called when a wide/multibyte character input method determines a string
that is being input to the window.
- str == the string that is being input
ensures
- does not change the state of mutex wm
!*/
private:
virtual void paint (
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_BASE_WIDGETs_CPP_
#define DLIB_BASE_WIDGETs_CPP_
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_BASE_WIDGETs_
......@@ -16,6 +16,7 @@
#include "../image_transforms.h"
#include "../array.h"
#include "../smart_pointers.h"
#include "../unicode.h"
#include <cctype>
......@@ -981,6 +982,20 @@ namespace dlib
void set_text (
const std::string& str
)
{
set_text(convert_mbstring_to_wstring(str));
}
void set_text (
const std::wstring& str
)
{
set_text(convert_wstring_to_utf32(str));
}
void set_text (
const ustring& str
)
{
auto_mutex M(m);
if (!stuff)
......@@ -994,9 +1009,21 @@ namespace dlib
const std::string text (
) const
{
return convert_wstring_to_mbstring(wtext());
}
const std::wstring wtext (
) const
{
return convert_utf32_to_wstring(utext());
}
const dlib::ustring utext (
) const
{
auto_mutex M(m);
std::string temp;
dlib::ustring temp;
if (stuff)
{
temp = stuff->win.text;
......@@ -1067,15 +1094,29 @@ namespace dlib
{
}
std::string text;
ustring text;
rectangle rect_all;
rectangle rect_text;
const unsigned long pad;
const font* mfont;
void set_text (
const std::string& str
)
{
set_text(convert_mbstring_to_wstring(str));
}
void set_text (
const std::wstring& str
)
{
set_text(convert_wstring_to_utf32(str));
}
void set_text (
const dlib::ustring& str
)
{
text = str.c_str();
......@@ -1227,15 +1268,11 @@ namespace dlib
class menu_item_submenu : public menu_item
{
public:
menu_item_submenu (
const std::string& str,
unichar hk = 0
) :
text(str),
f(default_font::get_font()),
hotkey(hk)
void initialize (
unichar hk
)
{
const dlib::ustring &str = text;
if (hk != 0)
{
std::string::size_type pos = str.find_first_of(hk);
......@@ -1252,6 +1289,39 @@ namespace dlib
}
}
}
public:
menu_item_submenu (
const std::string& str,
unichar hk = 0
) :
text(convert_wstring_to_utf32(convert_mbstring_to_wstring(str))),
f(default_font::get_font()),
hotkey(hk)
{
initialize(hk);
}
menu_item_submenu (
const std::wstring& str,
unichar hk = 0
) :
text(convert_wstring_to_utf32(str)),
f(default_font::get_font()),
hotkey(hk)
{
initialize(hk);
}
menu_item_submenu (
const dlib::ustring& str,
unichar hk = 0
) :
text(str),
f(default_font::get_font()),
hotkey(hk)
{
initialize(hk);
}
virtual unichar get_hot_key (
) const { return hotkey; }
......@@ -1337,7 +1407,7 @@ namespace dlib
}
private:
std::string text;
dlib::ustring text;
const font* f;
member_function_pointer<>::kernel_1a action;
unichar hotkey;
......@@ -1349,18 +1419,14 @@ namespace dlib
class menu_item_text : public menu_item
{
public:
template <typename T>
menu_item_text (
const std::string& str,
T& object,
void initialize (
T &object,
void (T::*event_handler_)(),
unichar hk = 0
) :
text(str),
f(default_font::get_font()),
hotkey(hk)
unichar hk
)
{
dlib::ustring &str = text;
action.set(object,event_handler_);
if (hk != 0)
......@@ -1380,6 +1446,49 @@ namespace dlib
}
}
public:
template <typename T>
menu_item_text (
const std::string& str,
T& object,
void (T::*event_handler_)(),
unichar hk = 0
) :
text(convert_wstring_to_utf32(convert_mbstring_to_wstring(str))),
f(default_font::get_font()),
hotkey(hk)
{
initialize(object, event_handler_, hk);
}
template <typename T>
menu_item_text (
const std::wstring& str,
T& object,
void (T::*event_handler_)(),
unichar hk = 0
) :
text(convert_wstring_to_utf32(str)),
f(default_font::get_font()),
hotkey(hk)
{
initialize(object, event_handler_, hk);
}
template <typename T>
menu_item_text (
const dlib::ustring& str,
T& object,
void (T::*event_handler_)(),
unichar hk = 0
) :
text(str),
f(default_font::get_font()),
hotkey(hk)
{
initialize(object, event_handler_, hk);
}
virtual unichar get_hot_key (
) const { return hotkey; }
......@@ -1444,7 +1553,7 @@ namespace dlib
) const { return true; }
private:
std::string text;
dlib::ustring text;
const font* f;
member_function_pointer<>::kernel_1a action;
unichar hotkey;
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_BASE_WIDGETs_ABSTRACT_
#ifdef DLIB_BASE_WIDGETs_ABSTRACT_
......@@ -907,6 +907,15 @@ namespace dlib
will display on the screen when the user hovers the mouse over it
!*/
// overloads for wide character strings
void set_text (
const std::wstring& str
);
void set_text (
const dlib::ustring& str
);
const std::string text (
) const;
/*!
......@@ -915,6 +924,22 @@ namespace dlib
tooltip
!*/
const std::wstring wtext (
) const;
/*!
ensures
- returns the text that is displayed inside this
tooltip
!*/
const dlib::ustring utext (
) const;
/*!
ensures
- returns the text that is displayed inside this
tooltip
!*/
private:
// restricted functions
......@@ -1102,6 +1127,27 @@ namespace dlib
clicked by the user.
- #get_hot_key() == hotkey
!*/
// overloads for wide charactor strings
template <
typename T
>
menu_item_text (
const std::wstring& str,
T& object,
void (T::*on_click_handler)(),
unichar hotkey = 0
);
template <
typename T
>
menu_item_text (
const dlib::ustring& str,
T& object,
void (T::*on_click_handler)(),
unichar hotkey = 0
);
};
// ----------------------------------------------------------------------------------------
......@@ -1126,6 +1172,17 @@ namespace dlib
clicked by the user.
- #get_hot_key() == hotkey
!*/
//overloads for wide charactor strings
menu_item_submenu (
const std::wstring& str,
unichar hotkey = 0
);
menu_item_submenu (
const dlib::ustring& str,
unichar hotkey = 0
);
};
// ----------------------------------------------------------------------------------------
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_DRAWABLe_CPP_
#define DLIB_DRAWABLe_CPP_
......@@ -330,6 +330,25 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
void drawable_window::
on_string_put (
const std::wstring &str
)
{
++event_id;
string_put.reset();
while (string_put.move_next())
{
if (string_put.element()->event_id != event_id)
{
string_put.element()->event_id = event_id;
string_put.element()->on_string_put(str);
}
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------- drawable object ----------------------------------------------------------
......@@ -399,6 +418,11 @@ namespace dlib
temp = this;
parent.window_moved.add(temp);
}
if (events & STRING_PUT)
{
temp = this;
parent.string_put.add(temp);
}
parent.invalidate_rectangle(rect);
}
}
......@@ -481,6 +505,8 @@ namespace dlib
parent.focus.remove(this,junk);
if (events & WINDOW_MOVED)
parent.window_moved.remove(this,junk);
if (events & STRING_PUT)
parent.string_put.remove(this,junk);
}
}
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net)
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_DRAWABLe_
......@@ -149,6 +149,10 @@ namespace dlib
unsigned long state
);
void on_string_put (
const std::wstring &str
);
void on_user_event (
void* p,
int i
......@@ -173,6 +177,7 @@ namespace dlib
set_of_drawables keyboard;
set_of_drawables focus;
set_of_drawables window_moved;
set_of_drawables string_put;
long lastx, lasty;
unsigned long event_id;
......@@ -201,7 +206,8 @@ namespace dlib
WINDOW_RESIZED = 8,
KEYBOARD_EVENTS = 16,
FOCUS_EVENTS = 32,
WINDOW_MOVED = 64
WINDOW_MOVED = 64,
STRING_PUT = 128
};
class drawable
......@@ -496,6 +502,10 @@ namespace dlib
bool is_printable,
unsigned long state
){}
virtual void on_string_put (
const std::wstring &str
){}
};
// ----------------------------------------------------------------------------------------
......
......@@ -132,7 +132,8 @@ namespace dlib
WINDOW_RESIZED = 8,
KEYBOARD_EVENTS = 16,
FOCUS_EVENTS = 32,
WINDOW_MOVED = 64
WINDOW_MOVED = 64,
STRING_PUT = 128
};
class drawable
......@@ -665,6 +666,19 @@ namespace dlib
- does not change the state of mutex m.
!*/
virtual void on_string_put (
const std::wstring &str
){}
/*!
requires
- events_are_enabled() == true
- mutex m is locked
- this is just the base_window::on_put_string() event forwarded to
this object. See the gui_core specs for the details about this event.
ensures
- does not change the state of mutex m.
!*/
virtual void draw (
const canvas& c
) const=0;
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), and Nils Labugt
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), and Nils Labugt, Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_FONTs_CPP_
#define DLIB_FONTs_CPP_
......@@ -11,6 +11,7 @@
#include "../compress_stream.h"
#include <fstream>
#include "../tokenizer.h"
#include "nativefont.h"
namespace dlib
{
......@@ -657,6 +658,14 @@ namespace dlib
return true;
}
// ----------------------------------------------------------------------------------------
const font* get_native_font (
)
{
return nativefont::native_font::get_font();
}
// ----------------------------------------------------------------------------------------
}
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), and Nils Labugt
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), and Nils Labugt, Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_FONTs_
......@@ -475,6 +475,10 @@ namespace dlib
};
// ----------------------------------------------------------------------------------------
const font* get_native_font ();
// ----------------------------------------------------------------------------------------
class default_font : public font
......
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), and Nils Labugt
// Copyright (C) 2005 Davis E. King (davisking@users.sourceforge.net), Nils Labugt, Keita Mochizuki
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_FONTs_ABSTRACT_
#ifdef DLIB_FONTs_ABSTRACT_
......@@ -379,7 +378,7 @@ namespace dlib
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an implementation of the Times New Roman font.
This is an implementation of the Helvetica 12 point font.
THREAD SAFETY
It is safe to call get_font() and access the returned font from any
......@@ -478,6 +477,15 @@ namespace dlib
};
// ----------------------------------------------------------------------------------------
font* get_native_font (
);
/*!
ensures
- returns a font object that uses the local font
!*/
// ----------------------------------------------------------------------------------------
}
......
This diff is collapsed.
......@@ -22,7 +22,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed
) const
{
......@@ -87,7 +87,7 @@ namespace dlib
rectangle button_style_default::
get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const
{
......@@ -112,7 +112,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed
) const
{
......@@ -186,7 +186,7 @@ namespace dlib
rectangle button_style_toolbar1::
get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const
{
......@@ -211,7 +211,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed
) const
{
......@@ -268,7 +268,7 @@ namespace dlib
rectangle button_style_toolbar_icon1::
get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const
{
......@@ -289,7 +289,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed,
const bool is_checked
) const
......@@ -355,7 +355,7 @@ namespace dlib
rectangle toggle_button_style_default::
get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const
{
......@@ -380,7 +380,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed,
const bool is_checked
) const
......@@ -447,7 +447,7 @@ namespace dlib
rectangle toggle_button_style_check_box::
get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const
{
......@@ -473,7 +473,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed,
const bool is_checked
) const
......@@ -567,7 +567,7 @@ namespace dlib
rectangle toggle_button_style_radio_button::
get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const
{
......
......@@ -12,6 +12,7 @@
#include "../unicode.h"
#include "../array2d.h"
#include "../pixel.h"
#include "fonts.h"
namespace dlib
......@@ -38,7 +39,7 @@ namespace dlib
) const { return false; }
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const = 0;
......@@ -50,7 +51,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed
) const = 0;
};
......@@ -70,12 +71,12 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed
) const;
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const;
......@@ -104,7 +105,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed
) const;
......@@ -112,7 +113,7 @@ namespace dlib
) const { return true; }
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const;
......@@ -153,7 +154,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed
) const;
......@@ -161,7 +162,7 @@ namespace dlib
) const { return true; }
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const;
......@@ -235,7 +236,7 @@ namespace dlib
) const { return false; }
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const = 0;
......@@ -247,7 +248,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed,
const bool is_checked
) const = 0;
......@@ -268,13 +269,13 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed,
const bool is_checked
) const;
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const;
......@@ -301,13 +302,13 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed,
const bool is_checked
) const;
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const;
......@@ -326,13 +327,13 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed,
const bool is_checked
) const;
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const;
......
......@@ -6,6 +6,7 @@
#include "../algs.h"
#include "../gui_core.h"
#include "widgets_abstract.h"
#include "../unicode/unicode_abstract.h"
namespace dlib
{
......@@ -42,7 +43,7 @@ namespace dlib
!*/
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const = 0;
/*!
......@@ -61,7 +62,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed
) const = 0;
/*!
......@@ -153,7 +154,7 @@ namespace dlib
!*/
virtual rectangle get_min_size (
const std::string& name,
const ustring& name,
const font& mfont
) const = 0;
/*!
......@@ -172,7 +173,7 @@ namespace dlib
const font& mfont,
const long lastx,
const long lasty,
const std::string& name,
const ustring& name,
const bool is_depressed,
const bool is_checked
) const = 0;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -118,6 +118,7 @@
#include "interfaces/enumerable.h"
#include "interfaces/map_pair.h"
#include "enable_if.h"
#include "unicode.h"
namespace dlib
{
......@@ -765,6 +766,40 @@ namespace dlib
if (!in) throw serialization_error("Error deserializing object of type std::wstring");
}
// ----------------------------------------------------------------------------------------
inline void serialize (
const ustring& item,
std::ostream& out
)
{
const unsigned long size = static_cast<unsigned long>(item.size());
try{ serialize(size,out); }
catch (serialization_error& e)
{ throw serialization_error(e.info + "\n while serializing object of type ustring"); }
for (unsigned long i = 0; i < item.size(); ++i)
serialize(item[i], out);
if (!out) throw serialization_error("Error serializing object of type ustring");
}
inline void deserialize (
ustring& item,
std::istream& in
)
{
unsigned long size;
try { deserialize(size,in); }
catch (serialization_error& e)
{ throw serialization_error(e.info + "\n while deserializing object of type ustring"); }
item.resize(size);
for (unsigned long i = 0; i < item.size(); ++i)
deserialize(item[i],in);
if (!in) throw serialization_error("Error deserializing object of type ustring");
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -3,9 +3,9 @@
#ifndef DLIB_STRINg_
#define DLIB_STRINg_
#include "../algs.h"
#include <string>
#include <iostream>
#include "../algs.h"
#include "../error.h"
#include "../assert.h"
#include "string_abstract.h"
......@@ -227,10 +227,12 @@ namespace dlib
std::string temp;
temp.reserve(str.size());
std::string::size_type i;
std::basic_ostringstream<charT,traits,alloc> sout;
for (i = 0; i < str.size(); ++i)
{
temp += sout.narrow(str[i],' ');
if (zero_extend_cast<unsigned long>(str[i]) > 255)
temp += ' ';
else
temp += zero_extend_cast<char>(str[i]);
}
return temp;
}
......
// Copyright (C) 2008 Keita Mochizuki, Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_UNICODe_CPp_
#define DLIB_UNICODe_CPp_
#include "unicode.h"
#include <cwchar>
#include "../string.h"
#include <vector>
namespace dlib
{
// ----------------------------------------------------------------------------------------
static const unichar SURROGATE_FIRST_TOP = 0xD800;
static const unichar SURROGATE_SECOND_TOP = 0xDC00;
static const unichar SURROGATE_CLEARING_MASK = 0x03FF;
static const unichar &SURROGATE_TOP = SURROGATE_FIRST_TOP;
static const unichar SURROGATE_END = 0xE000;
static const unichar SMP_TOP = 0x10000;
static const int VALID_BITS = 10;
// ----------------------------------------------------------------------------------------
template <typename T> bool is_surrogate(T ch)
{
return (zero_extend_cast<unichar>(ch) >= SURROGATE_TOP &&
zero_extend_cast<unichar>(ch) < SURROGATE_END);
}
// ----------------------------------------------------------------------------------------
template <typename T> unichar surrogate_pair_to_unichar(T first, T second)
{
return ((first & SURROGATE_CLEARING_MASK) << VALID_BITS) | (second & SURROGATE_CLEARING_MASK) + SMP_TOP;
}
//110110 0000000000
//110111 0000000000
// ----------------------------------------------------------------------------------------
void unichar_to_surrogate_pair(unichar input, unichar &first, unichar &second)
{
first = ((input - SMP_TOP) >> VALID_BITS) | SURROGATE_FIRST_TOP;
second = (input & SURROGATE_CLEARING_MASK) | SURROGATE_SECOND_TOP;
}
// ----------------------------------------------------------------------------------------
template <int N> void wstr2ustring_t(const wchar_t *src, size_t src_len, ustring &dest);
template <> void wstr2ustring_t<4>(const wchar_t *src, size_t src_len, ustring &dest)
{
dest.assign((const unichar *)(src));
}
template <> void wstr2ustring_t<2>(const wchar_t *src, size_t src_len, ustring &dest)
{
size_t wlen = 0;
for (size_t i = 0; i < src_len; i++)
{
is_surrogate(src[i]) ? i++, wlen++ : wlen++;
}
dest.resize(wlen);
for (size_t i = 0, ii = 0; ii < src_len; ++i)
{
if (is_surrogate(src[ii]))
{
dest[i] = surrogate_pair_to_unichar(src[ii], src[ii+1]);
ii += 2;
}else
{
dest[i] = zero_extend_cast<unichar>(src[ii]);
ii++;
}
}
}
// ----------------------------------------------------------------------------------------
const ustring convert_wstring_to_utf32(const std::wstring &src)
{
ustring dest;
wstr2ustring_t<sizeof(wchar_t)>(src.c_str(), src.size(), dest);
return dest;
}
// ----------------------------------------------------------------------------------------
template <int N> struct ustring2wstr
{
};
// for the environment of sizeof(wchar_t) == 2 (i.e. Win32)
template <> struct ustring2wstr<2>
{
wchar_t *wstr;
size_t wlen;
ustring2wstr(const ustring &src){
wlen = 0;
for (size_t i = 0; i < src.length(); ++i)
{
if (src[i] < SMP_TOP) wlen++;
else wlen += 2;
}
wstr = new wchar_t[wlen+1];
wstr[wlen] = L'\0';
size_t wi = 0;
for (size_t i = 0; i < src.length(); ++i)
{
if (src[i] < SMP_TOP)
{
wstr[wi++] = (wchar_t)src[i];
}else
{
unichar high, low;
unichar_to_surrogate_pair(src[i], high, low);
wstr[wi++] = (wchar_t)high;
wstr[wi++] = (wchar_t)low;
}
}
}
~ustring2wstr()
{
delete[] wstr;
}
};
// for the environment of sizeof(wchar_t) == 4 (i.e. Unix gcc)
template <> struct ustring2wstr<4>
{
const wchar_t *wstr;
size_t wlen;
ustring2wstr(const ustring &src){
wstr = (const wchar_t *)(src.c_str());
wlen = src.size();
}
};
// ----------------------------------------------------------------------------------------
const std::wstring convert_utf32_to_wstring(const ustring &src)
{
ustring2wstr<sizeof(wchar_t)> conv(src);
std::wstring dest(conv.wstr);
return dest;
}
// ----------------------------------------------------------------------------------------
const std::wstring convert_mbstring_to_wstring(const std::string &src)
{
std::vector<wchar_t> wstr(src.length()+1);
std::mbstowcs(&wstr.front(), src.c_str(), src.length()+1);
return std::wstring(&wstr.front());
}
// ----------------------------------------------------------------------------------------
const std::string convert_wstring_to_mbstring(const std::wstring &src)
{
using namespace std;
std::string str;
str.resize((src.length() + 1) * MB_CUR_MAX);
wcstombs(&str[0], src.c_str(), str.size());
return std::string(&str[0]);
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_UNICODe_CPp_
......@@ -7,6 +7,8 @@
#include "../algs.h"
#include "unicode_abstract.h"
#include <string>
#include <cstring>
#include <fstream>
namespace dlib
......@@ -15,7 +17,94 @@ namespace dlib
// ----------------------------------------------------------------------------------------
typedef uint32 unichar;
#if defined(__GNUC__) && __GNUC__ < 4 && __GNUC_MINOR__ < 4
struct unichar_traits
{
typedef dlib::unichar char_type;
typedef dlib::unichar int_type;
typedef std::streamoff off_type;
typedef std::streampos pos_type;
typedef std::mbstate_t state_type;
static void assign(char_type& c1, const char_type& c2) { c1 = c2; }
static bool eq(const char_type& c1, const char_type& c2) { return c1 == c2; }
static bool lt(const char_type& c1, const char_type& c2) { return c1 < c2; }
static int compare(const char_type* s1, const char_type* s2, size_t n)
{
for (size_t i = 0; i < n; ++i)
{
if (s1[i] < s2[i])
return -1;
else if (s1[i] > s2[i])
return 1;
}
return 0;
}
static size_t length(const char_type* s)
{
size_t i = 0;
while (s[i] != 0)
++i;
return i;
}
static const char_type* find(const char_type* s, size_t n,
const char_type& a)
{
for (size_t i = 0; i < n; ++i)
{
if (s[i] == a)
{
return s+i;
}
}
return 0;
}
static char_type* move(char_type* s1, const char_type* s2, size_t n)
{
return static_cast<char_type*>(std::memmove(s1, s2, sizeof(char_type)*n));
}
static char_type* copy(char_type* s1, const char_type* s2, size_t n)
{
for (size_t i = 0; i < n; ++i)
s1[i] = s2[i];
return s1;
}
static char_type* assign(char_type* s, size_t n, char_type a)
{
for (size_t i = 0; i < n; ++i)
s[i] = a;
return s;
}
static int_type not_eof(const int_type& c)
{
if (!eq_int_type(c,eof()))
return to_int_type(c);
else
return 0;
}
static char_type to_char_type(const int_type& c) { return static_cast<char_type>(c); }
static int_type to_int_type(const char_type& c) { return zero_extend_cast<int_type>(c); }
static bool eq_int_type(const int_type& c1, const int_type& c2) { return c1 == c2; }
static int_type eof() { return static_cast<int_type>(EOF); }
};
typedef std::basic_string<unichar, unichar_traits> ustring;
#else
typedef std::basic_string<unichar> ustring;
#endif
// ----------------------------------------------------------------------------------------
......@@ -419,6 +508,31 @@ namespace dlib
return temp;
}
// ----------------------------------------------------------------------------------------
bool is_surrogate(unichar ch);
unichar surrogate_pair_to_unichar(unichar first, unichar second);
void unichar_to_surrogate_pair(unichar unicode, unichar &first, unichar &second);
const ustring convert_wstring_to_utf32 (
const std::wstring &wstr
);
const std::wstring convert_utf32_to_wstring (
const ustring &src
);
const std::wstring convert_mbstring_to_wstring (
const std::string &src
);
const std::string convert_wstring_to_mbstring(
const std::wstring &src
);
// ----------------------------------------------------------------------------------------
template <typename charT>
......@@ -490,5 +604,9 @@ namespace dlib
}
#ifdef NO_MAKEFILE
#include "unicode.cpp"
#endif
#endif // DLIB_UNICODe_H_
......@@ -33,6 +33,44 @@ namespace dlib
- returns false
!*/
bool is_surrogate(
unichar ch
);
/*!
ensures
- if (ch is a unicode surrogate character) then
- returns true
- else
- returns false
!*/
unichar surrogate_pair_to_unichar(
unichar first,
unichar second
);
/*!
requires
- 0xD800 <= first < 0xDC00
- 0xDC00 <= second < 0xE000
- is_surrogate(first) == true
- is_surrogate(second) == true
ensures
- converts two surrogates into one unicode character
!*/
void unichar_to_surrogate_pair(
unichar ch,
unichar& first,
unichar& second
);
/*!
requires
- ch >= 0x10000 (i.e. is not in Basic Multilingual Plane)
ensures
- surrogate_pair_to_unichar(#first,#second) == ch
(i.e. converts ch into two surrogate characters)
!*/
// ----------------------------------------------------------------------------------------
class invalid_utf8_error : public error
......@@ -53,6 +91,58 @@ namespace dlib
- throws invalid_utf8_error
!*/
// ----------------------------------------------------------------------------------------
const ustring convert_wstring_to_utf32 (
const std::wstring &wstr
);
/*!
requires
- wstr is a valid UTF-16 string when sizeof(wchar_t) == 2
- wstr is a valid UTF-32 string when sizeof(wchar_t) == 4
ensures
- converts wstr into UTF-32 string
!*/
// ----------------------------------------------------------------------------------------
const std::wstring convert_utf32_to_wstring (
const ustring &str
);
/*!
requires
- str is a valid UTF-32 encoded string
ensures
- converts str into wstring whose encoding is UTF-16 when sizeof(wchar_t) == 2
- converts str into wstring whose encoding is UTF-32 when sizeof(wchar_t) == 4
!*/
// ----------------------------------------------------------------------------------------
const std::wstring convert_mbstring_to_wstring (
const std::string &str
);
/*!
requires
- str is a valid multibyte string whose encoding is same as current locale setting
ensures
- converts str into wstring whose encoding is UTF-16 when sizeof(wchar_t) == 2
- converts str into wstring whose encoding is UTF-32 when sizeof(wchar_t) == 4
!*/
// ----------------------------------------------------------------------------------------
const std::string convert_wstring_to_mbstring (
const std::wstring &src
);
/*!
requires
- str is a valid wide character string string whose encoding is same as current
locale setting
ensures
- returns a multibyte encoded version of the given string
!*/
// ----------------------------------------------------------------------------------------
template <
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment