Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit f5944ab

Browse files
committed
[[ SVG ]] Static Viewport
This patch changes the drawing format and svg compiler so that a fixed width/height is *always* present in the header - this much better reflects that drawings are currently entirely static and simplifies the compiling of SVG files in that form. Additionally it also means that a suitable size is always available - which is important for drawings to behave like images, as images require a 'pixel size' for various operations in the engine.
1 parent 1a2b958 commit f5944ab

File tree

3 files changed

+186
-607
lines changed

3 files changed

+186
-607
lines changed

engine/src/iimport.cpp

Lines changed: 25 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -56,145 +56,41 @@ bool read_all(IO_handle p_stream, uint8_t *&r_data, uindex_t &r_data_size)
5656
return t_success;
5757
}
5858

59-
#define META_HEAD_SIZE 44
60-
#define EMF_HEAD_SIZE 80
59+
#define DRAWING_HEAD_SIZE 12
6160
bool MCImageGetMetafileGeometry(IO_handle p_stream, uindex_t &r_width, uindex_t &r_height)
6261
{
6362
bool t_success = true;
6463
bool t_is_meta = false;
6564

6665
uindex_t t_stream_pos = MCS_tell(p_stream);
67-
uint8_t t_head[EMF_HEAD_SIZE];
68-
uindex_t t_size = META_HEAD_SIZE;
66+
uint8_t t_head[DRAWING_HEAD_SIZE];
67+
uindex_t t_size = DRAWING_HEAD_SIZE;
6968

7069
t_success = IO_NORMAL == MCS_readfixed(t_head, t_size, p_stream) &&
71-
t_size == META_HEAD_SIZE;
72-
73-
if (t_success)
70+
t_size == DRAWING_HEAD_SIZE;
71+
72+
/* A drawing's header is of the form:
73+
* 0: LCD\0
74+
* 4: width
75+
* 8: height
76+
*/
77+
if (t_success &&
78+
memcmp(t_head, "LCD\0", 4) == 0)
7479
{
75-
if (memcmp(t_head, "LCD\0", 4) == 0)
76-
{
77-
/* A drawing's header is of the form:
78-
* 0: LCD\0
79-
* 4: flags
80-
* bit 0: has width
81-
* bit 1: has height
82-
* bit 2: has viewport
83-
* 8: scalar_count
84-
* 12: width if 'has width'
85-
* 16: height if 'has height'
86-
* 20: viewbox if 'has viewport'.
87-
*
88-
* If a drawing has a width >= 0 then that is its intrinsic width
89-
* Else if it has a viewport then the viewbox width is its intrinsic width
90-
* Else the intrinsic width is unknown
91-
*
92-
* If a drawing has a height >= 0, then that is its intrinsic height
93-
* Else if it has a viewport, then the viewbox height is its intrisic width
94-
* Else the intrisic height is unknown
95-
*
96-
* Currently unknown intrinsic width/height is reported as 256.
97-
*/
98-
uint32_t t_flags = *(uint32_t *)(t_head + 4);
99-
const float *t_scalars = (const float *)(t_head + 12);
100-
float t_width = -1, t_height = -1;
101-
102-
/* Get the width, if the width flag (bit 0) is set. */
103-
if ((t_flags & (1 << 0)) != 0)
104-
{
105-
t_width = *t_scalars++;
106-
}
107-
108-
/* Get the height, if the height flag (bit 1) is set. */
109-
if ((t_flags & (1 << 1)) != 0)
110-
{
111-
t_height = *t_scalars++;
112-
}
113-
114-
/* Get the viewbox width/height, if the viewport flag (bit 2) is set. */
115-
if ((t_flags & (1 << 2)) != 0)
116-
{
117-
auto t_viewbox = reinterpret_cast<const MCGRectangle*>(t_scalars);
118-
119-
if (t_width < 0)
120-
{
121-
t_width = t_viewbox->size.width;
122-
}
123-
if (t_height < 0)
124-
{
125-
t_height = t_viewbox->size.height;
126-
}
127-
}
128-
129-
/* If the width is not absolute still, take a default of 256. */
130-
if (t_width < 0)
131-
{
132-
t_width = 256;
133-
}
134-
135-
/* If the height is not absolute still, take a default of 256. */
136-
if (t_height < 0)
137-
{
138-
t_height = 256;
139-
}
140-
141-
/* The engine deals with integer bounds on sub-pixel co-ords, so
142-
* take the ceiling of the computer (float) width/height. */
143-
r_width = (uindex_t)ceilf(t_width);
144-
r_height = (uindex_t)ceilf(t_height);
145-
146-
t_is_meta = true;
147-
}
148-
// graphics metafile (wmf)
149-
else if (memcmp(t_head, "\xD7\xCD\xC6\x9A", 4) == 0)
150-
{
151-
int16_t *t_bounds = (int16_t *)&t_head[6];
152-
r_width = ((t_bounds[2] - t_bounds[0]) * 72 + t_bounds[4] - 1) / t_bounds[4];
153-
r_height = ((t_bounds[3] - t_bounds[1]) * 72 + t_bounds[4] - 1) / t_bounds[4];
154-
t_is_meta = true;
155-
}
156-
// win32 metafile (emf)
157-
else if (memcmp(&t_head[40], "\x20\x45\x4D\x46", 4) == 0)
158-
{
159-
t_success =
160-
IO_NORMAL == MCS_seek_set(p_stream, t_stream_pos) &&
161-
IO_NORMAL == MCS_readfixed(t_head, EMF_HEAD_SIZE, p_stream);
162-
163-
if (t_success)
164-
{
165-
int32_t *t_bounds;
166-
t_bounds = (int32_t*)&t_head[72];
167-
r_width = t_bounds[0];
168-
r_height = t_bounds[1];
169-
t_is_meta = true;
170-
}
171-
}
172-
else
173-
{
174-
uindex_t t_offset = 0;
175-
if (memcmp(t_head, "\0\0\0\0\0\0\0\0", 8) == 0)
176-
{
177-
t_offset = 512;
178-
t_success =
179-
IO_NORMAL == MCS_seek_set(p_stream, t_stream_pos + t_offset) &&
180-
IO_NORMAL == MCS_readfixed(t_head, META_HEAD_SIZE, p_stream);
181-
}
182-
183-
// PICT file
184-
if (t_success && memcmp(&t_head[10], "\0\021\002\377", 4) == 0)
185-
{
186-
uint16_t *t_bounds = (uint16_t *)t_head;
187-
r_width = swap_uint2(&t_bounds[4]) - swap_uint2(&t_bounds[2]);
188-
r_height = swap_uint2(&t_bounds[3]) - swap_uint2(&t_bounds[1]);
189-
t_is_meta = true;
190-
t_stream_pos += t_offset;
191-
}
192-
}
193-
}
194-
80+
float t_width = *(const float *)(t_head + 4);
81+
float t_height = *(const float *)(t_head + 8);
82+
83+
/* The engine deals with integer bounds on sub-pixel co-ords, so
84+
* take the ceiling of the computer (float) width/height. */
85+
r_width = (uindex_t)ceilf(t_width);
86+
r_height = (uindex_t)ceilf(t_height);
87+
88+
t_is_meta = true;
89+
}
90+
19591
MCS_seek_set(p_stream, t_stream_pos);
196-
197-
return t_success && t_is_meta;
92+
93+
return t_success && t_is_meta;
19894
}
19995

20096
bool MCImageBitmapApplyMask(MCImageBitmap *p_bitmap, MCImageBitmap *p_mask)

0 commit comments

Comments
 (0)