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
31 /***********************************************************************
32 * IsLegalDBCSChar for cp932/936/949/950/euc
35 int IsLegalDBCSChar_cp932( BYTE lead, BYTE trail )
37 return ( ( ( lead >= (BYTE)0x81 && lead <= (BYTE)0x9f ) ||
38 ( lead >= (BYTE)0xe0 && lead <= (BYTE)0xfc ) ) &&
39 ( ( trail >= (BYTE)0x40 && trail <= (BYTE)0x7e ) ||
40 ( trail >= (BYTE)0x80 && trail <= (BYTE)0xfc ) ) );
44 int IsLegalDBCSChar_cp936( BYTE lead, BYTE trail )
46 return ( ( lead >= (BYTE)0x81 && lead <= (BYTE)0xfe ) &&
47 ( trail >= (BYTE)0x40 && trail <= (BYTE)0xfe ) );
51 int IsLegalDBCSChar_cp949( BYTE lead, BYTE trail )
53 return ( ( lead >= (BYTE)0x81 && lead <= (BYTE)0xfe ) &&
54 ( trail >= (BYTE)0x41 && trail <= (BYTE)0xfe ) );
58 int IsLegalDBCSChar_cp950( BYTE lead, BYTE trail )
60 return ( ( lead >= (BYTE)0x81 && lead <= (BYTE)0xfe ) &&
61 ( ( trail >= (BYTE)0x40 && trail <= (BYTE)0x7e ) ||
62 ( trail >= (BYTE)0xa1 && trail <= (BYTE)0xfe ) ) );
66 int IsLegalDBCSChar_euc( BYTE lead, BYTE trail )
68 return ( ( lead >= (BYTE)0xa1 && lead <= (BYTE)0xfe ) &&
69 ( trail >= (BYTE)0xa1 && trail <= (BYTE)0xfe ) );
73 /***********************************************************************
74 * DBCSCharToXChar2b for cp932/euc
78 void DBCSCharToXChar2b_cp932( XChar2b* pch, BYTE lead, BYTE trail )
80 unsigned int high, low;
82 high = (unsigned int)lead;
83 low = (unsigned int)trail;
86 high = (high<<1) - 0xe0;
88 high = (high<<1) - 0x160;
102 pch->byte1 = (unsigned char)high;
103 pch->byte2 = (unsigned char)low;
107 void DBCSCharToXChar2b_euc( XChar2b* pch, BYTE lead, BYTE trail )
109 pch->byte1 = lead & (BYTE)0x7f;
110 pch->byte2 = trail & (BYTE)0x7f;
116 static WORD X11DRV_enum_subfont_charset_normal( UINT index )
118 return DEFAULT_CHARSET;
121 static WORD X11DRV_enum_subfont_charset_cp932( UINT index )
125 case 0: return X11FONT_JISX0201_CHARSET;
126 case 1: return X11FONT_JISX0212_CHARSET;
129 return DEFAULT_CHARSET;
132 static WORD X11DRV_enum_subfont_charset_cp936( UINT index )
136 case 0: return ANSI_CHARSET;
139 return DEFAULT_CHARSET;
142 static WORD X11DRV_enum_subfont_charset_cp949( UINT index )
146 case 0: return ANSI_CHARSET;
149 return DEFAULT_CHARSET;
152 static WORD X11DRV_enum_subfont_charset_cp950( UINT index )
156 case 0: return ANSI_CHARSET;
159 return DEFAULT_CHARSET;
163 static XChar2b* X11DRV_unicode_to_char2b_sbcs( fontObject* pfo,
164 LPCWSTR lpwstr, UINT count )
169 UINT codepage = pfo->fi->codepage;
170 char ch = pfo->fs->default_char;
172 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
174 if (!(str = HeapAlloc( GetProcessHeap(), 0, count )))
176 HeapFree( GetProcessHeap(), 0, str2b );
180 WideCharToMultiByte( codepage, 0, lpwstr, count, str, count, &ch, NULL );
182 for (i = 0; i < count; i++)
185 str2b[i].byte2 = str[i];
187 HeapFree( GetProcessHeap(), 0, str );
192 static XChar2b* X11DRV_unicode_to_char2b_unicode( fontObject* pfo,
193 LPCWSTR lpwstr, UINT count )
198 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
201 for (i = 0; i < count; i++)
203 str2b[i].byte1 = lpwstr[i] >> 8;
204 str2b[i].byte2 = lpwstr[i] & 0xff;
210 /* FIXME: handle jisx0212.1990... */
211 static XChar2b* X11DRV_unicode_to_char2b_cp932( fontObject* pfo,
212 LPCWSTR lpwstr, UINT count )
219 char ch = pfo->fs->default_char;
221 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
223 if (!(str = HeapAlloc( GetProcessHeap(), 0, count*2 )))
225 HeapFree( GetProcessHeap(), 0, str2b );
229 /* handle jisx0212.1990... */
230 WideCharToMultiByte( 932, 0, lpwstr, count, str, count*2, &ch, NULL );
232 str_src = (BYTE*) str;
234 for (i = 0; i < count; i++, str_src++, str2b_dst++)
236 if ( IsLegalDBCSChar_cp932( *str_src, *(str_src+1) ) )
238 DBCSCharToXChar2b_cp932( str2b_dst, *str_src, *(str_src+1) );
243 str2b_dst->byte1 = 0;
244 str2b_dst->byte2 = *str_src;
248 HeapFree( GetProcessHeap(), 0, str );
254 static XChar2b* X11DRV_unicode_to_char2b_cp936( fontObject* pfo,
255 LPCWSTR lpwstr, UINT count )
262 char ch = pfo->fs->default_char;
264 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
266 if (!(str = HeapAlloc( GetProcessHeap(), 0, count*2 )))
268 HeapFree( GetProcessHeap(), 0, str2b );
271 WideCharToMultiByte( 936, 0, lpwstr, count, str, count*2, &ch, NULL );
273 str_src = (BYTE*) str;
275 for (i = 0; i < count; i++, str_src++, str2b_dst++)
277 if ( IsLegalDBCSChar_cp936( *str_src, *(str_src+1) ) )
279 str2b_dst->byte1 = *str_src;
280 str2b_dst->byte2 = *(str_src+1);
285 str2b_dst->byte1 = 0;
286 str2b_dst->byte2 = *str_src;
290 HeapFree( GetProcessHeap(), 0, str );
295 static XChar2b* X11DRV_unicode_to_char2b_cp949( fontObject* pfo,
296 LPCWSTR lpwstr, UINT count )
303 char ch = pfo->fs->default_char;
305 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
307 if (!(str = HeapAlloc( GetProcessHeap(), 0, count*2 )))
309 HeapFree( GetProcessHeap(), 0, str2b );
312 WideCharToMultiByte( 949, 0, lpwstr, count, str, count*2, &ch, NULL );
314 str_src = (BYTE*) str;
316 for (i = 0; i < count; i++, str_src++, str2b_dst++)
318 if ( IsLegalDBCSChar_cp949( *str_src, *(str_src+1) ) )
320 str2b_dst->byte1 = *str_src;
321 str2b_dst->byte2 = *(str_src+1);
326 str2b_dst->byte1 = 0;
327 str2b_dst->byte2 = *str_src;
331 HeapFree( GetProcessHeap(), 0, str );
337 static XChar2b* X11DRV_unicode_to_char2b_cp950( fontObject* pfo,
338 LPCWSTR lpwstr, UINT count )
345 char ch = pfo->fs->default_char;
347 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
349 if (!(str = HeapAlloc( GetProcessHeap(), 0, count*2 )))
351 HeapFree( GetProcessHeap(), 0, str2b );
354 WideCharToMultiByte( 950, 0, lpwstr, count, str, count*2, &ch, NULL );
356 str_src = (BYTE*) str;
358 for (i = 0; i < count; i++, str_src++, str2b_dst++)
360 if ( IsLegalDBCSChar_cp950( *str_src, *(str_src+1) ) )
362 str2b_dst->byte1 = *str_src;
363 str2b_dst->byte2 = *(str_src+1);
368 str2b_dst->byte1 = 0;
369 str2b_dst->byte2 = *str_src;
373 HeapFree( GetProcessHeap(), 0, str );
378 static XChar2b* X11DRV_unicode_to_char2b_symbol( fontObject* pfo,
379 LPCWSTR lpwstr, UINT count )
383 char ch = pfo->fs->default_char;
385 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
388 for (i = 0; i < count; i++)
391 if(lpwstr[i] >= 0xf000 && lpwstr[i] < 0xf100)
392 str2b[i].byte2 = lpwstr[i] - 0xf000;
393 else if(lpwstr[i] < 0x100)
394 str2b[i].byte2 = lpwstr[i];
403 static void X11DRV_DrawString_normal( fontObject* pfo, Display* pdisp,
404 Drawable d, GC gc, int x, int y,
405 XChar2b* pstr, int count )
408 XDrawString16( pdisp, d, gc, x, y, pstr, count );
412 static int X11DRV_TextWidth_normal( fontObject* pfo, XChar2b* pstr, int count )
416 ret = XTextWidth16( pfo->fs, pstr, count );
421 static void X11DRV_DrawText_normal( fontObject* pfo, Display* pdisp, Drawable d,
422 GC gc, int x, int y, XTextItem16* pitems,
426 XDrawText16( pdisp, d, gc, x, y, pitems, count );
430 static void X11DRV_TextExtents_normal( fontObject* pfo, XChar2b* pstr, int count,
431 int* pdir, int* pascent, int* pdescent,
432 int* pwidth, int max_extent, int* pfit,
433 int* partial_extents )
436 int ascent, descent, width;
444 for ( i = 0; i < count; i++ )
446 XTextExtents16( pfo->fs, pstr, 1, pdir, &ascent, &descent, &info );
447 if ( *pascent < ascent ) *pascent = ascent;
448 if ( *pdescent < descent ) *pdescent = descent;
450 if ( partial_extents ) partial_extents[i] = width;
451 if ( width < max_extent ) fit++;
457 if ( pfit ) *pfit = fit;
460 static void X11DRV_GetTextMetricsW_normal( fontObject* pfo, LPTEXTMETRICW pTM )
462 LPIFONTINFO16 pdf = &pfo->fi->df;
464 if( ! pfo->lpX11Trans ) {
465 pTM->tmAscent = pfo->fs->ascent;
466 pTM->tmDescent = pfo->fs->descent;
468 pTM->tmAscent = pfo->lpX11Trans->ascent;
469 pTM->tmDescent = pfo->lpX11Trans->descent;
472 pTM->tmAscent *= pfo->rescale;
473 pTM->tmDescent *= pfo->rescale;
475 pTM->tmHeight = pTM->tmAscent + pTM->tmDescent;
477 pTM->tmAveCharWidth = pfo->foAvgCharWidth * pfo->rescale;
478 pTM->tmMaxCharWidth = pfo->foMaxCharWidth * pfo->rescale;
480 pTM->tmInternalLeading = pfo->foInternalLeading * pfo->rescale;
481 pTM->tmExternalLeading = pdf->dfExternalLeading * pfo->rescale;
483 pTM->tmStruckOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT )
484 ? 1 : pdf->dfStrikeOut;
485 pTM->tmUnderlined = (pfo->fo_flags & FO_SYNTH_UNDERLINE )
486 ? 1 : pdf->dfUnderline;
489 if( pfo->fo_flags & FO_SYNTH_ITALIC )
491 pTM->tmOverhang += pTM->tmHeight/3;
494 pTM->tmItalic = pdf->dfItalic;
496 pTM->tmWeight = pdf->dfWeight;
497 if( pfo->fo_flags & FO_SYNTH_BOLD )
500 pTM->tmWeight += 100;
503 pTM->tmFirstChar = pdf->dfFirstChar;
504 pTM->tmLastChar = pdf->dfLastChar;
505 pTM->tmDefaultChar = pdf->dfDefaultChar;
506 pTM->tmBreakChar = pdf->dfBreakChar;
508 pTM->tmCharSet = pdf->dfCharSet;
509 pTM->tmPitchAndFamily = pdf->dfPitchAndFamily;
511 pTM->tmDigitizedAspectX = pdf->dfHorizRes;
512 pTM->tmDigitizedAspectY = pdf->dfVertRes;
518 void X11DRV_DrawString_dbcs( fontObject* pfo, Display* pdisp,
519 Drawable d, GC gc, int x, int y,
520 XChar2b* pstr, int count )
528 X11DRV_cptable[pfo->fi->cptable].pDrawText(
529 pfo, pdisp, d, gc, x, y, &item, 1 );
533 int X11DRV_TextWidth_dbcs_2fonts( fontObject* pfo, XChar2b* pstr, int count )
538 fontObject* pfos[X11FONT_REFOBJS_MAX+1];
540 pfos[0] = XFONT_GetFontObject( pfo->prefobjs[0] );
542 if ( pfos[0] == NULL ) pfos[0] = pfo;
546 for ( i = 0; i < count; i++ )
548 curfont = ( pstr->byte1 != 0 ) ? 1 : 0;
549 width += XTextWidth16( pfos[curfont]->fs, pstr, 1 );
557 void X11DRV_DrawText_dbcs_2fonts( fontObject* pfo, Display* pdisp, Drawable d,
558 GC gc, int x, int y, XTextItem16* pitems,
561 int i, nitems, prevfont = -1, curfont;
565 fontObject* pfos[X11FONT_REFOBJS_MAX+1];
567 pfos[0] = XFONT_GetFontObject( pfo->prefobjs[0] );
569 if ( pfos[0] == NULL ) pfos[0] = pfo;
572 for ( i = 0; i < count; i++ )
573 nitems += pitems->nchars;
574 ptibuf = HeapAlloc( GetProcessHeap(), 0, sizeof(XTextItem16) * nitems );
575 if ( ptibuf == NULL )
576 return; /* out of memory */
579 while ( count-- > 0 )
581 pti->chars = pstr = pitems->chars;
582 pti->delta = pitems->delta;
584 for ( i = 0; i < pitems->nchars; i++, pstr++ )
586 curfont = ( pstr->byte1 != 0 ) ? 1 : 0;
587 if ( curfont != prevfont )
589 if ( pstr != pti->chars )
591 pti->nchars = pstr - pti->chars;
596 pti->font = pfos[curfont]->fs->fid;
600 pti->nchars = pstr - pti->chars;
604 XDrawText16( pdisp, d, gc, x, y, ptibuf, pti - ptibuf );
606 HeapFree( GetProcessHeap(), 0, ptibuf );
610 void X11DRV_TextExtents_dbcs_2fonts( fontObject* pfo, XChar2b* pstr, int count,
611 int* pdir, int* pascent, int* pdescent,
612 int* pwidth, int max_extent, int* pfit,
613 int* partial_extents )
616 int ascent, descent, width;
620 fontObject* pfos[X11FONT_REFOBJS_MAX+1];
622 pfos[0] = XFONT_GetFontObject( pfo->prefobjs[0] );
624 if ( pfos[0] == NULL ) pfos[0] = pfo;
631 for ( i = 0; i < count; i++ )
633 curfont = ( pstr->byte1 != 0 ) ? 1 : 0;
634 XTextExtents16( pfos[curfont]->fs, pstr, 1, pdir, &ascent, &descent, &info );
635 if ( *pascent < ascent ) *pascent = ascent;
636 if ( *pdescent < descent ) *pdescent = descent;
638 if ( partial_extents ) partial_extents[i] = width;
639 if ( width <= max_extent ) fit++;
645 if ( pfit ) *pfit = fit;
648 static void X11DRV_GetTextMetricsW_cp932( fontObject* pfo, LPTEXTMETRICW pTM )
650 fontObject* pfo_ansi = XFONT_GetFontObject( pfo->prefobjs[0] );
651 LPIFONTINFO16 pdf = &pfo->fi->df;
652 LPIFONTINFO16 pdf_ansi;
654 pdf_ansi = ( pfo_ansi != NULL ) ? (&pfo_ansi->fi->df) : pdf;
656 if( ! pfo->lpX11Trans ) {
657 pTM->tmAscent = pfo->fs->ascent;
658 pTM->tmDescent = pfo->fs->descent;
660 pTM->tmAscent = pfo->lpX11Trans->ascent;
661 pTM->tmDescent = pfo->lpX11Trans->descent;
664 pTM->tmAscent *= pfo->rescale;
665 pTM->tmDescent *= pfo->rescale;
667 pTM->tmHeight = pTM->tmAscent + pTM->tmDescent;
669 if ( pfo_ansi != NULL )
671 pTM->tmAveCharWidth = floor((pfo_ansi->foAvgCharWidth * 2.0 + pfo->foAvgCharWidth) / 3.0 * pfo->rescale + 0.5);
672 pTM->tmMaxCharWidth = max(pfo_ansi->foMaxCharWidth, pfo->foMaxCharWidth) * pfo->rescale;
676 pTM->tmAveCharWidth = floor((pfo->foAvgCharWidth * pfo->rescale + 1.0) / 2.0);
677 pTM->tmMaxCharWidth = pfo->foMaxCharWidth * pfo->rescale;
680 pTM->tmInternalLeading = pfo->foInternalLeading * pfo->rescale;
681 pTM->tmExternalLeading = pdf->dfExternalLeading * pfo->rescale;
683 pTM->tmStruckOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT )
684 ? 1 : pdf->dfStrikeOut;
685 pTM->tmUnderlined = (pfo->fo_flags & FO_SYNTH_UNDERLINE )
686 ? 1 : pdf->dfUnderline;
689 if( pfo->fo_flags & FO_SYNTH_ITALIC )
691 pTM->tmOverhang += pTM->tmHeight/3;
694 pTM->tmItalic = pdf->dfItalic;
696 pTM->tmWeight = pdf->dfWeight;
697 if( pfo->fo_flags & FO_SYNTH_BOLD )
700 pTM->tmWeight += 100;
703 pTM->tmFirstChar = pdf_ansi->dfFirstChar;
704 pTM->tmLastChar = pdf_ansi->dfLastChar;
705 pTM->tmDefaultChar = pdf_ansi->dfDefaultChar;
706 pTM->tmBreakChar = pdf_ansi->dfBreakChar;
708 pTM->tmCharSet = pdf->dfCharSet;
709 pTM->tmPitchAndFamily = pdf->dfPitchAndFamily;
711 pTM->tmDigitizedAspectX = pdf->dfHorizRes;
712 pTM->tmDigitizedAspectY = pdf->dfVertRes;
719 const X11DRV_CP X11DRV_cptable[X11DRV_CPTABLE_COUNT] =
722 X11DRV_enum_subfont_charset_normal,
723 X11DRV_unicode_to_char2b_sbcs,
724 X11DRV_DrawString_normal,
725 X11DRV_TextWidth_normal,
726 X11DRV_DrawText_normal,
727 X11DRV_TextExtents_normal,
728 X11DRV_GetTextMetricsW_normal,
731 X11DRV_enum_subfont_charset_normal,
732 X11DRV_unicode_to_char2b_unicode,
733 X11DRV_DrawString_normal,
734 X11DRV_TextWidth_normal,
735 X11DRV_DrawText_normal,
736 X11DRV_TextExtents_normal,
737 X11DRV_GetTextMetricsW_normal,
740 X11DRV_enum_subfont_charset_cp932,
741 X11DRV_unicode_to_char2b_cp932,
742 X11DRV_DrawString_dbcs,
743 X11DRV_TextWidth_dbcs_2fonts,
744 X11DRV_DrawText_dbcs_2fonts,
745 X11DRV_TextExtents_dbcs_2fonts,
746 X11DRV_GetTextMetricsW_cp932,
749 X11DRV_enum_subfont_charset_cp936,
750 X11DRV_unicode_to_char2b_cp936,
751 X11DRV_DrawString_dbcs,
752 X11DRV_TextWidth_dbcs_2fonts,
753 X11DRV_DrawText_dbcs_2fonts,
754 X11DRV_TextExtents_dbcs_2fonts,
755 X11DRV_GetTextMetricsW_normal, /* FIXME */
758 X11DRV_enum_subfont_charset_cp949,
759 X11DRV_unicode_to_char2b_cp949,
760 X11DRV_DrawString_dbcs,
761 X11DRV_TextWidth_dbcs_2fonts,
762 X11DRV_DrawText_dbcs_2fonts,
763 X11DRV_TextExtents_dbcs_2fonts,
764 X11DRV_GetTextMetricsW_normal, /* FIXME */
767 X11DRV_enum_subfont_charset_cp950,
768 X11DRV_unicode_to_char2b_cp950,
769 X11DRV_DrawString_dbcs,
770 X11DRV_TextWidth_dbcs_2fonts,
771 X11DRV_DrawText_dbcs_2fonts,
772 X11DRV_TextExtents_dbcs_2fonts,
773 X11DRV_GetTextMetricsW_cp932,
776 X11DRV_enum_subfont_charset_normal,
777 X11DRV_unicode_to_char2b_symbol,
778 X11DRV_DrawString_normal,
779 X11DRV_TextWidth_normal,
780 X11DRV_DrawText_normal,
781 X11DRV_TextExtents_normal,
782 X11DRV_GetTextMetricsW_normal,