2 * X11 codepage handling
4 * Copyright 2000 Hidenori Takeshima <hidenori@a2.ctktv.ne.jp>
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(text
);
20 /***********************************************************************
21 * IsLegalDBCSChar for cp932/936/949/950/euc
24 int IsLegalDBCSChar_cp932( BYTE lead
, BYTE trail
)
26 return ( ( ( lead
>= (BYTE
)0x81 && lead
<= (BYTE
)0x9f ) ||
27 ( lead
>= (BYTE
)0xe0 && lead
<= (BYTE
)0xfc ) ) &&
28 ( ( trail
>= (BYTE
)0x40 && trail
<= (BYTE
)0x7e ) ||
29 ( trail
>= (BYTE
)0x80 && trail
<= (BYTE
)0xfc ) ) );
33 int IsLegalDBCSChar_cp936( BYTE lead
, BYTE trail
)
35 return ( ( lead
>= (BYTE
)0x81 && lead
<= (BYTE
)0xfe ) &&
36 ( trail
>= (BYTE
)0x40 && trail
<= (BYTE
)0xfe ) );
40 int IsLegalDBCSChar_cp949( BYTE lead
, BYTE trail
)
42 return ( ( lead
>= (BYTE
)0x81 && lead
<= (BYTE
)0xfe ) &&
43 ( trail
>= (BYTE
)0x41 && trail
<= (BYTE
)0xfe ) );
47 int IsLegalDBCSChar_cp950( BYTE lead
, BYTE trail
)
49 return ( ( lead
>= (BYTE
)0x81 && lead
<= (BYTE
)0xfe ) &&
50 ( ( trail
>= (BYTE
)0x40 && trail
<= (BYTE
)0x7e ) ||
51 ( trail
>= (BYTE
)0xa1 && trail
<= (BYTE
)0xfe ) ) );
55 int IsLegalDBCSChar_euc( BYTE lead
, BYTE trail
)
57 return ( ( lead
>= (BYTE
)0xa1 && lead
<= (BYTE
)0xfe ) &&
58 ( trail
>= (BYTE
)0xa1 && trail
<= (BYTE
)0xfe ) );
62 /***********************************************************************
63 * DBCSCharToXChar2b for cp932/euc
67 void DBCSCharToXChar2b_cp932( XChar2b
* pch
, BYTE lead
, BYTE trail
)
69 unsigned int high
, low
;
71 high
= (unsigned int)lead
;
72 low
= (unsigned int)trail
;
75 high
= (high
<<1) - 0xe0;
77 high
= (high
<<1) - 0x160;
91 pch
->byte1
= (unsigned char)high
;
92 pch
->byte2
= (unsigned char)low
;
96 void DBCSCharToXChar2b_euc( XChar2b
* pch
, BYTE lead
, BYTE trail
)
98 pch
->byte1
= lead
& (BYTE
)0x7f;
99 pch
->byte2
= trail
& (BYTE
)0x7f;
105 static WORD
X11DRV_enum_subfont_charset_normal( UINT index
)
107 return DEFAULT_CHARSET
;
110 static WORD
X11DRV_enum_subfont_charset_cp932( UINT index
)
114 case 0: return X11FONT_JISX0201_CHARSET
;
115 case 1: return X11FONT_JISX0212_CHARSET
;
118 return DEFAULT_CHARSET
;
121 static WORD
X11DRV_enum_subfont_charset_cp936( UINT index
)
125 case 0: return ANSI_CHARSET
;
128 return DEFAULT_CHARSET
;
131 static WORD
X11DRV_enum_subfont_charset_cp949( UINT index
)
135 case 0: return ANSI_CHARSET
;
138 return DEFAULT_CHARSET
;
141 static WORD
X11DRV_enum_subfont_charset_cp950( UINT index
)
145 case 0: return ANSI_CHARSET
;
148 return DEFAULT_CHARSET
;
152 static XChar2b
* X11DRV_unicode_to_char2b_sbcs( fontObject
* pfo
,
153 LPCWSTR lpwstr
, UINT count
)
158 UINT codepage
= pfo
->fi
->codepage
;
159 char ch
= pfo
->fs
->default_char
;
161 if (!(str2b
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(XChar2b
) )))
163 if (!(str
= HeapAlloc( GetProcessHeap(), 0, count
)))
165 HeapFree( GetProcessHeap(), 0, str2b
);
169 WideCharToMultiByte( codepage
, 0, lpwstr
, count
, str
, count
, &ch
, NULL
);
171 for (i
= 0; i
< count
; i
++)
174 str2b
[i
].byte2
= str
[i
];
176 HeapFree( GetProcessHeap(), 0, str
);
181 static XChar2b
* X11DRV_unicode_to_char2b_unicode( fontObject
* pfo
,
182 LPCWSTR lpwstr
, UINT count
)
187 if (!(str2b
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(XChar2b
) )))
190 for (i
= 0; i
< count
; i
++)
192 str2b
[i
].byte1
= lpwstr
[i
] >> 8;
193 str2b
[i
].byte2
= lpwstr
[i
] & 0xff;
199 /* FIXME: handle jisx0212.1990... */
200 static XChar2b
* X11DRV_unicode_to_char2b_cp932( fontObject
* pfo
,
201 LPCWSTR lpwstr
, UINT count
)
208 char ch
= pfo
->fs
->default_char
;
210 if (!(str2b
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(XChar2b
) )))
212 if (!(str
= HeapAlloc( GetProcessHeap(), 0, count
*2 )))
214 HeapFree( GetProcessHeap(), 0, str2b
);
218 /* handle jisx0212.1990... */
219 WideCharToMultiByte( 932, 0, lpwstr
, count
, str
, count
*2, &ch
, NULL
);
223 for (i
= 0; i
< count
; i
++, str_src
++, str2b_dst
++)
225 if ( IsLegalDBCSChar_cp932( *str_src
, *(str_src
+1) ) )
227 DBCSCharToXChar2b_cp932( str2b_dst
, *str_src
, *(str_src
+1) );
232 str2b_dst
->byte1
= 0;
233 str2b_dst
->byte2
= *str_src
;
237 HeapFree( GetProcessHeap(), 0, str
);
243 static XChar2b
* X11DRV_unicode_to_char2b_cp936( fontObject
* pfo
,
244 LPCWSTR lpwstr
, UINT count
)
251 char ch
= pfo
->fs
->default_char
;
253 if (!(str2b
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(XChar2b
) )))
255 if (!(str
= HeapAlloc( GetProcessHeap(), 0, count
*2 )))
257 HeapFree( GetProcessHeap(), 0, str2b
);
260 WideCharToMultiByte( 936, 0, lpwstr
, count
, str
, count
*2, &ch
, NULL
);
264 for (i
= 0; i
< count
; i
++, str_src
++, str2b_dst
++)
266 if ( IsLegalDBCSChar_cp936( *str_src
, *(str_src
+1) ) )
268 str2b_dst
->byte1
= *str_src
;
269 str2b_dst
->byte2
= *(str_src
+1);
274 str2b_dst
->byte1
= 0;
275 str2b_dst
->byte2
= *str_src
;
279 HeapFree( GetProcessHeap(), 0, str
);
284 static XChar2b
* X11DRV_unicode_to_char2b_cp949( fontObject
* pfo
,
285 LPCWSTR lpwstr
, UINT count
)
292 char ch
= pfo
->fs
->default_char
;
294 if (!(str2b
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(XChar2b
) )))
296 if (!(str
= HeapAlloc( GetProcessHeap(), 0, count
*2 )))
298 HeapFree( GetProcessHeap(), 0, str2b
);
301 WideCharToMultiByte( 949, 0, lpwstr
, count
, str
, count
*2, &ch
, NULL
);
305 for (i
= 0; i
< count
; i
++, str_src
++, str2b_dst
++)
307 if ( IsLegalDBCSChar_cp949( *str_src
, *(str_src
+1) ) )
309 str2b_dst
->byte1
= *str_src
;
310 str2b_dst
->byte2
= *(str_src
+1);
315 str2b_dst
->byte1
= 0;
316 str2b_dst
->byte2
= *str_src
;
320 HeapFree( GetProcessHeap(), 0, str
);
326 static XChar2b
* X11DRV_unicode_to_char2b_cp950( fontObject
* pfo
,
327 LPCWSTR lpwstr
, UINT count
)
334 char ch
= pfo
->fs
->default_char
;
336 if (!(str2b
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(XChar2b
) )))
338 if (!(str
= HeapAlloc( GetProcessHeap(), 0, count
*2 )))
340 HeapFree( GetProcessHeap(), 0, str2b
);
343 WideCharToMultiByte( 950, 0, lpwstr
, count
, str
, count
*2, &ch
, NULL
);
347 for (i
= 0; i
< count
; i
++, str_src
++, str2b_dst
++)
349 if ( IsLegalDBCSChar_cp950( *str_src
, *(str_src
+1) ) )
351 str2b_dst
->byte1
= *str_src
;
352 str2b_dst
->byte2
= *(str_src
+1);
357 str2b_dst
->byte1
= 0;
358 str2b_dst
->byte2
= *str_src
;
362 HeapFree( GetProcessHeap(), 0, str
);
367 static XChar2b
* X11DRV_unicode_to_char2b_symbol( fontObject
* pfo
,
368 LPCWSTR lpwstr
, UINT count
)
372 char ch
= pfo
->fs
->default_char
;
374 if (!(str2b
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(XChar2b
) )))
377 for (i
= 0; i
< count
; i
++)
380 if(lpwstr
[i
] >= 0xf000 && lpwstr
[i
] < 0xf100)
381 str2b
[i
].byte2
= lpwstr
[i
] - 0xf000;
382 else if(lpwstr
[i
] < 0x100)
383 str2b
[i
].byte2
= lpwstr
[i
];
392 static void X11DRV_DrawString_normal( fontObject
* pfo
, Display
* pdisp
,
393 Drawable d
, GC gc
, int x
, int y
,
394 XChar2b
* pstr
, int count
)
396 TSXDrawString16( pdisp
, d
, gc
, x
, y
, pstr
, count
);
399 static int X11DRV_TextWidth_normal( fontObject
* pfo
, XChar2b
* pstr
, int count
)
401 return TSXTextWidth16( pfo
->fs
, pstr
, count
);
404 static void X11DRV_DrawText_normal( fontObject
* pfo
, Display
* pdisp
, Drawable d
,
405 GC gc
, int x
, int y
, XTextItem16
* pitems
,
408 TSXDrawText16( pdisp
, d
, gc
, x
, y
, pitems
, count
);
411 static void X11DRV_TextExtents_normal( fontObject
* pfo
, XChar2b
* pstr
, int count
,
412 int* pdir
, int* pascent
, int* pdescent
,
417 TSXTextExtents16( pfo
->fs
, pstr
, count
, pdir
, pascent
, pdescent
, &info
);
418 *pwidth
= info
.width
;
421 static void X11DRV_GetTextMetricsA_normal( fontObject
* pfo
, LPTEXTMETRICA pTM
)
423 LPIFONTINFO16 pdf
= &pfo
->fi
->df
;
425 if( ! pfo
->lpX11Trans
) {
426 pTM
->tmAscent
= pfo
->fs
->ascent
;
427 pTM
->tmDescent
= pfo
->fs
->descent
;
429 pTM
->tmAscent
= pfo
->lpX11Trans
->ascent
;
430 pTM
->tmDescent
= pfo
->lpX11Trans
->descent
;
433 pTM
->tmAscent
*= pfo
->rescale
;
434 pTM
->tmDescent
*= pfo
->rescale
;
436 pTM
->tmHeight
= pTM
->tmAscent
+ pTM
->tmDescent
;
438 pTM
->tmAveCharWidth
= pfo
->foAvgCharWidth
* pfo
->rescale
;
439 pTM
->tmMaxCharWidth
= pfo
->foMaxCharWidth
* pfo
->rescale
;
441 pTM
->tmInternalLeading
= pfo
->foInternalLeading
* pfo
->rescale
;
442 pTM
->tmExternalLeading
= pdf
->dfExternalLeading
* pfo
->rescale
;
444 pTM
->tmStruckOut
= (pfo
->fo_flags
& FO_SYNTH_STRIKEOUT
)
445 ? 1 : pdf
->dfStrikeOut
;
446 pTM
->tmUnderlined
= (pfo
->fo_flags
& FO_SYNTH_UNDERLINE
)
447 ? 1 : pdf
->dfUnderline
;
450 if( pfo
->fo_flags
& FO_SYNTH_ITALIC
)
452 pTM
->tmOverhang
+= pTM
->tmHeight
/3;
455 pTM
->tmItalic
= pdf
->dfItalic
;
457 pTM
->tmWeight
= pdf
->dfWeight
;
458 if( pfo
->fo_flags
& FO_SYNTH_BOLD
)
461 pTM
->tmWeight
+= 100;
464 pTM
->tmFirstChar
= pdf
->dfFirstChar
;
465 pTM
->tmLastChar
= pdf
->dfLastChar
;
466 pTM
->tmDefaultChar
= pdf
->dfDefaultChar
;
467 pTM
->tmBreakChar
= pdf
->dfBreakChar
;
469 pTM
->tmCharSet
= pdf
->dfCharSet
;
470 pTM
->tmPitchAndFamily
= pdf
->dfPitchAndFamily
;
472 pTM
->tmDigitizedAspectX
= pdf
->dfHorizRes
;
473 pTM
->tmDigitizedAspectY
= pdf
->dfVertRes
;
479 void X11DRV_DrawString_dbcs( fontObject
* pfo
, Display
* pdisp
,
480 Drawable d
, GC gc
, int x
, int y
,
481 XChar2b
* pstr
, int count
)
489 X11DRV_cptable
[pfo
->fi
->cptable
].pDrawText(
490 pfo
, pdisp
, d
, gc
, x
, y
, &item
, 1 );
494 int X11DRV_TextWidth_dbcs_2fonts( fontObject
* pfo
, XChar2b
* pstr
, int count
)
499 fontObject
* pfos
[X11FONT_REFOBJS_MAX
+1];
501 pfos
[0] = XFONT_GetFontObject( pfo
->prefobjs
[0] );
503 if ( pfos
[0] == NULL
) pfos
[0] = pfo
;
506 for ( i
= 0; i
< count
; i
++ )
508 curfont
= ( pstr
->byte1
!= 0 ) ? 1 : 0;
509 width
+= TSXTextWidth16( pfos
[curfont
]->fs
, pstr
, 1 );
517 void X11DRV_DrawText_dbcs_2fonts( fontObject
* pfo
, Display
* pdisp
, Drawable d
,
518 GC gc
, int x
, int y
, XTextItem16
* pitems
,
521 int i
, nitems
, prevfont
= -1, curfont
;
525 fontObject
* pfos
[X11FONT_REFOBJS_MAX
+1];
527 pfos
[0] = XFONT_GetFontObject( pfo
->prefobjs
[0] );
529 if ( pfos
[0] == NULL
) pfos
[0] = pfo
;
532 for ( i
= 0; i
< count
; i
++ )
533 nitems
+= pitems
->nchars
;
534 ptibuf
= HeapAlloc( GetProcessHeap(), 0, sizeof(XTextItem16
) * nitems
);
535 if ( ptibuf
== NULL
)
536 return; /* out of memory */
539 while ( count
-- > 0 )
541 pti
->chars
= pstr
= pitems
->chars
;
542 pti
->delta
= pitems
->delta
;
544 for ( i
= 0; i
< pitems
->nchars
; i
++, pstr
++ )
546 curfont
= ( pstr
->byte1
!= 0 ) ? 1 : 0;
547 if ( curfont
!= prevfont
)
549 if ( pstr
!= pti
->chars
)
551 pti
->nchars
= pstr
- pti
->chars
;
556 pti
->font
= pfos
[curfont
]->fs
->fid
;
560 pti
->nchars
= pstr
- pti
->chars
;
563 TSXDrawText16( pdisp
, d
, gc
, x
, y
, ptibuf
, pti
- ptibuf
);
564 HeapFree( GetProcessHeap(), 0, ptibuf
);
568 void X11DRV_TextExtents_dbcs_2fonts( fontObject
* pfo
, XChar2b
* pstr
, int count
,
569 int* pdir
, int* pascent
, int* pdescent
,
573 int ascent
, descent
, width
;
576 fontObject
* pfos
[X11FONT_REFOBJS_MAX
+1];
578 pfos
[0] = XFONT_GetFontObject( pfo
->prefobjs
[0] );
580 if ( pfos
[0] == NULL
) pfos
[0] = pfo
;
585 for ( i
= 0; i
< count
; i
++ )
587 curfont
= ( pstr
->byte1
!= 0 ) ? 1 : 0;
588 TSXTextExtents16( pfos
[curfont
]->fs
, pstr
, 1, pdir
,
589 &ascent
, &descent
, &info
);
590 if ( *pascent
< ascent
) *pascent
= ascent
;
591 if ( *pdescent
< descent
) *pdescent
= descent
;
600 static void X11DRV_GetTextMetricsA_cp932( fontObject
* pfo
, LPTEXTMETRICA pTM
)
602 fontObject
* pfo_ansi
= XFONT_GetFontObject( pfo
->prefobjs
[0] );
603 LPIFONTINFO16 pdf
= &pfo
->fi
->df
;
604 LPIFONTINFO16 pdf_ansi
;
606 pdf_ansi
= ( pfo_ansi
!= NULL
) ? (&pfo_ansi
->fi
->df
) : pdf
;
608 if( ! pfo
->lpX11Trans
) {
609 pTM
->tmAscent
= pfo
->fs
->ascent
;
610 pTM
->tmDescent
= pfo
->fs
->descent
;
612 pTM
->tmAscent
= pfo
->lpX11Trans
->ascent
;
613 pTM
->tmDescent
= pfo
->lpX11Trans
->descent
;
616 pTM
->tmAscent
*= pfo
->rescale
;
617 pTM
->tmDescent
*= pfo
->rescale
;
619 pTM
->tmHeight
= pTM
->tmAscent
+ pTM
->tmDescent
;
621 if ( pfo_ansi
!= NULL
)
623 pTM
->tmAveCharWidth
= floor((pfo_ansi
->foAvgCharWidth
* 2.0 + pfo
->foAvgCharWidth
) / 3.0 * pfo
->rescale
+ 0.5);
624 pTM
->tmMaxCharWidth
= max(pfo_ansi
->foMaxCharWidth
, pfo
->foMaxCharWidth
) * pfo
->rescale
;
628 pTM
->tmAveCharWidth
= floor((pfo
->foAvgCharWidth
* pfo
->rescale
+ 1.0) / 2.0);
629 pTM
->tmMaxCharWidth
= pfo
->foMaxCharWidth
* pfo
->rescale
;
632 pTM
->tmInternalLeading
= pfo
->foInternalLeading
* pfo
->rescale
;
633 pTM
->tmExternalLeading
= pdf
->dfExternalLeading
* pfo
->rescale
;
635 pTM
->tmStruckOut
= (pfo
->fo_flags
& FO_SYNTH_STRIKEOUT
)
636 ? 1 : pdf
->dfStrikeOut
;
637 pTM
->tmUnderlined
= (pfo
->fo_flags
& FO_SYNTH_UNDERLINE
)
638 ? 1 : pdf
->dfUnderline
;
641 if( pfo
->fo_flags
& FO_SYNTH_ITALIC
)
643 pTM
->tmOverhang
+= pTM
->tmHeight
/3;
646 pTM
->tmItalic
= pdf
->dfItalic
;
648 pTM
->tmWeight
= pdf
->dfWeight
;
649 if( pfo
->fo_flags
& FO_SYNTH_BOLD
)
652 pTM
->tmWeight
+= 100;
655 pTM
->tmFirstChar
= pdf_ansi
->dfFirstChar
;
656 pTM
->tmLastChar
= pdf_ansi
->dfLastChar
;
657 pTM
->tmDefaultChar
= pdf_ansi
->dfDefaultChar
;
658 pTM
->tmBreakChar
= pdf_ansi
->dfBreakChar
;
660 pTM
->tmCharSet
= pdf
->dfCharSet
;
661 pTM
->tmPitchAndFamily
= pdf
->dfPitchAndFamily
;
663 pTM
->tmDigitizedAspectX
= pdf
->dfHorizRes
;
664 pTM
->tmDigitizedAspectY
= pdf
->dfVertRes
;
671 const X11DRV_CP X11DRV_cptable
[X11DRV_CPTABLE_COUNT
] =
674 X11DRV_enum_subfont_charset_normal
,
675 X11DRV_unicode_to_char2b_sbcs
,
676 X11DRV_DrawString_normal
,
677 X11DRV_TextWidth_normal
,
678 X11DRV_DrawText_normal
,
679 X11DRV_TextExtents_normal
,
680 X11DRV_GetTextMetricsA_normal
,
683 X11DRV_enum_subfont_charset_normal
,
684 X11DRV_unicode_to_char2b_unicode
,
685 X11DRV_DrawString_normal
,
686 X11DRV_TextWidth_normal
,
687 X11DRV_DrawText_normal
,
688 X11DRV_TextExtents_normal
,
689 X11DRV_GetTextMetricsA_normal
,
692 X11DRV_enum_subfont_charset_cp932
,
693 X11DRV_unicode_to_char2b_cp932
,
694 X11DRV_DrawString_dbcs
,
695 X11DRV_TextWidth_dbcs_2fonts
,
696 X11DRV_DrawText_dbcs_2fonts
,
697 X11DRV_TextExtents_dbcs_2fonts
,
698 X11DRV_GetTextMetricsA_cp932
,
701 X11DRV_enum_subfont_charset_cp936
,
702 X11DRV_unicode_to_char2b_cp936
,
703 X11DRV_DrawString_dbcs
,
704 X11DRV_TextWidth_dbcs_2fonts
,
705 X11DRV_DrawText_dbcs_2fonts
,
706 X11DRV_TextExtents_dbcs_2fonts
,
707 X11DRV_GetTextMetricsA_normal
, /* FIXME */
710 X11DRV_enum_subfont_charset_cp949
,
711 X11DRV_unicode_to_char2b_cp949
,
712 X11DRV_DrawString_dbcs
,
713 X11DRV_TextWidth_dbcs_2fonts
,
714 X11DRV_DrawText_dbcs_2fonts
,
715 X11DRV_TextExtents_dbcs_2fonts
,
716 X11DRV_GetTextMetricsA_normal
, /* FIXME */
719 X11DRV_enum_subfont_charset_cp950
,
720 X11DRV_unicode_to_char2b_cp950
,
721 X11DRV_DrawString_dbcs
,
722 X11DRV_TextWidth_dbcs_2fonts
,
723 X11DRV_DrawText_dbcs_2fonts
,
724 X11DRV_TextExtents_dbcs_2fonts
,
725 X11DRV_GetTextMetricsA_cp932
,
728 X11DRV_enum_subfont_charset_normal
,
729 X11DRV_unicode_to_char2b_symbol
,
730 X11DRV_DrawString_normal
,
731 X11DRV_TextWidth_normal
,
732 X11DRV_DrawText_normal
,
733 X11DRV_TextExtents_normal
,
734 X11DRV_GetTextMetricsA_normal
,