From 0093ac3edca10da8ef2e478913b9931e36018597 Mon Sep 17 00:00:00 2001 From: Vincent Wei Date: Fri, 8 Mar 2019 14:39:48 +0800 Subject: [PATCH] New APIs: UCHAR2ACHAR, UChar2AChar, UChars2AChars --- include/gdi.h | 52 +++++++++++++++++++++++ src/newgdi/achar.c | 103 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) diff --git a/include/gdi.h b/include/gdi.h index 704f9a14..4179296a 100644 --- a/include/gdi.h +++ b/include/gdi.h @@ -11143,6 +11143,58 @@ MG_EXPORT Uchar32 GUIAPI AChar2UChar(LOGFONT* logfont, Achar32 chv); MG_EXPORT int GUIAPI AChars2UChars(LOGFONT* logfont, const Achar32* chs, Uchar32* ucs, int n); +/** + * \def UCHAR2ACHAR(uc) + * \brief The macro to convert a Uchar32 value to Achar32 value. + * + * \note The converted Achar32 value should be used only with LOGFONT objects + * in Unicode charset (encodings like UTF-8, UTF-16LE, and UTF-16BE). + * + * \sa UChar2AChar, UChars2AChars + */ +#define UCHAR2ACHAR(uc) ((uc) | 0x80000000) + +/** + * \fn BOOL UChar2AChar(LOGFONT* logfont, Uchar32 uc, Achar32* ac) + * \brief Get the LOGFONT Achar32 value from a Unicode character value. + * + * This function converts a Unicode character value \a uc to the abstract + * character value which is defined by the charset of the LOGFONT object + * \a logfont. + * + * \param logfont The LOGFONT object + * \param uc The Unicode character value. + * \param ac The buffer to store the converted Achar32 value. + * + * \return TRUE for success, otherwise FALSE. When the return value + * is FALSE, it means that the charset of the LOGFONT object + * does not contain a code point for the Unicode character. + * + * \note Only available when support for UNICODE is enabled. + * + * \sa UChars2AChars, UCHAR2ACHAR + */ +MG_EXPORT BOOL GUIAPI UChar2AChar(LOGFONT* logfont, Uchar32 uc, Achar32* ac); + +/** + * \fn int UChars2AChars(LOGFONT* logfont, const Uchar32* ucs, + * Achar32* acs, int n) + * \brief Convert an Uchar32 array to Unicode character array. + * + * Only valid for UNICODE. + * + * \param logfont The LOGFONT object + * \param chs The array of LOGFONT character values. + * + * \return The number of characters converted successfully. + * + * \note Only available when support for UNICODE is enabled. + * + * \sa UChar2AChar, UCHAR2ACHAR + */ +MG_EXPORT int GUIAPI UChars2AChars(LOGFONT* logfont, const Uchar32* ucs, + Achar32* acs, int n); + /** * \defgroup glyph_render_flags Glyph Rendering Flags * diff --git a/src/newgdi/achar.c b/src/newgdi/achar.c index bd048839..c9db7788 100644 --- a/src/newgdi/achar.c +++ b/src/newgdi/achar.c @@ -228,4 +228,107 @@ int GUIAPI AChars2UChars(LOGFONT* logfont, const Achar32* chs, return TRUE; } +BOOL GUIAPI UChar2AChar(LOGFONT* logfont, Uchar32 uc, Achar32* ac) +{ + DEVFONT* mbc_devfont; + DEVFONT* sbc_devfont; + unsigned char mchar [16]; + int l = 0; + + if (logfont == NULL || uc == 0 || ac == NULL) + return FALSE; + + mbc_devfont = logfont->devfonts[1]; + sbc_devfont = logfont->devfonts[0]; + if (mbc_devfont) { + if (mbc_devfont->charset_ops->conv_to_uc32) { + // not in Unicode encoding + l = mbc_devfont->charset_ops->conv_from_uc32(uc, mchar); + if (l > 0) { + *ac = mbc_devfont->charset_ops->get_char_value(NULL, 0, + mchar, l); + *ac = SET_MBCHV(*ac); + } + } + else { + l = 1; + *ac = SET_MBCHV(uc); + } + } + + if (l <= 0) { + if (sbc_devfont->charset_ops->conv_to_uc32) { + // not in Unicode encoding + l = sbc_devfont->charset_ops->conv_from_uc32(uc, mchar); + if (l > 0) { + *ac = mbc_devfont->charset_ops->get_char_value(NULL, 0, + mchar, l); + } + } + else { + l = 1; + *ac = uc; + } + } + + if (l <= 0) + return FALSE; + + return TRUE; +} + +int GUIAPI UChars2AChars(LOGFONT* logfont, const Uchar32* ucs, + Achar32* acs, int n) +{ + DEVFONT* mbc_devfont; + DEVFONT* sbc_devfont; + unsigned char mchar [16]; + int i, l = 0; + + if (logfont == NULL || ucs == NULL || acs == NULL) + return FALSE; + + mbc_devfont = logfont->devfonts[1]; + sbc_devfont = logfont->devfonts[0]; + + for (i = 0; i < n; i++) { + if (mbc_devfont) { + if (mbc_devfont->charset_ops->conv_to_uc32) { + // not in Unicode encoding + l = mbc_devfont->charset_ops->conv_from_uc32(ucs[i], mchar); + if (l > 0) { + acs[i] = mbc_devfont->charset_ops->get_char_value(NULL, 0, + mchar, l); + acs[i] = SET_MBCHV(acs[i]); + } + } + else { + l = 1; + acs[i] = SET_MBCHV(ucs[i]); + } + + } + + if (l <= 0) { + if (sbc_devfont->charset_ops->conv_to_uc32) { + // not in Unicode encoding + l = sbc_devfont->charset_ops->conv_from_uc32(ucs[i], mchar); + if (l > 0) { + acs[i] = mbc_devfont->charset_ops->get_char_value(NULL, 0, + mchar, l); + } + } + else { + l = 1; + acs[i] = ucs[i]; + } + } + + if (l <= 0) + return i; + } + + return i; +} + #endif /* _MGCHARSET_UNICODE */