source: branches/kmk/Lucide/plugins/ludoc/cpconv.cpp @ 306

Last change on this file since 306 was 306, checked in by dmik, 12 years ago

branches/kmk: Made all Lucide SOM plugins build with kmk/gcc.

File size: 4.9 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: CDDL 1.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the COMMON DEVELOPMENT AND
5 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy of
7 * the License at http://www.sun.com/cddl/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is
15 * Eugene Romanenko, netlabs.org.
16 * Portions created by the Initial Developer are Copyright (C) 2006
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the LGPL are applicable instead of those
24 * above. If you wish to allow use of your version of this file only under the
25 * terms of the LGPL, and not to allow others to use your version of this file
26 * under the terms of the CDDL, indicate your decision by deleting the
27 * provisions above and replace them with the notice and other provisions
28 * required by the LGPL. If you do not delete the provisions above, a recipient
29 * may use your version of this file under the terms of any one of the CDDL
30 * or the LGPL.
31 *
32 * ***** END LICENSE BLOCK ***** */
33
34
35#include <os2.h>
36#include <uconv.h>
37
38
39// Converts special non-ascii chars to suitable ascii chars
40extern "C" VOID APIENTRY uniConvertSpChars( UniChar *uni )
41{
42    while ( *uni )
43    {
44        switch ( *uni )
45        {
46            case 0x2018:
47            case 0x2019:
48            case 0x2032:
49                *uni = 0x0027; // '
50                break;
51            case 0x201C:
52            case 0x201D:
53            case 0x00AB:
54            case 0x00BB:
55                *uni = 0x0022; // "
56                break;
57            case 0x2013:
58            case 0x2014:
59                *uni = 0x002D; // -
60                break;
61        }
62        *uni++;
63    }
64}
65
66
67// Ligatures table
68struct Ligature { UniChar unicode;  wchar_t *equivalent;  int equivalentLength; };
69
70// Table from [http://en.wikipedia.org/wiki/Ligature_(typography)#Unicode]
71#define NUM_LIGATURES   38
72static Ligature ligatures[ NUM_LIGATURES ] = {
73    { 0x00DF, L"fs",  2 }, { 0x00E6, L"AE",  2 }, { 0x00C6, L"ae",  2 },
74    { 0x0152, L"OE",  2 }, { 0x0153, L"oe",  2 }, { 0x0276, L"oe",  2 },
75    { 0x0132, L"IJ",  2 }, { 0x0133, L"ij",  2 }, { 0x014A, L"Ng",  2 },
76    { 0x014B, L"ng",  2 }, { 0x01F6, L"Hv",  2 }, { 0x0195, L"hv",  2 },
77    { 0x01C4, L"DZ",  2 }, { 0x01C5, L"Dz",  2 }, { 0x01C6, L"dz",  2 },
78    { 0x01C7, L"LJ",  2 }, { 0x01C8, L"Lj",  2 }, { 0x01C9, L"lj",  2 },
79    { 0x01CA, L"NJ",  2 }, { 0x01CB, L"Nj",  2 }, { 0x01CC, L"nj",  2 },
80    { 0x01F1, L"DZ",  2 }, { 0x01F2, L"Dz",  2 }, { 0x01F3, L"dz",  2 },
81    { 0x02A3, L"dz",  2 }, { 0x02A6, L"ts",  2 }, { 0x02A9, L"fng", 3 },
82    { 0x02AA, L"ls",  2 }, { 0x02AB, L"lz",  2 }, { 0x02AC, L"ww",  2 },
83    { 0x1D6B, L"ue",  2 }, { 0xFB00, L"ff",  2 }, { 0xFB01, L"fi",  2 },
84    { 0xFB02, L"fl",  2 }, { 0xFB03, L"ffi", 3 }, { 0xFB04, L"ffl", 3 },
85    { 0xFB05, L"ft",  2 }, { 0xFB06, L"st",  2 }
86};
87
88// If unichar is ligature - returns number of additional chars
89// which replaces the ligature, zero otherwise.
90inline int isLigature( UniChar ch )
91{
92    for ( int i = 0; i < NUM_LIGATURES; i++ ) {
93        if ( ch == ligatures[ i ].unicode ) {
94            return ligatures[ i ].equivalentLength - 1;
95        }
96    }
97    return 0;
98}
99
100// If unichar is ligature - returns pointer to struct Ligature
101// which contains replacement for ligature, NULL otherwise.
102inline Ligature *getReplLigature( UniChar ch )
103{
104    for ( int i = 0; i < NUM_LIGATURES; i++ ) {
105        if ( ch == ligatures[ i ].unicode ) {
106            return &( ligatures[ i ] );
107        }
108    }
109    return NULL;
110}
111
112// Return number of chars which should be added to string
113// length to fit the string with converted ligatures.
114// If no ligatures in string - returns zero.
115extern "C" LONG APIENTRY uniLigaturesLength( UniChar *str )
116{
117    LONG llen = 0;
118    while ( *str != 0 ) {
119        llen += isLigature( *str++ );
120    }
121    return llen;
122}
123
124// replaces ligatures in src into dst
125// src remains unchanged
126extern "C" VOID APIENTRY uniReplaceLigatures( UniChar *src, UniChar *dst )
127{
128    while ( *src != 0 )
129    {
130        Ligature *lig = getReplLigature( *src );
131        if ( lig == NULL ) {
132            *dst++ = *src++;
133        }
134        else {
135            for ( int i = 0; i < lig->equivalentLength; i++ ) {
136                *dst++ = lig->equivalent[ i ];
137            }
138            *src++;
139        }
140    }
141}
142
Note: See TracBrowser for help on using the repository browser.