]> code.delx.au - gnu-emacs/blobdiff - src/w32bdf.c
(indent-for-tab-command): Indent the region if
[gnu-emacs] / src / w32bdf.c
index 912728bec757779d7701b54481ba0d0fbf2dab34..80ef098d8ded749eadd76445e0056db343cd7730 100644 (file)
@@ -1,11 +1,12 @@
 /* Implementation of BDF font handling on the Microsoft W32 API.
 /* Implementation of BDF font handling on the Microsoft W32 API.
-   Copyright (C) 1999 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005,
+                 2006, 2007  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -15,16 +16,21 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* Based heavily on code by H. Miyashita for Meadow (a descendant of
    MULE for W32). */
 
 #include <windows.h>
 
 /* Based heavily on code by H. Miyashita for Meadow (a descendant of
    MULE for W32). */
 
 #include <windows.h>
-#include "config.h"
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include "lisp.h"
 #include "charset.h"
 #include "lisp.h"
 #include "charset.h"
+#include "keyboard.h"
 #include "frame.h"
 #include "dispextern.h"
 #include "fontset.h"
 #include "frame.h"
 #include "dispextern.h"
 #include "fontset.h"
@@ -33,9 +39,6 @@ Boston, MA 02111-1307, USA.  */
 #include "w32term.h"
 #include "w32bdf.h"
 
 #include "w32term.h"
 #include "w32bdf.h"
 
-#define min(a, b) ((a) < (b) ? (a) : (b))
-#define max(a, b) ((a) > (b) ? (a) : (b))
-
 /* 10 planes */
 #define BDF_CODEPOINT_HEAP_INITIAL_SIZE (96 * 10)
 /* about 96 characters */
 /* 10 planes */
 #define BDF_CODEPOINT_HEAP_INITIAL_SIZE (96 * 10)
 /* about 96 characters */
@@ -52,7 +55,7 @@ cache_bitmap *pcached_bitmap_latest = cached_bitmap_slots;
 
 #define FONT_CACHE_SLOT_OVER_P(p) ((p) >= cached_bitmap_slots + BDF_FONT_CACHE_SIZE)
 
 
 #define FONT_CACHE_SLOT_OVER_P(p) ((p) >= cached_bitmap_slots + BDF_FONT_CACHE_SIZE)
 
-static int 
+static int
 search_file_line(char *key, char *start, int len, char **val, char **next)
 {
   unsigned int linelen;
 search_file_line(char *key, char *start, int len, char **val, char **next)
 {
   unsigned int linelen;
@@ -60,7 +63,7 @@ search_file_line(char *key, char *start, int len, char **val, char **next)
 
   p = memchr(start, '\n', len);
   if (!p) return -1;
 
   p = memchr(start, '\n', len);
   if (!p) return -1;
-  for (;start < p;start++)
+  for (;(unsigned char *)start < p;start++)
     {
       if ((*start != ' ') && (*start != '\t')) break;
     }
     {
       if ((*start != ' ') && (*start != '\t')) break;
     }
@@ -71,7 +74,7 @@ search_file_line(char *key, char *start, int len, char **val, char **next)
       *val = start + strlen(key);
       return 1;
     }
       *val = start + strlen(key);
       return 1;
     }
-  
+
   return 0;
 }
 
   return 0;
 }
 
@@ -129,15 +132,16 @@ set_bdf_font_info(bdffont *fontp)
   fontp->slant = NULL;
 /*  fontp->width = NULL; */
 
   fontp->slant = NULL;
 /*  fontp->width = NULL; */
 
-  flag = proceed_file_line("FONTBOUNDINGBOX", start, &len, &p, &q);
+  flag = proceed_file_line("FONTBOUNDINGBOX", start, &len,
+                          (char **)&p, (char **)&q);
   if (!flag) return 0;
   if (!flag) return 0;
-  bbw = strtol(p, &start, 10);
+  bbw = strtol(p, (char **)&start, 10);
   p = start;
   p = start;
-  bbh = strtol(p, &start, 10);
+  bbh = strtol(p, (char **)&start, 10);
   p = start;
   p = start;
-  bbx = strtol(p, &start, 10);
+  bbx = strtol(p, (char **)&start, 10);
   p = start;
   p = start;
-  bby = strtol(p, &start, 10);
+  bby = strtol(p, (char **)&start, 10);
 
   fontp->llx = bbx;
   fontp->lly = bby;
 
   fontp->llx = bbx;
   fontp->lly = bby;
