]> git.sur5r.net Git - cc65/blob - libsrc/tgi/tgi_load_vectorfont.c
Fixed _textcolor definition.
[cc65] / libsrc / tgi / tgi_load_vectorfont.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                           tgi_load_vectorfont.c                           */
4 /*                                                                           */
5 /*                  Loader module for TGI vector font files                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2009,      Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40
41 #include <tgi.h>
42 #include <tgi/tgi-kernel.h>
43 #include <tgi/tgi-vectorfont.h>
44
45
46
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50
51
52
53 const tgi_vectorfont* __fastcall__ tgi_load_vectorfont (const char* name)
54 /* Load a vector font into memory and return it. In case of errors, NULL is
55 ** returned and an error is set, which can be retrieved using tgi_geterror.
56 ** To use the font, it has to be installed using tgi_install_vectorfont.
57 */
58 {
59     static const char Magic[4] = {
60         0x54, 0x43, 0x48, TGI_VF_VERSION
61     };
62
63     tgi_vectorfont_header H;
64     int                   F;
65     tgi_vectorfont*       Font = 0;
66     unsigned              V;
67     unsigned char         I;
68
69
70     /* Assume we have an error loading the font */
71     tgi_error = TGI_ERR_CANNOT_LOAD;
72
73     /* Open the file */
74     F = open (name, O_RDONLY);
75     if (F < 0) {
76         /* Cannot open file */
77         goto LoadError;
78     }
79
80     /* Read the header */
81     if (read (F, &H, sizeof (H)) != sizeof (H)) {
82         /* Cannot read header bytes */
83         goto LoadError;
84     }
85
86     /* Check the header */
87     if (memcmp (&H, Magic, sizeof (Magic)) != 0) {
88         /* Header magic not ok */
89         goto LoadError;
90     }
91
92     /* Allocate memory for the data */
93     Font = malloc (H.size);
94     if (Font == 0) {
95         /* Out of memory */
96         tgi_error = TGI_ERR_NO_RES;
97         goto LoadError;
98     }
99
100     /* Read the whole font file into the buffer */
101     if (read (F, Font, H.size) != H.size) {
102         /* Problem reading font data */
103         free (Font);
104         goto LoadError;
105     }
106
107     /* Close the file */
108     close (F);
109
110     /* Fix the offset pointers. When loaded, they contain numeric offsets
111     ** into the VectorOps, with the start of the VectorOps at offset zero.
112     ** We will add a pointer to the VectorOps to make them actual pointers
113     ** that may be used independently from anything else.
114     */
115     V = (unsigned) &Font->vec_ops;
116     for (I = 0; I < TGI_VF_CCOUNT; ++I) {
117         Font->chars[I] += V;
118     }
119
120     /* Clear the error */
121     tgi_error = TGI_ERR_OK;
122
123     /* Return the vector font loaded */
124     return Font;
125
126 LoadError:
127     /* Some sort of load problem. If the file is still open, be sure to
128     ** close it
129     */
130     if (F >= 0) {
131         close (F);
132     }
133     return 0;
134 }
135
136
137