2 * X11 codepage handling
4 * Copyright 2000 Hidenori Takeshima <hidenori@a2.ctktv.ne.jp>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(text);
34 /***********************************************************************
35 * IsLegalDBCSChar for cp932/936/949/950/euc
38 int IsLegalDBCSChar_cp932( BYTE lead, BYTE trail )
40 return ( ( ( lead >= (BYTE)0x81 && lead <= (BYTE)0x9f ) ||
41 ( lead >= (BYTE)0xe0 && lead <= (BYTE)0xfc ) ) &&
42 ( ( trail >= (BYTE)0x40 && trail <= (BYTE)0x7e ) ||
43 ( trail >= (BYTE)0x80 && trail <= (BYTE)0xfc ) ) );
47 int IsLegalDBCSChar_cp936( BYTE lead, BYTE trail )
49 return ( ( lead >= (BYTE)0x81 && lead <= (BYTE)0xfe ) &&
50 ( trail >= (BYTE)0x40 && trail <= (BYTE)0xfe ) );
54 int IsLegalDBCSChar_cp949( BYTE lead, BYTE trail )
56 return ( ( lead >= (BYTE)0x81 && lead <= (BYTE)0xfe ) &&
57 ( trail >= (BYTE)0x41 && trail <= (BYTE)0xfe ) );
61 int IsLegalDBCSChar_cp950( BYTE lead, BYTE trail )
63 return ( ( lead >= (BYTE)0x81 && lead <= (BYTE)0xfe ) &&
64 ( ( trail >= (BYTE)0x40 && trail <= (BYTE)0x7e ) ||
65 ( trail >= (BYTE)0xa1 && trail <= (BYTE)0xfe ) ) );
69 int IsLegalDBCSChar_euc( BYTE lead, BYTE trail )
71 return ( ( lead >= (BYTE)0xa1 && lead <= (BYTE)0xfe ) &&
72 ( trail >= (BYTE)0xa1 && trail <= (BYTE)0xfe ) );
76 /***********************************************************************
77 * DBCSCharToXChar2b for cp932/euc
81 void DBCSCharToXChar2b_cp932( XChar2b* pch, BYTE lead, BYTE trail )
83 unsigned int high, low;
85 high = (unsigned int)lead;
86 low = (unsigned int)trail;
89 high = (high<<1) - 0xe0;
91 high = (high<<1) - 0x160;
105 pch->byte1 = (unsigned char)high;
106 pch->byte2 = (unsigned char)low;
110 void DBCSCharToXChar2b_euc( XChar2b* pch, BYTE lead, BYTE trail )
112 pch->byte1 = lead & (BYTE)0x7f;
113 pch->byte2 = trail & (BYTE)0x7f;
119 static WORD X11DRV_enum_subfont_charset_normal( UINT index )
121 return DEFAULT_CHARSET;
124 static WORD X11DRV_enum_subfont_charset_cp932( UINT index )
128 case 0: return X11FONT_JISX0201_CHARSET;
129 case 1: return X11FONT_JISX0212_CHARSET;
132 return DEFAULT_CHARSET;
135 static WORD X11DRV_enum_subfont_charset_cp936( UINT index )
139 case 0: return ANSI_CHARSET;
142 return DEFAULT_CHARSET;
145 static WORD X11DRV_enum_subfont_charset_cp949( UINT index )
149 case 0: return ANSI_CHARSET;
152 return DEFAULT_CHARSET;
155 static WORD X11DRV_enum_subfont_charset_cp950( UINT index )
159 case 0: return ANSI_CHARSET;
162 return DEFAULT_CHARSET;
166 static XChar2b* X11DRV_unicode_to_char2b_sbcs( fontObject* pfo,
167 LPCWSTR lpwstr, UINT count )
172 UINT codepage = pfo->fi->codepage;
173 char ch = pfo->fs->default_char;
175 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
177 if (!(str = HeapAlloc( GetProcessHeap(), 0, count )))
179 HeapFree( GetProcessHeap(), 0, str2b );
183 WideCharToMultiByte( codepage, 0, lpwstr, count, str, count, &ch, NULL );
185 for (i = 0; i < count; i++)
188 str2b[i].byte2 = str[i];
190 HeapFree( GetProcessHeap(), 0, str );
195 static XChar2b* X11DRV_unicode_to_char2b_unicode( fontObject* pfo,
196 LPCWSTR lpwstr, UINT count )
201 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
204 for (i = 0; i < count; i++)
206 str2b[i].byte1 = lpwstr[i] >> 8;
207 str2b[i].byte2 = lpwstr[i] & 0xff;
213 /* FIXME: handle jisx0212.1990... */
214 static XChar2b* X11DRV_unicode_to_char2b_cp932( fontObject* pfo,
215 LPCWSTR lpwstr, UINT count )
222 char ch = pfo->fs->default_char;
224 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
226 if (!(str = HeapAlloc( GetProcessHeap(), 0, count*2 )))
228 HeapFree( GetProcessHeap(), 0, str2b );
232 /* handle jisx0212.1990... */
233 WideCharToMultiByte( 932, 0, lpwstr, count, str, count*2, &ch, NULL );
235 str_src = (BYTE*) str;
237 for (i = 0; i < count; i++, str_src++, str2b_dst++)
239 if ( IsLegalDBCSChar_cp932( *str_src, *(str_src+1) ) )
241 DBCSCharToXChar2b_cp932( str2b_dst, *str_src, *(str_src+1) );
246 str2b_dst->byte1 = 0;
247 str2b_dst->byte2 = *str_src;
251 HeapFree( GetProcessHeap(), 0, str );
257 static XChar2b* X11DRV_unicode_to_char2b_cp936( fontObject* pfo,
258 LPCWSTR lpwstr, UINT count )
265 char ch = pfo->fs->default_char;
267 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
269 if (!(str = HeapAlloc( GetProcessHeap(), 0, count*2 )))
271 HeapFree( GetProcessHeap(), 0, str2b );
274 WideCharToMultiByte( 936, 0, lpwstr, count, str, count*2, &ch, NULL );
276 str_src = (BYTE*) str;
278 for (i = 0; i < count; i++, str_src++, str2b_dst++)
280 if ( IsLegalDBCSChar_cp936( *str_src, *(str_src+1) ) )
282 str2b_dst->byte1 = *str_src;
283 str2b_dst->byte2 = *(str_src+1);
288 str2b_dst->byte1 = 0;
289 str2b_dst->byte2 = *str_src;
293 HeapFree( GetProcessHeap(), 0, str );
298 static XChar2b* X11DRV_unicode_to_char2b_cp949( fontObject* pfo,
299 LPCWSTR lpwstr, UINT count )
306 char ch = pfo->fs->default_char;
308 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
310 if (!(str = HeapAlloc( GetProcessHeap(), 0, count*2 )))
312 HeapFree( GetProcessHeap(), 0, str2b );
315 WideCharToMultiByte( 949, 0, lpwstr, count, str, count*2, &ch, NULL );
317 str_src = (BYTE*) str;
319 for (i = 0; i < count; i++, str_src++, str2b_dst++)
321 if ( IsLegalDBCSChar_cp949( *str_src, *(str_src+1) ) )
323 str2b_dst->byte1 = *str_src;
324 str2b_dst->byte2 = *(str_src+1);
329 str2b_dst->byte1 = 0;
330 str2b_dst->byte2 = *str_src;
334 HeapFree( GetProcessHeap(), 0, str );
340 static XChar2b* X11DRV_unicode_to_char2b_cp950( fontObject* pfo,
341 LPCWSTR lpwstr, UINT count )
348 char ch = pfo->fs->default_char;
350 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
352 if (!(str = HeapAlloc( GetProcessHeap(), 0, count*2 )))
354 HeapFree( GetProcessHeap(), 0, str2b );
357 WideCharToMultiByte( 950, 0, lpwstr, count, str, count*2, &ch, NULL );
359 str_src = (BYTE*) str;
361 for (i = 0; i < count; i++, str_src++, str2b_dst++)
363 if ( IsLegalDBCSChar_cp950( *str_src, *(str_src+1) ) )
365 str2b_dst->byte1 = *str_src;
366 str2b_dst->byte2 = *(str_src+1);
371 str2b_dst->byte1 = 0;
372 str2b_dst->byte2 = *str_src;
376 HeapFree( GetProcessHeap(), 0, str );
381 static XChar2b* X11DRV_unicode_to_char2b_symbol( fontObject* pfo,
382 LPCWSTR lpwstr, UINT count )
386 char ch = pfo->fs->default_char;
388 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
391 for (i = 0; i < count; i++)
394 if(lpwstr[i] >= 0xf000 && lpwstr[i] < 0xf100)
395 str2b[i].byte2 = lpwstr[i] - 0xf000;
396 else if(lpwstr[i] < 0x100)
397 str2b[i].byte2 = lpwstr[i];
406 static void X11DRV_DrawString_normal( fontObject* pfo, Display* pdisp,
407 Drawable d, GC gc, int x, int y,
408 XChar2b* pstr, int count )
411 XDrawString16( pdisp, d, gc, x, y, pstr, count );
415 static int X11DRV_TextWidth_normal( fontObject* pfo, XChar2b* pstr, int count )
419 ret = XTextWidth16( pfo->fs, pstr, count );
424 static void X11DRV_DrawText_normal( fontObject* pfo, Display* pdisp, Drawable d,
425 GC gc, int x, int y, XTextItem16* pitems,
429 XDrawText16( pdisp, d, gc, x, y, pitems, count );
433 static void X11DRV_TextExtents_normal( fontObject* pfo, XChar2b* pstr, int count,
434 int* pdir, int* pascent, int* pdescent,
435 int* pwidth, int max_extent, int* pfit,
436 int* partial_extents )
439 int ascent, descent, width;
447 for ( i = 0; i < count; i++ )
449 XTextExtents16( pfo->fs, pstr, 1, pdir, &ascent, &descent, &info );
450 if ( *pascent < ascent ) *pascent = ascent;
451 if ( *pdescent < descent ) *pdescent = descent;
453 if ( partial_extents ) partial_extents[i] = width;
454 if ( width < max_extent ) fit++;
460 if ( pfit ) *pfit = fit;
463 static void X11DRV_GetTextMetricsW_normal( fontObject* pfo, LPTEXTMETRICW pTM )
465 LPIFONTINFO16 pdf = &pfo->fi->df;
467 if( ! pfo->lpX11Trans ) {
468 pTM->tmAscent = pfo->fs->ascent;
469 pTM->tmDescent = pfo->fs->descent;
471 pTM->tmAscent = pfo->lpX11Trans->ascent;
472 pTM->tmDescent = pfo->lpX11Trans->descent;
475 pTM->tmAscent *= pfo->rescale;
476 pTM->tmDescent *= pfo->rescale;
478 pTM->tmHeight = pTM->tmAscent + pTM->tmDescent;
480 pTM->tmAveCharWidth = pfo->foAvgCharWidth * pfo->rescale;
481 pTM->tmMaxCharWidth = pfo->foMaxCharWidth * pfo->rescale;
483 pTM->tmInternalLeading = pfo->foInternalLeading * pfo->rescale;
484 pTM->tmExternalLeading = pdf->dfExternalLeading * pfo->rescale;
486 pTM->tmStruckOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT )
487 ? 1 : pdf->dfStrikeOut;
488 pTM->tmUnderlined = (pfo->fo_flags & FO_SYNTH_UNDERLINE )
489 ? 1 : pdf->dfUnderline;
492 if( pfo->fo_flags & FO_SYNTH_ITALIC )
494 pTM->tmOverhang += pTM->tmHeight/3;
497 pTM->tmItalic = pdf->dfItalic;
499 pTM->tmWeight = pdf->dfWeight;
500 if( pfo->fo_flags & FO_SYNTH_BOLD )
503 pTM->tmWeight += 100;
506 pTM->tmFirstChar = pdf->dfFirstChar;
507 pTM->tmLastChar = pdf->dfLastChar;
508 pTM->tmDefaultChar = pdf->dfDefaultChar;
509 pTM->tmBreakChar = pdf->dfBreakChar;
511 pTM->tmCharSet = pdf->dfCharSet;
512 pTM->tmPitchAndFamily = pdf->dfPitchAndFamily;
514 pTM->tmDigitizedAspectX = pdf->dfHorizRes;
515 pTM->tmDigitizedAspectY = pdf->dfVertRes;
521 void X11DRV_DrawString_dbcs( fontObject* pfo, Display* pdisp,
522 Drawable d, GC gc, int x, int y,
523 XChar2b* pstr, int count )
531 X11DRV_cptable[pfo->fi->cptable].pDrawText(
532 pfo, pdisp, d, gc, x, y, &item, 1 );
536 int X11DRV_TextWidth_dbcs_2fonts( fontObject* pfo, XChar2b* pstr, int count )
541 fontObject* pfos[X11FONT_REFOBJS_MAX+1];
543 pfos[0] = XFONT_GetFontObject( pfo->prefobjs[0] );
545 if ( pfos[0] == NULL ) pfos[0] = pfo;
549 for ( i = 0; i < count; i++ )
551 curfont = ( pstr->byte1 != 0 ) ? 1 : 0;
552 width += XTextWidth16( pfos[curfont]->fs, pstr, 1 );
560 void X11DRV_DrawText_dbcs_2fonts( fontObject* pfo, Display* pdisp, Drawable d,
561 GC gc, int x, int y, XTextItem16* pitems,
564 int i, nitems, prevfont = -1, curfont;
568 fontObject* pfos[X11FONT_REFOBJS_MAX+1];
570 pfos[0] = XFONT_GetFontObject( pfo->prefobjs[0] );
572 if ( pfos[0] == NULL ) pfos[0] = pfo;
575 for ( i = 0; i < count; i++ )
576 nitems += pitems->nchars;
577 ptibuf = HeapAlloc( GetProcessHeap(), 0, sizeof(XTextItem16) * nitems );
578 if ( ptibuf == NULL )
579 return; /* out of memory */
582 while ( count-- > 0 )
584 pti->chars = pstr = pitems->chars;
585 pti->delta = pitems->delta;
587 for ( i = 0; i < pitems->nchars; i++, pstr++ )
589 curfont = ( pstr->byte1 != 0 ) ? 1 : 0;
590 if ( curfont != prevfont )
592 if ( pstr != pti->chars )
594 pti->nchars = pstr - pti->chars;
599 pti->font = pfos[curfont]->fs->fid;
603 pti->nchars = pstr - pti->chars;
607 XDrawText16( pdisp, d, gc, x, y, ptibuf, pti - ptibuf );
609 HeapFree( GetProcessHeap(), 0, ptibuf );
613 void X11DRV_TextExtents_dbcs_2fonts( fontObject* pfo, XChar2b* pstr, int count,
614 int* pdir, int* pascent, int* pdescent,
615 int* pwidth, int max_extent, int* pfit,
616 int* partial_extents )
619 int ascent, descent, width;
623 fontObject* pfos[X11FONT_REFOBJS_MAX+1];
625 pfos[0] = XFONT_GetFontObject( pfo->prefobjs[0] );
627 if ( pfos[0] == NULL ) pfos[0] = pfo;
634 for ( i = 0; i < count; i++ )
636 curfont = ( pstr->byte1 != 0 ) ? 1 : 0;
637 XTextExtents16( pfos[curfont]->fs, pstr, 1, pdir, &ascent, &descent, &info );
638 if ( *pascent < ascent ) *pascent = ascent;
639 if ( *pdescent < descent ) *pdescent = descent;
641 if ( partial_extents ) partial_extents[i] = width;
642 if ( width <= max_extent ) fit++;
648 if ( pfit ) *pfit = fit;
651 static void X11DRV_GetTextMetricsW_cp932( fontObject* pfo, LPTEXTMETRICW pTM )
653 fontObject* pfo_ansi = XFONT_GetFontObject( pfo->prefobjs[0] );
654 LPIFONTINFO16 pdf = &pfo->fi->df;
655 LPIFONTINFO16 pdf_ansi;
657 pdf_ansi = ( pfo_ansi != NULL ) ? (&pfo_ansi->fi->df) : pdf;
659 if( ! pfo->lpX11Trans ) {
660 pTM->tmAscent = pfo->fs->ascent;
661 pTM->tmDescent = pfo->fs->descent;
663 pTM->tmAscent = pfo->lpX11Trans->ascent;
664 pTM->tmDescent = pfo->lpX11Trans->descent;
667 pTM->tmAscent *= pfo->rescale;
668 pTM->tmDescent *= pfo->rescale;
670 pTM->tmHeight = pTM->tmAscent + pTM->tmDescent;
672 if ( pfo_ansi != NULL )
674 pTM->tmAveCharWidth = floor((pfo_ansi->foAvgCharWidth * 2.0 + pfo->foAvgCharWidth) / 3.0 * pfo->rescale + 0.5);
675 pTM->tmMaxCharWidth = max(pfo_ansi->foMaxCharWidth, pfo->foMaxCharWidth) * pfo->rescale;
679 pTM->tmAveCharWidth = floor((pfo->foAvgCharWidth * pfo->rescale + 1.0) / 2.0);
680 pTM->tmMaxCharWidth = pfo->foMaxCharWidth * pfo->rescale;
683 pTM->tmInternalLeading = pfo->foInternalLeading * pfo->rescale;
684 pTM->tmExternalLeading = pdf->dfExternalLeading * pfo->rescale;
686 pTM->tmStruckOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT )
687 ? 1 : pdf->dfStrikeOut;
688 pTM->tmUnderlined = (pfo->fo_flags & FO_SYNTH_UNDERLINE )
689 ? 1 : pdf->dfUnderline;
692 if( pfo->fo_flags & FO_SYNTH_ITALIC )
694 pTM->tmOverhang += pTM->tmHeight/3;
697 pTM->tmItalic = pdf->dfItalic;
699 pTM->tmWeight = pdf->dfWeight;
700 if( pfo->fo_flags & FO_SYNTH_BOLD )
703 pTM->tmWeight += 100;
706 pTM->tmFirstChar = pdf_ansi->dfFirstChar;
707 pTM->tmLastChar = pdf_ansi->dfLastChar;
708 pTM->tmDefaultChar = pdf_ansi->dfDefaultChar;
709 pTM->tmBreakChar = pdf_ansi->dfBreakChar;
711 pTM->tmCharSet = pdf->dfCharSet;
712 pTM->tmPitchAndFamily = pdf->dfPitchAndFamily;
714 pTM->tmDigitizedAspectX = pdf->dfHorizRes;
715 pTM->tmDigitizedAspectY = pdf->dfVertRes;
722 const X11DRV_CP X11DRV_cptable[X11DRV_CPTABLE_COUNT] =
725 X11DRV_enum_subfont_charset_normal,
726 X11DRV_unicode_to_char2b_sbcs,
727 X11DRV_DrawString_normal,
728 X11DRV_TextWidth_normal,
729 X11DRV_DrawText_normal,
730 X11DRV_TextExtents_normal,
731 X11DRV_GetTextMetricsW_normal,
734 X11DRV_enum_subfont_charset_normal,
735 X11DRV_unicode_to_char2b_unicode,
736 X11DRV_DrawString_normal,
737 X11DRV_TextWidth_normal,
738 X11DRV_DrawText_normal,
739 X11DRV_TextExtents_normal,
740 X11DRV_GetTextMetricsW_normal,
743 X11DRV_enum_subfont_charset_cp932,
744 X11DRV_unicode_to_char2b_cp932,
745 X11DRV_DrawString_dbcs,
746 X11DRV_TextWidth_dbcs_2fonts,
747 X11DRV_DrawText_dbcs_2fonts,
748 X11DRV_TextExtents_dbcs_2fonts,
749 X11DRV_GetTextMetricsW_cp932,
752 X11DRV_enum_subfont_charset_cp936,
753 X11DRV_unicode_to_char2b_cp936,
754 X11DRV_DrawString_dbcs,
755 X11DRV_TextWidth_dbcs_2fonts,
756 X11DRV_DrawText_dbcs_2fonts,
757 X11DRV_TextExtents_dbcs_2fonts,
758 X11DRV_GetTextMetricsW_normal, /* FIXME */
761 X11DRV_enum_subfont_charset_cp949,
762 X11DRV_unicode_to_char2b_cp949,
763 X11DRV_DrawString_dbcs,
764 X11DRV_TextWidth_dbcs_2fonts,
765 X11DRV_DrawText_dbcs_2fonts,
766 X11DRV_TextExtents_dbcs_2fonts,
767 X11DRV_GetTextMetricsW_normal, /* FIXME */
770 X11DRV_enum_subfont_charset_cp950,
771 X11DRV_unicode_to_char2b_cp950,
772 X11DRV_DrawString_dbcs,
773 X11DRV_TextWidth_dbcs_2fonts,
774 X11DRV_DrawText_dbcs_2fonts,
775 X11DRV_TextExtents_dbcs_2fonts,
776 X11DRV_GetTextMetricsW_cp932,
779 X11DRV_enum_subfont_charset_normal,
780 X11DRV_unicode_to_char2b_symbol,
781 X11DRV_DrawString_normal,
782 X11DRV_TextWidth_normal,
783 X11DRV_DrawText_normal,
784 X11DRV_TextExtents_normal,
785 X11DRV_GetTextMetricsW_normal,