File: pps-render-context.c

package info (click to toggle)
papers 49.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 21,044 kB
  • sloc: ansic: 37,728; sh: 197; xml: 127; makefile: 113
file content (196 lines) | stat: -rw-r--r-- 4,962 bytes parent folder | download | duplicates (2)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/* this file is part of papers, a gnome document viewer
 *
 *  Copyright (C) 2005 Jonathan Blandford <jrb@gnome.org>
 */

#include "pps-render-context.h"
#include <config.h>

static void pps_render_context_init (PpsRenderContext *rc);
static void pps_render_context_class_init (PpsRenderContextClass *class);

G_DEFINE_TYPE (PpsRenderContext, pps_render_context, G_TYPE_OBJECT);

#define FLIP_DIMENSIONS(rc) ((rc)->rotation == 90 || (rc)->rotation == 270)

static void
pps_render_context_init (PpsRenderContext *rc)
{ /* Do Nothing */
}

static void
pps_render_context_dispose (GObject *object)
{
	PpsRenderContext *rc;

	rc = (PpsRenderContext *) object;

	g_clear_object (&rc->page);

	(*G_OBJECT_CLASS (pps_render_context_parent_class)->dispose) (object);
}

static void
pps_render_context_class_init (PpsRenderContextClass *class)
{
	GObjectClass *oclass;

	oclass = G_OBJECT_CLASS (class);

	oclass->dispose = pps_render_context_dispose;
}

PpsRenderContext *
pps_render_context_new (PpsPage *page,
                        gint rotation,
                        gdouble scale,
                        PpsRenderAnnotsFlags annot_flags)
{
	PpsRenderContext *rc;

	rc = (PpsRenderContext *) g_object_new (PPS_TYPE_RENDER_CONTEXT, NULL);

	rc->page = page ? g_object_ref (page) : NULL;
	rc->rotation = rotation;
	rc->scale = scale;
	rc->target_width = -1;
	rc->target_height = -1;
	rc->annot_flags = annot_flags;

	return rc;
}

void
pps_render_context_set_page (PpsRenderContext *rc,
                             PpsPage *page)
{
	g_return_if_fail (rc != NULL);
	g_return_if_fail (PPS_IS_PAGE (page));

	g_set_object (&rc->page, page);
}

void
pps_render_context_set_rotation (PpsRenderContext *rc,
                                 int rotation)
{
	g_return_if_fail (rc != NULL);

	rc->rotation = rotation;
}

void
pps_render_context_set_scale (PpsRenderContext *rc,
                              gdouble scale)
{
	g_return_if_fail (rc != NULL);

	rc->scale = scale;
}

void
pps_render_context_set_target_size (PpsRenderContext *rc,
                                    int target_width,
                                    int target_height)
{
	g_return_if_fail (rc != NULL);

	rc->target_width = target_width;
	rc->target_height = target_height;
}

/**
 * pps_render_context_compute_scaled_size:
 * @rc: an #PpsRenderContext
 * @width_points:
 * @height_points:
 * @scaled_width: (out):
 * @scaled_height: (out):
 *
 */
void
pps_render_context_compute_scaled_size (PpsRenderContext *rc,
                                        double width_points,
                                        double height_points,
                                        int *scaled_width,
                                        int *scaled_height)
{
	g_return_if_fail (rc != NULL);

	if (scaled_width) {
		if (rc->target_width >= 0) {
			*scaled_width = FLIP_DIMENSIONS (rc) ? rc->target_height : rc->target_width;
		} else {
			*scaled_width = (int) (width_points * rc->scale + 0.5);
		}
	}

	if (scaled_height) {
		if (rc->target_height >= 0) {
			*scaled_height = FLIP_DIMENSIONS (rc) ? rc->target_width : rc->target_height;
		} else {
			*scaled_height = (int) (height_points * rc->scale + 0.5);
		}
	}
}

/**
 * pps_render_context_compute_transformed_size:
 * @rc: an #PpsRenderContext
 * @width_points:
 * @height_points:
 * @transformed_width: (out):
 * @transformed_height: (out):
 *
 */
void
pps_render_context_compute_transformed_size (PpsRenderContext *rc,
                                             double width_points,
                                             double height_points,
                                             int *transformed_width,
                                             int *transformed_height)
{
	int scaled_width, scaled_height;

	g_return_if_fail (rc != NULL);

	pps_render_context_compute_scaled_size (rc, width_points, height_points,
	                                        &scaled_width, &scaled_height);

	if (transformed_width)
		*transformed_width = FLIP_DIMENSIONS (rc) ? scaled_height : scaled_width;

	if (transformed_height)
		*transformed_height = FLIP_DIMENSIONS (rc) ? scaled_width : scaled_height;
}

/**
 * pps_render_context_compute_scales:
 * @rc: an #PpsRenderContext
 * @width_points:
 * @height_points:
 * @scale_x: (out):
 * @scale_y: (out):
 *
 */
void
pps_render_context_compute_scales (PpsRenderContext *rc,
                                   double width_points,
                                   double height_points,
                                   double *scale_x,
                                   double *scale_y)
{
	int scaled_width, scaled_height;

	g_return_if_fail (rc != NULL);

	pps_render_context_compute_scaled_size (rc, width_points, height_points,
	                                        &scaled_width, &scaled_height);

	if (scale_x)
		*scale_x = scaled_width / width_points;

	if (scale_y)
		*scale_y = scaled_height / height_points;
}