@@ -146,70 +150,82 @@ set_bdf_font_info(bdffont *fontp)
   fontp->width = bbw;
   fontp->height = bbh;
   start = q;
   fontp->width = bbw;
   fontp->height = bbh;
   start = q;
-  flag = proceed_file_line("STARTPROPERTIES", start, &len, &p, &q);
+  flag = proceed_file_line("STARTPROPERTIES", start, &len,
+                          (char **)&p, (char **)&q);
   if (!flag) return 1;
 
   flag = 0;
 
   do {
     start = q;
   if (!flag) return 1;
 
   flag = 0;
 
   do {
     start = q;
-    if (search_file_line("PIXEL_SIZE", start, len, &p, &q) == 1)
+    if (search_file_line("PIXEL_SIZE", start, len,
+                        (char **)&p, (char **)&q) == 1)
       {
        val1 = atoi(p);
         fontp->pixsz = val1;
       }
       {
        val1 = atoi(p);
         fontp->pixsz = val1;
       }
-    else if (search_file_line("FONT_ASCENT", start, len, &p, &q) == 1)
+    else if (search_file_line("FONT_ASCENT", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
        val1 = atoi(p);
        fontp->ury = val1;
       }
       {
        val1 = atoi(p);
        fontp->ury = val1;
       }
-    else if (search_file_line("FONT_DESCENT", start, len, &p, &q) == 1)
+    else if (search_file_line("FONT_DESCENT", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
        val1 = atoi(p);
        fontp->lly = -val1;
       }
       {
        val1 = atoi(p);
        fontp->lly = -val1;
       }
-    else if (search_file_line("_MULE_BASELINE_OFFSET", start, len, &p, &q) == 1)
+    else if (search_file_line("_MULE_BASELINE_OFFSET", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
        val1 = atoi(p);
        fontp->yoffset = -val1;
       }
       {
        val1 = atoi(p);
        fontp->yoffset = -val1;
       }
-    else if (search_file_line("_MULE_RELATIVE_COMPOSE", start, len, &p, &q) == 1)
+    else if (search_file_line("_MULE_RELATIVE_COMPOSE", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
        val1 = atoi(p);
        fontp->relative_compose = val1;
       }
       {
        val1 = atoi(p);
        fontp->relative_compose = val1;
       }
-    else if (search_file_line("_MULE_DEFAULT_ASCENT", start, len, &p, &q) == 1)
+    else if (search_file_line("_MULE_DEFAULT_ASCENT", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
        val1 = atoi(p);
        fontp->default_ascent = val1;
       }
       {
        val1 = atoi(p);
        fontp->default_ascent = val1;
       }
-    else if (search_file_line("CHARSET_REGISTRY", start, len, &p, &q) == 1)
+    else if (search_file_line("CHARSET_REGISTRY", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
         fontp->registry = get_quoted_string(p, q);
       }
       {
         fontp->registry = get_quoted_string(p, q);
       }
-    else if (search_file_line("CHARSET_ENCODING", start, len, &p, &q) == 1)
+    else if (search_file_line("CHARSET_ENCODING", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
         fontp->encoding = get_quoted_string(p, q);
       }
       {
         fontp->encoding = get_quoted_string(p, q);
       }
-    else if (search_file_line("SLANT", start, len, &p, &q) == 1)
+    else if (search_file_line("SLANT", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
         fontp->slant = get_quoted_string(p, q);
       }
 /*
       {
         fontp->slant = get_quoted_string(p, q);
       }
 /*
-    else if (search_file_line("SETWIDTH_NAME", start, len, &p, &q) == 1)
+    else if (search_file_line("SETWIDTH_NAME", start, len,
+                             (char **)&p, (char **)&q) == 1)
       {
         fontp->width = get_quoted_string(p, q);
       }
 */
     else
       {
       {
         fontp->width = get_quoted_string(p, q);
       }
 */
     else
       {
-       flag = search_file_line("ENDPROPERTIES", start, len, &p, &q);
+       flag = search_file_line("ENDPROPERTIES", start, len,
+                               (char **)&p, (char **)&q);
       }
     if (flag == -1) return 0;
     len -= (q - start);
   }while(flag == 0);
   start = q;
       }
     if (flag == -1) return 0;
     len -= (q - start);
   }while(flag == 0);
   start = q;
-  flag = proceed_file_line("CHARS", start, &len, &p, &q);
+  flag = proceed_file_line("CHARS", start, &len, (char **)&p, (char **)&q);
   if (!flag) return 0;
   fontp->nchars = atoi(p);
   fontp->seeked = q;
   if (!flag) return 0;
   fontp->nchars = atoi(p);
   fontp->seeked = q;
@@ -232,7 +248,7 @@ w32_init_bdf_font(char *filename)
     hbdf_bmp_heap = HeapCreate(0, BDF_BITMAP_HEAP_INITIAL_SIZE, 0);
 
   if (!hbdf_cp_heap || !hbdf_bmp_heap)
     hbdf_bmp_heap = HeapCreate(0, BDF_BITMAP_HEAP_INITIAL_SIZE, 0);
 
   if (!hbdf_cp_heap || !hbdf_bmp_heap)
-    error("Fail to create heap for BDF.");
+    error("Fail to create heap for BDF");
 
   hfile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 
   hfile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
@@ -242,13 +258,13 @@ w32_init_bdf_font(char *filename)
       (fileinfo.nFileSizeLow > BDF_FILE_SIZE_MAX))
     {
       CloseHandle(hfile);
       (fileinfo.nFileSizeLow > BDF_FILE_SIZE_MAX))
     {
       CloseHandle(hfile);
-      error("Fail to open BDF file.");
+      error("Fail to open BDF file");
     }
   hfilemap = CreateFileMapping(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
   if (hfilemap == INVALID_HANDLE_VALUE)
     {
       CloseHandle(hfile);
     }
   hfilemap = CreateFileMapping(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
   if (hfilemap == INVALID_HANDLE_VALUE)
     {
       CloseHandle(hfile);
-      error("Can't map font.");
+      error("Can't map font");
     }
 
   font = MapViewOfFile(hfilemap, FILE_MAP_READ, 0, 0, 0);
     }
 
   font = MapViewOfFile(hfilemap, FILE_MAP_READ, 0, 0, 0);
@@ -257,11 +273,11 @@ w32_init_bdf_font(char *filename)
     {
       CloseHandle(hfile);
       CloseHandle(hfilemap);
     {
       CloseHandle(hfile);
       CloseHandle(hfilemap);
-      error("Can't view font.");
+      error("Can't view font");
     }
 
   bdffontp = (bdffont *) xmalloc(sizeof(bdffont));
     }
 
   bdffontp = (bdffont *) xmalloc(sizeof(bdffont));
-  
+
   for(i = 0;i < BDF_FIRST_OFFSET_TABLE;i++)
     bdffontp->chtbl[i] = NULL;
   bdffontp->size = fileinfo.nFileSizeLow;
   for(i = 0;i < BDF_FIRST_OFFSET_TABLE;i++)
     bdffontp->chtbl[i] = NULL;
   bdffontp->size = fileinfo.nFileSizeLow;
@@ -270,7 +286,7 @@ w32_init_bdf_font(char *filename)
   bdffontp->hfilemap = hfilemap;
   bdffontp->filename = (char*) xmalloc(strlen(filename) + 1);
   strcpy(bdffontp->filename, filename);
   bdffontp->hfilemap = hfilemap;
   bdffontp->filename = (char*) xmalloc(strlen(filename) + 1);
   strcpy(bdffontp->filename, filename);
-  
+
   if (!set_bdf_font_info(bdffontp))
     {
       w32_free_bdf_font(bdffontp);
   if (!set_bdf_font_info(bdffontp))
     {
       w32_free_bdf_font(bdffontp);
@@ -286,7 +302,7 @@ w32_free_bdf_font(bdffont *fontp)
   font_char *pch;
   cache_bitmap *pcb;
 
   font_char *pch;
   cache_bitmap *pcb;
 
-  UnmapViewOfFile(fontp->hfilemap);
+  UnmapViewOfFile(fontp->font);
   CloseHandle(fontp->hfilemap);
   CloseHandle(fontp->hfile);
 
   CloseHandle(fontp->hfilemap);
   CloseHandle(fontp->hfile);
 
@@ -349,7 +365,7 @@ cache_char_offset(bdffont *fontp, int index, unsigned char *offset)
     {
       pch = fontp->chtbl[BDF_FIRST_OFFSET(index)] =
        (font_char*) HeapAlloc(hbdf_cp_heap,
     {
       pch = fontp->chtbl[BDF_FIRST_OFFSET(index)] =
        (font_char*) HeapAlloc(hbdf_cp_heap,
-                              HEAP_ZERO_MEMORY, 
+                              HEAP_ZERO_MEMORY,
                               sizeof(font_char) *
                                BDF_SECOND_OFFSET_TABLE);
       if (!pch) return NULL;
                               sizeof(font_char) *
                                BDF_SECOND_OFFSET_TABLE);
       if (!pch) return NULL;
@@ -375,7 +391,8 @@ seek_char(bdffont *fontp, int index)
   len = fontp->size - (start - fontp->font);
 
   do {
   len = fontp->size - (start - fontp->font);
 
   do {
-    flag = proceed_file_line("ENCODING", start, &len, &p, &q);
+    flag = proceed_file_line("ENCODING", start, &len,
+                            (char **)&p, (char **)&q);
     if (!flag)
       {
        fontp->seeked = NULL;
     if (!flag)
       {
        fontp->seeked = NULL;
@@ -446,27 +463,27 @@ w32_get_bdf_glyph(bdffont *fontp, int index, int size, glyph_struct *glyph)
 
   len = fontp->size - (start - fontp->font);
 
 
   len = fontp->size - (start - fontp->font);
 
-  flag = proceed_file_line("DWIDTH", start, &len, &p, &q);
+  flag = proceed_file_line("DWIDTH", start, &len, (char **)&p, (char **)&q);
   if (!flag)
     return 0;
   glyph->metric.dwidth = atoi(p);
 
   start = q;
   if (!flag)
     return 0;
   glyph->metric.dwidth = atoi(p);
 
   start = q;
-  flag = proceed_file_line("BBX", start, &len, &p, &q);
+  flag = proceed_file_line("BBX", start, &len, (char **)&p, (char **)&q);
   if (!flag)
     return 0;
   if (!flag)
     return 0;
-  glyph->metric.bbw = strtol(p, &start, 10);
+  glyph->metric.bbw = strtol(p, (char **)&start, 10);
   p = start;
   p = start;
-  glyph->metric.bbh = strtol(p, &start, 10);
+  glyph->metric.bbh = strtol(p, (char **)&start, 10);
   p = start;
   p = start;
-  glyph->metric.bbox = strtol(p, &start, 10);
+  glyph->metric.bbox = strtol(p, (char **)&start, 10);
   p = start;
   p = start;
-  glyph->metric.bboy = strtol(p, &start, 10);
+  glyph->metric.bboy = strtol(p, (char **)&start, 10);
 
   if (size == 0) return 1;
 
   start = q;
 
   if (size == 0) return 1;
 
   start = q;
-  flag = proceed_file_line("BITMAP", start, &len, &p, &q);
+  flag = proceed_file_line("BITMAP", start, &len, (char **)&p, (char **)&q);
   if (!flag)
     return 0;
 
   if (!flag)
     return 0;
 
@@ -488,11 +505,14 @@ w32_get_bdf_glyph(bdffont *fontp, int index, int size, glyph_struct *glyph)
       if (!q) return 0;
       for(j = 0;((q > p) && (j < rowbytes));j++)
        {
       if (!q) return 0;
       for(j = 0;((q > p) && (j < rowbytes));j++)
        {
-         val1 = GET_HEX_VAL(*p);
-         if (val1 == -1) return 0;
+         int ival = GET_HEX_VAL(*p);
+
+         if (ival == -1) return 0;
+         val1 = ival;
          p++;
          p++;
-         val2 = GET_HEX_VAL(*p);
-         if (val2 == -1) return 0;
+         ival = GET_HEX_VAL(*p);
+         if (ival == -1) return 0;
+         val2 = ival;
          p++;
          val = (unsigned char)((val1 << 4) | val2);
          if (val) flag = 1;
          p++;
          val = (unsigned char)((val1 << 4) | val2);
          if (val) flag = 1;
@@ -559,7 +579,7 @@ get_bitmap_with_cache(bdffont *fontp, int index)
   pcb->row_byte_size = glyph.row_byte_size;
 
   pch->pcbmp = pcb;
   pcb->row_byte_size = glyph.row_byte_size;
 
   pch->pcbmp = pcb;
-  
+
   pcached_bitmap_latest++;
   if (FONT_CACHE_SLOT_OVER_P(pcached_bitmap_latest))
     pcached_bitmap_latest = cached_bitmap_slots;
   pcached_bitmap_latest++;
   if (FONT_CACHE_SLOT_OVER_P(pcached_bitmap_latest))
     pcached_bitmap_latest = cached_bitmap_slots;
@@ -585,7 +605,7 @@ create_offscreen_bitmap(HDC hdc, int width, int height, unsigned char **bitsp)
   info.c[1].rgbRed = info.c[1].rgbGreen = info.c[1].rgbBlue = 255;
 
   return CreateDIBSection(hdc, (LPBITMAPINFO)&info,
   info.c[1].rgbRed = info.c[1].rgbGreen = info.c[1].rgbBlue = 255;
 
   return CreateDIBSection(hdc, (LPBITMAPINFO)&info,
-                         DIB_RGB_COLORS, bitsp, NULL, 0);
+                         DIB_RGB_COLORS, (void **)bitsp, NULL, 0);
 }
 
 glyph_metric *
 }
 
 glyph_metric *
@@ -630,7 +650,7 @@ w32_BDF_TextOut(bdffont *fontp, HDC hdc, int left,
     return 0;
 
   textalign = GetTextAlign(hdc);
     return 0;
 
   textalign = GetTextAlign(hdc);
-  
+
   hFgBrush = CreateSolidBrush(GetTextColor(hdc));
   hOrgBrush = SelectObject(hdc, hFgBrush);
 
   hFgBrush = CreateSolidBrush(GetTextColor(hdc));
   hOrgBrush = SelectObject(hdc, hFgBrush);
 
@@ -660,7 +680,7 @@ w32_BDF_TextOut(bdffont *fontp, HDC hdc, int left,
        {
          width = pcb->metric.bbw;
          height = pcb->metric.bbh;
        {
          width = pcb->metric.bbw;
          height = pcb->metric.bbh;
-         
+
          if (!(hBMP
                && (DIBsection_hdc == hdc)
                && (DIBsection_width == width)
          if (!(hBMP
                && (DIBsection_hdc == hdc)
                && (DIBsection_width == width)
@@ -749,12 +769,16 @@ struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
 
   /* Now fill in the slots of *FONTP.  */
   BLOCK_INPUT;
 
   /* Now fill in the slots of *FONTP.  */
   BLOCK_INPUT;
+  bzero (fontp, sizeof (*fontp));
   fontp->font = font;
   fontp->font_idx = dpyinfo->n_fonts;
   fontp->name = (char *) xmalloc (strlen (fontname) + 1);
   bcopy (fontname, fontp->name, strlen (fontname) + 1);
   fontp->full_name = fontp->name;
   fontp->font = font;
   fontp->font_idx = dpyinfo->n_fonts;
   fontp->name = (char *) xmalloc (strlen (fontname) + 1);
   bcopy (fontname, fontp->name, strlen (fontname) + 1);
   fontp->full_name = fontp->name;
-  fontp->size = FONT_WIDTH (font);
+  /* FIXME: look at BDF spec to see if there are better ways of finding
+     average_width and space_width, hopefully that don't involve working out
+     the values for ourselves from the data.  */
+  fontp->size = fontp->average_width = fontp->space_width = FONT_WIDTH (font);
   fontp->height = FONT_HEIGHT (font);
 
     /* The slot `encoding' specifies how to map a character
   fontp->height = FONT_HEIGHT (font);
 
     /* The slot `encoding' specifies how to map a character
@@ -771,12 +795,19 @@ struct font_info *w32_load_bdf_font (struct frame *f, char *fontname,
     fontp->relative_compose = bdf_font->relative_compose;
     fontp->default_ascent = bdf_font->default_ascent;
 
     fontp->relative_compose = bdf_font->relative_compose;
     fontp->default_ascent = bdf_font->default_ascent;
 
+    /* Set global flag fonts_changed_p to non-zero if the font loaded
+       has a character with a smaller width than any other character
+       before, or if the font loaded has a smaller height than any
+       other font loaded before.  If this happens, it will make a
+       glyph matrix reallocation necessary.  */
+    fonts_changed_p |= x_compute_min_glyph_bounds (f);
+
     UNBLOCK_INPUT;
     dpyinfo->n_fonts++;
     return fontp;
 }
 
     UNBLOCK_INPUT;
     dpyinfo->n_fonts++;
     return fontp;
 }
 
-/* Check a file for an XFLD string describing it.  */
+/* Check a file for an XLFD string describing it.  */
 int w32_BDF_to_x_font (char *file, char* xstr, int len)
 {
   HANDLE hfile, hfilemap;
 int w32_BDF_to_x_font (char *file, char* xstr, int len)
 {
   HANDLE hfile, hfilemap;
@@ -836,7 +867,11 @@ int w32_BDF_to_x_font (char *file, char* xstr, int len)
           retval = 1;
         }
     }
           retval = 1;
         }
     }
+  UnmapViewOfFile (font);
   CloseHandle (hfile);
   CloseHandle (hfilemap);
   return retval;
 }
   CloseHandle (hfile);
   CloseHandle (hfilemap);
   return retval;
 }
+
+/* arch-tag: 2e9a45de-0c54-4a0e-95c8-2d67b2b1fa32
+   (do not change this comment) */