1 /* Implementation of BDF font handling on the Microsoft W32 API.
2 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Based heavily on code by H. Miyashita for Meadow (a descendant of
29 #include "dispextern.h"
31 #include "blockinput.h"
36 #define min(a, b) ((a) < (b) ? (a) : (b))
37 #define max(a, b) ((a) > (b) ? (a) : (b))
40 #define BDF_CODEPOINT_HEAP_INITIAL_SIZE (96 * 10)
41 /* about 96 characters */
42 #define BDF_BITMAP_HEAP_INITIAL_SIZE (64 * 96)
44 HANDLE hbdf_cp_heap
= INVALID_HANDLE_VALUE
;
45 HANDLE hbdf_bmp_heap
= INVALID_HANDLE_VALUE
;
47 void w32_free_bdf_font(bdffont
*fontp
);
48 bdffont
*w32_init_bdf_font(char *filename
);
50 cache_bitmap cached_bitmap_slots
[BDF_FONT_CACHE_SIZE
];
51 cache_bitmap
*pcached_bitmap_latest
= cached_bitmap_slots
;
53 #define FONT_CACHE_SLOT_OVER_P(p) ((p) >= cached_bitmap_slots + BDF_FONT_CACHE_SIZE)
56 search_file_line(char *key
, char *start
, int len
, char **val
, char **next
)
61 p
= memchr(start
, '\n', len
);
63 for (;start
< p
;start
++)
65 if ((*start
!= ' ') && (*start
!= '\t')) break;
67 linelen
= p
- start
+ 1;
69 if (strncmp(start
, key
, min(strlen(key
), linelen
)) == 0)
71 *val
= start
+ strlen(key
);
79 proceed_file_line(char *key
, char *start
, int *len
, char **val
, char **next
)
84 flag
= search_file_line(key
, start
, *len
, val
, next
);
85 *len
-= (int)(*next
- start
);
89 if (flag
== -1) return 0;
94 get_quoted_string(char *start
, char *end
)
98 p
= memchr(start
, '\"', end
- start
);
101 q
= memchr(p
, '\"', end
- p
);
104 result
= (char*) xmalloc(q
- p
+ 1);
106 memcpy(result
, p
, q
- p
);
107 result
[q
- p
] = '\0';
113 set_bdf_font_info(bdffont
*fontp
)
115 unsigned char *start
, *p
, *q
;
117 int bbw
, bbh
, bbx
, bby
;
124 fontp
->relative_compose
= 0;
125 fontp
->default_ascent
= 0;
127 fontp
->registry
= NULL
;
128 fontp
->encoding
= NULL
;
130 /* fontp->width = NULL; */
132 flag
= proceed_file_line("FONTBOUNDINGBOX", start
, &len
, &p
, &q
);
134 bbw
= strtol(p
, &start
, 10);
136 bbh
= strtol(p
, &start
, 10);
138 bbx
= strtol(p
, &start
, 10);
140 bby
= strtol(p
, &start
, 10);
144 fontp
->urx
= bbw
+ bbx
;
145 fontp
->ury
= bbh
+ bby
;
149 flag
= proceed_file_line("STARTPROPERTIES", start
, &len
, &p
, &q
);
156 if (search_file_line("PIXEL_SIZE", start
, len
, &p
, &q
) == 1)
161 else if (search_file_line("FONT_ASCENT", start
, len
, &p
, &q
) == 1)
166 else if (search_file_line("FONT_DESCENT", start
, len
, &p
, &q
) == 1)
171 else if (search_file_line("_MULE_BASELINE_OFFSET", start
, len
, &p
, &q
) == 1)
174 fontp
->yoffset
= -val1
;
176 else if (search_file_line("_MULE_RELATIVE_COMPOSE", start
, len
, &p
, &q
) == 1)
179 fontp
->relative_compose
= val1
;
181 else if (search_file_line("_MULE_DEFAULT_ASCENT", start
, len
, &p
, &q
) == 1)
184 fontp
->default_ascent
= val1
;
186 else if (search_file_line("CHARSET_REGISTRY", start
, len
, &p
, &q
) == 1)
188 fontp
->registry
= get_quoted_string(p
, q
);
190 else if (search_file_line("CHARSET_ENCODING", start
, len
, &p
, &q
) == 1)
192 fontp
->encoding
= get_quoted_string(p
, q
);
194 else if (search_file_line("SLANT", start
, len
, &p
, &q
) == 1)
196 fontp
->slant
= get_quoted_string(p
, q
);
199 else if (search_file_line("SETWIDTH_NAME", start, len, &p, &q) == 1)
201 fontp->width = get_quoted_string(p, q);
206 flag
= search_file_line("ENDPROPERTIES", start
, len
, &p
, &q
);
208 if (flag
== -1) return 0;
212 flag
= proceed_file_line("CHARS", start
, &len
, &p
, &q
);
220 w32_init_bdf_font(char *filename
)
222 HANDLE hfile
, hfilemap
;
225 BY_HANDLE_FILE_INFORMATION fileinfo
;
228 if (hbdf_cp_heap
== INVALID_HANDLE_VALUE
)
229 hbdf_cp_heap
= HeapCreate(0, BDF_CODEPOINT_HEAP_INITIAL_SIZE
, 0);
230 if (hbdf_bmp_heap
= INVALID_HANDLE_VALUE
)
231 hbdf_bmp_heap
= HeapCreate(0, BDF_BITMAP_HEAP_INITIAL_SIZE
, 0);
233 if (!hbdf_cp_heap
|| !hbdf_bmp_heap
)
234 error("Fail to create heap for BDF.");
236 hfile
= CreateFile(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
237 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
238 if (hfile
== INVALID_HANDLE_VALUE
) return NULL
;
239 if (!GetFileInformationByHandle(hfile
, &fileinfo
) ||
240 (fileinfo
.nFileSizeHigh
!= 0) ||
241 (fileinfo
.nFileSizeLow
> BDF_FILE_SIZE_MAX
))
244 error("Fail to open BDF file.");
246 hfilemap
= CreateFileMapping(hfile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
247 if (hfilemap
== INVALID_HANDLE_VALUE
)
250 error("Can't map font.");
253 font
= MapViewOfFile(hfilemap
, FILE_MAP_READ
, 0, 0, 0);
258 CloseHandle(hfilemap
);
259 error("Can't view font.");
262 bdffontp
= (bdffont
*) xmalloc(sizeof(bdffont
));
264 for(i
= 0;i
< BDF_FIRST_OFFSET_TABLE
;i
++)
265 bdffontp
->chtbl
[i
] = NULL
;
266 bdffontp
->size
= fileinfo
.nFileSizeLow
;
267 bdffontp
->font
= font
;
268 bdffontp
->hfile
= hfile
;
269 bdffontp
->hfilemap
= hfilemap
;
270 bdffontp
->filename
= (char*) xmalloc(strlen(filename
) + 1);
271 strcpy(bdffontp
->filename
, filename
);
273 if (!set_bdf_font_info(bdffontp
))
275 w32_free_bdf_font(bdffontp
);
276 error("Invalid BDF font!");
282 w32_free_bdf_font(bdffont
*fontp
)
288 UnmapViewOfFile(fontp
->hfilemap
);
289 CloseHandle(fontp
->hfilemap
);
290 CloseHandle(fontp
->hfile
);
292 if (fontp
->registry
) xfree(fontp
->registry
);
293 if (fontp
->encoding
) xfree(fontp
->encoding
);
294 if (fontp
->slant
) xfree(fontp
->slant
);
295 /* if (fontp->width) xfree(fontp->width); */
297 xfree(fontp
->filename
);
298 for(i
= 0;i
< BDF_FIRST_OFFSET_TABLE
;i
++)
300 pch
= fontp
->chtbl
[i
];
303 for (j
= 0;j
< BDF_SECOND_OFFSET_TABLE
;j
++)
309 HeapFree(hbdf_bmp_heap
, 0, pcb
->pbmp
);
313 HeapFree(hbdf_cp_heap
, 0, pch
);
320 get_cached_font_char(bdffont
*fontp
, int index
)
322 font_char
*pch
, *result
;
325 if (!BDF_CODEPOINT_RANGE_COVER_P(index
))
328 pch
= fontp
->chtbl
[BDF_FIRST_OFFSET(index
)];
332 result
= &pch
[BDF_SECOND_OFFSET(index
)];
334 if (!result
->offset
) return NULL
;
340 cache_char_offset(bdffont
*fontp
, int index
, unsigned char *offset
)
342 font_char
*pch
, *result
;
345 if (!BDF_CODEPOINT_RANGE_COVER_P(index
))
348 pch
= fontp
->chtbl
[BDF_FIRST_OFFSET(index
)];
351 pch
= fontp
->chtbl
[BDF_FIRST_OFFSET(index
)] =
352 (font_char
*) HeapAlloc(hbdf_cp_heap
,
355 BDF_SECOND_OFFSET_TABLE
);
356 if (!pch
) return NULL
;
357 /* memset(pch, 0, sizeof(font_char) * BDF_SECOND_OFFSET_TABLE); */
360 result
= &pch
[BDF_SECOND_OFFSET(index
)];
361 result
->offset
= offset
;
367 seek_char(bdffont
*fontp
, int index
)
370 int len
, flag
, font_index
;
371 unsigned char *start
, *p
, *q
;
373 if (!fontp
->seeked
) return NULL
;
375 start
= fontp
->seeked
;
376 len
= fontp
->size
- (start
- fontp
->font
);
379 flag
= proceed_file_line("ENCODING", start
, &len
, &p
, &q
);
382 fontp
->seeked
= NULL
;
385 font_index
= atoi(p
);
386 result
= cache_char_offset(fontp
, font_index
, q
);
387 if (!result
) return NULL
;
389 start
= result
->offset
;
390 } while (font_index
!= index
);
391 fontp
->seeked
= start
;
397 clear_cached_bitmap_slots()
402 p
= pcached_bitmap_latest
;
403 for (i
= 0;i
< BDF_FONT_CLEAR_SIZE
;i
++)
408 HeapFree(hbdf_bmp_heap
, 0, p
->pbmp
);
409 p
->psrc
->pcbmp
= NULL
;
413 if (FONT_CACHE_SLOT_OVER_P(p
))
414 p
= cached_bitmap_slots
;
418 #define GET_HEX_VAL(x) ((isdigit(x)) ? ((x) - '0') : \
419 (((x) >= 'A') && ((x) <= 'F')) ? ((x) - 'A' + 10) : \
420 (((x) >= 'a') && ((x) <= 'f')) ? ((x) - 'a' + 10) : \
424 w32_get_bdf_glyph(bdffont
*fontp
, int index
, int size
, glyph_struct
*glyph
)
427 unsigned char *start
, *p
, *q
, *bitmapp
;
428 unsigned char val
, val1
, val2
;
429 int i
, j
, len
, flag
, consumed
;
432 pch
= get_cached_font_char(fontp
, index
);
435 pch
= seek_char(fontp
, index
);
442 if ((size
== 0) && pch
->pcbmp
)
444 glyph
->metric
= pch
->pcbmp
->metric
;
448 len
= fontp
->size
- (start
- fontp
->font
);
450 flag
= proceed_file_line("DWIDTH", start
, &len
, &p
, &q
);
453 glyph
->metric
.dwidth
= atoi(p
);
456 flag
= proceed_file_line("BBX", start
, &len
, &p
, &q
);
459 glyph
->metric
.bbw
= strtol(p
, &start
, 10);
461 glyph
->metric
.bbh
= strtol(p
, &start
, 10);
463 glyph
->metric
.bbox
= strtol(p
, &start
, 10);
465 glyph
->metric
.bboy
= strtol(p
, &start
, 10);
467 if (size
== 0) return 1;
470 flag
= proceed_file_line("BITMAP", start
, &len
, &p
, &q
);
477 bitmapp
= glyph
->bitmap
;
478 rowbytes
= (glyph
->metric
.bbw
+ 7) / 8;
479 /* DIB requires DWORD alignment. */
480 align
= sizeof(DWORD
) - rowbytes
% sizeof(DWORD
);
481 consumed
= glyph
->metric
.bbh
* (rowbytes
+ align
);
482 glyph
->bitmap_size
= consumed
;
483 glyph
->row_byte_size
= rowbytes
;
484 if (size
< consumed
) return 0;
486 for(i
= 0;i
< glyph
->metric
.bbh
;i
++)
488 q
= memchr(p
, '\n', len
);
490 for(j
= 0;((q
> p
) && (j
< rowbytes
));j
++)
492 val1
= GET_HEX_VAL(*p
);
493 if (val1
== -1) return 0;
495 val2
= GET_HEX_VAL(*p
);
496 if (val2
== -1) return 0;
498 val
= (unsigned char)((val1
<< 4) | val2
);
502 for(j
= 0;j
< align
;j
++)
507 /* If this glyph is white space, return -1. */
508 if (flag
== 0) return -1;
515 get_bitmap_with_cache(bdffont
*fontp
, int index
)
517 int bitmap_size
, bitmap_real_size
;
523 pch
= get_cached_font_char(fontp
, index
);
530 bitmap_size
= ((fontp
->urx
- fontp
->llx
) / 8 + 3) * (fontp
->ury
- fontp
->lly
)
532 glyph
.bitmap
= (unsigned char*) alloca(sizeof(unsigned char) * bitmap_size
);
534 bitmap_real_size
= w32_get_bdf_glyph(fontp
, index
, bitmap_size
, &glyph
);
536 if (bitmap_real_size
== 0)
539 pch
= get_cached_font_char(fontp
, index
);
540 if (!pch
) return NULL
;
542 if (bitmap_real_size
> 0)
544 pbmp
= (unsigned char*) HeapAlloc(hbdf_bmp_heap
, 0,
546 if (!pbmp
) return NULL
;
547 memcpy(pbmp
, glyph
.bitmap
, bitmap_real_size
);
550 pbmp
= NULL
; /* white space character */
552 pcb
= pcached_bitmap_latest
;
554 clear_cached_bitmap_slots();
557 pcb
->metric
= glyph
.metric
;
559 pcb
->bitmap_size
= glyph
.bitmap_size
;
560 pcb
->row_byte_size
= glyph
.row_byte_size
;
564 pcached_bitmap_latest
++;
565 if (FONT_CACHE_SLOT_OVER_P(pcached_bitmap_latest
))
566 pcached_bitmap_latest
= cached_bitmap_slots
;
572 create_offscreen_bitmap(HDC hdc
, int width
, int height
, unsigned char **bitsp
)
580 memset(&info
, 0, sizeof(info
));
581 info
.h
.biSize
= sizeof(BITMAPINFOHEADER
);
582 info
.h
.biWidth
= width
;
583 info
.h
.biHeight
= -height
;
585 info
.h
.biBitCount
= 1;
586 info
.h
.biCompression
= BI_RGB
;
587 info
.c
[1].rgbRed
= info
.c
[1].rgbGreen
= info
.c
[1].rgbBlue
= 255;
589 return CreateDIBSection(hdc
, (LPBITMAPINFO
)&info
,
590 DIB_RGB_COLORS
, bitsp
, NULL
, 0);
594 w32_BDF_TextMetric(bdffont
*fontp
, unsigned char *text
, int dim
)
602 index
= MAKELENDSHORT(text
[1], text
[0]);
604 pcb
= get_bitmap_with_cache(fontp
, index
);
608 return &(pcb
->metric
);
612 w32_BDF_TextOut(bdffont
*fontp
, HDC hdc
, int left
,
613 int top
, unsigned char *text
, int dim
, int bytelen
,
614 int fixed_pitch_size
)
617 unsigned char *textp
;
619 HBRUSH hFgBrush
, hOrgBrush
;
625 static HBITMAP hBMP
= 0;
626 static HDC DIBsection_hdc
= 0;
627 static int DIBsection_width
, DIBsection_height
;
628 static unsigned char *bits
;
630 hCompatDC
= CreateCompatibleDC(hdc
);
634 textalign
= GetTextAlign(hdc
);
636 hFgBrush
= CreateSolidBrush(GetTextColor(hdc
));
637 hOrgBrush
= SelectObject(hdc
, hFgBrush
);
651 if (bytelen
< 0) break;
652 index
= MAKELENDSHORT(textp
[1], textp
[0]);
655 pcb
= get_bitmap_with_cache(fontp
, index
);
663 width
= pcb
->metric
.bbw
;
664 height
= pcb
->metric
.bbh
;
667 && (DIBsection_hdc
== hdc
)
668 && (DIBsection_width
== width
)
669 && (DIBsection_height
== height
)))
671 if (hBMP
) DeleteObject(hBMP
);
672 hBMP
= create_offscreen_bitmap(hdc
, width
, height
, &bits
);
673 DIBsection_hdc
= hdc
;
674 DIBsection_width
= width
;
675 DIBsection_height
= height
;
679 memcpy(bits
, pcb
->pbmp
, pcb
->bitmap_size
);
681 if (textalign
& TA_BASELINE
)
682 btop
= top
- (pcb
->metric
.bbh
+ pcb
->metric
.bboy
);
683 else if (textalign
& TA_BOTTOM
)
684 btop
= top
- pcb
->metric
.bbh
;
688 horgobj
= SelectObject(hCompatDC
, hBMP
);
689 BitBlt(hdc
, left
, btop
, width
, height
, hCompatDC
, 0, 0, 0xE20746);
690 SelectObject(hCompatDC
, horgobj
);
693 if (fixed_pitch_size
)
694 left
+= fixed_pitch_size
;
696 left
+= pcb
->metric
.dwidth
;
701 SelectObject(hdc
, hOrgBrush
);
702 DeleteObject(hFgBrush
);
707 struct font_info
*w32_load_bdf_font (struct frame
*f
, char *fontname
,
708 int size
, char* filename
)
710 struct w32_display_info
*dpyinfo
= FRAME_W32_DISPLAY_INFO (f
);
711 struct font_info
*fontp
;
715 bdf_font
= w32_init_bdf_font (filename
);
717 if (!bdf_font
) return NULL
;
719 font
= (XFontStruct
*) xmalloc (sizeof (XFontStruct
));
721 font
->bdf
= bdf_font
;
724 /* NTEMACS_TODO: Recognize DBCS fonts. */
725 font
->double_byte_p
= 0;
727 /* Do we need to create the table? */
728 if (dpyinfo
->font_table_size
== 0)
730 dpyinfo
->font_table_size
= 16;
732 = (struct font_info
*) xmalloc (dpyinfo
->font_table_size
733 * sizeof (struct font_info
));
735 /* Do we need to grow the table? */
736 else if (dpyinfo
->n_fonts
737 >= dpyinfo
->font_table_size
)
739 dpyinfo
->font_table_size
*= 2;
741 = (struct font_info
*) xrealloc (dpyinfo
->font_table
,
742 (dpyinfo
->font_table_size
743 * sizeof (struct font_info
)));
746 fontp
= dpyinfo
->font_table
+ dpyinfo
->n_fonts
;
748 /* Now fill in the slots of *FONTP. */
751 fontp
->font_idx
= dpyinfo
->n_fonts
;
752 fontp
->name
= (char *) xmalloc (strlen (fontname
) + 1);
753 bcopy (fontname
, fontp
->name
, strlen (fontname
) + 1);
754 fontp
->full_name
= fontp
->name
;
755 fontp
->size
= FONT_WIDTH (font
);
756 fontp
->height
= FONT_HEIGHT (font
);
758 /* The slot `encoding' specifies how to map a character
759 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
760 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF, 0:0x2020..0x7F7F,
761 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF,
762 0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF, or
763 2:0xA020..0xFF7F). For the moment, we don't know which charset
764 uses this font. So, we set informatoin in fontp->encoding[1]
765 which is never used by any charset. If mapping can't be
766 decided, set FONT_ENCODING_NOT_DECIDED. */
767 fontp
->encoding
[1] = FONT_ENCODING_NOT_DECIDED
;
768 fontp
->baseline_offset
= bdf_font
->yoffset
;
769 fontp
->relative_compose
= bdf_font
->relative_compose
;
770 fontp
->default_ascent
= bdf_font
->default_ascent
;
777 /* Check a file for an XFLD string describing it. */
778 int w32_BDF_to_x_font (char *file
, char* xstr
, int len
)
780 HANDLE hfile
, hfilemap
;
781 BY_HANDLE_FILE_INFORMATION fileinfo
;
782 char *font
, *start
, *p
, *q
;
783 int flag
, size
, retval
= 0;
785 hfile
= CreateFile (file
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
786 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
787 if (hfile
== INVALID_HANDLE_VALUE
) return 0;
788 if (!GetFileInformationByHandle(hfile
, &fileinfo
) ||
789 (fileinfo
.nFileSizeHigh
!= 0) ||
790 (fileinfo
.nFileSizeLow
> BDF_FILE_SIZE_MAX
))
795 size
= fileinfo
.nFileSizeLow
;
797 hfilemap
= CreateFileMapping (hfile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
798 if (hfilemap
== INVALID_HANDLE_VALUE
)
804 font
= MapViewOfFile (hfilemap
, FILE_MAP_READ
, 0, 0, 0);
808 CloseHandle (hfilemap
);
813 flag
= proceed_file_line ("FONT ", start
, &size
, &p
, &q
);
816 /* If font provides a description of itself, check it is a
817 full XLFD before accepting it. */
821 for (s
= p
; s
< q
; s
++)
826 if (count
== 14 && q
- p
- 1 <= len
)
828 strncpy (xstr
, p
, q
-p
-1);
830 /* Files may have DOS line ends (ie still ^M on end). */
831 if (iscntrl(xstr
[q
-p
-2]))
838 CloseHandle (hfilemap
);