This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SDL_rtf.c
204 lines (179 loc) · 5.57 KB
/
SDL_rtf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
SDL_rtf: A companion library to SDL for displaying RTF format text
Copyright (C) 2003-2020 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* $Id$ */
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#include "SDL_rtf.h"
#include "rtftype.h"
#include "rtfdecl.h"
#include "SDL_rtfreadr.h"
/* rcg06192001 get linked library's version. */
const SDL_version *RTF_Linked_Version(void)
{
static SDL_version linked_version;
SDL_RTF_VERSION(&linked_version);
return(&linked_version);
}
/* Create an RTF display context, with the given font engine.
* Once a context is created, it can be used to load and display
* text in Microsoft RTF format.
*/
RTF_Context *RTF_CreateContext(SDL_Renderer *renderer, RTF_FontEngine *fontEngine)
{
RTF_Context *ctx;
if ( fontEngine->version != RTF_FONT_ENGINE_VERSION ) {
RTF_SetError("Unknown font engine version");
return(NULL);
}
ctx = (RTF_Context *)malloc(sizeof(*ctx));
if ( ctx == NULL ) {
RTF_SetError("Out of memory");
return(NULL);
}
memset(ctx, 0, sizeof(*ctx));
ctx->renderer = renderer;
ctx->fontEngine = malloc(sizeof *fontEngine);
if ( ctx->fontEngine == NULL ) {
RTF_SetError("Out of memory");
free(ctx);
return(NULL);
}
memcpy(ctx->fontEngine, fontEngine, sizeof(*fontEngine));
return(ctx);
}
/* Set the text of an RTF context.
* This function returns 0 if it succeeds or -1 if it fails.
* Use RTF_GetError() to get a text message corresponding to the error.
*/
int RTF_Load_RW(RTF_Context *ctx, SDL_RWops *src, int freesrc)
{
int retval;
ecClearContext(ctx);
/* Set up the input stream for loading */
ctx->rds = 0;
ctx->ris = 0;
ctx->cbBin = 0;
ctx->fSkipDestIfUnk = 0;
ctx->stream = src;
ctx->nextch = -1;
/* Parse the RTF text and clean up */
switch(ecRtfParse(ctx)) {
case ecOK:
retval = 0;
break;
case ecStackUnderflow:
RTF_SetError("Unmatched '}'");
retval = -1;
break;
case ecStackOverflow:
RTF_SetError("Too many '{' -- memory exhausted");
retval = -1;
break;
case ecUnmatchedBrace:
RTF_SetError("RTF ended during an open group");
retval = -1;
break;
case ecInvalidHex:
RTF_SetError("Invalid hex character found in data");
retval = -1;
break;
case ecBadTable:
RTF_SetError("RTF table (sym or prop) invalid");
retval = -1;
break;
case ecAssertion:
RTF_SetError("Assertion failure");
retval = -1;
break;
case ecEndOfFile:
RTF_SetError("End of file reached while reading RTF");
retval = -1;
break;
case ecFontNotFound:
RTF_SetError("Couldn't find font for text");
retval = -1;
break;
}
while ( ctx->psave ) {
ecPopRtfState(ctx);
}
ctx->stream = NULL;
if ( freesrc ) {
SDL_RWclose(src);
}
return(retval);
}
int RTF_Load(RTF_Context *ctx, const char *file)
{
SDL_RWops *rw = SDL_RWFromFile(file, "rb");
if ( rw == NULL ) {
/*RTF_SetError(SDL_GetError());*/
return -1;
}
return RTF_Load_RW(ctx, rw, 1);
}
/* Get the title of an RTF document */
const char *RTF_GetTitle(RTF_Context *ctx)
{
return ctx->title ? ctx->title : "";
}
/* Get the subject of an RTF document */
const char *RTF_GetSubject(RTF_Context *ctx)
{
return ctx->subject ? ctx->subject : "";
}
/* Get the author of an RTF document */
const char *RTF_GetAuthor(RTF_Context *ctx)
{
return ctx->author ? ctx->author : "";
}
/* Get the height of an RTF render area given a certain width.
* The text is automatically reflowed to this new width, and should match
* the width of the clipping rectangle used for rendering later.
*/
int RTF_GetHeight(RTF_Context *ctx, int width)
{
ecReflowText(ctx, width);
return ctx->displayHeight;
}
/* Render the RTF document to a rectangle of a surface.
The text is reflowed to match the width of the rectangle.
The rendering is offset up (and clipped) by yOffset pixels.
*/
void RTF_Render(RTF_Context *ctx, SDL_Rect *rect, int yOffset)
{
SDL_Renderer *renderer = (SDL_Renderer *)ctx->renderer;
SDL_Rect fullRect;
if ( !rect ) {
SDL_RenderGetViewport(renderer, &fullRect);
fullRect.x = 0;
fullRect.y = 0;
rect = &fullRect;
}
ecRenderText(ctx, rect, -yOffset);
}
/* Free an RTF display context */
void RTF_FreeContext(RTF_Context *ctx)
{
/* Free it all! */
ecClearContext(ctx);
free(ctx->fontEngine);
free(ctx);
}
/* vi: set ts=4 sw=4 expandtab: */