~ubuntu-branches/ubuntu/vivid/libspectre/vivid-proposed

« back to all changes in this revision

Viewing changes to libspectre/spectre-page.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Rosewarne
  • Date: 2008-01-06 21:58:55 UTC
  • Revision ID: james.westby@ubuntu.com-20080106215855-zmmkfhf4m3190zsj
Tags: upstream-0.2.0.ds
ImportĀ upstreamĀ versionĀ 0.2.0.ds

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Libspectre.
 
2
 * 
 
3
 * Copyright (C) 2007 Albert Astals Cid <aacid@kde.org>
 
4
 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
 
5
 *
 
6
 * Libspectre is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2, or (at your option)
 
9
 * any later version.
 
10
 *
 
11
 * Libspectre is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 */
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
 
 
24
#include "spectre-page.h"
 
25
 
 
26
#include "spectre-device.h"
 
27
#include "spectre-private.h"
 
28
#include "spectre-utils.h"
 
29
 
 
30
struct SpectrePage
 
31
{
 
32
        struct document *doc;
 
33
        
 
34
        SpectreStatus    status;
 
35
 
 
36
        unsigned int     index;
 
37
        int              width;
 
38
        int              height;
 
39
};
 
40
 
 
41
SpectrePage *
 
42
_spectre_page_new (unsigned int     page_index,
 
43
                   struct document *doc)
 
44
{
 
45
        SpectrePage *page;
 
46
 
 
47
        page = calloc (1, sizeof (SpectrePage));
 
48
        if (!page)
 
49
                return NULL;
 
50
 
 
51
        page->index = page_index;
 
52
        page->width = -1;
 
53
        page->height = -1;
 
54
        page->doc = psdocreference (doc);
 
55
 
 
56
        return page;
 
57
}
 
58
 
 
59
void
 
60
spectre_page_free (SpectrePage *page)
 
61
{
 
62
        if (!page)
 
63
                return;
 
64
 
 
65
        if (page->doc) {
 
66
                psdocdestroy (page->doc);
 
67
                page->doc = NULL;
 
68
        }
 
69
        
 
70
        free (page);
 
71
}
 
72
 
 
73
SpectreStatus
 
74
spectre_page_status (SpectrePage *page)
 
75
{
 
76
        _spectre_return_val_if_fail (page != NULL, SPECTRE_STATUS_INVALID_PAGE);
 
77
        
 
78
        return page->status;
 
79
}
 
80
 
 
81
unsigned int
 
82
spectre_page_get_index (SpectrePage *page)
 
83
{
 
84
        _spectre_return_val_if_fail (page != NULL, 0);
 
85
        
 
86
        return page->index;
 
87
}
 
88
 
 
89
const char *
 
90
spectre_page_get_label (SpectrePage *page)
 
91
{
 
92
        _spectre_return_val_if_fail (page != NULL, NULL);
 
93
        
 
94
        return page->doc->numpages > 0 ? page->doc->pages[page->index].label : NULL;
 
95
}
 
96
 
 
97
SpectreOrientation
 
98
spectre_page_get_orientation (SpectrePage *page)
 
99
{
 
100
        int page_orientation = NONE;
 
101
 
 
102
        _spectre_return_val_if_fail (page != NULL, SPECTRE_ORIENTATION_PORTRAIT);
 
103
 
 
104
        if (page->doc->numpages > 0) {
 
105
                page_orientation = page->doc->pages[page->index].orientation != NONE ?
 
106
                        page->doc->pages[page->index].orientation :
 
107
                        page->doc->default_page_orientation;
 
108
        }
 
109
 
 
110
        if (page_orientation == NONE)
 
111
                page_orientation = page->doc->orientation;
 
112
 
 
113
        switch (page_orientation) {
 
114
        default:
 
115
        case PORTRAIT:
 
116
                return SPECTRE_ORIENTATION_PORTRAIT;
 
117
        case LANDSCAPE:
 
118
                return SPECTRE_ORIENTATION_LANDSCAPE;
 
119
        case SEASCAPE:
 
120
                return SPECTRE_ORIENTATION_REVERSE_LANDSCAPE;
 
121
        case UPSIDEDOWN:
 
122
                return SPECTRE_ORIENTATION_REVERSE_PORTRAIT;
 
123
        }
 
124
}
 
125
 
 
126
void
 
127
spectre_page_get_size (SpectrePage *page,
 
128
                       int         *width,
 
129
                       int         *height)
 
130
{
 
131
        _spectre_return_if_fail (page != NULL);
 
132
 
 
133
        if (page->width == -1 || page->height == -1) {
 
134
                int urx, ury, llx, lly;
 
135
        
 
136
                psgetpagebox (page->doc, page->index,
 
137
                              &urx, &ury, &llx, &lly);
 
138
 
 
139
                page->width = urx - llx;
 
140
                page->height = ury - lly;
 
141
        }
 
142
 
 
143
        if (width)
 
144
                *width = page->width;
 
145
        if (height)
 
146
                *height = page->height;
 
147
}
 
148
 
 
149
void
 
150
spectre_page_render (SpectrePage          *page,
 
151
                     SpectreRenderContext *rc,
 
152
                     unsigned char       **page_data,
 
153
                     int                  *row_length)
 
154
{
 
155
        SpectreDevice *device;
 
156
        int            width, height;
 
157
 
 
158
        _spectre_return_if_fail (page != NULL);
 
159
        _spectre_return_if_fail (rc != NULL);
 
160
 
 
161
        spectre_page_get_size (page, &width, &height);
 
162
 
 
163
        device = spectre_device_new (page->doc);
 
164
        page->status = spectre_device_render (device, page->index, rc,
 
165
                                              0, 0, width, height,
 
166
                                              page_data, row_length);
 
167
        spectre_device_free (device);
 
168
}
 
169
 
 
170
void
 
171
spectre_page_render_slice (SpectrePage          *page,
 
172
                           SpectreRenderContext *rc,
 
173
                           int                   x,
 
174
                           int                   y,
 
175
                           int                   width,
 
176
                           int                   height,
 
177
                           unsigned char       **page_data,
 
178
                           int                  *row_length)
 
179
{
 
180
        SpectreDevice *device;
 
181
        int            page_height;
 
182
 
 
183
        _spectre_return_if_fail (page != NULL);
 
184
        _spectre_return_if_fail (rc != NULL);
 
185
 
 
186
        spectre_page_get_size (page, NULL, &page_height);
 
187
 
 
188
        device = spectre_device_new (page->doc);
 
189
        page->status = spectre_device_render (device, page->index, rc,
 
190
                                              x, page_height - (y + height),
 
191
                                              width, height,
 
192
                                              page_data, row_length);
 
193
        spectre_device_free (device);
 
194
}