Use `Control_Type' to handle different segment directions.
[ttfautohint.git] / lib / tafile.c
blobadff99ca35a8b6803f840395a24bb63a062c3b46
1 /* tafile.c */
3 /*
4 * Copyright (C) 2011-2014 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 #include "ta.h"
19 #define BUF_SIZE 0x10000
21 FT_Error
22 TA_font_file_read(FONT* font,
23 FILE* in_file)
25 FT_Byte buf[BUF_SIZE];
26 size_t in_len = 0;
27 size_t read_bytes;
30 font->in_buf = (FT_Byte*)malloc(BUF_SIZE);
31 if (!font->in_buf)
32 return FT_Err_Out_Of_Memory;
34 while ((read_bytes = fread(buf, 1, BUF_SIZE, in_file)) > 0)
36 FT_Byte* in_buf_new;
39 in_buf_new = (FT_Byte*)realloc(font->in_buf, in_len + read_bytes);
40 if (!in_buf_new)
41 return FT_Err_Out_Of_Memory;
42 else
43 font->in_buf = in_buf_new;
45 memcpy(font->in_buf + in_len, buf, read_bytes);
47 in_len += read_bytes;
50 if (ferror(in_file))
51 return FT_Err_Invalid_Stream_Read;
53 /* a valid TTF can never be that small */
54 if (in_len < 100)
55 return TA_Err_Invalid_Font_Type;
57 font->in_len = in_len;
59 return TA_Err_Ok;
63 FT_Error
64 TA_font_file_write(FONT* font,
65 FILE* out_file)
67 if (fwrite(font->out_buf, 1, font->out_len, out_file) != font->out_len)
68 return TA_Err_Invalid_Stream_Write;
70 return TA_Err_Ok;
74 FT_Error
75 TA_control_file_read(FONT* font,
76 FILE* control_file)
78 char* buf[BUF_SIZE];
79 size_t control_len = 0;
80 size_t read_bytes;
83 font->control_buf = (char*)malloc(BUF_SIZE);
84 if (!font->control_buf)
85 return FT_Err_Out_Of_Memory;
87 while ((read_bytes = fread(buf, 1, BUF_SIZE, control_file)) > 0)
89 char* control_buf_new;
92 /* we store the data as a C string, allocating one more byte */
93 control_buf_new = (char*)realloc(font->control_buf,
94 control_len + read_bytes + 1);
95 if (!control_buf_new)
96 return FT_Err_Out_Of_Memory;
97 else
98 font->control_buf = control_buf_new;
100 memcpy(font->control_buf + control_len, buf, read_bytes);
102 control_len += read_bytes;
105 if (ferror(control_file))
106 return FT_Err_Invalid_Stream_Read;
108 font->control_len = control_len;
109 font->control_buf[control_len] = '\0';
111 return TA_Err_Ok;
114 /* end of tafile.c */