Coverage Report

Created: 2025-07-23 09:13

/src/gdal/frmts/pdf/pdfcreatecopy.cpp
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Project:  PDF driver
4
 * Purpose:  GDALDataset driver for PDF dataset.
5
 * Author:   Even Rouault, <even dot rouault at spatialys.com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2012-2019, Even Rouault <even dot rouault at spatialys.com>
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#include "gdal_pdf.h"
14
#include "pdfcreatecopy.h"
15
16
#include "cpl_vsi_virtual.h"
17
#include "cpl_conv.h"
18
#include "cpl_error.h"
19
#include "ogr_spatialref.h"
20
#include "ogr_geometry.h"
21
#include "memdataset.h"
22
#include "vrtdataset.h"
23
24
#include "pdfobject.h"
25
26
#include <cmath>
27
#include <algorithm>
28
#include <utility>
29
#include <vector>
30
31
// #define HACK_TO_GENERATE_OCMD can be set to produce a (single layer)
32
// non-structured vector PDF with a OCMD (Optional Content Group Membership
33
// Dictionary) similar to test case of https://github.com/OSGeo/gdal/issues/8372
34
// like with "ogr2ogr poly.pdf poly.shp -dsco STREAM_COMPRESS=NONE -limit 1"
35
36
0
GDALFakePDFDataset::~GDALFakePDFDataset() = default;
37
38
/************************************************************************/
39
/*                        GDALPDFBaseWriter()                           */
40
/************************************************************************/
41
42
756
GDALPDFBaseWriter::GDALPDFBaseWriter(VSILFILE *fp) : m_fp(fp)
43
756
{
44
756
}
45
46
/************************************************************************/
47
/*                       ~GDALPDFBaseWriter()                           */
48
/************************************************************************/
49
50
GDALPDFBaseWriter::~GDALPDFBaseWriter()
51
756
{
52
756
    Close();
53
756
}
54
55
/************************************************************************/
56
/*                              ~Close()                                */
57
/************************************************************************/
58
59
void GDALPDFBaseWriter::Close()
60
2.26k
{
61
2.26k
    if (m_fp)
62
756
    {
63
756
        VSIFCloseL(m_fp);
64
756
        m_fp = nullptr;
65
756
    }
66
2.26k
}
67
68
/************************************************************************/
69
/*                           GDALPDFUpdateWriter()                      */
70
/************************************************************************/
71
72
0
GDALPDFUpdateWriter::GDALPDFUpdateWriter(VSILFILE *fp) : GDALPDFBaseWriter(fp)
73
0
{
74
0
}
75
76
/************************************************************************/
77
/*                          ~GDALPDFUpdateWriter()                      */
78
/************************************************************************/
79
80
GDALPDFUpdateWriter::~GDALPDFUpdateWriter()
81
0
{
82
0
    Close();
83
0
}
84
85
/************************************************************************/
86
/*                              ~Close()                                */
87
/************************************************************************/
88
89
void GDALPDFUpdateWriter::Close()
90
0
{
91
0
    if (m_fp)
92
0
    {
93
0
        CPLAssert(!m_bInWriteObj);
94
0
        if (m_bUpdateNeeded)
95
0
        {
96
0
            WriteXRefTableAndTrailer(true, m_nLastStartXRef);
97
0
        }
98
0
    }
99
0
    GDALPDFBaseWriter::Close();
100
0
}
101
102
/************************************************************************/
103
/*                          StartNewDoc()                               */
104
/************************************************************************/
105
106
void GDALPDFBaseWriter::StartNewDoc()
107
756
{
108
756
    VSIFPrintfL(m_fp, "%%PDF-1.6\n");
109
110
    // See PDF 1.7 reference, page 92. Write 4 non-ASCII bytes to indicate
111
    // that the content will be binary.
112
756
    VSIFPrintfL(m_fp, "%%%c%c%c%c\n", 0xFF, 0xFF, 0xFF, 0xFF);
113
114
756
    m_nPageResourceId = AllocNewObject();
115
756
    m_nCatalogId = AllocNewObject();
116
756
}
117
118
/************************************************************************/
119
/*                         GDALPDFWriter()                              */
120
/************************************************************************/
121
122
756
GDALPDFWriter::GDALPDFWriter(VSILFILE *fpIn) : GDALPDFBaseWriter(fpIn)
123
756
{
124
756
    StartNewDoc();
125
756
}
126
127
/************************************************************************/
128
/*                         ~GDALPDFWriter()                             */
129
/************************************************************************/
130
131
GDALPDFWriter::~GDALPDFWriter()
132
756
{
133
756
    Close();
134
756
}
135
136
/************************************************************************/
137
/*                          ParseIndirectRef()                          */
138
/************************************************************************/
139
140
static int ParseIndirectRef(const char *pszStr, GDALPDFObjectNum &nNum,
141
                            int &nGen)
142
0
{
143
0
    while (*pszStr == ' ')
144
0
        pszStr++;
145
146
0
    nNum = atoi(pszStr);
147
0
    while (*pszStr >= '0' && *pszStr <= '9')
148
0
        pszStr++;
149
0
    if (*pszStr != ' ')
150
0
        return FALSE;
151
152
0
    while (*pszStr == ' ')
153
0
        pszStr++;
154
155
0
    nGen = atoi(pszStr);
156
0
    while (*pszStr >= '0' && *pszStr <= '9')
157
0
        pszStr++;
158
0
    if (*pszStr != ' ')
159
0
        return FALSE;
160
161
0
    while (*pszStr == ' ')
162
0
        pszStr++;
163
164
0
    return *pszStr == 'R';
165
0
}
166
167
/************************************************************************/
168
/*                       ParseTrailerAndXRef()                          */
169
/************************************************************************/
170
171
int GDALPDFUpdateWriter::ParseTrailerAndXRef()
172
0
{
173
0
    VSIFSeekL(m_fp, 0, SEEK_END);
174
0
    char szBuf[1024 + 1];
175
0
    vsi_l_offset nOffset = VSIFTellL(m_fp);
176
177
0
    if (nOffset > 128)
178
0
        nOffset -= 128;
179
0
    else
180
0
        nOffset = 0;
181
182
    /* Find startxref section */
183
0
    VSIFSeekL(m_fp, nOffset, SEEK_SET);
184
0
    int nRead = static_cast<int>(VSIFReadL(szBuf, 1, 128, m_fp));
185
0
    szBuf[nRead] = 0;
186
0
    if (nRead < 9)
187
0
        return FALSE;
188
189
0
    const char *pszStartXRef = nullptr;
190
0
    int i;
191
0
    for (i = nRead - 9; i >= 0; i--)
192
0
    {
193
0
        if (STARTS_WITH(szBuf + i, "startxref"))
194
0
        {
195
0
            pszStartXRef = szBuf + i;
196
0
            break;
197
0
        }
198
0
    }
199
0
    if (pszStartXRef == nullptr)
200
0
    {
201
0
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot find startxref");
202
0
        return FALSE;
203
0
    }
204
0
    pszStartXRef += 9;
205
0
    while (*pszStartXRef == '\r' || *pszStartXRef == '\n')
206
0
        pszStartXRef++;
207
0
    if (*pszStartXRef == '\0')
208
0
    {
209
0
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot find startxref");
210
0
        return FALSE;
211
0
    }
212
213
0
    m_nLastStartXRef = CPLScanUIntBig(pszStartXRef, 16);
214
215
    /* Skip to beginning of xref section */
216
0
    VSIFSeekL(m_fp, m_nLastStartXRef, SEEK_SET);
217
218
    /* And skip to trailer */
219
0
    const char *pszLine = nullptr;
220
0
    while ((pszLine = CPLReadLineL(m_fp)) != nullptr)
221
0
    {
222
0
        if (STARTS_WITH(pszLine, "trailer"))
223
0
            break;
224
0
    }
225
226
0
    if (pszLine == nullptr)
227
0
    {
228
0
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot find trailer");
229
0
        return FALSE;
230
0
    }
231
232
    /* Read trailer content */
233
0
    nRead = static_cast<int>(VSIFReadL(szBuf, 1, 1024, m_fp));
234
0
    szBuf[nRead] = 0;
235
236
    /* Find XRef size */
237
0
    const char *pszSize = strstr(szBuf, "/Size");
238
0
    if (pszSize == nullptr)
239
0
    {
240
0
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot find trailer /Size");
241
0
        return FALSE;
242
0
    }
243
0
    pszSize += 5;
244
0
    while (*pszSize == ' ')
245
0
        pszSize++;
246
0
    m_nLastXRefSize = atoi(pszSize);
247
248
    /* Find Root object */
249
0
    const char *pszRoot = strstr(szBuf, "/Root");
250
0
    if (pszRoot == nullptr)
251
0
    {
252
0
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot find trailer /Root");
253
0
        return FALSE;
254
0
    }
255
0
    pszRoot += 5;
256
0
    while (*pszRoot == ' ')
257
0
        pszRoot++;
258
259
0
    if (!ParseIndirectRef(pszRoot, m_nCatalogId, m_nCatalogGen))
260
0
    {
261
0
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot parse trailer /Root");
262
0
        return FALSE;
263
0
    }
264
265
    /* Find Info object */
266
0
    const char *pszInfo = strstr(szBuf, "/Info");
267
0
    if (pszInfo != nullptr)
268
0
    {
269
0
        pszInfo += 5;
270
0
        while (*pszInfo == ' ')
271
0
            pszInfo++;
272
273
0
        if (!ParseIndirectRef(pszInfo, m_nInfoId, m_nInfoGen))
274
0
        {
275
0
            CPLError(CE_Failure, CPLE_AppDefined, "Cannot parse trailer /Info");
276
0
            m_nInfoId = 0;
277
0
            m_nInfoGen = 0;
278
0
        }
279
0
    }
280
281
0
    VSIFSeekL(m_fp, 0, SEEK_END);
282
283
0
    return TRUE;
284
0
}
285
286
/************************************************************************/
287
/*                              Close()                                 */
288
/************************************************************************/
289
290
void GDALPDFWriter::Close()
291
1.51k
{
292
1.51k
    if (m_fp)
293
756
    {
294
756
        CPLAssert(!m_bInWriteObj);
295
756
        if (m_nPageResourceId.toBool())
296
756
        {
297
756
            WritePages();
298
756
            WriteXRefTableAndTrailer(false, 0);
299
756
        }
300
756
    }
301
1.51k
    GDALPDFBaseWriter::Close();
302
1.51k
}
303
304
/************************************************************************/
305
/*                           UpdateProj()                               */
306
/************************************************************************/
307
308
void GDALPDFUpdateWriter::UpdateProj(GDALDataset *poSrcDS, double dfDPI,
309
                                     GDALPDFDictionaryRW *poPageDict,
310
                                     const GDALPDFObjectNum &nPageId,
311
                                     int nPageGen)
312
0
{
313
0
    m_bUpdateNeeded = true;
314
0
    if (static_cast<int>(m_asXRefEntries.size()) < m_nLastXRefSize - 1)
315
0
        m_asXRefEntries.resize(m_nLastXRefSize - 1);
316
317
0
    GDALPDFObjectNum nViewportId;
318
0
    GDALPDFObjectNum nLGIDictId;
319
320
0
    CPLAssert(nPageId.toBool());
321
0
    CPLAssert(poPageDict != nullptr);
322
323
0
    PDFMargins sMargins;
324
325
0
    const char *pszGEO_ENCODING =
326
0
        CPLGetConfigOption("GDAL_PDF_GEO_ENCODING", "ISO32000");
327
0
    if (EQUAL(pszGEO_ENCODING, "ISO32000") || EQUAL(pszGEO_ENCODING, "BOTH"))
328
0
        nViewportId = WriteSRS_ISO32000(poSrcDS, dfDPI * USER_UNIT_IN_INCH,
329
0
                                        nullptr, &sMargins, TRUE);
330
331
#ifdef invalidate_xref_entry
332
    GDALPDFObject *poVP = poPageDict->Get("VP");
333
    if (poVP)
334
    {
335
        if (poVP->GetType() == PDFObjectType_Array &&
336
            poVP->GetArray()->GetLength() == 1)
337
            poVP = poVP->GetArray()->Get(0);
338
339
        int nVPId = poVP->GetRefNum();
340
        if (nVPId)
341
        {
342
            m_asXRefEntries[nVPId - 1].bFree = TRUE;
343
            m_asXRefEntries[nVPId - 1].nGen++;
344
        }
345
    }
346
#endif
347
348
0
    poPageDict->Remove("VP");
349
0
    poPageDict->Remove("LGIDict");
350
351
0
    if (nViewportId.toBool())
352
0
    {
353
0
        poPageDict->Add("VP", &((new GDALPDFArrayRW())->Add(nViewportId, 0)));
354
0
    }
355
356
0
    if (nLGIDictId.toBool())
357
0
    {
358
0
        poPageDict->Add("LGIDict", nLGIDictId, 0);
359
0
    }
360
361
0
    StartObj(nPageId, nPageGen);
362
0
    VSIFPrintfL(m_fp, "%s\n", poPageDict->Serialize().c_str());
363
0
    EndObj();
364
0
}
365
366
/************************************************************************/
367
/*                           UpdateInfo()                               */
368
/************************************************************************/
369
370
void GDALPDFUpdateWriter::UpdateInfo(GDALDataset *poSrcDS)
371
0
{
372
0
    m_bUpdateNeeded = true;
373
0
    if (static_cast<int>(m_asXRefEntries.size()) < m_nLastXRefSize - 1)
374
0
        m_asXRefEntries.resize(m_nLastXRefSize - 1);
375
376
0
    auto nNewInfoId = SetInfo(poSrcDS, nullptr);
377
    /* Write empty info, because podofo driver will find the dangling info
378
     * instead */
379
0
    if (!nNewInfoId.toBool() && m_nInfoId.toBool())
380
0
    {
381
#ifdef invalidate_xref_entry
382
        m_asXRefEntries[m_nInfoId.toInt() - 1].bFree = TRUE;
383
        m_asXRefEntries[m_nInfoId.toInt() - 1].nGen++;
384
#else
385
0
        StartObj(m_nInfoId, m_nInfoGen);
386
0
        VSIFPrintfL(m_fp, "<< >>\n");
387
0
        EndObj();
388
0
#endif
389
0
    }
390
0
}
391
392
/************************************************************************/
393
/*                           UpdateXMP()                                */
394
/************************************************************************/
395
396
void GDALPDFUpdateWriter::UpdateXMP(GDALDataset *poSrcDS,
397
                                    GDALPDFDictionaryRW *poCatalogDict)
398
0
{
399
0
    m_bUpdateNeeded = true;
400
0
    if (static_cast<int>(m_asXRefEntries.size()) < m_nLastXRefSize - 1)
401
0
        m_asXRefEntries.resize(m_nLastXRefSize - 1);
402
403
0
    CPLAssert(m_nCatalogId.toBool());
404
0
    CPLAssert(poCatalogDict != nullptr);
405
406
0
    GDALPDFObject *poMetadata = poCatalogDict->Get("Metadata");
407
0
    if (poMetadata)
408
0
    {
409
0
        m_nXMPId = poMetadata->GetRefNum();
410
0
        m_nXMPGen = poMetadata->GetRefGen();
411
0
    }
412
413
0
    poCatalogDict->Remove("Metadata");
414
0
    auto nNewXMPId = SetXMP(poSrcDS, nullptr);
415
416
    /* Write empty metadata, because podofo driver will find the dangling info
417
     * instead */
418
0
    if (!nNewXMPId.toBool() && m_nXMPId.toBool())
419
0
    {
420
0
        StartObj(m_nXMPId, m_nXMPGen);
421
0
        VSIFPrintfL(m_fp, "<< >>\n");
422
0
        EndObj();
423
0
    }
424
425
0
    if (m_nXMPId.toBool())
426
0
        poCatalogDict->Add("Metadata", m_nXMPId, 0);
427
428
0
    StartObj(m_nCatalogId, m_nCatalogGen);
429
0
    VSIFPrintfL(m_fp, "%s\n", poCatalogDict->Serialize().c_str());
430
0
    EndObj();
431
0
}
432
433
/************************************************************************/
434
/*                           AllocNewObject()                           */
435
/************************************************************************/
436
437
GDALPDFObjectNum GDALPDFBaseWriter::AllocNewObject()
438
14.9k
{
439
14.9k
    m_asXRefEntries.push_back(GDALXRefEntry());
440
14.9k
    return GDALPDFObjectNum(static_cast<int>(m_asXRefEntries.size()));
441
14.9k
}
442
443
/************************************************************************/
444
/*                        WriteXRefTableAndTrailer()                    */
445
/************************************************************************/
446
447
void GDALPDFBaseWriter::WriteXRefTableAndTrailer(bool bUpdate,
448
                                                 vsi_l_offset nLastStartXRef)
449
756
{
450
756
    vsi_l_offset nOffsetXREF = VSIFTellL(m_fp);
451
756
    VSIFPrintfL(m_fp, "xref\n");
452
453
756
    char buffer[16];
454
756
    if (bUpdate)
455
0
    {
456
0
        VSIFPrintfL(m_fp, "0 1\n");
457
0
        VSIFPrintfL(m_fp, "0000000000 65535 f \n");
458
0
        for (size_t i = 0; i < m_asXRefEntries.size();)
459
0
        {
460
0
            if (m_asXRefEntries[i].nOffset != 0 || m_asXRefEntries[i].bFree)
461
0
            {
462
                /* Find number of consecutive objects */
463
0
                size_t nCount = 1;
464
0
                while (i + nCount < m_asXRefEntries.size() &&
465
0
                       (m_asXRefEntries[i + nCount].nOffset != 0 ||
466
0
                        m_asXRefEntries[i + nCount].bFree))
467
0
                    nCount++;
468
469
0
                VSIFPrintfL(m_fp, "%d %d\n", static_cast<int>(i) + 1,
470
0
                            static_cast<int>(nCount));
471
0
                size_t iEnd = i + nCount;
472
0
                for (; i < iEnd; i++)
473
0
                {
474
0
                    snprintf(buffer, sizeof(buffer),
475
0
                             "%010" CPL_FRMT_GB_WITHOUT_PREFIX "u",
476
0
                             m_asXRefEntries[i].nOffset);
477
0
                    VSIFPrintfL(m_fp, "%s %05d %c \n", buffer,
478
0
                                m_asXRefEntries[i].nGen,
479
0
                                m_asXRefEntries[i].bFree ? 'f' : 'n');
480
0
                }
481
0
            }
482
0
            else
483
0
            {
484
0
                i++;
485
0
            }
486
0
        }
487
0
    }
488
756
    else
489
756
    {
490
756
        VSIFPrintfL(m_fp, "%d %d\n", 0,
491
756
                    static_cast<int>(m_asXRefEntries.size()) + 1);
492
756
        VSIFPrintfL(m_fp, "0000000000 65535 f \n");
493
15.7k
        for (size_t i = 0; i < m_asXRefEntries.size(); i++)
494
14.9k
        {
495
14.9k
            snprintf(buffer, sizeof(buffer),
496
14.9k
                     "%010" CPL_FRMT_GB_WITHOUT_PREFIX "u",
497
14.9k
                     m_asXRefEntries[i].nOffset);
498
14.9k
            VSIFPrintfL(m_fp, "%s %05d n \n", buffer, m_asXRefEntries[i].nGen);
499
14.9k
        }
500
756
    }
501
502
756
    VSIFPrintfL(m_fp, "trailer\n");
503
756
    GDALPDFDictionaryRW oDict;
504
756
    oDict.Add("Size", static_cast<int>(m_asXRefEntries.size()) + 1)
505
756
        .Add("Root", m_nCatalogId, m_nCatalogGen);
506
756
    if (m_nInfoId.toBool())
507
0
        oDict.Add("Info", m_nInfoId, m_nInfoGen);
508
756
    if (nLastStartXRef)
509
0
        oDict.Add("Prev", static_cast<double>(nLastStartXRef));
510
756
    VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
511
512
756
    VSIFPrintfL(m_fp,
513
756
                "startxref\n" CPL_FRMT_GUIB "\n"
514
756
                "%%%%EOF\n",
515
756
                nOffsetXREF);
516
756
}
517
518
/************************************************************************/
519
/*                              StartObj()                              */
520
/************************************************************************/
521
522
void GDALPDFBaseWriter::StartObj(const GDALPDFObjectNum &nObjectId, int nGen)
523
14.3k
{
524
14.3k
    CPLAssert(!m_bInWriteObj);
525
14.3k
    CPLAssert(nObjectId.toInt() - 1 < static_cast<int>(m_asXRefEntries.size()));
526
14.3k
    CPLAssert(m_asXRefEntries[nObjectId.toInt() - 1].nOffset == 0);
527
14.3k
    m_asXRefEntries[nObjectId.toInt() - 1].nOffset = VSIFTellL(m_fp);
528
14.3k
    m_asXRefEntries[nObjectId.toInt() - 1].nGen = nGen;
529
14.3k
    VSIFPrintfL(m_fp, "%d %d obj\n", nObjectId.toInt(), nGen);
530
14.3k
    m_bInWriteObj = true;
531
14.3k
}
532
533
/************************************************************************/
534
/*                               EndObj()                               */
535
/************************************************************************/
536
537
void GDALPDFBaseWriter::EndObj()
538
14.3k
{
539
14.3k
    CPLAssert(m_bInWriteObj);
540
14.3k
    CPLAssert(!m_fpBack);
541
14.3k
    VSIFPrintfL(m_fp, "endobj\n");
542
14.3k
    m_bInWriteObj = false;
543
14.3k
}
544
545
/************************************************************************/
546
/*                         StartObjWithStream()                         */
547
/************************************************************************/
548
549
void GDALPDFBaseWriter::StartObjWithStream(const GDALPDFObjectNum &nObjectId,
550
                                           GDALPDFDictionaryRW &oDict,
551
                                           bool bDeflate)
552
2.93k
{
553
2.93k
    CPLAssert(!m_nContentLengthId.toBool());
554
2.93k
    CPLAssert(!m_fpGZip);
555
2.93k
    CPLAssert(!m_fpBack);
556
2.93k
    CPLAssert(m_nStreamStart == 0);
557
558
2.93k
    m_nContentLengthId = AllocNewObject();
559
560
2.93k
    StartObj(nObjectId);
561
2.93k
    {
562
2.93k
        oDict.Add("Length", m_nContentLengthId, 0);
563
2.93k
        if (bDeflate)
564
2.93k
        {
565
2.93k
            oDict.Add("Filter", GDALPDFObjectRW::CreateName("FlateDecode"));
566
2.93k
        }
567
2.93k
        VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
568
2.93k
    }
569
570
    /* -------------------------------------------------------------- */
571
    /*  Write content stream                                          */
572
    /* -------------------------------------------------------------- */
573
2.93k
    VSIFPrintfL(m_fp, "stream\n");
574
2.93k
    m_nStreamStart = VSIFTellL(m_fp);
575
576
2.93k
    m_fpGZip = nullptr;
577
2.93k
    m_fpBack = m_fp;
578
2.93k
    if (bDeflate)
579
2.93k
    {
580
2.93k
        m_fpGZip = VSICreateGZipWritable(m_fp, TRUE, FALSE);
581
2.93k
        m_fp = m_fpGZip;
582
2.93k
    }
583
2.93k
}
584
585
/************************************************************************/
586
/*                          EndObjWithStream()                          */
587
/************************************************************************/
588
589
void GDALPDFBaseWriter::EndObjWithStream()
590
2.93k
{
591
2.93k
    if (m_fpGZip)
592
2.93k
        VSIFCloseL(m_fpGZip);
593
2.93k
    m_fp = m_fpBack;
594
2.93k
    m_fpBack = nullptr;
595
596
2.93k
    vsi_l_offset nStreamEnd = VSIFTellL(m_fp);
597
2.93k
    if (m_fpGZip)
598
2.93k
        VSIFPrintfL(m_fp, "\n");
599
2.93k
    m_fpGZip = nullptr;
600
2.93k
    VSIFPrintfL(m_fp, "endstream\n");
601
2.93k
    EndObj();
602
603
2.93k
    StartObj(m_nContentLengthId);
604
2.93k
    VSIFPrintfL(m_fp, "   %ld\n",
605
2.93k
                static_cast<long>(nStreamEnd - m_nStreamStart));
606
2.93k
    EndObj();
607
608
2.93k
    m_nContentLengthId = GDALPDFObjectNum();
609
2.93k
    m_nStreamStart = 0;
610
2.93k
}
611
612
/************************************************************************/
613
/*                         GDALPDFFind4Corners()                        */
614
/************************************************************************/
615
616
static void GDALPDFFind4Corners(const GDAL_GCP *pasGCPList, int &iUL, int &iUR,
617
                                int &iLR, int &iLL)
618
11
{
619
11
    double dfMeanX = 0.0;
620
11
    double dfMeanY = 0.0;
621
11
    int i;
622
623
11
    iUL = 0;
624
11
    iUR = 0;
625
11
    iLR = 0;
626
11
    iLL = 0;
627
628
55
    for (i = 0; i < 4; i++)
629
44
    {
630
44
        dfMeanX += pasGCPList[i].dfGCPPixel;
631
44
        dfMeanY += pasGCPList[i].dfGCPLine;
632
44
    }
633
11
    dfMeanX /= 4;
634
11
    dfMeanY /= 4;
635
636
55
    for (i = 0; i < 4; i++)
637
44
    {
638
44
        if (pasGCPList[i].dfGCPPixel < dfMeanX &&
639
44
            pasGCPList[i].dfGCPLine < dfMeanY)
640
8
            iUL = i;
641
642
36
        else if (pasGCPList[i].dfGCPPixel > dfMeanX &&
643
36
                 pasGCPList[i].dfGCPLine < dfMeanY)
644
8
            iUR = i;
645
646
28
        else if (pasGCPList[i].dfGCPPixel > dfMeanX &&
647
28
                 pasGCPList[i].dfGCPLine > dfMeanY)
648
6
            iLR = i;
649
650
22
        else if (pasGCPList[i].dfGCPPixel < dfMeanX &&
651
22
                 pasGCPList[i].dfGCPLine > dfMeanY)
652
10
            iLL = i;
653
44
    }
654
11
}
655
656
/************************************************************************/
657
/*                         WriteSRS_ISO32000()                          */
658
/************************************************************************/
659
660
GDALPDFObjectNum GDALPDFBaseWriter::WriteSRS_ISO32000(GDALDataset *poSrcDS,
661
                                                      double dfUserUnit,
662
                                                      const char *pszNEATLINE,
663
                                                      PDFMargins *psMargins,
664
                                                      int bWriteViewport)
665
756
{
666
756
    int nWidth = poSrcDS->GetRasterXSize();
667
756
    int nHeight = poSrcDS->GetRasterYSize();
668
756
    const char *pszWKT = poSrcDS->GetProjectionRef();
669
756
    GDALGeoTransform gt;
670
671
756
    int bHasGT = (poSrcDS->GetGeoTransform(gt) == CE_None);
672
756
    const GDAL_GCP *pasGCPList =
673
756
        (poSrcDS->GetGCPCount() == 4) ? poSrcDS->GetGCPs() : nullptr;
674
756
    if (pasGCPList != nullptr)
675
11
        pszWKT = poSrcDS->GetGCPProjection();
676
677
756
    if (!bHasGT && pasGCPList == nullptr)
678
224
        return GDALPDFObjectNum();
679
680
532
    if (pszWKT == nullptr || EQUAL(pszWKT, ""))
681
60
        return GDALPDFObjectNum();
682
683
472
    double adfGPTS[8];
684
685
472
    double dfULPixel = 0;
686
472
    double dfULLine = 0;
687
472
    double dfLRPixel = nWidth;
688
472
    double dfLRLine = nHeight;
689
690
472
    std::vector<gdal::GCP> asNeatLineGCPs(4);
691
472
    if (pszNEATLINE == nullptr)
692
472
        pszNEATLINE = poSrcDS->GetMetadataItem("NEATLINE");
693
472
    if (bHasGT && pszNEATLINE != nullptr && pszNEATLINE[0] != '\0')
694
0
    {
695
0
        auto [poGeom, _] = OGRGeometryFactory::createFromWkt(pszNEATLINE);
696
0
        if (poGeom != nullptr &&
697
0
            wkbFlatten(poGeom->getGeometryType()) == wkbPolygon)
698
0
        {
699
0
            OGRLineString *poLS = poGeom->toPolygon()->getExteriorRing();
700
0
            GDALGeoTransform invGT;
701
0
            if (poLS != nullptr && poLS->getNumPoints() == 5 &&
702
0
                gt.GetInverse(invGT))
703
0
            {
704
0
                for (int i = 0; i < 4; i++)
705
0
                {
706
0
                    const double X = poLS->getX(i);
707
0
                    const double Y = poLS->getY(i);
708
0
                    asNeatLineGCPs[i].X() = X;
709
0
                    asNeatLineGCPs[i].Y() = Y;
710
0
                    const double x = invGT[0] + X * invGT[1] + Y * invGT[2];
711
0
                    const double y = invGT[3] + X * invGT[4] + Y * invGT[5];
712
0
                    asNeatLineGCPs[i].Pixel() = x;
713
0
                    asNeatLineGCPs[i].Line() = y;
714
0
                }
715
716
0
                int iUL = 0;
717
0
                int iUR = 0;
718
0
                int iLR = 0;
719
0
                int iLL = 0;
720
0
                GDALPDFFind4Corners(gdal::GCP::c_ptr(asNeatLineGCPs), iUL, iUR,
721
0
                                    iLR, iLL);
722
723
0
                if (fabs(asNeatLineGCPs[iUL].Pixel() -
724
0
                         asNeatLineGCPs[iLL].Pixel()) > .5 ||
725
0
                    fabs(asNeatLineGCPs[iUR].Pixel() -
726
0
                         asNeatLineGCPs[iLR].Pixel()) > .5 ||
727
0
                    fabs(asNeatLineGCPs[iUL].Line() -
728
0
                         asNeatLineGCPs[iUR].Line()) > .5 ||
729
0
                    fabs(asNeatLineGCPs[iLL].Line() -
730
0
                         asNeatLineGCPs[iLR].Line()) > .5)
731
0
                {
732
0
                    CPLError(CE_Warning, CPLE_NotSupported,
733
0
                             "Neatline coordinates should form a rectangle in "
734
0
                             "pixel space. Ignoring it");
735
0
                    for (int i = 0; i < 4; i++)
736
0
                    {
737
0
                        CPLDebug("PDF", "pixel[%d] = %.1f, line[%d] = %.1f", i,
738
0
                                 asNeatLineGCPs[i].Pixel(), i,
739
0
                                 asNeatLineGCPs[i].Line());
740
0
                    }
741
0
                }
742
0
                else
743
0
                {
744
0
                    pasGCPList = gdal::GCP::c_ptr(asNeatLineGCPs);
745
0
                }
746
0
            }
747
0
        }
748
0
    }
749
750
472
    if (pasGCPList)
751
11
    {
752
11
        int iUL = 0;
753
11
        int iUR = 0;
754
11
        int iLR = 0;
755
11
        int iLL = 0;
756
11
        GDALPDFFind4Corners(pasGCPList, iUL, iUR, iLR, iLL);
757
758
11
        if (fabs(pasGCPList[iUL].dfGCPPixel - pasGCPList[iLL].dfGCPPixel) >
759
11
                .5 ||
760
11
            fabs(pasGCPList[iUR].dfGCPPixel - pasGCPList[iLR].dfGCPPixel) >
761
10
                .5 ||
762
11
            fabs(pasGCPList[iUL].dfGCPLine - pasGCPList[iUR].dfGCPLine) > .5 ||
763
11
            fabs(pasGCPList[iLL].dfGCPLine - pasGCPList[iLR].dfGCPLine) > .5)
764
4
        {
765
4
            CPLError(CE_Failure, CPLE_NotSupported,
766
4
                     "GCPs should form a rectangle in pixel space");
767
4
            return GDALPDFObjectNum();
768
4
        }
769
770
7
        dfULPixel = pasGCPList[iUL].dfGCPPixel;
771
7
        dfULLine = pasGCPList[iUL].dfGCPLine;
772
7
        dfLRPixel = pasGCPList[iLR].dfGCPPixel;
773
7
        dfLRLine = pasGCPList[iLR].dfGCPLine;
774
775
        /* Upper-left */
776
7
        adfGPTS[0] = pasGCPList[iUL].dfGCPX;
777
7
        adfGPTS[1] = pasGCPList[iUL].dfGCPY;
778
779
        /* Lower-left */
780
7
        adfGPTS[2] = pasGCPList[iLL].dfGCPX;
781
7
        adfGPTS[3] = pasGCPList[iLL].dfGCPY;
782
783
        /* Lower-right */
784
7
        adfGPTS[4] = pasGCPList[iLR].dfGCPX;
785
7
        adfGPTS[5] = pasGCPList[iLR].dfGCPY;
786
787
        /* Upper-right */
788
7
        adfGPTS[6] = pasGCPList[iUR].dfGCPX;
789
7
        adfGPTS[7] = pasGCPList[iUR].dfGCPY;
790
7
    }
791
461
    else
792
461
    {
793
        /* Upper-left */
794
461
        adfGPTS[0] = APPLY_GT_X(gt, 0, 0);
795
461
        adfGPTS[1] = APPLY_GT_Y(gt, 0, 0);
796
797
        /* Lower-left */
798
461
        adfGPTS[2] = APPLY_GT_X(gt, 0, nHeight);
799
461
        adfGPTS[3] = APPLY_GT_Y(gt, 0, nHeight);
800
801
        /* Lower-right */
802
461
        adfGPTS[4] = APPLY_GT_X(gt, nWidth, nHeight);
803
461
        adfGPTS[5] = APPLY_GT_Y(gt, nWidth, nHeight);
804
805
        /* Upper-right */
806
461
        adfGPTS[6] = APPLY_GT_X(gt, nWidth, 0);
807
461
        adfGPTS[7] = APPLY_GT_Y(gt, nWidth, 0);
808
461
    }
809
810
468
    OGRSpatialReferenceH hSRS = OSRNewSpatialReference(pszWKT);
811
468
    if (hSRS == nullptr)
812
12
        return GDALPDFObjectNum();
813
456
    OSRSetAxisMappingStrategy(hSRS, OAMS_TRADITIONAL_GIS_ORDER);
814
456
    OGRSpatialReferenceH hSRSGeog = OSRCloneGeogCS(hSRS);
815
456
    if (hSRSGeog == nullptr)
816
18
    {
817
18
        OSRDestroySpatialReference(hSRS);
818
18
        return GDALPDFObjectNum();
819
18
    }
820
438
    OSRSetAxisMappingStrategy(hSRSGeog, OAMS_TRADITIONAL_GIS_ORDER);
821
438
    OGRCoordinateTransformationH hCT =
822
438
        OCTNewCoordinateTransformation(hSRS, hSRSGeog);
823
438
    if (hCT == nullptr)
824
99
    {
825
99
        OSRDestroySpatialReference(hSRS);
826
99
        OSRDestroySpatialReference(hSRSGeog);
827
99
        return GDALPDFObjectNum();
828
99
    }
829
830
339
    int bSuccess = TRUE;
831
832
339
    bSuccess &= (OCTTransform(hCT, 1, adfGPTS + 0, adfGPTS + 1, nullptr) == 1);
833
339
    bSuccess &= (OCTTransform(hCT, 1, adfGPTS + 2, adfGPTS + 3, nullptr) == 1);
834
339
    bSuccess &= (OCTTransform(hCT, 1, adfGPTS + 4, adfGPTS + 5, nullptr) == 1);
835
339
    bSuccess &= (OCTTransform(hCT, 1, adfGPTS + 6, adfGPTS + 7, nullptr) == 1);
836
837
339
    if (!bSuccess)
838
49
    {
839
49
        OSRDestroySpatialReference(hSRS);
840
49
        OSRDestroySpatialReference(hSRSGeog);
841
49
        OCTDestroyCoordinateTransformation(hCT);
842
49
        return GDALPDFObjectNum();
843
49
    }
844
845
290
    const char *pszAuthorityCode = OSRGetAuthorityCode(hSRS, nullptr);
846
290
    const char *pszAuthorityName = OSRGetAuthorityName(hSRS, nullptr);
847
290
    int nEPSGCode = 0;
848
290
    if (pszAuthorityName != nullptr && pszAuthorityCode != nullptr &&
849
290
        (EQUAL(pszAuthorityName, "EPSG") ||
850
32
         (EQUAL(pszAuthorityName, "ESRI") &&
851
0
          CPLTestBool(
852
0
              CPLGetConfigOption("GDAL_PDF_WRITE_ESRI_CODE_AS_EPSG", "NO")))))
853
32
    {
854
32
        nEPSGCode = atoi(pszAuthorityCode);
855
32
    }
856
857
290
    int bIsGeographic = OSRIsGeographic(hSRS);
858
859
290
    OSRMorphToESRI(hSRS);
860
290
    char *pszESRIWKT = nullptr;
861
290
    OSRExportToWkt(hSRS, &pszESRIWKT);
862
863
290
    OSRDestroySpatialReference(hSRS);
864
290
    OSRDestroySpatialReference(hSRSGeog);
865
290
    OCTDestroyCoordinateTransformation(hCT);
866
290
    hSRS = nullptr;
867
290
    hSRSGeog = nullptr;
868
290
    hCT = nullptr;
869
870
290
    if (pszESRIWKT == nullptr)
871
0
        return GDALPDFObjectNum();
872
873
290
    auto nViewportId = (bWriteViewport) ? AllocNewObject() : GDALPDFObjectNum();
874
290
    auto nMeasureId = AllocNewObject();
875
290
    auto nGCSId = AllocNewObject();
876
877
290
    if (nViewportId.toBool())
878
290
    {
879
290
        StartObj(nViewportId);
880
290
        GDALPDFDictionaryRW oViewPortDict;
881
290
        oViewPortDict.Add("Type", GDALPDFObjectRW::CreateName("Viewport"))
882
290
            .Add("Name", "Layer")
883
290
            .Add("BBox", &((new GDALPDFArrayRW())
884
290
                               ->Add(dfULPixel / dfUserUnit + psMargins->nLeft)
885
290
                               .Add((nHeight - dfLRLine) / dfUserUnit +
886
290
                                    psMargins->nBottom)
887
290
                               .Add(dfLRPixel / dfUserUnit + psMargins->nLeft)
888
290
                               .Add((nHeight - dfULLine) / dfUserUnit +
889
290
                                    psMargins->nBottom)))
890
290
            .Add("Measure", nMeasureId, 0);
891
290
        VSIFPrintfL(m_fp, "%s\n", oViewPortDict.Serialize().c_str());
892
290
        EndObj();
893
290
    }
894
895
290
    StartObj(nMeasureId);
896
290
    GDALPDFDictionaryRW oMeasureDict;
897
290
    oMeasureDict.Add("Type", GDALPDFObjectRW::CreateName("Measure"))
898
290
        .Add("Subtype", GDALPDFObjectRW::CreateName("GEO"))
899
290
        .Add("Bounds", &((new GDALPDFArrayRW())
900
290
                             ->Add(0)
901
290
                             .Add(1)
902
290
                             .Add(0)
903
290
                             .Add(0)
904
290
                             .Add(1)
905
290
                             .Add(0)
906
290
                             .Add(1)
907
290
                             .Add(1)))
908
290
        .Add("GPTS", &((new GDALPDFArrayRW())
909
290
                           ->Add(adfGPTS[1])
910
290
                           .Add(adfGPTS[0])
911
290
                           .Add(adfGPTS[3])
912
290
                           .Add(adfGPTS[2])
913
290
                           .Add(adfGPTS[5])
914
290
                           .Add(adfGPTS[4])
915
290
                           .Add(adfGPTS[7])
916
290
                           .Add(adfGPTS[6])))
917
290
        .Add("LPTS", &((new GDALPDFArrayRW())
918
290
                           ->Add(0)
919
290
                           .Add(1)
920
290
                           .Add(0)
921
290
                           .Add(0)
922
290
                           .Add(1)
923
290
                           .Add(0)
924
290
                           .Add(1)
925
290
                           .Add(1)))
926
290
        .Add("GCS", nGCSId, 0);
927
290
    VSIFPrintfL(m_fp, "%s\n", oMeasureDict.Serialize().c_str());
928
290
    EndObj();
929
930
290
    StartObj(nGCSId);
931
290
    GDALPDFDictionaryRW oGCSDict;
932
290
    oGCSDict
933
290
        .Add("Type",
934
290
             GDALPDFObjectRW::CreateName(bIsGeographic ? "GEOGCS" : "PROJCS"))
935
290
        .Add("WKT", pszESRIWKT);
936
290
    if (nEPSGCode)
937
32
        oGCSDict.Add("EPSG", nEPSGCode);
938
290
    VSIFPrintfL(m_fp, "%s\n", oGCSDict.Serialize().c_str());
939
290
    EndObj();
940
941
290
    CPLFree(pszESRIWKT);
942
943
290
    return nViewportId.toBool() ? nViewportId : nMeasureId;
944
290
}
945
946
/************************************************************************/
947
/*                     GDALPDFGetValueFromDSOrOption()                  */
948
/************************************************************************/
949
950
static const char *GDALPDFGetValueFromDSOrOption(GDALDataset *poSrcDS,
951
                                                 char **papszOptions,
952
                                                 const char *pszKey)
953
5.29k
{
954
5.29k
    const char *pszValue = CSLFetchNameValue(papszOptions, pszKey);
955
5.29k
    if (pszValue == nullptr)
956
5.29k
        pszValue = poSrcDS->GetMetadataItem(pszKey);
957
5.29k
    if (pszValue != nullptr && pszValue[0] == '\0')
958
0
        return nullptr;
959
5.29k
    else
960
5.29k
        return pszValue;
961
5.29k
}
962
963
/************************************************************************/
964
/*                             SetInfo()                                */
965
/************************************************************************/
966
967
GDALPDFObjectNum GDALPDFBaseWriter::SetInfo(GDALDataset *poSrcDS,
968
                                            char **papszOptions)
969
756
{
970
756
    const char *pszAUTHOR =
971
756
        GDALPDFGetValueFromDSOrOption(poSrcDS, papszOptions, "AUTHOR");
972
756
    const char *pszPRODUCER =
973
756
        GDALPDFGetValueFromDSOrOption(poSrcDS, papszOptions, "PRODUCER");
974
756
    const char *pszCREATOR =
975
756
        GDALPDFGetValueFromDSOrOption(poSrcDS, papszOptions, "CREATOR");
976
756
    const char *pszCREATION_DATE =
977
756
        GDALPDFGetValueFromDSOrOption(poSrcDS, papszOptions, "CREATION_DATE");
978
756
    const char *pszSUBJECT =
979
756
        GDALPDFGetValueFromDSOrOption(poSrcDS, papszOptions, "SUBJECT");
980
756
    const char *pszTITLE =
981
756
        GDALPDFGetValueFromDSOrOption(poSrcDS, papszOptions, "TITLE");
982
756
    const char *pszKEYWORDS =
983
756
        GDALPDFGetValueFromDSOrOption(poSrcDS, papszOptions, "KEYWORDS");
984
756
    return SetInfo(pszAUTHOR, pszPRODUCER, pszCREATOR, pszCREATION_DATE,
985
756
                   pszSUBJECT, pszTITLE, pszKEYWORDS);
986
756
}
987
988
/************************************************************************/
989
/*                             SetInfo()                                */
990
/************************************************************************/
991
992
GDALPDFObjectNum
993
GDALPDFBaseWriter::SetInfo(const char *pszAUTHOR, const char *pszPRODUCER,
994
                           const char *pszCREATOR, const char *pszCREATION_DATE,
995
                           const char *pszSUBJECT, const char *pszTITLE,
996
                           const char *pszKEYWORDS)
997
756
{
998
756
    if (pszAUTHOR == nullptr && pszPRODUCER == nullptr &&
999
756
        pszCREATOR == nullptr && pszCREATION_DATE == nullptr &&
1000
756
        pszSUBJECT == nullptr && pszTITLE == nullptr && pszKEYWORDS == nullptr)
1001
756
        return GDALPDFObjectNum();
1002
1003
0
    if (!m_nInfoId.toBool())
1004
0
        m_nInfoId = AllocNewObject();
1005
0
    StartObj(m_nInfoId, m_nInfoGen);
1006
0
    GDALPDFDictionaryRW oDict;
1007
0
    if (pszAUTHOR != nullptr)
1008
0
        oDict.Add("Author", pszAUTHOR);
1009
0
    if (pszPRODUCER != nullptr)
1010
0
        oDict.Add("Producer", pszPRODUCER);
1011
0
    if (pszCREATOR != nullptr)
1012
0
        oDict.Add("Creator", pszCREATOR);
1013
0
    if (pszCREATION_DATE != nullptr)
1014
0
        oDict.Add("CreationDate", pszCREATION_DATE);
1015
0
    if (pszSUBJECT != nullptr)
1016
0
        oDict.Add("Subject", pszSUBJECT);
1017
0
    if (pszTITLE != nullptr)
1018
0
        oDict.Add("Title", pszTITLE);
1019
0
    if (pszKEYWORDS != nullptr)
1020
0
        oDict.Add("Keywords", pszKEYWORDS);
1021
0
    VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
1022
0
    EndObj();
1023
1024
0
    return m_nInfoId;
1025
756
}
1026
1027
/************************************************************************/
1028
/*                             SetXMP()                                 */
1029
/************************************************************************/
1030
1031
GDALPDFObjectNum GDALPDFBaseWriter::SetXMP(GDALDataset *poSrcDS,
1032
                                           const char *pszXMP)
1033
711
{
1034
711
    if (pszXMP != nullptr && STARTS_WITH_CI(pszXMP, "NO"))
1035
0
        return GDALPDFObjectNum();
1036
711
    if (pszXMP != nullptr && pszXMP[0] == '\0')
1037
0
        return GDALPDFObjectNum();
1038
1039
711
    if (poSrcDS && pszXMP == nullptr)
1040
711
    {
1041
711
        char **papszXMP = poSrcDS->GetMetadata("xml:XMP");
1042
711
        if (papszXMP != nullptr && papszXMP[0] != nullptr)
1043
0
            pszXMP = papszXMP[0];
1044
711
    }
1045
1046
711
    if (pszXMP == nullptr)
1047
711
        return GDALPDFObjectNum();
1048
1049
0
    CPLXMLNode *psNode = CPLParseXMLString(pszXMP);
1050
0
    if (psNode == nullptr)
1051
0
        return GDALPDFObjectNum();
1052
0
    CPLDestroyXMLNode(psNode);
1053
1054
0
    if (!m_nXMPId.toBool())
1055
0
        m_nXMPId = AllocNewObject();
1056
0
    StartObj(m_nXMPId, m_nXMPGen);
1057
0
    GDALPDFDictionaryRW oDict;
1058
0
    oDict.Add("Type", GDALPDFObjectRW::CreateName("Metadata"))
1059
0
        .Add("Subtype", GDALPDFObjectRW::CreateName("XML"))
1060
0
        .Add("Length", static_cast<int>(strlen(pszXMP)));
1061
0
    VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
1062
0
    VSIFPrintfL(m_fp, "stream\n");
1063
0
    VSIFPrintfL(m_fp, "%s\n", pszXMP);
1064
0
    VSIFPrintfL(m_fp, "endstream\n");
1065
0
    EndObj();
1066
0
    return m_nXMPId;
1067
0
}
1068
1069
/************************************************************************/
1070
/*                              WriteOCG()                              */
1071
/************************************************************************/
1072
1073
GDALPDFObjectNum GDALPDFBaseWriter::WriteOCG(const char *pszLayerName,
1074
                                             const GDALPDFObjectNum &nParentId)
1075
2.42k
{
1076
2.42k
    if (pszLayerName == nullptr || pszLayerName[0] == '\0')
1077
1.25k
        return GDALPDFObjectNum();
1078
1079
1.17k
    auto nOCGId = AllocNewObject();
1080
1081
1.17k
    GDALPDFOCGDesc oOCGDesc;
1082
1.17k
    oOCGDesc.nId = nOCGId;
1083
1.17k
    oOCGDesc.nParentId = nParentId;
1084
1.17k
    oOCGDesc.osLayerName = pszLayerName;
1085
1086
1.17k
    m_asOCGs.push_back(std::move(oOCGDesc));
1087
1088
1.17k
    StartObj(nOCGId);
1089
1.17k
    {
1090
1.17k
        GDALPDFDictionaryRW oDict;
1091
1.17k
        oDict.Add("Type", GDALPDFObjectRW::CreateName("OCG"));
1092
1.17k
        oDict.Add("Name", pszLayerName);
1093
1.17k
        VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
1094
1.17k
    }
1095
1.17k
    EndObj();
1096
1097
1.17k
    return nOCGId;
1098
2.42k
}
1099
1100
/************************************************************************/
1101
/*                              StartPage()                             */
1102
/************************************************************************/
1103
1104
bool GDALPDFWriter::StartPage(GDALDataset *poClippingDS, double dfDPI,
1105
                              bool bWriteUserUnit, const char *pszGEO_ENCODING,
1106
                              const char *pszNEATLINE, PDFMargins *psMargins,
1107
                              PDFCompressMethod eStreamCompressMethod,
1108
                              int bHasOGRData)
1109
756
{
1110
756
    int nWidth = poClippingDS->GetRasterXSize();
1111
756
    int nHeight = poClippingDS->GetRasterYSize();
1112
756
    int nBands = poClippingDS->GetRasterCount();
1113
1114
756
    double dfUserUnit = dfDPI * USER_UNIT_IN_INCH;
1115
756
    double dfWidthInUserUnit =
1116
756
        nWidth / dfUserUnit + psMargins->nLeft + psMargins->nRight;
1117
756
    double dfHeightInUserUnit =
1118
756
        nHeight / dfUserUnit + psMargins->nBottom + psMargins->nTop;
1119
1120
756
    auto nPageId = AllocNewObject();
1121
756
    m_asPageId.push_back(nPageId);
1122
1123
756
    auto nContentId = AllocNewObject();
1124
756
    auto nResourcesId = AllocNewObject();
1125
1126
756
    auto nAnnotsId = AllocNewObject();
1127
1128
756
    const bool bISO32000 =
1129
756
        EQUAL(pszGEO_ENCODING, "ISO32000") || EQUAL(pszGEO_ENCODING, "BOTH");
1130
1131
756
    GDALPDFObjectNum nViewportId;
1132
756
    if (bISO32000)
1133
756
        nViewportId = WriteSRS_ISO32000(poClippingDS, dfUserUnit, pszNEATLINE,
1134
756
                                        psMargins, TRUE);
1135
1136
756
    StartObj(nPageId);
1137
756
    GDALPDFDictionaryRW oDictPage;
1138
756
    oDictPage.Add("Type", GDALPDFObjectRW::CreateName("Page"))
1139
756
        .Add("Parent", m_nPageResourceId, 0)
1140
756
        .Add("MediaBox", &((new GDALPDFArrayRW())
1141
756
                               ->Add(0)
1142
756
                               .Add(0)
1143
756
                               .Add(dfWidthInUserUnit)
1144
756
                               .Add(dfHeightInUserUnit)));
1145
756
    if (bWriteUserUnit)
1146
756
        oDictPage.Add("UserUnit", dfUserUnit);
1147
756
    oDictPage.Add("Contents", nContentId, 0)
1148
756
        .Add("Resources", nResourcesId, 0)
1149
756
        .Add("Annots", nAnnotsId, 0);
1150
1151
756
    if (nBands == 4)
1152
75
    {
1153
75
        oDictPage.Add(
1154
75
            "Group",
1155
75
            &((new GDALPDFDictionaryRW())
1156
75
                  ->Add("Type", GDALPDFObjectRW::CreateName("Group"))
1157
75
                  .Add("S", GDALPDFObjectRW::CreateName("Transparency"))
1158
75
                  .Add("CS", GDALPDFObjectRW::CreateName("DeviceRGB"))));
1159
75
    }
1160
756
    if (nViewportId.toBool())
1161
290
    {
1162
290
        oDictPage.Add("VP", &((new GDALPDFArrayRW())->Add(nViewportId, 0)));
1163
290
    }
1164
1165
756
#ifndef HACK_TO_GENERATE_OCMD
1166
756
    if (bHasOGRData)
1167
45
        oDictPage.Add("StructParents", 0);
1168
756
#endif
1169
1170
756
    VSIFPrintfL(m_fp, "%s\n", oDictPage.Serialize().c_str());
1171
756
    EndObj();
1172
1173
756
    oPageContext.poClippingDS = poClippingDS;
1174
756
    oPageContext.nPageId = nPageId;
1175
756
    oPageContext.nContentId = nContentId;
1176
756
    oPageContext.nResourcesId = nResourcesId;
1177
756
    oPageContext.nAnnotsId = nAnnotsId;
1178
756
    oPageContext.dfDPI = dfDPI;
1179
756
    oPageContext.sMargins = *psMargins;
1180
756
    oPageContext.eStreamCompressMethod = eStreamCompressMethod;
1181
1182
756
    return true;
1183
756
}
1184
1185
/************************************************************************/
1186
/*                             WriteColorTable()                        */
1187
/************************************************************************/
1188
1189
GDALPDFObjectNum GDALPDFBaseWriter::WriteColorTable(GDALDataset *poSrcDS)
1190
1.34k
{
1191
    /* Does the source image has a color table ? */
1192
1.34k
    GDALColorTable *poCT = nullptr;
1193
1.34k
    if (poSrcDS->GetRasterCount() > 0)
1194
1.34k
        poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
1195
1.34k
    GDALPDFObjectNum nColorTableId;
1196
1.34k
    if (poCT != nullptr && poCT->GetColorEntryCount() <= 256)
1197
74
    {
1198
74
        int nColors = poCT->GetColorEntryCount();
1199
74
        nColorTableId = AllocNewObject();
1200
1201
74
        auto nLookupTableId = AllocNewObject();
1202
1203
        /* Index object */
1204
74
        StartObj(nColorTableId);
1205
74
        {
1206
74
            GDALPDFArrayRW oArray;
1207
74
            oArray.Add(GDALPDFObjectRW::CreateName("Indexed"))
1208
74
                .Add(&((new GDALPDFArrayRW())
1209
74
                           ->Add(GDALPDFObjectRW::CreateName("DeviceRGB"))))
1210
74
                .Add(nColors - 1)
1211
74
                .Add(nLookupTableId, 0);
1212
74
            VSIFPrintfL(m_fp, "%s\n", oArray.Serialize().c_str());
1213
74
        }
1214
74
        EndObj();
1215
1216
        /* Lookup table object */
1217
74
        StartObj(nLookupTableId);
1218
74
        {
1219
74
            GDALPDFDictionaryRW oDict;
1220
74
            oDict.Add("Length", nColors * 3);
1221
74
            VSIFPrintfL(m_fp, "%s %% Lookup table\n",
1222
74
                        oDict.Serialize().c_str());
1223
74
        }
1224
74
        VSIFPrintfL(m_fp, "stream\n");
1225
74
        GByte pabyLookup[768];
1226
4.91k
        for (int i = 0; i < nColors; i++)
1227
4.84k
        {
1228
4.84k
            const GDALColorEntry *poEntry = poCT->GetColorEntry(i);
1229
4.84k
            pabyLookup[3 * i + 0] = static_cast<GByte>(poEntry->c1);
1230
4.84k
            pabyLookup[3 * i + 1] = static_cast<GByte>(poEntry->c2);
1231
4.84k
            pabyLookup[3 * i + 2] = static_cast<GByte>(poEntry->c3);
1232
4.84k
        }
1233
74
        VSIFWriteL(pabyLookup, 3 * nColors, 1, m_fp);
1234
74
        VSIFPrintfL(m_fp, "\n");
1235
74
        VSIFPrintfL(m_fp, "endstream\n");
1236
74
        EndObj();
1237
74
    }
1238
1239
1.34k
    return nColorTableId;
1240
1.34k
}
1241
1242
/************************************************************************/
1243
/*                             WriteImagery()                           */
1244
/************************************************************************/
1245
1246
bool GDALPDFWriter::WriteImagery(GDALDataset *poDS, const char *pszLayerName,
1247
                                 PDFCompressMethod eCompressMethod,
1248
                                 int nPredictor, int nJPEGQuality,
1249
                                 const char *pszJPEG2000_DRIVER,
1250
                                 int nBlockXSize, int nBlockYSize,
1251
                                 GDALProgressFunc pfnProgress,
1252
                                 void *pProgressData)
1253
711
{
1254
711
    int nWidth = poDS->GetRasterXSize();
1255
711
    int nHeight = poDS->GetRasterYSize();
1256
711
    double dfUserUnit = oPageContext.dfDPI * USER_UNIT_IN_INCH;
1257
1258
711
    GDALPDFRasterDesc oRasterDesc;
1259
1260
711
    if (pfnProgress == nullptr)
1261
0
        pfnProgress = GDALDummyProgress;
1262
1263
711
    oRasterDesc.nOCGRasterId = WriteOCG(pszLayerName);
1264
1265
    /* Does the source image has a color table ? */
1266
711
    auto nColorTableId = WriteColorTable(poDS);
1267
1268
711
    int nXBlocks = DIV_ROUND_UP(nWidth, nBlockXSize);
1269
711
    int nYBlocks = DIV_ROUND_UP(nHeight, nBlockYSize);
1270
711
    int nBlocks = nXBlocks * nYBlocks;
1271
711
    int nBlockXOff, nBlockYOff;
1272
1.20k
    for (nBlockYOff = 0; nBlockYOff < nYBlocks; nBlockYOff++)
1273
711
    {
1274
1.20k
        for (nBlockXOff = 0; nBlockXOff < nXBlocks; nBlockXOff++)
1275
711
        {
1276
711
            const int nReqWidth =
1277
711
                std::min(nBlockXSize, nWidth - nBlockXOff * nBlockXSize);
1278
711
            const int nReqHeight =
1279
711
                std::min(nBlockYSize, nHeight - nBlockYOff * nBlockYSize);
1280
711
            int iImage = nBlockYOff * nXBlocks + nBlockXOff;
1281
1282
711
            void *pScaledData = GDALCreateScaledProgress(
1283
711
                iImage / double(nBlocks), (iImage + 1) / double(nBlocks),
1284
711
                pfnProgress, pProgressData);
1285
711
            int nX = nBlockXOff * nBlockXSize;
1286
711
            int nY = nBlockYOff * nBlockYSize;
1287
1288
711
            auto nImageId =
1289
711
                WriteBlock(poDS, nX, nY, nReqWidth, nReqHeight, nColorTableId,
1290
711
                           eCompressMethod, nPredictor, nJPEGQuality,
1291
711
                           pszJPEG2000_DRIVER, GDALScaledProgress, pScaledData);
1292
1293
711
            GDALDestroyScaledProgress(pScaledData);
1294
1295
711
            if (!nImageId.toBool())
1296
217
                return false;
1297
1298
494
            GDALPDFImageDesc oImageDesc;
1299
494
            oImageDesc.nImageId = nImageId;
1300
494
            oImageDesc.dfXOff = nX / dfUserUnit + oPageContext.sMargins.nLeft;
1301
494
            oImageDesc.dfYOff = (nHeight - nY - nReqHeight) / dfUserUnit +
1302
494
                                oPageContext.sMargins.nBottom;
1303
494
            oImageDesc.dfXSize = nReqWidth / dfUserUnit;
1304
494
            oImageDesc.dfYSize = nReqHeight / dfUserUnit;
1305
1306
494
            oRasterDesc.asImageDesc.push_back(oImageDesc);
1307
494
        }
1308
711
    }
1309
1310
494
    oPageContext.asRasterDesc.push_back(std::move(oRasterDesc));
1311
1312
494
    return true;
1313
711
}
1314
1315
/************************************************************************/
1316
/*                        WriteClippedImagery()                         */
1317
/************************************************************************/
1318
1319
bool GDALPDFWriter::WriteClippedImagery(
1320
    GDALDataset *poDS, const char *pszLayerName,
1321
    PDFCompressMethod eCompressMethod, int nPredictor, int nJPEGQuality,
1322
    const char *pszJPEG2000_DRIVER, int nBlockXSize, int nBlockYSize,
1323
    GDALProgressFunc pfnProgress, void *pProgressData)
1324
0
{
1325
0
    double dfUserUnit = oPageContext.dfDPI * USER_UNIT_IN_INCH;
1326
1327
0
    GDALPDFRasterDesc oRasterDesc;
1328
1329
    /* Get clipping dataset bounding-box */
1330
0
    GDALGeoTransform clippingGT;
1331
0
    GDALDataset *poClippingDS = oPageContext.poClippingDS;
1332
0
    poClippingDS->GetGeoTransform(clippingGT);
1333
0
    int nClippingWidth = poClippingDS->GetRasterXSize();
1334
0
    int nClippingHeight = poClippingDS->GetRasterYSize();
1335
0
    double dfClippingMinX = clippingGT[0];
1336
0
    double dfClippingMaxX = dfClippingMinX + nClippingWidth * clippingGT[1];
1337
0
    double dfClippingMaxY = clippingGT[3];
1338
0
    double dfClippingMinY = dfClippingMaxY + nClippingHeight * clippingGT[5];
1339
1340
0
    if (dfClippingMaxY < dfClippingMinY)
1341
0
    {
1342
0
        std::swap(dfClippingMinY, dfClippingMaxY);
1343
0
    }
1344
1345
    /* Get current dataset dataset bounding-box */
1346
0
    GDALGeoTransform gt;
1347
0
    poDS->GetGeoTransform(gt);
1348
0
    int nWidth = poDS->GetRasterXSize();
1349
0
    int nHeight = poDS->GetRasterYSize();
1350
0
    double dfRasterMinX = gt[0];
1351
    // double dfRasterMaxX = dfRasterMinX + nWidth * gt[1];
1352
0
    double dfRasterMaxY = gt[3];
1353
0
    double dfRasterMinY = dfRasterMaxY + nHeight * gt[5];
1354
1355
0
    if (dfRasterMaxY < dfRasterMinY)
1356
0
    {
1357
0
        std::swap(dfRasterMinY, dfRasterMaxY);
1358
0
    }
1359
1360
0
    if (pfnProgress == nullptr)
1361
0
        pfnProgress = GDALDummyProgress;
1362
1363
0
    oRasterDesc.nOCGRasterId = WriteOCG(pszLayerName);
1364
1365
    /* Does the source image has a color table ? */
1366
0
    auto nColorTableId = WriteColorTable(poDS);
1367
1368
0
    int nXBlocks = DIV_ROUND_UP(nWidth, nBlockXSize);
1369
0
    int nYBlocks = DIV_ROUND_UP(nHeight, nBlockYSize);
1370
0
    int nBlocks = nXBlocks * nYBlocks;
1371
0
    int nBlockXOff, nBlockYOff;
1372
0
    for (nBlockYOff = 0; nBlockYOff < nYBlocks; nBlockYOff++)
1373
0
    {
1374
0
        for (nBlockXOff = 0; nBlockXOff < nXBlocks; nBlockXOff++)
1375
0
        {
1376
0
            int nReqWidth =
1377
0
                std::min(nBlockXSize, nWidth - nBlockXOff * nBlockXSize);
1378
0
            int nReqHeight =
1379
0
                std::min(nBlockYSize, nHeight - nBlockYOff * nBlockYSize);
1380
0
            int iImage = nBlockYOff * nXBlocks + nBlockXOff;
1381
1382
0
            void *pScaledData = GDALCreateScaledProgress(
1383
0
                iImage / double(nBlocks), (iImage + 1) / double(nBlocks),
1384
0
                pfnProgress, pProgressData);
1385
1386
0
            int nX = nBlockXOff * nBlockXSize;
1387
0
            int nY = nBlockYOff * nBlockYSize;
1388
1389
            /* Compute extent of block to write */
1390
0
            double dfBlockMinX = gt[0] + nX * gt[1];
1391
0
            double dfBlockMaxX = gt[0] + (nX + nReqWidth) * gt[1];
1392
0
            double dfBlockMinY = gt[3] + (nY + nReqHeight) * gt[5];
1393
0
            double dfBlockMaxY = gt[3] + nY * gt[5];
1394
1395
0
            if (dfBlockMaxY < dfBlockMinY)
1396
0
            {
1397
0
                std::swap(dfBlockMinY, dfBlockMaxY);
1398
0
            }
1399
1400
            // Clip the extent of the block with the extent of the main raster.
1401
0
            const double dfIntersectMinX =
1402
0
                std::max(dfBlockMinX, dfClippingMinX);
1403
0
            const double dfIntersectMinY =
1404
0
                std::max(dfBlockMinY, dfClippingMinY);
1405
0
            const double dfIntersectMaxX =
1406
0
                std::min(dfBlockMaxX, dfClippingMaxX);
1407
0
            const double dfIntersectMaxY =
1408
0
                std::min(dfBlockMaxY, dfClippingMaxY);
1409
1410
0
            if (dfIntersectMinX < dfIntersectMaxX &&
1411
0
                dfIntersectMinY < dfIntersectMaxY)
1412
0
            {
1413
                /* Re-compute (x,y,width,height) subwindow of current raster
1414
                 * from */
1415
                /* the extent of the clipped block */
1416
0
                nX = static_cast<int>((dfIntersectMinX - dfRasterMinX) / gt[1] +
1417
0
                                      0.5);
1418
0
                if (gt[5] < 0)
1419
0
                    nY = static_cast<int>(
1420
0
                        (dfRasterMaxY - dfIntersectMaxY) / (-gt[5]) + 0.5);
1421
0
                else
1422
0
                    nY = static_cast<int>(
1423
0
                        (dfIntersectMinY - dfRasterMinY) / gt[5] + 0.5);
1424
0
                nReqWidth =
1425
0
                    static_cast<int>((dfIntersectMaxX - dfRasterMinX) / gt[1] +
1426
0
                                     0.5) -
1427
0
                    nX;
1428
0
                if (gt[5] < 0)
1429
0
                    nReqHeight =
1430
0
                        static_cast<int>(
1431
0
                            (dfRasterMaxY - dfIntersectMinY) / (-gt[5]) + 0.5) -
1432
0
                        nY;
1433
0
                else
1434
0
                    nReqHeight =
1435
0
                        static_cast<int>(
1436
0
                            (dfIntersectMaxY - dfRasterMinY) / gt[5] + 0.5) -
1437
0
                        nY;
1438
1439
0
                if (nReqWidth > 0 && nReqHeight > 0)
1440
0
                {
1441
0
                    auto nImageId = WriteBlock(
1442
0
                        poDS, nX, nY, nReqWidth, nReqHeight, nColorTableId,
1443
0
                        eCompressMethod, nPredictor, nJPEGQuality,
1444
0
                        pszJPEG2000_DRIVER, GDALScaledProgress, pScaledData);
1445
1446
0
                    if (!nImageId.toBool())
1447
0
                    {
1448
0
                        GDALDestroyScaledProgress(pScaledData);
1449
0
                        return false;
1450
0
                    }
1451
1452
                    /* Compute the subwindow in image coordinates of the main
1453
                     * raster corresponding */
1454
                    /* to the extent of the clipped block */
1455
0
                    double dfXInClippingUnits, dfYInClippingUnits,
1456
0
                        dfReqWidthInClippingUnits, dfReqHeightInClippingUnits;
1457
1458
0
                    dfXInClippingUnits =
1459
0
                        (dfIntersectMinX - dfClippingMinX) / clippingGT[1];
1460
0
                    if (clippingGT[5] < 0)
1461
0
                        dfYInClippingUnits =
1462
0
                            (dfClippingMaxY - dfIntersectMaxY) /
1463
0
                            (-clippingGT[5]);
1464
0
                    else
1465
0
                        dfYInClippingUnits =
1466
0
                            (dfIntersectMinY - dfClippingMinY) / clippingGT[5];
1467
0
                    dfReqWidthInClippingUnits =
1468
0
                        (dfIntersectMaxX - dfClippingMinX) / clippingGT[1] -
1469
0
                        dfXInClippingUnits;
1470
0
                    if (clippingGT[5] < 0)
1471
0
                        dfReqHeightInClippingUnits =
1472
0
                            (dfClippingMaxY - dfIntersectMinY) /
1473
0
                                (-clippingGT[5]) -
1474
0
                            dfYInClippingUnits;
1475
0
                    else
1476
0
                        dfReqHeightInClippingUnits =
1477
0
                            (dfIntersectMaxY - dfClippingMinY) / clippingGT[5] -
1478
0
                            dfYInClippingUnits;
1479
1480
0
                    GDALPDFImageDesc oImageDesc;
1481
0
                    oImageDesc.nImageId = nImageId;
1482
0
                    oImageDesc.dfXOff = dfXInClippingUnits / dfUserUnit +
1483
0
                                        oPageContext.sMargins.nLeft;
1484
0
                    oImageDesc.dfYOff = (nClippingHeight - dfYInClippingUnits -
1485
0
                                         dfReqHeightInClippingUnits) /
1486
0
                                            dfUserUnit +
1487
0
                                        oPageContext.sMargins.nBottom;
1488
0
                    oImageDesc.dfXSize = dfReqWidthInClippingUnits / dfUserUnit;
1489
0
                    oImageDesc.dfYSize =
1490
0
                        dfReqHeightInClippingUnits / dfUserUnit;
1491
1492
0
                    oRasterDesc.asImageDesc.push_back(oImageDesc);
1493
0
                }
1494
0
            }
1495
1496
0
            GDALDestroyScaledProgress(pScaledData);
1497
0
        }
1498
0
    }
1499
1500
0
    oPageContext.asRasterDesc.push_back(std::move(oRasterDesc));
1501
1502
0
    return true;
1503
0
}
1504
1505
/************************************************************************/
1506
/*                          WriteOGRDataSource()                        */
1507
/************************************************************************/
1508
1509
bool GDALPDFWriter::WriteOGRDataSource(const char *pszOGRDataSource,
1510
                                       const char *pszOGRDisplayField,
1511
                                       const char *pszOGRDisplayLayerNames,
1512
                                       const char *pszOGRLinkField,
1513
                                       int bWriteOGRAttributes)
1514
0
{
1515
0
    GDALDatasetH hDS =
1516
0
        GDALOpenEx(pszOGRDataSource, GDAL_OF_VECTOR | GDAL_OF_VERBOSE_ERROR,
1517
0
                   nullptr, nullptr, nullptr);
1518
0
    if (hDS == nullptr)
1519
0
        return false;
1520
1521
0
    int iObj = 0;
1522
1523
0
    int nLayers = GDALDatasetGetLayerCount(hDS);
1524
1525
0
    char **papszLayerNames =
1526
0
        CSLTokenizeString2(pszOGRDisplayLayerNames, ",", 0);
1527
1528
0
    for (int iLayer = 0; iLayer < nLayers; iLayer++)
1529
0
    {
1530
0
        CPLString osLayerName;
1531
0
        if (CSLCount(papszLayerNames) < nLayers)
1532
0
            osLayerName = OGR_L_GetName(GDALDatasetGetLayer(hDS, iLayer));
1533
0
        else
1534
0
            osLayerName = papszLayerNames[iLayer];
1535
1536
0
        WriteOGRLayer(hDS, iLayer, pszOGRDisplayField, pszOGRLinkField,
1537
0
                      osLayerName, bWriteOGRAttributes, iObj);
1538
0
    }
1539
1540
0
    GDALClose(hDS);
1541
1542
0
    CSLDestroy(papszLayerNames);
1543
1544
0
    return true;
1545
0
}
1546
1547
/************************************************************************/
1548
/*                           StartOGRLayer()                            */
1549
/************************************************************************/
1550
1551
GDALPDFLayerDesc GDALPDFWriter::StartOGRLayer(const std::string &osLayerName,
1552
                                              int bWriteOGRAttributes)
1553
1.17k
{
1554
1.17k
    GDALPDFLayerDesc osVectorDesc;
1555
1.17k
    osVectorDesc.osLayerName = osLayerName;
1556
#ifdef HACK_TO_GENERATE_OCMD
1557
    osVectorDesc.bWriteOGRAttributes = false;
1558
    auto nParentOCGId = WriteOCG("parent");
1559
    osVectorDesc.nOCGId = WriteOCG(osLayerName.c_str(), nParentOCGId);
1560
#else
1561
1.17k
    osVectorDesc.bWriteOGRAttributes = bWriteOGRAttributes;
1562
1.17k
    osVectorDesc.nOCGId = WriteOCG(osLayerName.c_str());
1563
1.17k
#endif
1564
1.17k
    if (bWriteOGRAttributes)
1565
1.17k
        osVectorDesc.nFeatureLayerId = AllocNewObject();
1566
1567
1.17k
    return osVectorDesc;
1568
1.17k
}
1569
1570
/************************************************************************/
1571
/*                           EndOGRLayer()                              */
1572
/************************************************************************/
1573
1574
void GDALPDFWriter::EndOGRLayer(GDALPDFLayerDesc &osVectorDesc)
1575
1.17k
{
1576
1.17k
    if (osVectorDesc.bWriteOGRAttributes)
1577
1.17k
    {
1578
1.17k
        StartObj(osVectorDesc.nFeatureLayerId);
1579
1580
1.17k
        GDALPDFDictionaryRW oDict;
1581
1.17k
        oDict.Add("A", &(new GDALPDFDictionaryRW())
1582
1.17k
                            ->Add("O", GDALPDFObjectRW::CreateName(
1583
1.17k
                                           "UserProperties")));
1584
1585
1.17k
        GDALPDFArrayRW *poArray = new GDALPDFArrayRW();
1586
1.17k
        oDict.Add("K", poArray);
1587
1588
1.17k
        for (const auto &prop : osVectorDesc.aUserPropertiesIds)
1589
1.64k
        {
1590
1.64k
            poArray->Add(prop, 0);
1591
1.64k
        }
1592
1593
1.17k
        if (!m_nStructTreeRootId.toBool())
1594
45
            m_nStructTreeRootId = AllocNewObject();
1595
1596
1.17k
        oDict.Add("P", m_nStructTreeRootId, 0);
1597
1.17k
        oDict.Add("S", GDALPDFObjectRW::CreateName("Feature"));
1598
1.17k
        oDict.Add("T", osVectorDesc.osLayerName);
1599
1600
1.17k
        VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
1601
1602
1.17k
        EndObj();
1603
1.17k
    }
1604
1605
1.17k
    oPageContext.asVectorDesc.push_back(osVectorDesc);
1606
1.17k
}
1607
1608
/************************************************************************/
1609
/*                           WriteOGRLayer()                            */
1610
/************************************************************************/
1611
1612
int GDALPDFWriter::WriteOGRLayer(GDALDatasetH hDS, int iLayer,
1613
                                 const char *pszOGRDisplayField,
1614
                                 const char *pszOGRLinkField,
1615
                                 const std::string &osLayerName,
1616
                                 int bWriteOGRAttributes, int &iObj)
1617
1.17k
{
1618
1.17k
    GDALDataset *poClippingDS = oPageContext.poClippingDS;
1619
1.17k
    GDALGeoTransform gt;
1620
1.17k
    if (poClippingDS->GetGeoTransform(gt) != CE_None)
1621
0
        return FALSE;
1622
1623
1.17k
    GDALPDFLayerDesc osVectorDesc =
1624
1.17k
        StartOGRLayer(osLayerName, bWriteOGRAttributes);
1625
1.17k
    OGRLayerH hLyr = GDALDatasetGetLayer(hDS, iLayer);
1626
1627
1.17k
    const auto poLayerDefn = OGRLayer::FromHandle(hLyr)->GetLayerDefn();
1628
9.75k
    for (int i = 0; i < poLayerDefn->GetFieldCount(); i++)
1629
8.57k
    {
1630
8.57k
        const auto poFieldDefn = poLayerDefn->GetFieldDefn(i);
1631
8.57k
        const char *pszName = poFieldDefn->GetNameRef();
1632
8.57k
        osVectorDesc.aosIncludedFields.push_back(pszName);
1633
8.57k
    }
1634
1635
1.17k
    OGRSpatialReferenceH hGDAL_SRS = OGRSpatialReference::ToHandle(
1636
1.17k
        const_cast<OGRSpatialReference *>(poClippingDS->GetSpatialRef()));
1637
1.17k
    OGRSpatialReferenceH hOGR_SRS = OGR_L_GetSpatialRef(hLyr);
1638
1.17k
    OGRCoordinateTransformationH hCT = nullptr;
1639
1640
1.17k
    if (hGDAL_SRS == nullptr && hOGR_SRS != nullptr)
1641
69
    {
1642
69
        CPLError(CE_Warning, CPLE_AppDefined,
1643
69
                 "Vector layer has a SRS set, but Raster layer has no SRS set. "
1644
69
                 "Assuming they are the same.");
1645
69
    }
1646
1.10k
    else if (hGDAL_SRS != nullptr && hOGR_SRS == nullptr)
1647
232
    {
1648
232
        CPLError(CE_Warning, CPLE_AppDefined,
1649
232
                 "Vector layer has no SRS set, but Raster layer has a SRS set. "
1650
232
                 "Assuming they are the same.");
1651
232
    }
1652
877
    else if (hGDAL_SRS != nullptr && hOGR_SRS != nullptr)
1653
25
    {
1654
25
        if (!OSRIsSame(hGDAL_SRS, hOGR_SRS))
1655
16
        {
1656
16
            hCT = OCTNewCoordinateTransformation(hOGR_SRS, hGDAL_SRS);
1657
16
            if (hCT == nullptr)
1658
1
            {
1659
1
                CPLError(CE_Warning, CPLE_AppDefined,
1660
1
                         "Cannot compute coordinate transformation from vector "
1661
1
                         "SRS to raster SRS");
1662
1
            }
1663
16
        }
1664
25
    }
1665
1666
1.17k
    if (hCT == nullptr)
1667
1.16k
    {
1668
1.16k
        double dfXMin = gt[0];
1669
1.16k
        double dfYMin = gt[3] + poClippingDS->GetRasterYSize() * gt[5];
1670
1.16k
        double dfXMax = gt[0] + poClippingDS->GetRasterXSize() * gt[1];
1671
1.16k
        double dfYMax = gt[3];
1672
1.16k
        OGR_L_SetSpatialFilterRect(hLyr, dfXMin, dfYMin, dfXMax, dfYMax);
1673
1.16k
    }
1674
1675
1.17k
    OGRFeatureH hFeat;
1676
1677
158k
    while ((hFeat = OGR_L_GetNextFeature(hLyr)) != nullptr)
1678
156k
    {
1679
156k
        WriteOGRFeature(osVectorDesc, hFeat, hCT, pszOGRDisplayField,
1680
156k
                        pszOGRLinkField, bWriteOGRAttributes, iObj);
1681
1682
156k
        OGR_F_Destroy(hFeat);
1683
156k
    }
1684
1685
1.17k
    EndOGRLayer(osVectorDesc);
1686
1687
1.17k
    if (hCT != nullptr)
1688
15
        OCTDestroyCoordinateTransformation(hCT);
1689
1690
1.17k
    return TRUE;
1691
1.17k
}
1692
1693
/************************************************************************/
1694
/*                             DrawGeometry()                           */
1695
/************************************************************************/
1696
1697
static void DrawGeometry(CPLString &osDS, OGRGeometryH hGeom,
1698
                         const double adfMatrix[4], bool bPaint = true)
1699
1.03k
{
1700
1.03k
    switch (wkbFlatten(OGR_G_GetGeometryType(hGeom)))
1701
1.03k
    {
1702
394
        case wkbLineString:
1703
394
        {
1704
394
            int nPoints = OGR_G_GetPointCount(hGeom);
1705
1.18k
            for (int i = 0; i < nPoints; i++)
1706
789
            {
1707
789
                double dfX = OGR_G_GetX(hGeom, i) * adfMatrix[1] + adfMatrix[0];
1708
789
                double dfY = OGR_G_GetY(hGeom, i) * adfMatrix[3] + adfMatrix[2];
1709
789
                osDS +=
1710
789
                    CPLOPrintf("%f %f %c\n", dfX, dfY, (i == 0) ? 'm' : 'l');
1711
789
            }
1712
394
            if (bPaint)
1713
131
                osDS += CPLOPrintf("S\n");
1714
394
            break;
1715
0
        }
1716
1717
193
        case wkbPolygon:
1718
193
        {
1719
193
            int nParts = OGR_G_GetGeometryCount(hGeom);
1720
456
            for (int i = 0; i < nParts; i++)
1721
263
            {
1722
263
                DrawGeometry(osDS, OGR_G_GetGeometryRef(hGeom, i), adfMatrix,
1723
263
                             false);
1724
263
                osDS += CPLOPrintf("h\n");
1725
263
            }
1726
193
            if (bPaint)
1727
143
                osDS += CPLOPrintf("b*\n");
1728
193
            break;
1729
0
        }
1730
1731
0
        case wkbMultiLineString:
1732
0
        {
1733
0
            int nParts = OGR_G_GetGeometryCount(hGeom);
1734
0
            for (int i = 0; i < nParts; i++)
1735
0
            {
1736
0
                DrawGeometry(osDS, OGR_G_GetGeometryRef(hGeom, i), adfMatrix,
1737
0
                             false);
1738
0
            }
1739
0
            if (bPaint)
1740
0
                osDS += CPLOPrintf("S\n");
1741
0
            break;
1742
0
        }
1743
1744
50
        case wkbMultiPolygon:
1745
50
        {
1746
50
            int nParts = OGR_G_GetGeometryCount(hGeom);
1747
100
            for (int i = 0; i < nParts; i++)
1748
50
            {
1749
50
                DrawGeometry(osDS, OGR_G_GetGeometryRef(hGeom, i), adfMatrix,
1750
50
                             false);
1751
50
            }
1752
50
            if (bPaint)
1753
50
                osDS += CPLOPrintf("b*\n");
1754
50
            break;
1755
0
        }
1756
1757
397
        default:
1758
397
            break;
1759
1.03k
    }
1760
1.03k
}
1761
1762
/************************************************************************/
1763
/*                           CalculateText()                            */
1764
/************************************************************************/
1765
1766
static void CalculateText(const CPLString &osText, CPLString &osFont,
1767
                          const double dfSize, const bool bBold,
1768
                          const bool bItalic, double &dfWidth, double &dfHeight)
1769
0
{
1770
    // Character widths of Helvetica, Win-1252 characters 32 to 255
1771
    // Helvetica bold, oblique and bold oblique have their own widths,
1772
    // but for now we will put up with these widths on all Helvetica variants
1773
0
    constexpr GUInt16 anHelveticaCharWidths[] = {
1774
0
        569,  569,  727,  1139, 1139, 1821, 1366, 391,  682,  682,  797,  1196,
1775
0
        569,  682,  569,  569,  1139, 1139, 1139, 1139, 1139, 1139, 1139, 1139,
1776
0
        1139, 1139, 569,  569,  1196, 1196, 1196, 1139, 2079, 1366, 1366, 1479,
1777
0
        1479, 1366, 1251, 1593, 1479, 569,  1024, 1366, 1139, 1706, 1479, 1593,
1778
0
        1366, 1593, 1479, 1366, 1251, 1479, 1366, 1933, 1366, 1366, 1251, 569,
1779
0
        569,  569,  961,  1139, 682,  1139, 1139, 1024, 1139, 1139, 569,  1139,
1780
0
        1139, 455,  455,  1024, 455,  1706, 1139, 1139, 1139, 1139, 682,  1024,
1781
0
        569,  1139, 1024, 1479, 1024, 1024, 1024, 684,  532,  684,  1196, 1536,
1782
0
        1139, 2048, 455,  1139, 682,  2048, 1139, 1139, 682,  2048, 1366, 682,
1783
0
        2048, 2048, 1251, 2048, 2048, 455,  455,  682,  682,  717,  1139, 2048,
1784
0
        682,  2048, 1024, 682,  1933, 2048, 1024, 1366, 569,  682,  1139, 1139,
1785
0
        1139, 1139, 532,  1139, 682,  1509, 758,  1139, 1196, 682,  1509, 1131,
1786
0
        819,  1124, 682,  682,  682,  1180, 1100, 682,  682,  682,  748,  1139,
1787
0
        1708, 1708, 1708, 1251, 1366, 1366, 1366, 1366, 1366, 1366, 2048, 1479,
1788
0
        1366, 1366, 1366, 1366, 569,  569,  569,  569,  1479, 1479, 1593, 1593,
1789
0
        1593, 1593, 1593, 1196, 1593, 1479, 1479, 1479, 1479, 1366, 1366, 1251,
1790
0
        1139, 1139, 1139, 1139, 1139, 1139, 1821, 1024, 1139, 1139, 1139, 1139,
1791
0
        569,  569,  569,  569,  1139, 1139, 1139, 1139, 1139, 1139, 1139, 1124,
1792
0
        1251, 1139, 1139, 1139, 1139, 1024, 1139, 1024};
1793
1794
    // Character widths of Times-Roman, Win-1252 characters 32 to 255
1795
    // Times bold, italic and bold italic have their own widths,
1796
    // but for now we will put up with these widths on all Times variants
1797
0
    constexpr GUInt16 anTimesCharWidths[] = {
1798
0
        512,  682,  836,  1024, 1024, 1706, 1593, 369,  682,  682,  1024, 1155,
1799
0
        512,  682,  512,  569,  1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
1800
0
        1024, 1024, 569,  569,  1155, 1155, 1155, 909,  1886, 1479, 1366, 1366,
1801
0
        1479, 1251, 1139, 1479, 1479, 682,  797,  1479, 1251, 1821, 1479, 1479,
1802
0
        1139, 1479, 1366, 1139, 1251, 1479, 1479, 1933, 1479, 1479, 1251, 682,
1803
0
        569,  682,  961,  1024, 682,  909,  1024, 909,  1024, 909,  682,  1024,
1804
0
        1024, 569,  569,  1024, 569,  1593, 1024, 1024, 1024, 1024, 682,  797,
1805
0
        569,  1024, 1024, 1479, 1024, 1024, 909,  983,  410,  983,  1108, 0,
1806
0
        1024, 2048, 682,  1024, 909,  2048, 1024, 1024, 682,  2048, 1139, 682,
1807
0
        1821, 2048, 1251, 2048, 2048, 682,  682,  909,  909,  717,  1024, 2048,
1808
0
        682,  2007, 797,  682,  1479, 2048, 909,  1479, 512,  682,  1024, 1024,
1809
0
        1024, 1024, 410,  1024, 682,  1556, 565,  1024, 1155, 682,  1556, 1024,
1810
0
        819,  1124, 614,  614,  682,  1180, 928,  682,  682,  614,  635,  1024,
1811
0
        1536, 1536, 1536, 909,  1479, 1479, 1479, 1479, 1479, 1479, 1821, 1366,
1812
0
        1251, 1251, 1251, 1251, 682,  682,  682,  682,  1479, 1479, 1479, 1479,
1813
0
        1479, 1479, 1479, 1155, 1479, 1479, 1479, 1479, 1479, 1479, 1139, 1024,
1814
0
        909,  909,  909,  909,  909,  909,  1366, 909,  909,  909,  909,  909,
1815
0
        569,  569,  569,  569,  1024, 1024, 1024, 1024, 1024, 1024, 1024, 1124,
1816
0
        1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024};
1817
1818
0
    const GUInt16 *panCharacterWidths = nullptr;
1819
1820
0
    if (STARTS_WITH_CI(osFont, "times") ||
1821
0
        osFont.find("Serif", 0) != std::string::npos)
1822
0
    {
1823
0
        if (bBold && bItalic)
1824
0
            osFont = "Times-BoldItalic";
1825
0
        else if (bBold)
1826
0
            osFont = "Times-Bold";
1827
0
        else if (bItalic)
1828
0
            osFont = "Times-Italic";
1829
0
        else
1830
0
            osFont = "Times-Roman";
1831
1832
0
        panCharacterWidths = anTimesCharWidths;
1833
0
        dfHeight = dfSize * 1356.0 / 2048;
1834
0
    }
1835
0
    else if (STARTS_WITH_CI(osFont, "courier") ||
1836
0
             osFont.find("Mono", 0) != std::string::npos)
1837
0
    {
1838
0
        if (bBold && bItalic)
1839
0
            osFont = "Courier-BoldOblique";
1840
0
        else if (bBold)
1841
0
            osFont = "Courier-Bold";
1842
0
        else if (bItalic)
1843
0
            osFont = "Courier-Oblique";
1844
0
        else
1845
0
            osFont = "Courier";
1846
1847
0
        dfHeight = dfSize * 1170.0 / 2048;
1848
0
    }
1849
0
    else
1850
0
    {
1851
0
        if (bBold && bItalic)
1852
0
            osFont = "Helvetica-BoldOblique";
1853
0
        else if (bBold)
1854
0
            osFont = "Helvetica-Bold";
1855
0
        else if (bItalic)
1856
0
            osFont = "Helvetica-Oblique";
1857
0
        else
1858
0
            osFont = "Helvetica";
1859
1860
0
        panCharacterWidths = anHelveticaCharWidths;
1861
0
        dfHeight = dfSize * 1467.0 / 2048;
1862
0
    }
1863
1864
0
    dfWidth = 0.0;
1865
0
    for (const char &ch : osText)
1866
0
    {
1867
0
        const int nCh = static_cast<int>(ch);
1868
0
        if (nCh < 32)
1869
0
            continue;
1870
1871
0
        dfWidth +=
1872
0
            (panCharacterWidths ? panCharacterWidths[nCh - 32]
1873
0
                                : 1229);  // Courier's fixed character width
1874
0
    }
1875
0
    dfWidth *= dfSize / 2048;
1876
0
}
1877
1878
/************************************************************************/
1879
/*                          GetObjectStyle()                            */
1880
/************************************************************************/
1881
1882
void GDALPDFBaseWriter::GetObjectStyle(
1883
    const char *pszStyleString, OGRFeatureH hFeat, const double adfMatrix[4],
1884
    std::map<CPLString, GDALPDFImageDesc> oMapSymbolFilenameToDesc,
1885
    ObjectStyle &os)
1886
1.64k
{
1887
1.64k
    OGRStyleMgrH hSM = OGR_SM_Create(nullptr);
1888
1.64k
    if (pszStyleString)
1889
0
        OGR_SM_InitStyleString(hSM, pszStyleString);
1890
1.64k
    else
1891
1.64k
        OGR_SM_InitFromFeature(hSM, hFeat);
1892
1.64k
    int nCount = OGR_SM_GetPartCount(hSM, nullptr);
1893
1.64k
    for (int iPart = 0; iPart < nCount; iPart++)
1894
0
    {
1895
0
        OGRStyleToolH hTool = OGR_SM_GetPart(hSM, iPart, nullptr);
1896
0
        if (hTool)
1897
0
        {
1898
            // Figure out how to involve adfMatrix[3] here and below
1899
0
            OGR_ST_SetUnit(hTool, OGRSTUMM, 1000.0 / adfMatrix[1]);
1900
0
            if (OGR_ST_GetType(hTool) == OGRSTCPen)
1901
0
            {
1902
0
                os.bHasPenBrushOrSymbol = true;
1903
1904
0
                int bIsNull = TRUE;
1905
0
                const char *pszColor =
1906
0
                    OGR_ST_GetParamStr(hTool, OGRSTPenColor, &bIsNull);
1907
0
                if (pszColor && !bIsNull)
1908
0
                {
1909
0
                    unsigned int nRed = 0;
1910
0
                    unsigned int nGreen = 0;
1911
0
                    unsigned int nBlue = 0;
1912
0
                    unsigned int nAlpha = 255;
1913
0
                    int nVals = sscanf(pszColor, "#%2x%2x%2x%2x", &nRed,
1914
0
                                       &nGreen, &nBlue, &nAlpha);
1915
0
                    if (nVals >= 3)
1916
0
                    {
1917
0
                        os.nPenR = nRed;
1918
0
                        os.nPenG = nGreen;
1919
0
                        os.nPenB = nBlue;
1920
0
                        if (nVals == 4)
1921
0
                            os.nPenA = nAlpha;
1922
0
                    }
1923
0
                }
1924
1925
0
                const char *pszDash =
1926
0
                    OGR_ST_GetParamStr(hTool, OGRSTPenPattern, &bIsNull);
1927
0
                if (pszDash && !bIsNull)
1928
0
                {
1929
0
                    char **papszTokens = CSLTokenizeString2(pszDash, " ", 0);
1930
0
                    int nTokens = CSLCount(papszTokens);
1931
0
                    if ((nTokens % 2) == 0)
1932
0
                    {
1933
0
                        for (int i = 0; i < nTokens; i++)
1934
0
                        {
1935
0
                            double dfElement = CPLAtof(papszTokens[i]);
1936
0
                            dfElement *= adfMatrix[1];  // should involve
1937
                                                        // adfMatrix[3] too
1938
0
                            os.osDashArray += CPLSPrintf("%f ", dfElement);
1939
0
                        }
1940
0
                    }
1941
0
                    CSLDestroy(papszTokens);
1942
0
                }
1943
1944
                // OGRSTUnitId eUnit = OGR_ST_GetUnit(hTool);
1945
0
                double dfWidth =
1946
0
                    OGR_ST_GetParamDbl(hTool, OGRSTPenWidth, &bIsNull);
1947
0
                if (!bIsNull)
1948
0
                    os.dfPenWidth = dfWidth;
1949
0
            }
1950
0
            else if (OGR_ST_GetType(hTool) == OGRSTCBrush)
1951
0
            {
1952
0
                os.bHasPenBrushOrSymbol = true;
1953
1954
0
                int bIsNull;
1955
0
                const char *pszColor =
1956
0
                    OGR_ST_GetParamStr(hTool, OGRSTBrushFColor, &bIsNull);
1957
0
                if (pszColor)
1958
0
                {
1959
0
                    unsigned int nRed = 0;
1960
0
                    unsigned int nGreen = 0;
1961
0
                    unsigned int nBlue = 0;
1962
0
                    unsigned int nAlpha = 255;
1963
0
                    int nVals = sscanf(pszColor, "#%2x%2x%2x%2x", &nRed,
1964
0
                                       &nGreen, &nBlue, &nAlpha);
1965
0
                    if (nVals >= 3)
1966
0
                    {
1967
0
                        os.nBrushR = nRed;
1968
0
                        os.nBrushG = nGreen;
1969
0
                        os.nBrushB = nBlue;
1970
0
                        if (nVals == 4)
1971
0
                            os.nBrushA = nAlpha;
1972
0
                    }
1973
0
                }
1974
0
            }
1975
0
            else if (OGR_ST_GetType(hTool) == OGRSTCLabel)
1976
0
            {
1977
0
                int bIsNull;
1978
0
                const char *pszStr =
1979
0
                    OGR_ST_GetParamStr(hTool, OGRSTLabelTextString, &bIsNull);
1980
0
                if (pszStr)
1981
0
                {
1982
0
                    os.osLabelText = pszStr;
1983
1984
                    /* If the text is of the form {stuff}, then it means we want
1985
                     * to fetch */
1986
                    /* the value of the field "stuff" in the feature */
1987
0
                    if (!os.osLabelText.empty() && os.osLabelText[0] == '{' &&
1988
0
                        os.osLabelText.back() == '}')
1989
0
                    {
1990
0
                        os.osLabelText = pszStr + 1;
1991
0
                        os.osLabelText.resize(os.osLabelText.size() - 1);
1992
1993
0
                        int nIdxField =
1994
0
                            OGR_F_GetFieldIndex(hFeat, os.osLabelText);
1995
0
                        if (nIdxField >= 0)
1996
0
                            os.osLabelText =
1997
0
                                OGR_F_GetFieldAsString(hFeat, nIdxField);
1998
0
                        else
1999
0
                            os.osLabelText = "";
2000
0
                    }
2001
0
                }
2002
2003
0
                const char *pszColor =
2004
0
                    OGR_ST_GetParamStr(hTool, OGRSTLabelFColor, &bIsNull);
2005
0
                if (pszColor && !bIsNull)
2006
0
                {
2007
0
                    unsigned int nRed = 0;
2008
0
                    unsigned int nGreen = 0;
2009
0
                    unsigned int nBlue = 0;
2010
0
                    unsigned int nAlpha = 255;
2011
0
                    int nVals = sscanf(pszColor, "#%2x%2x%2x%2x", &nRed,
2012
0
                                       &nGreen, &nBlue, &nAlpha);
2013
0
                    if (nVals >= 3)
2014
0
                    {
2015
0
                        os.nTextR = nRed;
2016
0
                        os.nTextG = nGreen;
2017
0
                        os.nTextB = nBlue;
2018
0
                        if (nVals == 4)
2019
0
                            os.nTextA = nAlpha;
2020
0
                    }
2021
0
                }
2022
2023
0
                pszStr =
2024
0
                    OGR_ST_GetParamStr(hTool, OGRSTLabelFontName, &bIsNull);
2025
0
                if (pszStr && !bIsNull)
2026
0
                    os.osTextFont = pszStr;
2027
2028
0
                double dfVal =
2029
0
                    OGR_ST_GetParamDbl(hTool, OGRSTLabelSize, &bIsNull);
2030
0
                if (!bIsNull)
2031
0
                    os.dfTextSize = dfVal;
2032
2033
0
                dfVal = OGR_ST_GetParamDbl(hTool, OGRSTLabelAngle, &bIsNull);
2034
0
                if (!bIsNull)
2035
0
                    os.dfTextAngle = dfVal * M_PI / 180.0;
2036
2037
0
                dfVal = OGR_ST_GetParamDbl(hTool, OGRSTLabelStretch, &bIsNull);
2038
0
                if (!bIsNull)
2039
0
                    os.dfTextStretch = dfVal / 100.0;
2040
2041
0
                dfVal = OGR_ST_GetParamDbl(hTool, OGRSTLabelDx, &bIsNull);
2042
0
                if (!bIsNull)
2043
0
                    os.dfTextDx = dfVal;
2044
2045
0
                dfVal = OGR_ST_GetParamDbl(hTool, OGRSTLabelDy, &bIsNull);
2046
0
                if (!bIsNull)
2047
0
                    os.dfTextDy = dfVal;
2048
2049
0
                int nVal =
2050
0
                    OGR_ST_GetParamNum(hTool, OGRSTLabelAnchor, &bIsNull);
2051
0
                if (!bIsNull)
2052
0
                    os.nTextAnchor = nVal;
2053
2054
0
                nVal = OGR_ST_GetParamNum(hTool, OGRSTLabelBold, &bIsNull);
2055
0
                if (!bIsNull)
2056
0
                    os.bTextBold = (nVal != 0);
2057
2058
0
                nVal = OGR_ST_GetParamNum(hTool, OGRSTLabelItalic, &bIsNull);
2059
0
                if (!bIsNull)
2060
0
                    os.bTextItalic = (nVal != 0);
2061
0
            }
2062
0
            else if (OGR_ST_GetType(hTool) == OGRSTCSymbol)
2063
0
            {
2064
0
                os.bHasPenBrushOrSymbol = true;
2065
2066
0
                int bIsNull;
2067
0
                const char *pszSymbolId =
2068
0
                    OGR_ST_GetParamStr(hTool, OGRSTSymbolId, &bIsNull);
2069
0
                if (pszSymbolId && !bIsNull)
2070
0
                {
2071
0
                    os.osSymbolId = pszSymbolId;
2072
2073
0
                    if (strstr(pszSymbolId, "ogr-sym-") == nullptr)
2074
0
                    {
2075
0
                        if (oMapSymbolFilenameToDesc.find(os.osSymbolId) ==
2076
0
                            oMapSymbolFilenameToDesc.end())
2077
0
                        {
2078
0
                            CPLPushErrorHandler(CPLQuietErrorHandler);
2079
0
                            GDALDatasetH hImageDS =
2080
0
                                GDALOpen(os.osSymbolId, GA_ReadOnly);
2081
0
                            CPLPopErrorHandler();
2082
0
                            if (hImageDS != nullptr)
2083
0
                            {
2084
0
                                os.nImageWidth = GDALGetRasterXSize(hImageDS);
2085
0
                                os.nImageHeight = GDALGetRasterYSize(hImageDS);
2086
2087
0
                                os.nImageSymbolId = WriteBlock(
2088
0
                                    GDALDataset::FromHandle(hImageDS), 0, 0,
2089
0
                                    os.nImageWidth, os.nImageHeight,
2090
0
                                    GDALPDFObjectNum(), COMPRESS_DEFAULT, 0, -1,
2091
0
                                    nullptr, nullptr, nullptr);
2092
0
                                GDALClose(hImageDS);
2093
0
                            }
2094
2095
0
                            GDALPDFImageDesc oDesc;
2096
0
                            oDesc.nImageId = os.nImageSymbolId;
2097
0
                            oDesc.dfXOff = 0;
2098
0
                            oDesc.dfYOff = 0;
2099
0
                            oDesc.dfXSize = os.nImageWidth;
2100
0
                            oDesc.dfYSize = os.nImageHeight;
2101
0
                            oMapSymbolFilenameToDesc[os.osSymbolId] = oDesc;
2102
0
                        }
2103
0
                        else
2104
0
                        {
2105
0
                            const GDALPDFImageDesc &oDesc =
2106
0
                                oMapSymbolFilenameToDesc[os.osSymbolId];
2107
0
                            os.nImageSymbolId = oDesc.nImageId;
2108
0
                            os.nImageWidth = static_cast<int>(oDesc.dfXSize);
2109
0
                            os.nImageHeight = static_cast<int>(oDesc.dfYSize);
2110
0
                        }
2111
0
                    }
2112
0
                }
2113
2114
0
                double dfVal =
2115
0
                    OGR_ST_GetParamDbl(hTool, OGRSTSymbolSize, &bIsNull);
2116
0
                if (!bIsNull)
2117
0
                {
2118
0
                    os.dfSymbolSize = dfVal;
2119
0
                }
2120
2121
0
                const char *pszColor =
2122
0
                    OGR_ST_GetParamStr(hTool, OGRSTSymbolColor, &bIsNull);
2123
0
                if (pszColor && !bIsNull)
2124
0
                {
2125
0
                    unsigned int nRed = 0;
2126
0
                    unsigned int nGreen = 0;
2127
0
                    unsigned int nBlue = 0;
2128
0
                    unsigned int nAlpha = 255;
2129
0
                    int nVals = sscanf(pszColor, "#%2x%2x%2x%2x", &nRed,
2130
0
                                       &nGreen, &nBlue, &nAlpha);
2131
0
                    if (nVals >= 3)
2132
0
                    {
2133
0
                        os.bSymbolColorDefined = TRUE;
2134
0
                        os.nSymbolR = nRed;
2135
0
                        os.nSymbolG = nGreen;
2136
0
                        os.nSymbolB = nBlue;
2137
0
                        if (nVals == 4)
2138
0
                            os.nSymbolA = nAlpha;
2139
0
                    }
2140
0
                }
2141
0
            }
2142
2143
0
            OGR_ST_Destroy(hTool);
2144
0
        }
2145
0
    }
2146
1.64k
    OGR_SM_Destroy(hSM);
2147
2148
1.64k
    OGRGeometryH hGeom = OGR_F_GetGeometryRef(hFeat);
2149
1.64k
    if (wkbFlatten(OGR_G_GetGeometryType(hGeom)) == wkbPoint &&
2150
1.64k
        os.bSymbolColorDefined)
2151
0
    {
2152
0
        os.nPenR = os.nSymbolR;
2153
0
        os.nPenG = os.nSymbolG;
2154
0
        os.nPenB = os.nSymbolB;
2155
0
        os.nPenA = os.nSymbolA;
2156
0
        os.nBrushR = os.nSymbolR;
2157
0
        os.nBrushG = os.nSymbolG;
2158
0
        os.nBrushB = os.nSymbolB;
2159
0
        os.nBrushA = os.nSymbolA;
2160
0
    }
2161
1.64k
}
2162
2163
/************************************************************************/
2164
/*                           ComputeIntBBox()                           */
2165
/************************************************************************/
2166
2167
void GDALPDFBaseWriter::ComputeIntBBox(
2168
    OGRGeometryH hGeom, const OGREnvelope &sEnvelope, const double adfMatrix[4],
2169
    const GDALPDFWriter::ObjectStyle &os, double dfRadius, int &bboxXMin,
2170
    int &bboxYMin, int &bboxXMax, int &bboxYMax)
2171
1.64k
{
2172
1.64k
    if (wkbFlatten(OGR_G_GetGeometryType(hGeom)) == wkbPoint &&
2173
1.64k
        os.nImageSymbolId.toBool())
2174
0
    {
2175
0
        const double dfSemiWidth =
2176
0
            (os.nImageWidth >= os.nImageHeight)
2177
0
                ? dfRadius
2178
0
                : dfRadius * os.nImageWidth / os.nImageHeight;
2179
0
        const double dfSemiHeight =
2180
0
            (os.nImageWidth >= os.nImageHeight)
2181
0
                ? dfRadius * os.nImageHeight / os.nImageWidth
2182
0
                : dfRadius;
2183
0
        bboxXMin = static_cast<int>(
2184
0
            floor(sEnvelope.MinX * adfMatrix[1] + adfMatrix[0] - dfSemiWidth));
2185
0
        bboxYMin = static_cast<int>(
2186
0
            floor(sEnvelope.MinY * adfMatrix[3] + adfMatrix[2] - dfSemiHeight));
2187
0
        bboxXMax = static_cast<int>(
2188
0
            ceil(sEnvelope.MaxX * adfMatrix[1] + adfMatrix[0] + dfSemiWidth));
2189
0
        bboxYMax = static_cast<int>(
2190
0
            ceil(sEnvelope.MaxY * adfMatrix[3] + adfMatrix[2] + dfSemiHeight));
2191
0
    }
2192
1.64k
    else
2193
1.64k
    {
2194
1.64k
        double dfMargin = os.dfPenWidth;
2195
1.64k
        if (wkbFlatten(OGR_G_GetGeometryType(hGeom)) == wkbPoint)
2196
921
        {
2197
921
            if (os.osSymbolId == "ogr-sym-6" || os.osSymbolId == "ogr-sym-7")
2198
0
            {
2199
0
                const double dfSqrt3 = 1.73205080757;
2200
0
                dfMargin += dfRadius * 2 * dfSqrt3 / 3;
2201
0
            }
2202
921
            else
2203
921
                dfMargin += dfRadius;
2204
921
        }
2205
1.64k
        bboxXMin = static_cast<int>(
2206
1.64k
            floor(sEnvelope.MinX * adfMatrix[1] + adfMatrix[0] - dfMargin));
2207
1.64k
        bboxYMin = static_cast<int>(
2208
1.64k
            floor(sEnvelope.MinY * adfMatrix[3] + adfMatrix[2] - dfMargin));
2209
1.64k
        bboxXMax = static_cast<int>(
2210
1.64k
            ceil(sEnvelope.MaxX * adfMatrix[1] + adfMatrix[0] + dfMargin));
2211
1.64k
        bboxYMax = static_cast<int>(
2212
1.64k
            ceil(sEnvelope.MaxY * adfMatrix[3] + adfMatrix[2] + dfMargin));
2213
1.64k
    }
2214
1.64k
}
2215
2216
/************************************************************************/
2217
/*                              WriteLink()                             */
2218
/************************************************************************/
2219
2220
GDALPDFObjectNum GDALPDFBaseWriter::WriteLink(OGRFeatureH hFeat,
2221
                                              const char *pszOGRLinkField,
2222
                                              const double adfMatrix[4],
2223
                                              int bboxXMin, int bboxYMin,
2224
                                              int bboxXMax, int bboxYMax)
2225
1.64k
{
2226
1.64k
    GDALPDFObjectNum nAnnotId;
2227
1.64k
    int iField = -1;
2228
1.64k
    const char *pszLinkVal = nullptr;
2229
1.64k
    if (pszOGRLinkField != nullptr &&
2230
1.64k
        (iField = OGR_FD_GetFieldIndex(OGR_F_GetDefnRef(hFeat),
2231
0
                                       pszOGRLinkField)) >= 0 &&
2232
1.64k
        OGR_F_IsFieldSetAndNotNull(hFeat, iField) &&
2233
1.64k
        strcmp((pszLinkVal = OGR_F_GetFieldAsString(hFeat, iField)), "") != 0)
2234
0
    {
2235
0
        nAnnotId = AllocNewObject();
2236
0
        StartObj(nAnnotId);
2237
0
        {
2238
0
            GDALPDFDictionaryRW oDict;
2239
0
            oDict.Add("Type", GDALPDFObjectRW::CreateName("Annot"));
2240
0
            oDict.Add("Subtype", GDALPDFObjectRW::CreateName("Link"));
2241
0
            oDict.Add("Rect", &(new GDALPDFArrayRW())
2242
0
                                   ->Add(bboxXMin)
2243
0
                                   .Add(bboxYMin)
2244
0
                                   .Add(bboxXMax)
2245
0
                                   .Add(bboxYMax));
2246
0
            oDict.Add("A", &(new GDALPDFDictionaryRW())
2247
0
                                ->Add("S", GDALPDFObjectRW::CreateName("URI"))
2248
0
                                .Add("URI", pszLinkVal));
2249
0
            oDict.Add("BS",
2250
0
                      &(new GDALPDFDictionaryRW())
2251
0
                           ->Add("Type", GDALPDFObjectRW::CreateName("Border"))
2252
0
                           .Add("S", GDALPDFObjectRW::CreateName("S"))
2253
0
                           .Add("W", 0));
2254
0
            oDict.Add("Border", &(new GDALPDFArrayRW())->Add(0).Add(0).Add(0));
2255
0
            oDict.Add("H", GDALPDFObjectRW::CreateName("I"));
2256
2257
0
            OGRGeometryH hGeom = OGR_F_GetGeometryRef(hFeat);
2258
0
            if (wkbFlatten(OGR_G_GetGeometryType(hGeom)) == wkbPolygon &&
2259
0
                OGR_G_GetGeometryCount(hGeom) == 1)
2260
0
            {
2261
0
                OGRGeometryH hSubGeom = OGR_G_GetGeometryRef(hGeom, 0);
2262
0
                int nPoints = OGR_G_GetPointCount(hSubGeom);
2263
0
                if (nPoints == 4 || nPoints == 5)
2264
0
                {
2265
0
                    std::vector<double> adfX, adfY;
2266
0
                    for (int i = 0; i < nPoints; i++)
2267
0
                    {
2268
0
                        double dfX = OGR_G_GetX(hSubGeom, i) * adfMatrix[1] +
2269
0
                                     adfMatrix[0];
2270
0
                        double dfY = OGR_G_GetY(hSubGeom, i) * adfMatrix[3] +
2271
0
                                     adfMatrix[2];
2272
0
                        adfX.push_back(dfX);
2273
0
                        adfY.push_back(dfY);
2274
0
                    }
2275
0
                    if (nPoints == 4)
2276
0
                    {
2277
0
                        oDict.Add("QuadPoints", &(new GDALPDFArrayRW())
2278
0
                                                     ->Add(adfX[0])
2279
0
                                                     .Add(adfY[0])
2280
0
                                                     .Add(adfX[1])
2281
0
                                                     .Add(adfY[1])
2282
0
                                                     .Add(adfX[2])
2283
0
                                                     .Add(adfY[2])
2284
0
                                                     .Add(adfX[0])
2285
0
                                                     .Add(adfY[0]));
2286
0
                    }
2287
0
                    else if (nPoints == 5)
2288
0
                    {
2289
0
                        oDict.Add("QuadPoints", &(new GDALPDFArrayRW())
2290
0
                                                     ->Add(adfX[0])
2291
0
                                                     .Add(adfY[0])
2292
0
                                                     .Add(adfX[1])
2293
0
                                                     .Add(adfY[1])
2294
0
                                                     .Add(adfX[2])
2295
0
                                                     .Add(adfY[2])
2296
0
                                                     .Add(adfX[3])
2297
0
                                                     .Add(adfY[3]));
2298
0
                    }
2299
0
                }
2300
0
            }
2301
2302
0
            VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
2303
0
        }
2304
0
        EndObj();
2305
0
    }
2306
1.64k
    return nAnnotId;
2307
1.64k
}
2308
2309
/************************************************************************/
2310
/*                        GenerateDrawingStream()                       */
2311
/************************************************************************/
2312
2313
CPLString GDALPDFBaseWriter::GenerateDrawingStream(OGRGeometryH hGeom,
2314
                                                   const double adfMatrix[4],
2315
                                                   ObjectStyle &os,
2316
                                                   double dfRadius)
2317
1.64k
{
2318
1.64k
    CPLString osDS;
2319
2320
1.64k
    if (!os.nImageSymbolId.toBool())
2321
1.64k
    {
2322
1.64k
        osDS += CPLOPrintf("%f w\n"
2323
1.64k
                           "0 J\n"
2324
1.64k
                           "0 j\n"
2325
1.64k
                           "10 M\n"
2326
1.64k
                           "[%s]0 d\n",
2327
1.64k
                           os.dfPenWidth, os.osDashArray.c_str());
2328
2329
1.64k
        osDS += CPLOPrintf("%f %f %f RG\n", os.nPenR / 255.0, os.nPenG / 255.0,
2330
1.64k
                           os.nPenB / 255.0);
2331
1.64k
        osDS += CPLOPrintf("%f %f %f rg\n", os.nBrushR / 255.0,
2332
1.64k
                           os.nBrushG / 255.0, os.nBrushB / 255.0);
2333
1.64k
    }
2334
2335
1.64k
    if ((os.bHasPenBrushOrSymbol || os.osLabelText.empty()) &&
2336
1.64k
        wkbFlatten(OGR_G_GetGeometryType(hGeom)) == wkbPoint)
2337
921
    {
2338
921
        double dfX = OGR_G_GetX(hGeom, 0) * adfMatrix[1] + adfMatrix[0];
2339
921
        double dfY = OGR_G_GetY(hGeom, 0) * adfMatrix[3] + adfMatrix[2];
2340
2341
921
        if (os.nImageSymbolId.toBool())
2342
0
        {
2343
0
            const double dfSemiWidth =
2344
0
                (os.nImageWidth >= os.nImageHeight)
2345
0
                    ? dfRadius
2346
0
                    : dfRadius * os.nImageWidth / os.nImageHeight;
2347
0
            const double dfSemiHeight =
2348
0
                (os.nImageWidth >= os.nImageHeight)
2349
0
                    ? dfRadius * os.nImageHeight / os.nImageWidth
2350
0
                    : dfRadius;
2351
0
            osDS += CPLOPrintf("%f 0 0 %f %f %f cm\n", 2 * dfSemiWidth,
2352
0
                               2 * dfSemiHeight, dfX - dfSemiWidth,
2353
0
                               dfY - dfSemiHeight);
2354
0
            osDS += CPLOPrintf("/SymImage%d Do\n", os.nImageSymbolId.toInt());
2355
0
        }
2356
921
        else if (os.osSymbolId == "")
2357
921
            os.osSymbolId = "ogr-sym-3"; /* symbol by default */
2358
0
        else if (!(os.osSymbolId == "ogr-sym-0" ||
2359
0
                   os.osSymbolId == "ogr-sym-1" ||
2360
0
                   os.osSymbolId == "ogr-sym-2" ||
2361
0
                   os.osSymbolId == "ogr-sym-3" ||
2362
0
                   os.osSymbolId == "ogr-sym-4" ||
2363
0
                   os.osSymbolId == "ogr-sym-5" ||
2364
0
                   os.osSymbolId == "ogr-sym-6" ||
2365
0
                   os.osSymbolId == "ogr-sym-7" ||
2366
0
                   os.osSymbolId == "ogr-sym-8" ||
2367
0
                   os.osSymbolId == "ogr-sym-9"))
2368
0
        {
2369
0
            CPLDebug("PDF", "Unhandled symbol id : %s. Using ogr-sym-3 instead",
2370
0
                     os.osSymbolId.c_str());
2371
0
            os.osSymbolId = "ogr-sym-3";
2372
0
        }
2373
2374
921
        if (os.osSymbolId == "ogr-sym-0") /* cross (+)  */
2375
0
        {
2376
0
            osDS += CPLOPrintf("%f %f m\n", dfX - dfRadius, dfY);
2377
0
            osDS += CPLOPrintf("%f %f l\n", dfX + dfRadius, dfY);
2378
0
            osDS += CPLOPrintf("%f %f m\n", dfX, dfY - dfRadius);
2379
0
            osDS += CPLOPrintf("%f %f l\n", dfX, dfY + dfRadius);
2380
0
            osDS += CPLOPrintf("S\n");
2381
0
        }
2382
921
        else if (os.osSymbolId == "ogr-sym-1") /* diagcross (X) */
2383
0
        {
2384
0
            osDS += CPLOPrintf("%f %f m\n", dfX - dfRadius, dfY - dfRadius);
2385
0
            osDS += CPLOPrintf("%f %f l\n", dfX + dfRadius, dfY + dfRadius);
2386
0
            osDS += CPLOPrintf("%f %f m\n", dfX - dfRadius, dfY + dfRadius);
2387
0
            osDS += CPLOPrintf("%f %f l\n", dfX + dfRadius, dfY - dfRadius);
2388
0
            osDS += CPLOPrintf("S\n");
2389
0
        }
2390
921
        else if (os.osSymbolId == "ogr-sym-2" ||
2391
921
                 os.osSymbolId == "ogr-sym-3") /* circle */
2392
921
        {
2393
            /* See http://www.whizkidtech.redprince.net/bezier/circle/kappa/ */
2394
921
            const double dfKappa = 0.5522847498;
2395
2396
921
            osDS += CPLOPrintf("%f %f m\n", dfX - dfRadius, dfY);
2397
921
            osDS +=
2398
921
                CPLOPrintf("%f %f %f %f %f %f c\n", dfX - dfRadius,
2399
921
                           dfY - dfRadius * dfKappa, dfX - dfRadius * dfKappa,
2400
921
                           dfY - dfRadius, dfX, dfY - dfRadius);
2401
921
            osDS +=
2402
921
                CPLOPrintf("%f %f %f %f %f %f c\n", dfX + dfRadius * dfKappa,
2403
921
                           dfY - dfRadius, dfX + dfRadius,
2404
921
                           dfY - dfRadius * dfKappa, dfX + dfRadius, dfY);
2405
921
            osDS +=
2406
921
                CPLOPrintf("%f %f %f %f %f %f c\n", dfX + dfRadius,
2407
921
                           dfY + dfRadius * dfKappa, dfX + dfRadius * dfKappa,
2408
921
                           dfY + dfRadius, dfX, dfY + dfRadius);
2409
921
            osDS +=
2410
921
                CPLOPrintf("%f %f %f %f %f %f c\n", dfX - dfRadius * dfKappa,
2411
921
                           dfY + dfRadius, dfX - dfRadius,
2412
921
                           dfY + dfRadius * dfKappa, dfX - dfRadius, dfY);
2413
921
            if (os.osSymbolId == "ogr-sym-2")
2414
0
                osDS += CPLOPrintf("s\n"); /* not filled */
2415
921
            else
2416
921
                osDS += CPLOPrintf("b*\n"); /* filled */
2417
921
        }
2418
0
        else if (os.osSymbolId == "ogr-sym-4" ||
2419
0
                 os.osSymbolId == "ogr-sym-5") /* square */
2420
0
        {
2421
0
            osDS += CPLOPrintf("%f %f m\n", dfX - dfRadius, dfY + dfRadius);
2422
0
            osDS += CPLOPrintf("%f %f l\n", dfX + dfRadius, dfY + dfRadius);
2423
0
            osDS += CPLOPrintf("%f %f l\n", dfX + dfRadius, dfY - dfRadius);
2424
0
            osDS += CPLOPrintf("%f %f l\n", dfX - dfRadius, dfY - dfRadius);
2425
0
            if (os.osSymbolId == "ogr-sym-4")
2426
0
                osDS += CPLOPrintf("s\n"); /* not filled */
2427
0
            else
2428
0
                osDS += CPLOPrintf("b*\n"); /* filled */
2429
0
        }
2430
0
        else if (os.osSymbolId == "ogr-sym-6" ||
2431
0
                 os.osSymbolId == "ogr-sym-7") /* triangle */
2432
0
        {
2433
0
            const double dfSqrt3 = 1.73205080757;
2434
0
            osDS += CPLOPrintf("%f %f m\n", dfX - dfRadius,
2435
0
                               dfY - dfRadius * dfSqrt3 / 3);
2436
0
            osDS +=
2437
0
                CPLOPrintf("%f %f l\n", dfX, dfY + 2 * dfRadius * dfSqrt3 / 3);
2438
0
            osDS += CPLOPrintf("%f %f l\n", dfX + dfRadius,
2439
0
                               dfY - dfRadius * dfSqrt3 / 3);
2440
0
            if (os.osSymbolId == "ogr-sym-6")
2441
0
                osDS += CPLOPrintf("s\n"); /* not filled */
2442
0
            else
2443
0
                osDS += CPLOPrintf("b*\n"); /* filled */
2444
0
        }
2445
0
        else if (os.osSymbolId == "ogr-sym-8" ||
2446
0
                 os.osSymbolId == "ogr-sym-9") /* star */
2447
0
        {
2448
0
            const double dfSin18divSin126 = 0.38196601125;
2449
0
            osDS += CPLOPrintf("%f %f m\n", dfX, dfY + dfRadius);
2450
0
            for (int i = 1; i < 10; i++)
2451
0
            {
2452
0
                double dfFactor = ((i % 2) == 1) ? dfSin18divSin126 : 1.0;
2453
0
                osDS += CPLOPrintf("%f %f l\n",
2454
0
                                   dfX + cos(M_PI / 2 - i * M_PI * 36 / 180) *
2455
0
                                             dfRadius * dfFactor,
2456
0
                                   dfY + sin(M_PI / 2 - i * M_PI * 36 / 180) *
2457
0
                                             dfRadius * dfFactor);
2458
0
            }
2459
0
            if (os.osSymbolId == "ogr-sym-8")
2460
0
                osDS += CPLOPrintf("s\n"); /* not filled */
2461
0
            else
2462
0
                osDS += CPLOPrintf("b*\n"); /* filled */
2463
0
        }
2464
921
    }
2465
721
    else
2466
721
    {
2467
721
        DrawGeometry(osDS, hGeom, adfMatrix);
2468
721
    }
2469
2470
1.64k
    return osDS;
2471
1.64k
}
2472
2473
/************************************************************************/
2474
/*                          WriteAttributes()                           */
2475
/************************************************************************/
2476
2477
GDALPDFObjectNum GDALPDFBaseWriter::WriteAttributes(
2478
    OGRFeatureH hFeat, const std::vector<CPLString> &aosIncludedFields,
2479
    const char *pszOGRDisplayField, int nMCID, const GDALPDFObjectNum &oParent,
2480
    const GDALPDFObjectNum &oPage, CPLString &osOutFeatureName)
2481
1.64k
{
2482
2483
1.64k
    int iField = -1;
2484
1.64k
    if (pszOGRDisplayField)
2485
0
        iField =
2486
0
            OGR_FD_GetFieldIndex(OGR_F_GetDefnRef(hFeat), pszOGRDisplayField);
2487
1.64k
    if (iField >= 0)
2488
0
        osOutFeatureName = OGR_F_GetFieldAsString(hFeat, iField);
2489
1.64k
    else
2490
1.64k
        osOutFeatureName =
2491
1.64k
            CPLSPrintf("feature" CPL_FRMT_GIB, OGR_F_GetFID(hFeat));
2492
2493
1.64k
    auto nFeatureUserProperties = AllocNewObject();
2494
1.64k
    StartObj(nFeatureUserProperties);
2495
2496
1.64k
    GDALPDFDictionaryRW oDict;
2497
2498
1.64k
    GDALPDFDictionaryRW *poDictA = new GDALPDFDictionaryRW();
2499
1.64k
    oDict.Add("A", poDictA);
2500
1.64k
    poDictA->Add("O", GDALPDFObjectRW::CreateName("UserProperties"));
2501
2502
1.64k
    GDALPDFArrayRW *poArray = new GDALPDFArrayRW();
2503
1.64k
    for (const auto &fieldName : aosIncludedFields)
2504
20.3k
    {
2505
20.3k
        int i = OGR_F_GetFieldIndex(hFeat, fieldName);
2506
20.3k
        if (i >= 0 && OGR_F_IsFieldSetAndNotNull(hFeat, i))
2507
3.75k
        {
2508
3.75k
            OGRFieldDefnH hFDefn = OGR_F_GetFieldDefnRef(hFeat, i);
2509
3.75k
            GDALPDFDictionaryRW *poKV = new GDALPDFDictionaryRW();
2510
3.75k
            poKV->Add("N", OGR_Fld_GetNameRef(hFDefn));
2511
3.75k
            if (OGR_Fld_GetType(hFDefn) == OFTInteger)
2512
0
                poKV->Add("V", OGR_F_GetFieldAsInteger(hFeat, i));
2513
3.75k
            else if (OGR_Fld_GetType(hFDefn) == OFTReal)
2514
0
                poKV->Add("V", OGR_F_GetFieldAsDouble(hFeat, i));
2515
3.75k
            else
2516
3.75k
                poKV->Add("V", OGR_F_GetFieldAsString(hFeat, i));
2517
3.75k
            poArray->Add(poKV);
2518
3.75k
        }
2519
20.3k
    }
2520
2521
1.64k
    poDictA->Add("P", poArray);
2522
2523
1.64k
    oDict.Add("K", nMCID);
2524
1.64k
    oDict.Add("P", oParent, 0);
2525
1.64k
    oDict.Add("Pg", oPage, 0);
2526
1.64k
    oDict.Add("S", GDALPDFObjectRW::CreateName("feature"));
2527
1.64k
    oDict.Add("T", osOutFeatureName);
2528
2529
1.64k
    VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
2530
2531
1.64k
    EndObj();
2532
2533
1.64k
    return nFeatureUserProperties;
2534
1.64k
}
2535
2536
/************************************************************************/
2537
/*                            WriteLabel()                              */
2538
/************************************************************************/
2539
2540
GDALPDFObjectNum GDALPDFBaseWriter::WriteLabel(
2541
    OGRGeometryH hGeom, const double adfMatrix[4], ObjectStyle &os,
2542
    PDFCompressMethod eStreamCompressMethod, double bboxXMin, double bboxYMin,
2543
    double bboxXMax, double bboxYMax)
2544
0
{
2545
    /* -------------------------------------------------------------- */
2546
    /*  Work out the text metrics for alignment purposes              */
2547
    /* -------------------------------------------------------------- */
2548
0
    double dfWidth, dfHeight;
2549
0
    CalculateText(os.osLabelText, os.osTextFont, os.dfTextSize, os.bTextBold,
2550
0
                  os.bTextItalic, dfWidth, dfHeight);
2551
0
    dfWidth *= os.dfTextStretch;
2552
2553
0
    if (os.nTextAnchor % 3 == 2)  // horizontal center
2554
0
    {
2555
0
        os.dfTextDx -= (dfWidth / 2) * cos(os.dfTextAngle);
2556
0
        os.dfTextDy -= (dfWidth / 2) * sin(os.dfTextAngle);
2557
0
    }
2558
0
    else if (os.nTextAnchor % 3 == 0)  // right
2559
0
    {
2560
0
        os.dfTextDx -= dfWidth * cos(os.dfTextAngle);
2561
0
        os.dfTextDy -= dfWidth * sin(os.dfTextAngle);
2562
0
    }
2563
2564
0
    if (os.nTextAnchor >= 4 && os.nTextAnchor <= 6)  // vertical center
2565
0
    {
2566
0
        os.dfTextDx += (dfHeight / 2) * sin(os.dfTextAngle);
2567
0
        os.dfTextDy -= (dfHeight / 2) * cos(os.dfTextAngle);
2568
0
    }
2569
0
    else if (os.nTextAnchor >= 7 && os.nTextAnchor <= 9)  // top
2570
0
    {
2571
0
        os.dfTextDx += dfHeight * sin(os.dfTextAngle);
2572
0
        os.dfTextDy -= dfHeight * cos(os.dfTextAngle);
2573
0
    }
2574
    // modes 10,11,12 (baseline) unsupported for the time being
2575
2576
    /* -------------------------------------------------------------- */
2577
    /*  Write object dictionary                                       */
2578
    /* -------------------------------------------------------------- */
2579
0
    auto nObjectId = AllocNewObject();
2580
0
    GDALPDFDictionaryRW oDict;
2581
2582
0
    oDict.Add("Type", GDALPDFObjectRW::CreateName("XObject"))
2583
0
        .Add("BBox", &((new GDALPDFArrayRW())->Add(bboxXMin).Add(bboxYMin))
2584
0
                          .Add(bboxXMax)
2585
0
                          .Add(bboxYMax))
2586
0
        .Add("Subtype", GDALPDFObjectRW::CreateName("Form"));
2587
2588
0
    GDALPDFDictionaryRW *poResources = new GDALPDFDictionaryRW();
2589
2590
0
    if (os.nTextA != 255)
2591
0
    {
2592
0
        GDALPDFDictionaryRW *poGS1 = new GDALPDFDictionaryRW();
2593
0
        poGS1->Add("Type", GDALPDFObjectRW::CreateName("ExtGState"));
2594
0
        poGS1->Add("ca", (os.nTextA == 127 || os.nTextA == 128)
2595
0
                             ? 0.5
2596
0
                             : os.nTextA / 255.0);
2597
2598
0
        GDALPDFDictionaryRW *poExtGState = new GDALPDFDictionaryRW();
2599
0
        poExtGState->Add("GS1", poGS1);
2600
2601
0
        poResources->Add("ExtGState", poExtGState);
2602
0
    }
2603
2604
0
    GDALPDFDictionaryRW *poDictF1 = new GDALPDFDictionaryRW();
2605
0
    poDictF1->Add("Type", GDALPDFObjectRW::CreateName("Font"));
2606
0
    poDictF1->Add("BaseFont", GDALPDFObjectRW::CreateName(os.osTextFont));
2607
0
    poDictF1->Add("Encoding", GDALPDFObjectRW::CreateName("WinAnsiEncoding"));
2608
0
    poDictF1->Add("Subtype", GDALPDFObjectRW::CreateName("Type1"));
2609
2610
0
    GDALPDFDictionaryRW *poDictFont = new GDALPDFDictionaryRW();
2611
0
    poDictFont->Add("F1", poDictF1);
2612
0
    poResources->Add("Font", poDictFont);
2613
2614
0
    oDict.Add("Resources", poResources);
2615
2616
0
    StartObjWithStream(nObjectId, oDict,
2617
0
                       eStreamCompressMethod != COMPRESS_NONE);
2618
2619
    /* -------------------------------------------------------------- */
2620
    /*  Write object stream                                           */
2621
    /* -------------------------------------------------------------- */
2622
2623
0
    double dfX =
2624
0
        OGR_G_GetX(hGeom, 0) * adfMatrix[1] + adfMatrix[0] + os.dfTextDx;
2625
0
    double dfY =
2626
0
        OGR_G_GetY(hGeom, 0) * adfMatrix[3] + adfMatrix[2] + os.dfTextDy;
2627
2628
0
    VSIFPrintfL(m_fp, "q\n");
2629
0
    VSIFPrintfL(m_fp, "BT\n");
2630
0
    if (os.nTextA != 255)
2631
0
    {
2632
0
        VSIFPrintfL(m_fp, "/GS1 gs\n");
2633
0
    }
2634
2635
0
    VSIFPrintfL(m_fp, "%f %f %f %f %f %f Tm\n",
2636
0
                cos(os.dfTextAngle) * adfMatrix[1] * os.dfTextStretch,
2637
0
                sin(os.dfTextAngle) * adfMatrix[3] * os.dfTextStretch,
2638
0
                -sin(os.dfTextAngle) * adfMatrix[1],
2639
0
                cos(os.dfTextAngle) * adfMatrix[3], dfX, dfY);
2640
2641
0
    VSIFPrintfL(m_fp, "%f %f %f rg\n", os.nTextR / 255.0, os.nTextG / 255.0,
2642
0
                os.nTextB / 255.0);
2643
    // The factor of adfMatrix[1] is introduced in the call to SetUnit near the
2644
    // top of this function. Because we are handling the 2D stretch correctly in
2645
    // Tm above, we don't need that factor here
2646
0
    VSIFPrintfL(m_fp, "/F1 %f Tf\n", os.dfTextSize / adfMatrix[1]);
2647
0
    VSIFPrintfL(m_fp, "(");
2648
0
    for (size_t i = 0; i < os.osLabelText.size(); i++)
2649
0
    {
2650
0
        if (os.osLabelText[i] == '(' || os.osLabelText[i] == ')' ||
2651
0
            os.osLabelText[i] == '\\')
2652
0
        {
2653
0
            VSIFPrintfL(m_fp, "\\%c", os.osLabelText[i]);
2654
0
        }
2655
0
        else
2656
0
        {
2657
0
            VSIFPrintfL(m_fp, "%c", os.osLabelText[i]);
2658
0
        }
2659
0
    }
2660
0
    VSIFPrintfL(m_fp, ") Tj\n");
2661
0
    VSIFPrintfL(m_fp, "ET\n");
2662
0
    VSIFPrintfL(m_fp, "Q");
2663
2664
0
    EndObjWithStream();
2665
2666
0
    return nObjectId;
2667
0
}
2668
2669
/************************************************************************/
2670
/*                          WriteOGRFeature()                           */
2671
/************************************************************************/
2672
2673
int GDALPDFWriter::WriteOGRFeature(GDALPDFLayerDesc &osVectorDesc,
2674
                                   OGRFeatureH hFeat,
2675
                                   OGRCoordinateTransformationH hCT,
2676
                                   const char *pszOGRDisplayField,
2677
                                   const char *pszOGRLinkField,
2678
                                   int bWriteOGRAttributes, int &iObj)
2679
156k
{
2680
156k
    GDALDataset *const poClippingDS = oPageContext.poClippingDS;
2681
156k
    const int nHeight = poClippingDS->GetRasterYSize();
2682
156k
    const double dfUserUnit = oPageContext.dfDPI * USER_UNIT_IN_INCH;
2683
156k
    GDALGeoTransform gt;
2684
156k
    poClippingDS->GetGeoTransform(gt);
2685
2686
156k
    double adfMatrix[4];
2687
156k
    adfMatrix[0] = -gt[0] / (gt[1] * dfUserUnit) + oPageContext.sMargins.nLeft;
2688
156k
    adfMatrix[1] = 1.0 / (gt[1] * dfUserUnit);
2689
156k
    adfMatrix[2] = -(gt[3] + gt[5] * nHeight) / (-gt[5] * dfUserUnit) +
2690
156k
                   oPageContext.sMargins.nBottom;
2691
156k
    adfMatrix[3] = 1.0 / (-gt[5] * dfUserUnit);
2692
2693
156k
    OGRGeometryH hGeom = OGR_F_GetGeometryRef(hFeat);
2694
156k
    if (hGeom == nullptr)
2695
155k
    {
2696
155k
        return TRUE;
2697
155k
    }
2698
2699
1.64k
    OGREnvelope sEnvelope;
2700
2701
1.64k
    if (hCT != nullptr)
2702
0
    {
2703
        /* Reproject */
2704
0
        if (OGR_G_Transform(hGeom, hCT) != OGRERR_NONE)
2705
0
        {
2706
0
            return TRUE;
2707
0
        }
2708
2709
0
        OGREnvelope sRasterEnvelope;
2710
0
        sRasterEnvelope.MinX = gt[0];
2711
0
        sRasterEnvelope.MinY = gt[3] + poClippingDS->GetRasterYSize() * gt[5];
2712
0
        sRasterEnvelope.MaxX = gt[0] + poClippingDS->GetRasterXSize() * gt[1];
2713
0
        sRasterEnvelope.MaxY = gt[3];
2714
2715
        // Check that the reprojected geometry intersects the raster envelope.
2716
0
        OGR_G_GetEnvelope(hGeom, &sEnvelope);
2717
0
        if (!(sRasterEnvelope.Intersects(sEnvelope)))
2718
0
        {
2719
0
            return TRUE;
2720
0
        }
2721
0
    }
2722
1.64k
    else
2723
1.64k
    {
2724
1.64k
        OGR_G_GetEnvelope(hGeom, &sEnvelope);
2725
1.64k
    }
2726
2727
    /* -------------------------------------------------------------- */
2728
    /*  Get style                                                     */
2729
    /* -------------------------------------------------------------- */
2730
1.64k
    ObjectStyle os;
2731
1.64k
    GetObjectStyle(nullptr, hFeat, adfMatrix, m_oMapSymbolFilenameToDesc, os);
2732
2733
1.64k
    double dfRadius = os.dfSymbolSize * dfUserUnit;
2734
2735
    // For a POINT with only a LABEL style string and non-empty text, we do not
2736
    // output any geometry other than the text itself.
2737
1.64k
    const bool bLabelOnly =
2738
1.64k
        wkbFlatten(OGR_G_GetGeometryType(hGeom)) == wkbPoint &&
2739
1.64k
        !os.bHasPenBrushOrSymbol && !os.osLabelText.empty();
2740
2741
    /* -------------------------------------------------------------- */
2742
    /*  Write object dictionary                                       */
2743
    /* -------------------------------------------------------------- */
2744
1.64k
    if (!bLabelOnly)
2745
1.64k
    {
2746
1.64k
        auto nObjectId = AllocNewObject();
2747
2748
1.64k
        osVectorDesc.aIds.push_back(nObjectId);
2749
2750
1.64k
        int bboxXMin, bboxYMin, bboxXMax, bboxYMax;
2751
1.64k
        ComputeIntBBox(hGeom, sEnvelope, adfMatrix, os, dfRadius, bboxXMin,
2752
1.64k
                       bboxYMin, bboxXMax, bboxYMax);
2753
2754
1.64k
        auto nLinkId = WriteLink(hFeat, pszOGRLinkField, adfMatrix, bboxXMin,
2755
1.64k
                                 bboxYMin, bboxXMax, bboxYMax);
2756
1.64k
        if (nLinkId.toBool())
2757
0
            oPageContext.anAnnotationsId.push_back(nLinkId);
2758
2759
1.64k
        GDALPDFDictionaryRW oDict;
2760
1.64k
        GDALPDFArrayRW *poBBOX = new GDALPDFArrayRW();
2761
1.64k
        poBBOX->Add(bboxXMin).Add(bboxYMin).Add(bboxXMax).Add(bboxYMax);
2762
1.64k
        oDict.Add("Type", GDALPDFObjectRW::CreateName("XObject"))
2763
1.64k
            .Add("BBox", poBBOX)
2764
1.64k
            .Add("Subtype", GDALPDFObjectRW::CreateName("Form"));
2765
2766
1.64k
        GDALPDFDictionaryRW *poGS1 = new GDALPDFDictionaryRW();
2767
1.64k
        poGS1->Add("Type", GDALPDFObjectRW::CreateName("ExtGState"));
2768
1.64k
        if (os.nPenA != 255)
2769
0
            poGS1->Add("CA", (os.nPenA == 127 || os.nPenA == 128)
2770
0
                                 ? 0.5
2771
0
                                 : os.nPenA / 255.0);
2772
1.64k
        if (os.nBrushA != 255)
2773
1.64k
            poGS1->Add("ca", (os.nBrushA == 127 || os.nBrushA == 128)
2774
1.64k
                                 ? 0.5
2775
1.64k
                                 : os.nBrushA / 255.0);
2776
2777
1.64k
        GDALPDFDictionaryRW *poExtGState = new GDALPDFDictionaryRW();
2778
1.64k
        poExtGState->Add("GS1", poGS1);
2779
2780
1.64k
        GDALPDFDictionaryRW *poResources = new GDALPDFDictionaryRW();
2781
1.64k
        poResources->Add("ExtGState", poExtGState);
2782
2783
1.64k
        if (os.nImageSymbolId.toBool())
2784
0
        {
2785
0
            GDALPDFDictionaryRW *poDictXObject = new GDALPDFDictionaryRW();
2786
0
            poResources->Add("XObject", poDictXObject);
2787
2788
0
            poDictXObject->Add(
2789
0
                CPLSPrintf("SymImage%d", os.nImageSymbolId.toInt()),
2790
0
                os.nImageSymbolId, 0);
2791
0
        }
2792
2793
1.64k
        oDict.Add("Resources", poResources);
2794
2795
1.64k
        StartObjWithStream(nObjectId, oDict,
2796
1.64k
                           oPageContext.eStreamCompressMethod != COMPRESS_NONE);
2797
2798
        /* -------------------------------------------------------------- */
2799
        /*  Write object stream                                           */
2800
        /* -------------------------------------------------------------- */
2801
1.64k
        VSIFPrintfL(m_fp, "q\n");
2802
2803
1.64k
        VSIFPrintfL(m_fp, "/GS1 gs\n");
2804
2805
1.64k
        VSIFPrintfL(
2806
1.64k
            m_fp, "%s",
2807
1.64k
            GenerateDrawingStream(hGeom, adfMatrix, os, dfRadius).c_str());
2808
2809
1.64k
        VSIFPrintfL(m_fp, "Q");
2810
2811
1.64k
        EndObjWithStream();
2812
1.64k
    }
2813
0
    else
2814
0
    {
2815
0
        osVectorDesc.aIds.push_back(GDALPDFObjectNum());
2816
0
    }
2817
2818
    /* -------------------------------------------------------------- */
2819
    /*  Write label                                                   */
2820
    /* -------------------------------------------------------------- */
2821
1.64k
    if (!os.osLabelText.empty() &&
2822
1.64k
        wkbFlatten(OGR_G_GetGeometryType(hGeom)) == wkbPoint)
2823
0
    {
2824
0
        if (!osVectorDesc.nOCGTextId.toBool())
2825
0
            osVectorDesc.nOCGTextId = WriteOCG("Text", osVectorDesc.nOCGId);
2826
2827
0
        int nWidth = poClippingDS->GetRasterXSize();
2828
0
        double dfWidthInUserUnit = nWidth / dfUserUnit +
2829
0
                                   oPageContext.sMargins.nLeft +
2830
0
                                   oPageContext.sMargins.nRight;
2831
0
        double dfHeightInUserUnit = nHeight / dfUserUnit +
2832
0
                                    oPageContext.sMargins.nBottom +
2833
0
                                    oPageContext.sMargins.nTop;
2834
0
        auto nObjectId =
2835
0
            WriteLabel(hGeom, adfMatrix, os, oPageContext.eStreamCompressMethod,
2836
0
                       0, 0, dfWidthInUserUnit, dfHeightInUserUnit);
2837
2838
0
        osVectorDesc.aIdsText.push_back(nObjectId);
2839
0
    }
2840
1.64k
    else
2841
1.64k
    {
2842
1.64k
        osVectorDesc.aIdsText.push_back(GDALPDFObjectNum());
2843
1.64k
    }
2844
2845
    /* -------------------------------------------------------------- */
2846
    /*  Write feature attributes                                      */
2847
    /* -------------------------------------------------------------- */
2848
1.64k
    GDALPDFObjectNum nFeatureUserProperties;
2849
2850
1.64k
    CPLString osFeatureName;
2851
2852
1.64k
    if (bWriteOGRAttributes)
2853
1.64k
    {
2854
1.64k
        nFeatureUserProperties = WriteAttributes(
2855
1.64k
            hFeat, osVectorDesc.aosIncludedFields, pszOGRDisplayField, iObj,
2856
1.64k
            osVectorDesc.nFeatureLayerId, oPageContext.nPageId, osFeatureName);
2857
1.64k
    }
2858
2859
1.64k
    iObj++;
2860
2861
1.64k
    osVectorDesc.aUserPropertiesIds.push_back(nFeatureUserProperties);
2862
1.64k
    osVectorDesc.aFeatureNames.push_back(std::move(osFeatureName));
2863
2864
1.64k
    return TRUE;
2865
1.64k
}
2866
2867
/************************************************************************/
2868
/*                               EndPage()                              */
2869
/************************************************************************/
2870
2871
int GDALPDFWriter::EndPage(const char *pszExtraImages,
2872
                           const char *pszExtraStream,
2873
                           const char *pszExtraLayerName,
2874
                           const char *pszOffLayers,
2875
                           const char *pszExclusiveLayers)
2876
539
{
2877
539
    auto nLayerExtraId = WriteOCG(pszExtraLayerName);
2878
539
    if (pszOffLayers)
2879
0
        m_osOffLayers = pszOffLayers;
2880
539
    if (pszExclusiveLayers)
2881
0
        m_osExclusiveLayers = pszExclusiveLayers;
2882
2883
    /* -------------------------------------------------------------- */
2884
    /*  Write extra images                                            */
2885
    /* -------------------------------------------------------------- */
2886
539
    std::vector<GDALPDFImageDesc> asExtraImageDesc;
2887
539
    if (pszExtraImages)
2888
0
    {
2889
0
        if (GDALGetDriverCount() == 0)
2890
0
            GDALAllRegister();
2891
2892
0
        char **papszExtraImagesTokens =
2893
0
            CSLTokenizeString2(pszExtraImages, ",", 0);
2894
0
        double dfUserUnit = oPageContext.dfDPI * USER_UNIT_IN_INCH;
2895
0
        int nCount = CSLCount(papszExtraImagesTokens);
2896
0
        for (int i = 0; i + 4 <= nCount; /* */)
2897
0
        {
2898
0
            const char *pszImageFilename = papszExtraImagesTokens[i + 0];
2899
0
            double dfX = CPLAtof(papszExtraImagesTokens[i + 1]);
2900
0
            double dfY = CPLAtof(papszExtraImagesTokens[i + 2]);
2901
0
            double dfScale = CPLAtof(papszExtraImagesTokens[i + 3]);
2902
0
            const char *pszLinkVal = nullptr;
2903
0
            i += 4;
2904
0
            if (i < nCount &&
2905
0
                STARTS_WITH_CI(papszExtraImagesTokens[i], "link="))
2906
0
            {
2907
0
                pszLinkVal = papszExtraImagesTokens[i] + 5;
2908
0
                i++;
2909
0
            }
2910
0
            auto poImageDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
2911
0
                pszImageFilename, GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
2912
0
                nullptr, nullptr, nullptr));
2913
0
            if (poImageDS)
2914
0
            {
2915
0
                auto nImageId = WriteBlock(
2916
0
                    poImageDS.get(), 0, 0, poImageDS->GetRasterXSize(),
2917
0
                    poImageDS->GetRasterYSize(), GDALPDFObjectNum(),
2918
0
                    COMPRESS_DEFAULT, 0, -1, nullptr, nullptr, nullptr);
2919
2920
0
                if (nImageId.toBool())
2921
0
                {
2922
0
                    GDALPDFImageDesc oImageDesc;
2923
0
                    oImageDesc.nImageId = nImageId;
2924
0
                    oImageDesc.dfXSize =
2925
0
                        poImageDS->GetRasterXSize() / dfUserUnit * dfScale;
2926
0
                    oImageDesc.dfYSize =
2927
0
                        poImageDS->GetRasterYSize() / dfUserUnit * dfScale;
2928
0
                    oImageDesc.dfXOff = dfX;
2929
0
                    oImageDesc.dfYOff = dfY;
2930
2931
0
                    asExtraImageDesc.push_back(oImageDesc);
2932
2933
0
                    if (pszLinkVal != nullptr)
2934
0
                    {
2935
0
                        auto nAnnotId = AllocNewObject();
2936
0
                        oPageContext.anAnnotationsId.push_back(nAnnotId);
2937
0
                        StartObj(nAnnotId);
2938
0
                        {
2939
0
                            GDALPDFDictionaryRW oDict;
2940
0
                            oDict.Add("Type",
2941
0
                                      GDALPDFObjectRW::CreateName("Annot"));
2942
0
                            oDict.Add("Subtype",
2943
0
                                      GDALPDFObjectRW::CreateName("Link"));
2944
0
                            oDict.Add("Rect", &(new GDALPDFArrayRW())
2945
0
                                                   ->Add(oImageDesc.dfXOff)
2946
0
                                                   .Add(oImageDesc.dfYOff)
2947
0
                                                   .Add(oImageDesc.dfXOff +
2948
0
                                                        oImageDesc.dfXSize)
2949
0
                                                   .Add(oImageDesc.dfYOff +
2950
0
                                                        oImageDesc.dfYSize));
2951
0
                            oDict.Add(
2952
0
                                "A",
2953
0
                                &(new GDALPDFDictionaryRW())
2954
0
                                     ->Add("S",
2955
0
                                           GDALPDFObjectRW::CreateName("URI"))
2956
0
                                     .Add("URI", pszLinkVal));
2957
0
                            oDict.Add(
2958
0
                                "BS",
2959
0
                                &(new GDALPDFDictionaryRW())
2960
0
                                     ->Add("Type", GDALPDFObjectRW::CreateName(
2961
0
                                                       "Border"))
2962
0
                                     .Add("S", GDALPDFObjectRW::CreateName("S"))
2963
0
                                     .Add("W", 0));
2964
0
                            oDict.Add(
2965
0
                                "Border",
2966
0
                                &(new GDALPDFArrayRW())->Add(0).Add(0).Add(0));
2967
0
                            oDict.Add("H", GDALPDFObjectRW::CreateName("I"));
2968
2969
0
                            VSIFPrintfL(m_fp, "%s\n",
2970
0
                                        oDict.Serialize().c_str());
2971
0
                        }
2972
0
                        EndObj();
2973
0
                    }
2974
0
                }
2975
0
            }
2976
0
        }
2977
0
        CSLDestroy(papszExtraImagesTokens);
2978
0
    }
2979
2980
    /* -------------------------------------------------------------- */
2981
    /*  Write content stream                                          */
2982
    /* -------------------------------------------------------------- */
2983
539
    GDALPDFDictionaryRW oDictContent;
2984
539
    StartObjWithStream(oPageContext.nContentId, oDictContent,
2985
539
                       oPageContext.eStreamCompressMethod != COMPRESS_NONE);
2986
2987
    /* -------------------------------------------------------------- */
2988
    /*  Write drawing instructions for raster blocks                  */
2989
    /* -------------------------------------------------------------- */
2990
1.03k
    for (size_t iRaster = 0; iRaster < oPageContext.asRasterDesc.size();
2991
539
         iRaster++)
2992
494
    {
2993
494
        const GDALPDFRasterDesc &oDesc = oPageContext.asRasterDesc[iRaster];
2994
494
        if (oDesc.nOCGRasterId.toBool())
2995
0
            VSIFPrintfL(m_fp, "/OC /Lyr%d BDC\n", oDesc.nOCGRasterId.toInt());
2996
2997
988
        for (size_t iImage = 0; iImage < oDesc.asImageDesc.size(); iImage++)
2998
494
        {
2999
494
            VSIFPrintfL(m_fp, "q\n");
3000
494
            GDALPDFObjectRW *poXSize =
3001
494
                GDALPDFObjectRW::CreateReal(oDesc.asImageDesc[iImage].dfXSize);
3002
494
            GDALPDFObjectRW *poYSize =
3003
494
                GDALPDFObjectRW::CreateReal(oDesc.asImageDesc[iImage].dfYSize);
3004
494
            GDALPDFObjectRW *poXOff =
3005
494
                GDALPDFObjectRW::CreateReal(oDesc.asImageDesc[iImage].dfXOff);
3006
494
            GDALPDFObjectRW *poYOff =
3007
494
                GDALPDFObjectRW::CreateReal(oDesc.asImageDesc[iImage].dfYOff);
3008
494
            VSIFPrintfL(
3009
494
                m_fp, "%s 0 0 %s %s %s cm\n", poXSize->Serialize().c_str(),
3010
494
                poYSize->Serialize().c_str(), poXOff->Serialize().c_str(),
3011
494
                poYOff->Serialize().c_str());
3012
494
            delete poXSize;
3013
494
            delete poYSize;
3014
494
            delete poXOff;
3015
494
            delete poYOff;
3016
494
            VSIFPrintfL(m_fp, "/Image%d Do\n",
3017
494
                        oDesc.asImageDesc[iImage].nImageId.toInt());
3018
494
            VSIFPrintfL(m_fp, "Q\n");
3019
494
        }
3020
3021
494
        if (oDesc.nOCGRasterId.toBool())
3022
0
            VSIFPrintfL(m_fp, "EMC\n");
3023
494
    }
3024
3025
    /* -------------------------------------------------------------- */
3026
    /*  Write drawing instructions for vector features                */
3027
    /* -------------------------------------------------------------- */
3028
539
    int iObj = 0;
3029
1.71k
    for (size_t iLayer = 0; iLayer < oPageContext.asVectorDesc.size(); iLayer++)
3030
1.17k
    {
3031
1.17k
        const GDALPDFLayerDesc &oLayerDesc = oPageContext.asVectorDesc[iLayer];
3032
3033
1.17k
        VSIFPrintfL(m_fp, "/OC /Lyr%d BDC\n", oLayerDesc.nOCGId.toInt());
3034
3035
2.82k
        for (size_t iVector = 0; iVector < oLayerDesc.aIds.size(); iVector++)
3036
1.64k
        {
3037
1.64k
            if (oLayerDesc.aIds[iVector].toBool())
3038
1.64k
            {
3039
1.64k
                CPLString osName = oLayerDesc.aFeatureNames[iVector];
3040
1.64k
                if (!osName.empty())
3041
1.64k
                {
3042
1.64k
                    VSIFPrintfL(m_fp, "/feature <</MCID %d>> BDC\n", iObj);
3043
1.64k
                }
3044
3045
1.64k
                VSIFPrintfL(m_fp, "/Vector%d Do\n",
3046
1.64k
                            oLayerDesc.aIds[iVector].toInt());
3047
3048
1.64k
                if (!osName.empty())
3049
1.64k
                {
3050
1.64k
                    VSIFPrintfL(m_fp, "EMC\n");
3051
1.64k
                }
3052
1.64k
            }
3053
3054
1.64k
            iObj++;
3055
1.64k
        }
3056
3057
1.17k
        VSIFPrintfL(m_fp, "EMC\n");
3058
1.17k
    }
3059
3060
    /* -------------------------------------------------------------- */
3061
    /*  Write drawing instructions for labels of vector features      */
3062
    /* -------------------------------------------------------------- */
3063
539
    iObj = 0;
3064
539
    for (const GDALPDFLayerDesc &oLayerDesc : oPageContext.asVectorDesc)
3065
1.17k
    {
3066
1.17k
        if (oLayerDesc.nOCGTextId.toBool())
3067
0
        {
3068
0
            VSIFPrintfL(m_fp, "/OC /Lyr%d BDC\n", oLayerDesc.nOCGId.toInt());
3069
0
            VSIFPrintfL(m_fp, "/OC /Lyr%d BDC\n",
3070
0
                        oLayerDesc.nOCGTextId.toInt());
3071
3072
0
            for (size_t iVector = 0; iVector < oLayerDesc.aIdsText.size();
3073
0
                 iVector++)
3074
0
            {
3075
0
                if (oLayerDesc.aIdsText[iVector].toBool())
3076
0
                {
3077
0
                    CPLString osName = oLayerDesc.aFeatureNames[iVector];
3078
0
                    if (!osName.empty())
3079
0
                    {
3080
0
                        VSIFPrintfL(m_fp, "/feature <</MCID %d>> BDC\n", iObj);
3081
0
                    }
3082
3083
0
                    VSIFPrintfL(m_fp, "/Text%d Do\n",
3084
0
                                oLayerDesc.aIdsText[iVector].toInt());
3085
3086
0
                    if (!osName.empty())
3087
0
                    {
3088
0
                        VSIFPrintfL(m_fp, "EMC\n");
3089
0
                    }
3090
0
                }
3091
3092
0
                iObj++;
3093
0
            }
3094
3095
0
            VSIFPrintfL(m_fp, "EMC\n");
3096
0
            VSIFPrintfL(m_fp, "EMC\n");
3097
0
        }
3098
1.17k
        else
3099
1.17k
            iObj += static_cast<int>(oLayerDesc.aIds.size());
3100
1.17k
    }
3101
3102
    /* -------------------------------------------------------------- */
3103
    /*  Write drawing instructions for extra content.                 */
3104
    /* -------------------------------------------------------------- */
3105
539
    if (pszExtraStream || !asExtraImageDesc.empty())
3106
0
    {
3107
0
        if (nLayerExtraId.toBool())
3108
0
            VSIFPrintfL(m_fp, "/OC /Lyr%d BDC\n", nLayerExtraId.toInt());
3109
3110
        /* -------------------------------------------------------------- */
3111
        /*  Write drawing instructions for extra images.                  */
3112
        /* -------------------------------------------------------------- */
3113
0
        for (size_t iImage = 0; iImage < asExtraImageDesc.size(); iImage++)
3114
0
        {
3115
0
            VSIFPrintfL(m_fp, "q\n");
3116
0
            GDALPDFObjectRW *poXSize =
3117
0
                GDALPDFObjectRW::CreateReal(asExtraImageDesc[iImage].dfXSize);
3118
0
            GDALPDFObjectRW *poYSize =
3119
0
                GDALPDFObjectRW::CreateReal(asExtraImageDesc[iImage].dfYSize);
3120
0
            GDALPDFObjectRW *poXOff =
3121
0
                GDALPDFObjectRW::CreateReal(asExtraImageDesc[iImage].dfXOff);
3122
0
            GDALPDFObjectRW *poYOff =
3123
0
                GDALPDFObjectRW::CreateReal(asExtraImageDesc[iImage].dfYOff);
3124
0
            VSIFPrintfL(
3125
0
                m_fp, "%s 0 0 %s %s %s cm\n", poXSize->Serialize().c_str(),
3126
0
                poYSize->Serialize().c_str(), poXOff->Serialize().c_str(),
3127
0
                poYOff->Serialize().c_str());
3128
0
            delete poXSize;
3129
0
            delete poYSize;
3130
0
            delete poXOff;
3131
0
            delete poYOff;
3132
0
            VSIFPrintfL(m_fp, "/Image%d Do\n",
3133
0
                        asExtraImageDesc[iImage].nImageId.toInt());
3134
0
            VSIFPrintfL(m_fp, "Q\n");
3135
0
        }
3136
3137
0
        if (pszExtraStream)
3138
0
            VSIFPrintfL(m_fp, "%s\n", pszExtraStream);
3139
3140
0
        if (nLayerExtraId.toBool())
3141
0
            VSIFPrintfL(m_fp, "EMC\n");
3142
0
    }
3143
3144
539
    EndObjWithStream();
3145
3146
    /* -------------------------------------------------------------- */
3147
    /*  Write objects for feature tree.                               */
3148
    /* -------------------------------------------------------------- */
3149
539
    if (m_nStructTreeRootId.toBool())
3150
45
    {
3151
45
        auto nParentTreeId = AllocNewObject();
3152
45
        StartObj(nParentTreeId);
3153
45
        VSIFPrintfL(m_fp, "<< /Nums [ 0 ");
3154
45
        VSIFPrintfL(m_fp, "[ ");
3155
1.22k
        for (size_t iLayer = 0; iLayer < oPageContext.asVectorDesc.size();
3156
1.17k
             iLayer++)
3157
1.17k
        {
3158
1.17k
            const GDALPDFLayerDesc &oLayerDesc =
3159
1.17k
                oPageContext.asVectorDesc[iLayer];
3160
2.82k
            for (size_t iVector = 0; iVector < oLayerDesc.aIds.size();
3161
1.64k
                 iVector++)
3162
1.64k
            {
3163
1.64k
                const auto &nId = oLayerDesc.aUserPropertiesIds[iVector];
3164
1.64k
                if (nId.toBool())
3165
1.64k
                    VSIFPrintfL(m_fp, "%d 0 R ", nId.toInt());
3166
1.64k
            }
3167
1.17k
        }
3168
45
        VSIFPrintfL(m_fp, " ]\n");
3169
45
        VSIFPrintfL(m_fp, " ] >> \n");
3170
45
        EndObj();
3171
3172
45
        StartObj(m_nStructTreeRootId);
3173
45
        VSIFPrintfL(m_fp,
3174
45
                    "<< "
3175
45
                    "/Type /StructTreeRoot "
3176
45
                    "/ParentTree %d 0 R "
3177
45
                    "/K [ ",
3178
45
                    nParentTreeId.toInt());
3179
1.22k
        for (size_t iLayer = 0; iLayer < oPageContext.asVectorDesc.size();
3180
1.17k
             iLayer++)
3181
1.17k
        {
3182
1.17k
            VSIFPrintfL(
3183
1.17k
                m_fp, "%d 0 R ",
3184
1.17k
                oPageContext.asVectorDesc[iLayer].nFeatureLayerId.toInt());
3185
1.17k
        }
3186
45
        VSIFPrintfL(m_fp, "] >>\n");
3187
45
        EndObj();
3188
45
    }
3189
3190
    /* -------------------------------------------------------------- */
3191
    /*  Write page resource dictionary.                               */
3192
    /* -------------------------------------------------------------- */
3193
539
    StartObj(oPageContext.nResourcesId);
3194
539
    {
3195
539
        GDALPDFDictionaryRW oDict;
3196
539
        GDALPDFDictionaryRW *poDictXObject = new GDALPDFDictionaryRW();
3197
539
        oDict.Add("XObject", poDictXObject);
3198
539
        size_t iImage;
3199
539
        for (const GDALPDFRasterDesc &oDesc : oPageContext.asRasterDesc)
3200
494
        {
3201
988
            for (iImage = 0; iImage < oDesc.asImageDesc.size(); iImage++)
3202
494
            {
3203
494
                poDictXObject->Add(
3204
494
                    CPLSPrintf("Image%d",
3205
494
                               oDesc.asImageDesc[iImage].nImageId.toInt()),
3206
494
                    oDesc.asImageDesc[iImage].nImageId, 0);
3207
494
            }
3208
494
        }
3209
539
        for (iImage = 0; iImage < asExtraImageDesc.size(); iImage++)
3210
0
        {
3211
0
            poDictXObject->Add(
3212
0
                CPLSPrintf("Image%d",
3213
0
                           asExtraImageDesc[iImage].nImageId.toInt()),
3214
0
                asExtraImageDesc[iImage].nImageId, 0);
3215
0
        }
3216
539
        for (const GDALPDFLayerDesc &oLayerDesc : oPageContext.asVectorDesc)
3217
1.17k
        {
3218
2.82k
            for (size_t iVector = 0; iVector < oLayerDesc.aIds.size();
3219
1.64k
                 iVector++)
3220
1.64k
            {
3221
1.64k
                if (oLayerDesc.aIds[iVector].toBool())
3222
1.64k
                    poDictXObject->Add(
3223
1.64k
                        CPLSPrintf("Vector%d",
3224
1.64k
                                   oLayerDesc.aIds[iVector].toInt()),
3225
1.64k
                        oLayerDesc.aIds[iVector], 0);
3226
1.64k
            }
3227
2.82k
            for (size_t iVector = 0; iVector < oLayerDesc.aIdsText.size();
3228
1.64k
                 iVector++)
3229
1.64k
            {
3230
1.64k
                if (oLayerDesc.aIdsText[iVector].toBool())
3231
0
                    poDictXObject->Add(
3232
0
                        CPLSPrintf("Text%d",
3233
0
                                   oLayerDesc.aIdsText[iVector].toInt()),
3234
0
                        oLayerDesc.aIdsText[iVector], 0);
3235
1.64k
            }
3236
1.17k
        }
3237
3238
539
        if (pszExtraStream)
3239
0
        {
3240
0
            std::vector<CPLString> aosNeededFonts;
3241
0
            if (strstr(pszExtraStream, "/FTimes"))
3242
0
            {
3243
0
                aosNeededFonts.push_back("Times-Roman");
3244
0
                aosNeededFonts.push_back("Times-Bold");
3245
0
                aosNeededFonts.push_back("Times-Italic");
3246
0
                aosNeededFonts.push_back("Times-BoldItalic");
3247
0
            }
3248
0
            if (strstr(pszExtraStream, "/FHelvetica"))
3249
0
            {
3250
0
                aosNeededFonts.push_back("Helvetica");
3251
0
                aosNeededFonts.push_back("Helvetica-Bold");
3252
0
                aosNeededFonts.push_back("Helvetica-Oblique");
3253
0
                aosNeededFonts.push_back("Helvetica-BoldOblique");
3254
0
            }
3255
0
            if (strstr(pszExtraStream, "/FCourier"))
3256
0
            {
3257
0
                aosNeededFonts.push_back("Courier");
3258
0
                aosNeededFonts.push_back("Courier-Bold");
3259
0
                aosNeededFonts.push_back("Courier-Oblique");
3260
0
                aosNeededFonts.push_back("Courier-BoldOblique");
3261
0
            }
3262
0
            if (strstr(pszExtraStream, "/FSymbol"))
3263
0
                aosNeededFonts.push_back("Symbol");
3264
0
            if (strstr(pszExtraStream, "/FZapfDingbats"))
3265
0
                aosNeededFonts.push_back("ZapfDingbats");
3266
3267
0
            if (!aosNeededFonts.empty())
3268
0
            {
3269
0
                GDALPDFDictionaryRW *poDictFont = new GDALPDFDictionaryRW();
3270
3271
0
                for (CPLString &osFont : aosNeededFonts)
3272
0
                {
3273
0
                    GDALPDFDictionaryRW *poDictFontInner =
3274
0
                        new GDALPDFDictionaryRW();
3275
0
                    poDictFontInner->Add("Type",
3276
0
                                         GDALPDFObjectRW::CreateName("Font"));
3277
0
                    poDictFontInner->Add("BaseFont",
3278
0
                                         GDALPDFObjectRW::CreateName(osFont));
3279
0
                    poDictFontInner->Add(
3280
0
                        "Encoding",
3281
0
                        GDALPDFObjectRW::CreateName("WinAnsiEncoding"));
3282
0
                    poDictFontInner->Add("Subtype",
3283
0
                                         GDALPDFObjectRW::CreateName("Type1"));
3284
3285
0
                    osFont = "F" + osFont;
3286
0
                    const size_t nHyphenPos = osFont.find('-');
3287
0
                    if (nHyphenPos != std::string::npos)
3288
0
                        osFont.erase(nHyphenPos, 1);
3289
0
                    poDictFont->Add(osFont, poDictFontInner);
3290
0
                }
3291
3292
0
                oDict.Add("Font", poDictFont);
3293
0
            }
3294
0
        }
3295
3296
539
        if (!m_asOCGs.empty())
3297
45
        {
3298
45
            GDALPDFDictionaryRW *poDictProperties = new GDALPDFDictionaryRW();
3299
#ifdef HACK_TO_GENERATE_OCMD
3300
            GDALPDFDictionaryRW *poOCMD = new GDALPDFDictionaryRW();
3301
            poOCMD->Add("Type", GDALPDFObjectRW::CreateName("OCMD"));
3302
            GDALPDFArrayRW *poArray = new GDALPDFArrayRW();
3303
            poArray->Add(m_asOCGs[0].nId, 0);
3304
            poArray->Add(m_asOCGs[1].nId, 0);
3305
            poOCMD->Add("OCGs", poArray);
3306
            poDictProperties->Add(CPLSPrintf("Lyr%d", m_asOCGs[1].nId.toInt()),
3307
                                  poOCMD);
3308
#else
3309
1.22k
            for (size_t i = 0; i < m_asOCGs.size(); i++)
3310
1.17k
                poDictProperties->Add(
3311
1.17k
                    CPLSPrintf("Lyr%d", m_asOCGs[i].nId.toInt()),
3312
1.17k
                    m_asOCGs[i].nId, 0);
3313
45
#endif
3314
45
            oDict.Add("Properties", poDictProperties);
3315
45
        }
3316
3317
539
        VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
3318
539
    }
3319
539
    EndObj();
3320
3321
    /* -------------------------------------------------------------- */
3322
    /*  Write annotation arrays.                                      */
3323
    /* -------------------------------------------------------------- */
3324
539
    StartObj(oPageContext.nAnnotsId);
3325
539
    {
3326
539
        GDALPDFArrayRW oArray;
3327
539
        for (size_t i = 0; i < oPageContext.anAnnotationsId.size(); i++)
3328
0
        {
3329
0
            oArray.Add(oPageContext.anAnnotationsId[i], 0);
3330
0
        }
3331
539
        VSIFPrintfL(m_fp, "%s\n", oArray.Serialize().c_str());
3332
539
    }
3333
539
    EndObj();
3334
3335
539
    return TRUE;
3336
539
}
3337
3338
/************************************************************************/
3339
/*                             WriteMask()                              */
3340
/************************************************************************/
3341
3342
GDALPDFObjectNum GDALPDFBaseWriter::WriteMask(GDALDataset *poSrcDS, int nXOff,
3343
                                              int nYOff, int nReqXSize,
3344
                                              int nReqYSize,
3345
                                              PDFCompressMethod eCompressMethod)
3346
75
{
3347
75
    int nMaskSize = nReqXSize * nReqYSize;
3348
75
    GByte *pabyMask = static_cast<GByte *>(VSIMalloc(nMaskSize));
3349
75
    if (pabyMask == nullptr)
3350
0
        return GDALPDFObjectNum();
3351
3352
75
    CPLErr eErr;
3353
75
    eErr = poSrcDS->GetRasterBand(4)->RasterIO(
3354
75
        GF_Read, nXOff, nYOff, nReqXSize, nReqYSize, pabyMask, nReqXSize,
3355
75
        nReqYSize, GDT_Byte, 0, 0, nullptr);
3356
75
    if (eErr != CE_None)
3357
16
    {
3358
16
        VSIFree(pabyMask);
3359
16
        return GDALPDFObjectNum();
3360
16
    }
3361
3362
59
    int bOnly0or255 = TRUE;
3363
59
    int bOnly255 = TRUE;
3364
    /* int bOnly0 = TRUE; */
3365
59
    int i;
3366
366k
    for (i = 0; i < nReqXSize * nReqYSize; i++)
3367
366k
    {
3368
366k
        if (pabyMask[i] == 0)
3369
346k
            bOnly255 = FALSE;
3370
19.2k
        else if (pabyMask[i] == 255)
3371
19.1k
        {
3372
            /* bOnly0 = FALSE; */
3373
19.1k
        }
3374
30
        else
3375
30
        {
3376
            /* bOnly0 = FALSE; */
3377
30
            bOnly255 = FALSE;
3378
30
            bOnly0or255 = FALSE;
3379
30
            break;
3380
30
        }
3381
366k
    }
3382
3383
59
    if (bOnly255)
3384
1
    {
3385
1
        CPLFree(pabyMask);
3386
1
        return GDALPDFObjectNum();
3387
1
    }
3388
3389
58
    if (bOnly0or255)
3390
28
    {
3391
        /* Translate to 1 bit */
3392
28
        int nReqXSize1 = (nReqXSize + 7) / 8;
3393
28
        GByte *pabyMask1 =
3394
28
            static_cast<GByte *>(VSICalloc(nReqXSize1, nReqYSize));
3395
28
        if (pabyMask1 == nullptr)
3396
0
        {
3397
0
            CPLFree(pabyMask);
3398
0
            return GDALPDFObjectNum();
3399
0
        }
3400
38.1k
        for (int y = 0; y < nReqYSize; y++)
3401
38.1k
        {
3402
317k
            for (int x = 0; x < nReqXSize; x++)
3403
279k
            {
3404
279k
                if (pabyMask[y * nReqXSize + x])
3405
10.9k
                    pabyMask1[y * nReqXSize1 + x / 8] |= 1 << (7 - (x % 8));
3406
279k
            }
3407
38.1k
        }
3408
28
        VSIFree(pabyMask);
3409
28
        pabyMask = pabyMask1;
3410
28
        nMaskSize = nReqXSize1 * nReqYSize;
3411
28
    }
3412
3413
58
    auto nMaskId = AllocNewObject();
3414
3415
58
    GDALPDFDictionaryRW oDict;
3416
58
    oDict.Add("Type", GDALPDFObjectRW::CreateName("XObject"))
3417
58
        .Add("Subtype", GDALPDFObjectRW::CreateName("Image"))
3418
58
        .Add("Width", nReqXSize)
3419
58
        .Add("Height", nReqYSize)
3420
58
        .Add("ColorSpace", GDALPDFObjectRW::CreateName("DeviceGray"))
3421
58
        .Add("BitsPerComponent", (bOnly0or255) ? 1 : 8);
3422
3423
58
    StartObjWithStream(nMaskId, oDict, eCompressMethod != COMPRESS_NONE);
3424
3425
58
    VSIFWriteL(pabyMask, nMaskSize, 1, m_fp);
3426
58
    CPLFree(pabyMask);
3427
3428
58
    EndObjWithStream();
3429
3430
58
    return nMaskId;
3431
58
}
3432
3433
/************************************************************************/
3434
/*                             WriteBlock()                             */
3435
/************************************************************************/
3436
3437
GDALPDFObjectNum GDALPDFBaseWriter::WriteBlock(
3438
    GDALDataset *poSrcDS, int nXOff, int nYOff, int nReqXSize, int nReqYSize,
3439
    const GDALPDFObjectNum &nColorTableIdIn, PDFCompressMethod eCompressMethod,
3440
    int nPredictor, int nJPEGQuality, const char *pszJPEG2000_DRIVER,
3441
    GDALProgressFunc pfnProgress, void *pProgressData)
3442
711
{
3443
711
    int nBands = poSrcDS->GetRasterCount();
3444
711
    if (nBands == 0)
3445
0
        return GDALPDFObjectNum();
3446
3447
711
    GDALPDFObjectNum nColorTableId(nColorTableIdIn);
3448
711
    if (!nColorTableId.toBool())
3449
637
        nColorTableId = WriteColorTable(poSrcDS);
3450
3451
711
    CPLErr eErr = CE_None;
3452
711
    GDALDataset *poBlockSrcDS = nullptr;
3453
711
    std::unique_ptr<MEMDataset> poMEMDS;
3454
711
    GByte *pabyMEMDSBuffer = nullptr;
3455
3456
711
    if (eCompressMethod == COMPRESS_DEFAULT)
3457
710
    {
3458
710
        GDALDataset *poSrcDSToTest = poSrcDS;
3459
3460
        /* Test if we can directly copy original JPEG content */
3461
        /* if available */
3462
710
        if (VRTDataset *poVRTDS = dynamic_cast<VRTDataset *>(poSrcDS))
3463
690
        {
3464
690
            poSrcDSToTest = poVRTDS->GetSingleSimpleSource();
3465
690
        }
3466
3467
710
        if (poSrcDSToTest != nullptr && poSrcDSToTest->GetDriver() != nullptr &&
3468
710
            EQUAL(poSrcDSToTest->GetDriver()->GetDescription(), "JPEG") &&
3469
710
            nXOff == 0 && nYOff == 0 &&
3470
710
            nReqXSize == poSrcDSToTest->GetRasterXSize() &&
3471
710
            nReqYSize == poSrcDSToTest->GetRasterYSize() && nJPEGQuality < 0)
3472
0
        {
3473
0
            VSILFILE *fpSrc = VSIFOpenL(poSrcDSToTest->GetDescription(), "rb");
3474
0
            if (fpSrc != nullptr)
3475
0
            {
3476
0
                CPLDebug("PDF", "Copying directly original JPEG file");
3477
3478
0
                VSIFSeekL(fpSrc, 0, SEEK_END);
3479
0
                const int nLength = static_cast<int>(VSIFTellL(fpSrc));
3480
0
                VSIFSeekL(fpSrc, 0, SEEK_SET);
3481
3482
0
                auto nImageId = AllocNewObject();
3483
3484
0
                StartObj(nImageId);
3485
3486
0
                GDALPDFDictionaryRW oDict;
3487
0
                oDict.Add("Length", nLength)
3488
0
                    .Add("Type", GDALPDFObjectRW::CreateName("XObject"))
3489
0
                    .Add("Filter", GDALPDFObjectRW::CreateName("DCTDecode"))
3490
0
                    .Add("Subtype", GDALPDFObjectRW::CreateName("Image"))
3491
0
                    .Add("Width", nReqXSize)
3492
0
                    .Add("Height", nReqYSize)
3493
0
                    .Add("ColorSpace",
3494
0
                         (nBands == 1)
3495
0
                             ? GDALPDFObjectRW::CreateName("DeviceGray")
3496
0
                             : GDALPDFObjectRW::CreateName("DeviceRGB"))
3497
0
                    .Add("BitsPerComponent", 8);
3498
0
                VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
3499
0
                VSIFPrintfL(m_fp, "stream\n");
3500
3501
0
                GByte abyBuffer[1024];
3502
0
                for (int i = 0; i < nLength; i += 1024)
3503
0
                {
3504
0
                    const auto nRead = VSIFReadL(abyBuffer, 1, 1024, fpSrc);
3505
0
                    if (VSIFWriteL(abyBuffer, 1, nRead, m_fp) != nRead)
3506
0
                    {
3507
0
                        eErr = CE_Failure;
3508
0
                        break;
3509
0
                    }
3510
3511
0
                    if (eErr == CE_None && pfnProgress != nullptr &&
3512
0
                        !pfnProgress(double(i + nRead) / double(nLength),
3513
0
                                     nullptr, pProgressData))
3514
0
                    {
3515
0
                        CPLError(CE_Failure, CPLE_UserInterrupt,
3516
0
                                 "User terminated CreateCopy()");
3517
0
                        eErr = CE_Failure;
3518
0
                        break;
3519
0
                    }
3520
0
                }
3521
3522
0
                VSIFPrintfL(m_fp, "\nendstream\n");
3523
3524
0
                EndObj();
3525
3526
0
                VSIFCloseL(fpSrc);
3527
3528
0
                return eErr == CE_None ? nImageId : GDALPDFObjectNum();
3529
0
            }
3530
0
        }
3531
3532
710
        eCompressMethod = COMPRESS_DEFLATE;
3533
710
    }
3534
3535
711
    GDALPDFObjectNum nMaskId;
3536
711
    if (nBands == 4)
3537
75
    {
3538
75
        nMaskId = WriteMask(poSrcDS, nXOff, nYOff, nReqXSize, nReqYSize,
3539
75
                            eCompressMethod);
3540
75
    }
3541
3542
711
    if (nReqXSize == poSrcDS->GetRasterXSize() &&
3543
711
        nReqYSize == poSrcDS->GetRasterYSize() && nBands != 4)
3544
636
    {
3545
636
        poBlockSrcDS = poSrcDS;
3546
636
    }
3547
75
    else
3548
75
    {
3549
75
        if (nBands == 4)
3550
75
            nBands = 3;
3551
3552
75
        poMEMDS.reset(
3553
75
            MEMDataset::Create("", nReqXSize, nReqYSize, 0, GDT_Byte, nullptr));
3554
3555
75
        pabyMEMDSBuffer =
3556
75
            static_cast<GByte *>(VSIMalloc3(nReqXSize, nReqYSize, nBands));
3557
75
        if (pabyMEMDSBuffer == nullptr)
3558
0
        {
3559
0
            return GDALPDFObjectNum();
3560
0
        }
3561
3562
75
        eErr = poSrcDS->RasterIO(GF_Read, nXOff, nYOff, nReqXSize, nReqYSize,
3563
75
                                 pabyMEMDSBuffer, nReqXSize, nReqYSize,
3564
75
                                 GDT_Byte, nBands, nullptr, 0, 0, 0, nullptr);
3565
3566
75
        if (eErr != CE_None)
3567
17
        {
3568
17
            CPLFree(pabyMEMDSBuffer);
3569
17
            return GDALPDFObjectNum();
3570
17
        }
3571
3572
58
        int iBand;
3573
232
        for (iBand = 0; iBand < nBands; iBand++)
3574
174
        {
3575
174
            auto hBand = MEMCreateRasterBandEx(
3576
174
                poMEMDS.get(), iBand + 1,
3577
174
                pabyMEMDSBuffer + iBand * nReqXSize * nReqYSize, GDT_Byte, 0, 0,
3578
174
                false);
3579
174
            poMEMDS->AddMEMBand(hBand);
3580
174
        }
3581
3582
58
        poBlockSrcDS = poMEMDS.get();
3583
58
    }
3584
3585
694
    auto nImageId = AllocNewObject();
3586
3587
694
    GDALPDFObjectNum nMeasureId;
3588
694
    if (CPLTestBool(
3589
694
            CPLGetConfigOption("GDAL_PDF_WRITE_GEOREF_ON_IMAGE", "FALSE")) &&
3590
694
        nReqXSize == poSrcDS->GetRasterXSize() &&
3591
694
        nReqYSize == poSrcDS->GetRasterYSize())
3592
0
    {
3593
0
        PDFMargins sMargins;
3594
0
        nMeasureId = WriteSRS_ISO32000(poSrcDS, 1, nullptr, &sMargins, FALSE);
3595
0
    }
3596
3597
694
    GDALPDFDictionaryRW oDict;
3598
694
    oDict.Add("Type", GDALPDFObjectRW::CreateName("XObject"));
3599
3600
694
    if (eCompressMethod == COMPRESS_DEFLATE)
3601
693
    {
3602
693
        if (nPredictor == 2)
3603
0
            oDict.Add("DecodeParms", &((new GDALPDFDictionaryRW())
3604
0
                                           ->Add("Predictor", 2)
3605
0
                                           .Add("Colors", nBands)
3606
0
                                           .Add("Columns", nReqXSize)));
3607
693
    }
3608
1
    else if (eCompressMethod == COMPRESS_JPEG)
3609
0
    {
3610
0
        oDict.Add("Filter", GDALPDFObjectRW::CreateName("DCTDecode"));
3611
0
    }
3612
1
    else if (eCompressMethod == COMPRESS_JPEG2000)
3613
0
    {
3614
0
        oDict.Add("Filter", GDALPDFObjectRW::CreateName("JPXDecode"));
3615
0
    }
3616
3617
694
    oDict.Add("Subtype", GDALPDFObjectRW::CreateName("Image"))
3618
694
        .Add("Width", nReqXSize)
3619
694
        .Add("Height", nReqYSize)
3620
694
        .Add("ColorSpace",
3621
694
             (nColorTableId.toBool())
3622
694
                 ? GDALPDFObjectRW::CreateIndirect(nColorTableId, 0)
3623
694
             : (nBands == 1) ? GDALPDFObjectRW::CreateName("DeviceGray")
3624
620
                             : GDALPDFObjectRW::CreateName("DeviceRGB"))
3625
694
        .Add("BitsPerComponent", 8);
3626
694
    if (nMaskId.toBool())
3627
58
    {
3628
58
        oDict.Add("SMask", nMaskId, 0);
3629
58
    }
3630
694
    if (nMeasureId.toBool())
3631
0
    {
3632
0
        oDict.Add("Measure", nMeasureId, 0);
3633
0
    }
3634
3635
694
    StartObjWithStream(nImageId, oDict, eCompressMethod == COMPRESS_DEFLATE);
3636
3637
694
    if (eCompressMethod == COMPRESS_JPEG ||
3638
694
        eCompressMethod == COMPRESS_JPEG2000)
3639
0
    {
3640
0
        GDALDriver *poJPEGDriver = nullptr;
3641
0
        std::string osTmpfilename;
3642
0
        char **papszOptions = nullptr;
3643
3644
0
        bool bEcwEncodeKeyRequiredButNotFound = false;
3645
0
        if (eCompressMethod == COMPRESS_JPEG)
3646
0
        {
3647
0
            poJPEGDriver = GetGDALDriverManager()->GetDriverByName("JPEG");
3648
0
            if (poJPEGDriver != nullptr && nJPEGQuality > 0)
3649
0
                papszOptions = CSLAddString(
3650
0
                    papszOptions, CPLSPrintf("QUALITY=%d", nJPEGQuality));
3651
0
            osTmpfilename = VSIMemGenerateHiddenFilename("pdf_temp.jpg");
3652
0
        }
3653
0
        else
3654
0
        {
3655
0
            if (pszJPEG2000_DRIVER == nullptr ||
3656
0
                EQUAL(pszJPEG2000_DRIVER, "JP2KAK"))
3657
0
                poJPEGDriver =
3658
0
                    GetGDALDriverManager()->GetDriverByName("JP2KAK");
3659
0
            if (poJPEGDriver == nullptr)
3660
0
            {
3661
0
                if (pszJPEG2000_DRIVER == nullptr ||
3662
0
                    EQUAL(pszJPEG2000_DRIVER, "JP2ECW"))
3663
0
                {
3664
0
                    poJPEGDriver =
3665
0
                        GetGDALDriverManager()->GetDriverByName("JP2ECW");
3666
0
                    if (poJPEGDriver &&
3667
0
                        poJPEGDriver->GetMetadataItem(
3668
0
                            GDAL_DMD_CREATIONDATATYPES) == nullptr)
3669
0
                    {
3670
0
                        poJPEGDriver = nullptr;
3671
0
                    }
3672
0
                    else if (poJPEGDriver)
3673
0
                    {
3674
0
                        if (strstr(poJPEGDriver->GetMetadataItem(
3675
0
                                       GDAL_DMD_CREATIONOPTIONLIST),
3676
0
                                   "ECW_ENCODE_KEY"))
3677
0
                        {
3678
0
                            if (!CPLGetConfigOption("ECW_ENCODE_KEY", nullptr))
3679
0
                            {
3680
0
                                bEcwEncodeKeyRequiredButNotFound = true;
3681
0
                                poJPEGDriver = nullptr;
3682
0
                            }
3683
0
                        }
3684
0
                    }
3685
0
                }
3686
0
                if (poJPEGDriver)
3687
0
                {
3688
0
                    papszOptions = CSLAddString(papszOptions, "PROFILE=NPJE");
3689
0
                    papszOptions = CSLAddString(papszOptions, "LAYERS=1");
3690
0
                    papszOptions = CSLAddString(papszOptions, "GeoJP2=OFF");
3691
0
                    papszOptions = CSLAddString(papszOptions, "GMLJP2=OFF");
3692
0
                }
3693
0
            }
3694
0
            if (poJPEGDriver == nullptr)
3695
0
            {
3696
0
                if (pszJPEG2000_DRIVER == nullptr ||
3697
0
                    EQUAL(pszJPEG2000_DRIVER, "JP2OpenJPEG"))
3698
0
                    poJPEGDriver =
3699
0
                        GetGDALDriverManager()->GetDriverByName("JP2OpenJPEG");
3700
0
                if (poJPEGDriver)
3701
0
                {
3702
0
                    papszOptions = CSLAddString(papszOptions, "GeoJP2=OFF");
3703
0
                    papszOptions = CSLAddString(papszOptions, "GMLJP2=OFF");
3704
0
                }
3705
0
            }
3706
0
            osTmpfilename = VSIMemGenerateHiddenFilename("pdf_temp.jp2");
3707
0
        }
3708
3709
0
        if (poJPEGDriver == nullptr)
3710
0
        {
3711
0
            if (bEcwEncodeKeyRequiredButNotFound)
3712
0
            {
3713
0
                CPLError(CE_Failure, CPLE_NotSupported,
3714
0
                         "No JPEG2000 driver usable (JP2ECW detected but "
3715
0
                         "ECW_ENCODE_KEY configuration option not set");
3716
0
            }
3717
0
            else
3718
0
            {
3719
0
                CPLError(CE_Failure, CPLE_NotSupported, "No %s driver found",
3720
0
                         (eCompressMethod == COMPRESS_JPEG) ? "JPEG"
3721
0
                                                            : "JPEG2000");
3722
0
            }
3723
0
            eErr = CE_Failure;
3724
0
            goto end;
3725
0
        }
3726
3727
0
        GDALDataset *poJPEGDS =
3728
0
            poJPEGDriver->CreateCopy(osTmpfilename.c_str(), poBlockSrcDS, FALSE,
3729
0
                                     papszOptions, pfnProgress, pProgressData);
3730
3731
0
        CSLDestroy(papszOptions);
3732
0
        if (poJPEGDS == nullptr)
3733
0
        {
3734
0
            eErr = CE_Failure;
3735
0
            goto end;
3736
0
        }
3737
3738
0
        GDALClose(poJPEGDS);
3739
3740
0
        vsi_l_offset nJPEGDataSize = 0;
3741
0
        GByte *pabyJPEGData =
3742
0
            VSIGetMemFileBuffer(osTmpfilename.c_str(), &nJPEGDataSize, TRUE);
3743
0
        VSIFWriteL(pabyJPEGData, static_cast<size_t>(nJPEGDataSize), 1, m_fp);
3744
0
        CPLFree(pabyJPEGData);
3745
0
    }
3746
694
    else
3747
694
    {
3748
694
        GByte *pabyLine = static_cast<GByte *>(
3749
694
            CPLMalloc(static_cast<size_t>(nReqXSize) * nBands));
3750
488k
        for (int iLine = 0; iLine < nReqYSize; iLine++)
3751
488k
        {
3752
            /* Get pixel interleaved data */
3753
488k
            eErr = poBlockSrcDS->RasterIO(
3754
488k
                GF_Read, 0, iLine, nReqXSize, 1, pabyLine, nReqXSize, 1,
3755
488k
                GDT_Byte, nBands, nullptr, nBands, 0, 1, nullptr);
3756
488k
            if (eErr != CE_None)
3757
200
                break;
3758
3759
            /* Apply predictor if needed */
3760
488k
            if (nPredictor == 2)
3761
0
            {
3762
0
                if (nBands == 1)
3763
0
                {
3764
0
                    int nPrevValue = pabyLine[0];
3765
0
                    for (int iPixel = 1; iPixel < nReqXSize; iPixel++)
3766
0
                    {
3767
0
                        int nCurValue = pabyLine[iPixel];
3768
0
                        pabyLine[iPixel] =
3769
0
                            static_cast<GByte>(nCurValue - nPrevValue);
3770
0
                        nPrevValue = nCurValue;
3771
0
                    }
3772
0
                }
3773
0
                else if (nBands == 3)
3774
0
                {
3775
0
                    int nPrevValueR = pabyLine[0];
3776
0
                    int nPrevValueG = pabyLine[1];
3777
0
                    int nPrevValueB = pabyLine[2];
3778
0
                    for (int iPixel = 1; iPixel < nReqXSize; iPixel++)
3779
0
                    {
3780
0
                        int nCurValueR = pabyLine[3 * iPixel + 0];
3781
0
                        int nCurValueG = pabyLine[3 * iPixel + 1];
3782
0
                        int nCurValueB = pabyLine[3 * iPixel + 2];
3783
0
                        pabyLine[3 * iPixel + 0] =
3784
0
                            static_cast<GByte>(nCurValueR - nPrevValueR);
3785
0
                        pabyLine[3 * iPixel + 1] =
3786
0
                            static_cast<GByte>(nCurValueG - nPrevValueG);
3787
0
                        pabyLine[3 * iPixel + 2] =
3788
0
                            static_cast<GByte>(nCurValueB - nPrevValueB);
3789
0
                        nPrevValueR = nCurValueR;
3790
0
                        nPrevValueG = nCurValueG;
3791
0
                        nPrevValueB = nCurValueB;
3792
0
                    }
3793
0
                }
3794
0
            }
3795
3796
488k
            if (VSIFWriteL(pabyLine, static_cast<size_t>(nReqXSize) * nBands, 1,
3797
488k
                           m_fp) != 1)
3798
0
            {
3799
0
                eErr = CE_Failure;
3800
0
                break;
3801
0
            }
3802
3803
488k
            if (pfnProgress != nullptr &&
3804
488k
                !pfnProgress((iLine + 1) / double(nReqYSize), nullptr,
3805
488k
                             pProgressData))
3806
0
            {
3807
0
                CPLError(CE_Failure, CPLE_UserInterrupt,
3808
0
                         "User terminated CreateCopy()");
3809
0
                eErr = CE_Failure;
3810
0
                break;
3811
0
            }
3812
488k
        }
3813
3814
694
        CPLFree(pabyLine);
3815
694
    }
3816
3817
694
end:
3818
694
    CPLFree(pabyMEMDSBuffer);
3819
694
    pabyMEMDSBuffer = nullptr;
3820
3821
694
    EndObjWithStream();
3822
3823
694
    return eErr == CE_None ? nImageId : GDALPDFObjectNum();
3824
694
}
3825
3826
/************************************************************************/
3827
/*                          WriteJavascript()                           */
3828
/************************************************************************/
3829
3830
GDALPDFObjectNum GDALPDFBaseWriter::WriteJavascript(const char *pszJavascript,
3831
                                                    bool bDeflate)
3832
0
{
3833
0
    auto nJSId = AllocNewObject();
3834
0
    {
3835
0
        GDALPDFDictionaryRW oDict;
3836
0
        StartObjWithStream(nJSId, oDict, bDeflate);
3837
3838
0
        VSIFWriteL(pszJavascript, strlen(pszJavascript), 1, m_fp);
3839
0
        VSIFPrintfL(m_fp, "\n");
3840
3841
0
        EndObjWithStream();
3842
0
    }
3843
3844
0
    m_nNamesId = AllocNewObject();
3845
0
    StartObj(m_nNamesId);
3846
0
    {
3847
0
        GDALPDFDictionaryRW oDict;
3848
0
        GDALPDFDictionaryRW *poJavaScriptDict = new GDALPDFDictionaryRW();
3849
0
        oDict.Add("JavaScript", poJavaScriptDict);
3850
3851
0
        GDALPDFArrayRW *poNamesArray = new GDALPDFArrayRW();
3852
0
        poJavaScriptDict->Add("Names", poNamesArray);
3853
3854
0
        poNamesArray->Add("GDAL");
3855
3856
0
        GDALPDFDictionaryRW *poJSDict = new GDALPDFDictionaryRW();
3857
0
        poNamesArray->Add(poJSDict);
3858
3859
0
        poJSDict->Add("JS", nJSId, 0);
3860
0
        poJSDict->Add("S", GDALPDFObjectRW::CreateName("JavaScript"));
3861
3862
0
        VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
3863
0
    }
3864
0
    EndObj();
3865
3866
0
    return m_nNamesId;
3867
0
}
3868
3869
GDALPDFObjectNum GDALPDFWriter::WriteJavascript(const char *pszJavascript)
3870
0
{
3871
0
    return GDALPDFBaseWriter::WriteJavascript(
3872
0
        pszJavascript, oPageContext.eStreamCompressMethod != COMPRESS_NONE);
3873
0
}
3874
3875
/************************************************************************/
3876
/*                        WriteJavascriptFile()                         */
3877
/************************************************************************/
3878
3879
GDALPDFObjectNum
3880
GDALPDFWriter::WriteJavascriptFile(const char *pszJavascriptFile)
3881
0
{
3882
0
    GDALPDFObjectNum nId;
3883
0
    char *pszJavascriptToFree = static_cast<char *>(CPLMalloc(65536));
3884
0
    VSILFILE *fpJS = VSIFOpenL(pszJavascriptFile, "rb");
3885
0
    if (fpJS != nullptr)
3886
0
    {
3887
0
        const int nRead =
3888
0
            static_cast<int>(VSIFReadL(pszJavascriptToFree, 1, 65536, fpJS));
3889
0
        if (nRead < 65536)
3890
0
        {
3891
0
            pszJavascriptToFree[nRead] = '\0';
3892
0
            nId = WriteJavascript(pszJavascriptToFree);
3893
0
        }
3894
0
        VSIFCloseL(fpJS);
3895
0
    }
3896
0
    CPLFree(pszJavascriptToFree);
3897
0
    return nId;
3898
0
}
3899
3900
/************************************************************************/
3901
/*                              WritePages()                            */
3902
/************************************************************************/
3903
3904
void GDALPDFWriter::WritePages()
3905
756
{
3906
756
    StartObj(m_nPageResourceId);
3907
756
    {
3908
756
        GDALPDFDictionaryRW oDict;
3909
756
        GDALPDFArrayRW *poKids = new GDALPDFArrayRW();
3910
756
        oDict.Add("Type", GDALPDFObjectRW::CreateName("Pages"))
3911
756
            .Add("Count", static_cast<int>(m_asPageId.size()))
3912
756
            .Add("Kids", poKids);
3913
3914
1.51k
        for (size_t i = 0; i < m_asPageId.size(); i++)
3915
756
            poKids->Add(m_asPageId[i], 0);
3916
3917
756
        VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
3918
756
    }
3919
756
    EndObj();
3920
3921
756
    StartObj(m_nCatalogId);
3922
756
    {
3923
756
        GDALPDFDictionaryRW oDict;
3924
756
        oDict.Add("Type", GDALPDFObjectRW::CreateName("Catalog"))
3925
756
            .Add("Pages", m_nPageResourceId, 0);
3926
756
        if (m_nXMPId.toBool())
3927
0
            oDict.Add("Metadata", m_nXMPId, 0);
3928
756
        if (!m_asOCGs.empty())
3929
45
        {
3930
45
            GDALPDFDictionaryRW *poDictOCProperties = new GDALPDFDictionaryRW();
3931
45
            oDict.Add("OCProperties", poDictOCProperties);
3932
3933
45
            GDALPDFDictionaryRW *poDictD = new GDALPDFDictionaryRW();
3934
45
            poDictOCProperties->Add("D", poDictD);
3935
3936
            /* Build "Order" array of D dict */
3937
45
            GDALPDFArrayRW *poArrayOrder = new GDALPDFArrayRW();
3938
1.22k
            for (size_t i = 0; i < m_asOCGs.size(); i++)
3939
1.17k
            {
3940
1.17k
                poArrayOrder->Add(m_asOCGs[i].nId, 0);
3941
1.17k
                if (i + 1 < m_asOCGs.size() &&
3942
1.17k
                    m_asOCGs[i + 1].nParentId == m_asOCGs[i].nId)
3943
0
                {
3944
0
                    GDALPDFArrayRW *poSubArrayOrder = new GDALPDFArrayRW();
3945
0
                    poSubArrayOrder->Add(m_asOCGs[i + 1].nId, 0);
3946
0
                    poArrayOrder->Add(poSubArrayOrder);
3947
0
                    i++;
3948
0
                }
3949
1.17k
            }
3950
45
            poDictD->Add("Order", poArrayOrder);
3951
3952
            /* Build "OFF" array of D dict */
3953
45
            if (!m_osOffLayers.empty())
3954
0
            {
3955
0
                GDALPDFArrayRW *poArrayOFF = new GDALPDFArrayRW();
3956
0
                char **papszTokens = CSLTokenizeString2(m_osOffLayers, ",", 0);
3957
0
                for (int i = 0; papszTokens[i] != nullptr; i++)
3958
0
                {
3959
0
                    size_t j;
3960
0
                    int bFound = FALSE;
3961
0
                    for (j = 0; j < m_asOCGs.size(); j++)
3962
0
                    {
3963
0
                        if (strcmp(papszTokens[i], m_asOCGs[j].osLayerName) ==
3964
0
                            0)
3965
0
                        {
3966
0
                            poArrayOFF->Add(m_asOCGs[j].nId, 0);
3967
0
                            bFound = TRUE;
3968
0
                        }
3969
0
                        if (j + 1 < m_asOCGs.size() &&
3970
0
                            m_asOCGs[j + 1].nParentId == m_asOCGs[j].nId)
3971
0
                        {
3972
0
                            j++;
3973
0
                        }
3974
0
                    }
3975
0
                    if (!bFound)
3976
0
                    {
3977
0
                        CPLError(
3978
0
                            CE_Warning, CPLE_AppDefined,
3979
0
                            "Unknown layer name (%s) specified in OFF_LAYERS",
3980
0
                            papszTokens[i]);
3981
0
                    }
3982
0
                }
3983
0
                CSLDestroy(papszTokens);
3984
3985
0
                poDictD->Add("OFF", poArrayOFF);
3986
0
            }
3987
3988
            /* Build "RBGroups" array of D dict */
3989
45
            if (!m_osExclusiveLayers.empty())
3990
0
            {
3991
0
                GDALPDFArrayRW *poArrayRBGroups = new GDALPDFArrayRW();
3992
0
                char **papszTokens =
3993
0
                    CSLTokenizeString2(m_osExclusiveLayers, ",", 0);
3994
0
                for (int i = 0; papszTokens[i] != nullptr; i++)
3995
0
                {
3996
0
                    size_t j;
3997
0
                    int bFound = FALSE;
3998
0
                    for (j = 0; j < m_asOCGs.size(); j++)
3999
0
                    {
4000
0
                        if (strcmp(papszTokens[i], m_asOCGs[j].osLayerName) ==
4001
0
                            0)
4002
0
                        {
4003
0
                            poArrayRBGroups->Add(m_asOCGs[j].nId, 0);
4004
0
                            bFound = TRUE;
4005
0
                        }
4006
0
                        if (j + 1 < m_asOCGs.size() &&
4007
0
                            m_asOCGs[j + 1].nParentId == m_asOCGs[j].nId)
4008
0
                        {
4009
0
                            j++;
4010
0
                        }
4011
0
                    }
4012
0
                    if (!bFound)
4013
0
                    {
4014
0
                        CPLError(CE_Warning, CPLE_AppDefined,
4015
0
                                 "Unknown layer name (%s) specified in "
4016
0
                                 "EXCLUSIVE_LAYERS",
4017
0
                                 papszTokens[i]);
4018
0
                    }
4019
0
                }
4020
0
                CSLDestroy(papszTokens);
4021
4022
0
                if (poArrayRBGroups->GetLength())
4023
0
                {
4024
0
                    GDALPDFArrayRW *poMainArrayRBGroups = new GDALPDFArrayRW();
4025
0
                    poMainArrayRBGroups->Add(poArrayRBGroups);
4026
0
                    poDictD->Add("RBGroups", poMainArrayRBGroups);
4027
0
                }
4028
0
                else
4029
0
                    delete poArrayRBGroups;
4030
0
            }
4031
4032
45
            GDALPDFArrayRW *poArrayOGCs = new GDALPDFArrayRW();
4033
1.22k
            for (size_t i = 0; i < m_asOCGs.size(); i++)
4034
1.17k
                poArrayOGCs->Add(m_asOCGs[i].nId, 0);
4035
45
            poDictOCProperties->Add("OCGs", poArrayOGCs);
4036
45
        }
4037
4038
756
        if (m_nStructTreeRootId.toBool())
4039
45
        {
4040
45
            GDALPDFDictionaryRW *poDictMarkInfo = new GDALPDFDictionaryRW();
4041
45
            oDict.Add("MarkInfo", poDictMarkInfo);
4042
45
            poDictMarkInfo->Add("UserProperties",
4043
45
                                GDALPDFObjectRW::CreateBool(TRUE));
4044
4045
45
            oDict.Add("StructTreeRoot", m_nStructTreeRootId, 0);
4046
45
        }
4047
4048
756
        if (m_nNamesId.toBool())
4049
0
            oDict.Add("Names", m_nNamesId, 0);
4050
4051
756
        VSIFPrintfL(m_fp, "%s\n", oDict.Serialize().c_str());
4052
756
    }
4053
756
    EndObj();
4054
756
}
4055
4056
/************************************************************************/
4057
/*                        GDALPDFGetJPEGQuality()                       */
4058
/************************************************************************/
4059
4060
static int GDALPDFGetJPEGQuality(char **papszOptions)
4061
711
{
4062
711
    int nJpegQuality = -1;
4063
711
    const char *pszValue = CSLFetchNameValue(papszOptions, "JPEG_QUALITY");
4064
711
    if (pszValue != nullptr)
4065
0
    {
4066
0
        nJpegQuality = atoi(pszValue);
4067
0
        if (!(nJpegQuality >= 1 && nJpegQuality <= 100))
4068
0
        {
4069
0
            CPLError(CE_Warning, CPLE_IllegalArg,
4070
0
                     "JPEG_QUALITY=%s value not recognised, ignoring.",
4071
0
                     pszValue);
4072
0
            nJpegQuality = -1;
4073
0
        }
4074
0
    }
4075
711
    return nJpegQuality;
4076
711
}
4077
4078
/************************************************************************/
4079
/*                         GDALPDFClippingDataset                       */
4080
/************************************************************************/
4081
4082
class GDALPDFClippingDataset final : public GDALDataset
4083
{
4084
    GDALDataset *poSrcDS = nullptr;
4085
    GDALGeoTransform m_gt{};
4086
4087
    CPL_DISALLOW_COPY_ASSIGN(GDALPDFClippingDataset)
4088
4089
  public:
4090
    GDALPDFClippingDataset(GDALDataset *poSrcDSIn, double adfClippingExtent[4])
4091
0
        : poSrcDS(poSrcDSIn)
4092
0
    {
4093
0
        GDALGeoTransform srcGT;
4094
0
        poSrcDS->GetGeoTransform(srcGT);
4095
0
        m_gt[0] = adfClippingExtent[0];
4096
0
        m_gt[1] = srcGT[1];
4097
0
        m_gt[2] = 0.0;
4098
0
        m_gt[3] = srcGT[5] < 0 ? adfClippingExtent[3] : adfClippingExtent[1];
4099
0
        m_gt[4] = 0.0;
4100
0
        m_gt[5] = srcGT[5];
4101
0
        nRasterXSize = static_cast<int>(
4102
0
            (adfClippingExtent[2] - adfClippingExtent[0]) / srcGT[1]);
4103
0
        nRasterYSize = static_cast<int>(
4104
0
            (adfClippingExtent[3] - adfClippingExtent[1]) / fabs(srcGT[5]));
4105
0
    }
4106
4107
    virtual CPLErr GetGeoTransform(GDALGeoTransform &gt) const override
4108
0
    {
4109
0
        gt = m_gt;
4110
0
        return CE_None;
4111
0
    }
4112
4113
    const OGRSpatialReference *GetSpatialRef() const override;
4114
};
4115
4116
const OGRSpatialReference *GDALPDFClippingDataset::GetSpatialRef() const
4117
0
{
4118
0
    return poSrcDS->GetSpatialRef();
4119
0
}
4120
4121
/************************************************************************/
4122
/*                          GDALPDFCreateCopy()                         */
4123
/************************************************************************/
4124
4125
GDALDataset *GDALPDFCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
4126
                               int bStrict, char **papszOptions,
4127
                               GDALProgressFunc pfnProgress,
4128
                               void *pProgressData)
4129
728
{
4130
728
    const int nBands = poSrcDS->GetRasterCount();
4131
728
    const int nWidth = poSrcDS->GetRasterXSize();
4132
728
    const int nHeight = poSrcDS->GetRasterYSize();
4133
4134
    /* -------------------------------------------------------------------- */
4135
    /*      Some some rudimentary checks                                    */
4136
    /* -------------------------------------------------------------------- */
4137
728
    if (nWidth == 0 || nHeight == 0)
4138
3
    {
4139
3
        CPLError(CE_Failure, CPLE_NotSupported,
4140
3
                 "nWidth == 0 || nHeight == 0 not supported");
4141
3
        return nullptr;
4142
3
    }
4143
4144
725
    if (nBands != 1 && nBands != 3 && nBands != 4)
4145
14
    {
4146
14
        CPLError(CE_Failure, CPLE_NotSupported,
4147
14
                 "PDF driver doesn't support %d bands.  Must be 1 (grey or "
4148
14
                 "with color table), "
4149
14
                 "3 (RGB) or 4 bands.\n",
4150
14
                 nBands);
4151
4152
14
        return nullptr;
4153
14
    }
4154
4155
711
    GDALDataType eDT = poSrcDS->GetRasterBand(1)->GetRasterDataType();
4156
711
    if (eDT != GDT_Byte)
4157
307
    {
4158
307
        CPLError((bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported,
4159
307
                 "PDF driver doesn't support data type %s. "
4160
307
                 "Only eight bit byte bands supported.\n",
4161
307
                 GDALGetDataTypeName(
4162
307
                     poSrcDS->GetRasterBand(1)->GetRasterDataType()));
4163
4164
307
        if (bStrict)
4165
0
            return nullptr;
4166
307
    }
4167
4168
    /* -------------------------------------------------------------------- */
4169
    /*     Read options.                                                    */
4170
    /* -------------------------------------------------------------------- */
4171
711
    PDFCompressMethod eCompressMethod = COMPRESS_DEFAULT;
4172
711
    const char *pszCompressMethod = CSLFetchNameValue(papszOptions, "COMPRESS");
4173
711
    if (pszCompressMethod)
4174
3
    {
4175
3
        if (EQUAL(pszCompressMethod, "NONE"))
4176
1
            eCompressMethod = COMPRESS_NONE;
4177
2
        else if (EQUAL(pszCompressMethod, "DEFLATE"))
4178
0
            eCompressMethod = COMPRESS_DEFLATE;
4179
2
        else if (EQUAL(pszCompressMethod, "JPEG"))
4180
0
            eCompressMethod = COMPRESS_JPEG;
4181
2
        else if (EQUAL(pszCompressMethod, "JPEG2000"))
4182
0
            eCompressMethod = COMPRESS_JPEG2000;
4183
2
        else
4184
2
        {
4185
2
            CPLError((bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported,
4186
2
                     "Unsupported value for COMPRESS.");
4187
4188
2
            if (bStrict)
4189
0
                return nullptr;
4190
2
        }
4191
3
    }
4192
4193
711
    PDFCompressMethod eStreamCompressMethod = COMPRESS_DEFLATE;
4194
711
    const char *pszStreamCompressMethod =
4195
711
        CSLFetchNameValue(papszOptions, "STREAM_COMPRESS");
4196
711
    if (pszStreamCompressMethod)
4197
0
    {
4198
0
        if (EQUAL(pszStreamCompressMethod, "NONE"))
4199
0
            eStreamCompressMethod = COMPRESS_NONE;
4200
0
        else if (EQUAL(pszStreamCompressMethod, "DEFLATE"))
4201
0
            eStreamCompressMethod = COMPRESS_DEFLATE;
4202
0
        else
4203
0
        {
4204
0
            CPLError((bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported,
4205
0
                     "Unsupported value for STREAM_COMPRESS.");
4206
4207
0
            if (bStrict)
4208
0
                return nullptr;
4209
0
        }
4210
0
    }
4211
4212
711
    if (nBands == 1 && poSrcDS->GetRasterBand(1)->GetColorTable() != nullptr &&
4213
711
        (eCompressMethod == COMPRESS_JPEG ||
4214
68
         eCompressMethod == COMPRESS_JPEG2000))
4215
0
    {
4216
0
        CPLError(CE_Warning, CPLE_AppDefined,
4217
0
                 "The source raster band has a color table, which is not "
4218
0
                 "appropriate with JPEG or JPEG2000 compression.\n"
4219
0
                 "You should rather consider using color table expansion "
4220
0
                 "(-expand option in gdal_translate)");
4221
0
    }
4222
4223
711
    int nBlockXSize = nWidth;
4224
711
    int nBlockYSize = nHeight;
4225
4226
711
    const bool bTiled = CPLFetchBool(papszOptions, "TILED", false);
4227
711
    if (bTiled)
4228
0
    {
4229
0
        nBlockXSize = 256;
4230
0
        nBlockYSize = 256;
4231
0
    }
4232
4233
711
    const char *pszValue = CSLFetchNameValue(papszOptions, "BLOCKXSIZE");
4234
711
    if (pszValue != nullptr)
4235
0
    {
4236
0
        nBlockXSize = atoi(pszValue);
4237
0
        if (nBlockXSize <= 0 || nBlockXSize >= nWidth)
4238
0
            nBlockXSize = nWidth;
4239
0
    }
4240
4241
711
    pszValue = CSLFetchNameValue(papszOptions, "BLOCKYSIZE");
4242
711
    if (pszValue != nullptr)
4243
0
    {
4244
0
        nBlockYSize = atoi(pszValue);
4245
0
        if (nBlockYSize <= 0 || nBlockYSize >= nHeight)
4246
0
            nBlockYSize = nHeight;
4247
0
    }
4248
4249
711
    int nJPEGQuality = GDALPDFGetJPEGQuality(papszOptions);
4250
4251
711
    const char *pszJPEG2000_DRIVER =
4252
711
        CSLFetchNameValue(papszOptions, "JPEG2000_DRIVER");
4253
4254
711
    const char *pszGEO_ENCODING =
4255
711
        CSLFetchNameValueDef(papszOptions, "GEO_ENCODING", "ISO32000");
4256
711
    if (EQUAL(pszGEO_ENCODING, "OGC_BP"))
4257
0
    {
4258
0
        CPLError(CE_Failure, CPLE_NotSupported,
4259
0
                 "GEO_ENCODING=OGC_BP is no longer supported. Switch to using "
4260
0
                 "ISO32000");
4261
0
        return nullptr;
4262
0
    }
4263
711
    else if (EQUAL(pszGEO_ENCODING, "BOTH"))
4264
0
    {
4265
0
        CPLError(CE_Warning, CPLE_NotSupported,
4266
0
                 "GEO_ENCODING=BOTH is no longer strictly supported. This now "
4267
0
                 "fallbacks to ISO32000");
4268
0
        pszGEO_ENCODING = "ISO32000";
4269
0
    }
4270
4271
711
    const char *pszXMP = CSLFetchNameValue(papszOptions, "XMP");
4272
4273
711
    const char *pszPredictor = CSLFetchNameValue(papszOptions, "PREDICTOR");
4274
711
    int nPredictor = 1;
4275
711
    if (pszPredictor)
4276
0
    {
4277
0
        if (eCompressMethod == COMPRESS_DEFAULT)
4278
0
            eCompressMethod = COMPRESS_DEFLATE;
4279
4280
0
        if (eCompressMethod != COMPRESS_DEFLATE)
4281
0
        {
4282
0
            CPLError(CE_Warning, CPLE_NotSupported,
4283
0
                     "PREDICTOR option is only taken into account for DEFLATE "
4284
0
                     "compression");
4285
0
        }
4286
0
        else
4287
0
        {
4288
0
            nPredictor = atoi(pszPredictor);
4289
0
            if (nPredictor != 1 && nPredictor != 2)
4290
0
            {
4291
0
                CPLError(CE_Warning, CPLE_NotSupported,
4292
0
                         "Supported PREDICTOR values are 1 or 2");
4293
0
                nPredictor = 1;
4294
0
            }
4295
0
        }
4296
0
    }
4297
4298
711
    const char *pszNEATLINE = CSLFetchNameValue(papszOptions, "NEATLINE");
4299
4300
711
    int nMargin = atoi(CSLFetchNameValueDef(papszOptions, "MARGIN", "0"));
4301
4302
711
    PDFMargins sMargins;
4303
711
    sMargins.nLeft = nMargin;
4304
711
    sMargins.nRight = nMargin;
4305
711
    sMargins.nTop = nMargin;
4306
711
    sMargins.nBottom = nMargin;
4307
4308
711
    const char *pszLeftMargin = CSLFetchNameValue(papszOptions, "LEFT_MARGIN");
4309
711
    if (pszLeftMargin)
4310
0
        sMargins.nLeft = atoi(pszLeftMargin);
4311
4312
711
    const char *pszRightMargin =
4313
711
        CSLFetchNameValue(papszOptions, "RIGHT_MARGIN");
4314
711
    if (pszRightMargin)
4315
0
        sMargins.nRight = atoi(pszRightMargin);
4316
4317
711
    const char *pszTopMargin = CSLFetchNameValue(papszOptions, "TOP_MARGIN");
4318
711
    if (pszTopMargin)
4319
0
        sMargins.nTop = atoi(pszTopMargin);
4320
4321
711
    const char *pszBottomMargin =
4322
711
        CSLFetchNameValue(papszOptions, "BOTTOM_MARGIN");
4323
711
    if (pszBottomMargin)
4324
0
        sMargins.nBottom = atoi(pszBottomMargin);
4325
4326
711
    const char *pszDPI = CSLFetchNameValue(papszOptions, "DPI");
4327
711
    double dfDPI = DEFAULT_DPI;
4328
711
    if (pszDPI != nullptr)
4329
0
        dfDPI = CPLAtof(pszDPI);
4330
4331
711
    const char *pszWriteUserUnit =
4332
711
        CSLFetchNameValue(papszOptions, "WRITE_USERUNIT");
4333
711
    bool bWriteUserUnit;
4334
711
    if (pszWriteUserUnit != nullptr)
4335
0
        bWriteUserUnit = CPLTestBool(pszWriteUserUnit);
4336
711
    else
4337
711
        bWriteUserUnit = (pszDPI == nullptr);
4338
4339
711
    double dfUserUnit = dfDPI * USER_UNIT_IN_INCH;
4340
711
    double dfWidthInUserUnit =
4341
711
        nWidth / dfUserUnit + sMargins.nLeft + sMargins.nRight;
4342
711
    double dfHeightInUserUnit =
4343
711
        nHeight / dfUserUnit + sMargins.nBottom + sMargins.nTop;
4344
711
    if (dfWidthInUserUnit > MAXIMUM_SIZE_IN_UNITS ||
4345
711
        dfHeightInUserUnit > MAXIMUM_SIZE_IN_UNITS)
4346
3
    {
4347
3
        if (pszDPI == nullptr)
4348
3
        {
4349
3
            if (sMargins.nLeft + sMargins.nRight >= MAXIMUM_SIZE_IN_UNITS ||
4350
3
                sMargins.nBottom + sMargins.nTop >= MAXIMUM_SIZE_IN_UNITS)
4351
0
            {
4352
0
                CPLError(
4353
0
                    CE_Warning, CPLE_AppDefined,
4354
0
                    "Margins too big compared to maximum page dimension (%d) "
4355
0
                    "in user units allowed by Acrobat",
4356
0
                    MAXIMUM_SIZE_IN_UNITS);
4357
0
            }
4358
3
            else
4359
3
            {
4360
3
                if (dfWidthInUserUnit >= dfHeightInUserUnit)
4361
2
                {
4362
2
                    dfDPI = ceil(double(nWidth) /
4363
2
                                 (MAXIMUM_SIZE_IN_UNITS -
4364
2
                                  (sMargins.nLeft + sMargins.nRight)) /
4365
2
                                 USER_UNIT_IN_INCH);
4366
2
                }
4367
1
                else
4368
1
                {
4369
1
                    dfDPI = ceil(double(nHeight) /
4370
1
                                 (MAXIMUM_SIZE_IN_UNITS -
4371
1
                                  (sMargins.nBottom + sMargins.nTop)) /
4372
1
                                 USER_UNIT_IN_INCH);
4373
1
                }
4374
3
                CPLDebug("PDF",
4375
3
                         "Adjusting DPI to %d so that page dimension in "
4376
3
                         "user units remain in what is accepted by Acrobat",
4377
3
                         static_cast<int>(dfDPI));
4378
3
            }
4379
3
        }
4380
0
        else
4381
0
        {
4382
0
            CPLError(CE_Warning, CPLE_AppDefined,
4383
0
                     "The page dimension in user units is %d x %d whereas the "
4384
0
                     "maximum allowed by Acrobat is %d x %d",
4385
0
                     static_cast<int>(dfWidthInUserUnit + 0.5),
4386
0
                     static_cast<int>(dfHeightInUserUnit + 0.5),
4387
0
                     MAXIMUM_SIZE_IN_UNITS, MAXIMUM_SIZE_IN_UNITS);
4388
0
        }
4389
3
    }
4390
4391
711
    if (dfDPI < DEFAULT_DPI)
4392
0
        dfDPI = DEFAULT_DPI;
4393
4394
711
    const char *pszClippingExtent =
4395
711
        CSLFetchNameValue(papszOptions, "CLIPPING_EXTENT");
4396
711
    int bUseClippingExtent = FALSE;
4397
711
    double adfClippingExtent[4] = {0.0, 0.0, 0.0, 0.0};
4398
711
    if (pszClippingExtent != nullptr)
4399
0
    {
4400
0
        char **papszTokens = CSLTokenizeString2(pszClippingExtent, ",", 0);
4401
0
        if (CSLCount(papszTokens) == 4)
4402
0
        {
4403
0
            bUseClippingExtent = TRUE;
4404
0
            adfClippingExtent[0] = CPLAtof(papszTokens[0]);
4405
0
            adfClippingExtent[1] = CPLAtof(papszTokens[1]);
4406
0
            adfClippingExtent[2] = CPLAtof(papszTokens[2]);
4407
0
            adfClippingExtent[3] = CPLAtof(papszTokens[3]);
4408
0
            if (adfClippingExtent[0] > adfClippingExtent[2] ||
4409
0
                adfClippingExtent[1] > adfClippingExtent[3])
4410
0
            {
4411
0
                CPLError(CE_Warning, CPLE_AppDefined,
4412
0
                         "Invalid value for CLIPPING_EXTENT. Should be "
4413
0
                         "xmin,ymin,xmax,ymax");
4414
0
                bUseClippingExtent = FALSE;
4415
0
            }
4416
4417
0
            if (bUseClippingExtent)
4418
0
            {
4419
0
                GDALGeoTransform gt;
4420
0
                if (poSrcDS->GetGeoTransform(gt) == CE_None)
4421
0
                {
4422
0
                    if (gt[2] != 0.0 || gt[4] != 0.0)
4423
0
                    {
4424
0
                        CPLError(CE_Warning, CPLE_AppDefined,
4425
0
                                 "Cannot use CLIPPING_EXTENT because main "
4426
0
                                 "raster has a rotated geotransform");
4427
0
                        bUseClippingExtent = FALSE;
4428
0
                    }
4429
0
                }
4430
0
                else
4431
0
                {
4432
0
                    CPLError(CE_Warning, CPLE_AppDefined,
4433
0
                             "Cannot use CLIPPING_EXTENT because main raster "
4434
0
                             "has no geotransform");
4435
0
                    bUseClippingExtent = FALSE;
4436
0
                }
4437
0
            }
4438
0
        }
4439
0
        CSLDestroy(papszTokens);
4440
0
    }
4441
4442
711
    const char *pszLayerName = CSLFetchNameValue(papszOptions, "LAYER_NAME");
4443
4444
711
    const char *pszExtraImages =
4445
711
        CSLFetchNameValue(papszOptions, "EXTRA_IMAGES");
4446
711
    const char *pszExtraStream =
4447
711
        CSLFetchNameValue(papszOptions, "EXTRA_STREAM");
4448
711
    const char *pszExtraLayerName =
4449
711
        CSLFetchNameValue(papszOptions, "EXTRA_LAYER_NAME");
4450
4451
711
    const char *pszOGRDataSource =
4452
711
        CSLFetchNameValue(papszOptions, "OGR_DATASOURCE");
4453
711
    const char *pszOGRDisplayField =
4454
711
        CSLFetchNameValue(papszOptions, "OGR_DISPLAY_FIELD");
4455
711
    const char *pszOGRDisplayLayerNames =
4456
711
        CSLFetchNameValue(papszOptions, "OGR_DISPLAY_LAYER_NAMES");
4457
711
    const char *pszOGRLinkField =
4458
711
        CSLFetchNameValue(papszOptions, "OGR_LINK_FIELD");
4459
711
    const bool bWriteOGRAttributes =
4460
711
        CPLFetchBool(papszOptions, "OGR_WRITE_ATTRIBUTES", true);
4461
4462
711
    const char *pszExtraRasters =
4463
711
        CSLFetchNameValue(papszOptions, "EXTRA_RASTERS");
4464
711
    const char *pszExtraRastersLayerName =
4465
711
        CSLFetchNameValue(papszOptions, "EXTRA_RASTERS_LAYER_NAME");
4466
4467
711
    const char *pszOffLayers = CSLFetchNameValue(papszOptions, "OFF_LAYERS");
4468
711
    const char *pszExclusiveLayers =
4469
711
        CSLFetchNameValue(papszOptions, "EXCLUSIVE_LAYERS");
4470
4471
711
    const char *pszJavascript = CSLFetchNameValue(papszOptions, "JAVASCRIPT");
4472
711
    const char *pszJavascriptFile =
4473
711
        CSLFetchNameValue(papszOptions, "JAVASCRIPT_FILE");
4474
4475
711
    if (!pfnProgress(0.0, nullptr, pProgressData))
4476
0
        return nullptr;
4477
4478
    /* -------------------------------------------------------------------- */
4479
    /*      Create file.                                                    */
4480
    /* -------------------------------------------------------------------- */
4481
711
    VSILFILE *fp = VSIFOpenL(pszFilename, "wb");
4482
711
    if (fp == nullptr)
4483
0
    {
4484
0
        CPLError(CE_Failure, CPLE_OpenFailed, "Unable to create PDF file %s.\n",
4485
0
                 pszFilename);
4486
0
        return nullptr;
4487
0
    }
4488
4489
711
    GDALPDFWriter oWriter(fp);
4490
4491
711
    GDALDataset *poClippingDS = poSrcDS;
4492
711
    if (bUseClippingExtent)
4493
0
        poClippingDS = new GDALPDFClippingDataset(poSrcDS, adfClippingExtent);
4494
4495
711
    if (CPLFetchBool(papszOptions, "WRITE_INFO", true))
4496
711
        oWriter.SetInfo(poSrcDS, papszOptions);
4497
711
    oWriter.SetXMP(poClippingDS, pszXMP);
4498
4499
711
    oWriter.StartPage(poClippingDS, dfDPI, bWriteUserUnit, pszGEO_ENCODING,
4500
711
                      pszNEATLINE, &sMargins, eStreamCompressMethod,
4501
711
                      pszOGRDataSource != nullptr && bWriteOGRAttributes);
4502
4503
711
    int bRet;
4504
4505
711
    if (!bUseClippingExtent)
4506
711
    {
4507
711
        bRet = oWriter.WriteImagery(poSrcDS, pszLayerName, eCompressMethod,
4508
711
                                    nPredictor, nJPEGQuality,
4509
711
                                    pszJPEG2000_DRIVER, nBlockXSize,
4510
711
                                    nBlockYSize, pfnProgress, pProgressData);
4511
711
    }
4512
0
    else
4513
0
    {
4514
0
        bRet = oWriter.WriteClippedImagery(
4515
0
            poSrcDS, pszLayerName, eCompressMethod, nPredictor, nJPEGQuality,
4516
0
            pszJPEG2000_DRIVER, nBlockXSize, nBlockYSize, pfnProgress,
4517
0
            pProgressData);
4518
0
    }
4519
4520
711
    char **papszExtraRasters =
4521
711
        CSLTokenizeString2(pszExtraRasters ? pszExtraRasters : "", ",", 0);
4522
711
    char **papszExtraRastersLayerName = CSLTokenizeString2(
4523
711
        pszExtraRastersLayerName ? pszExtraRastersLayerName : "", ",", 0);
4524
711
    int bUseExtraRastersLayerName =
4525
711
        (CSLCount(papszExtraRasters) == CSLCount(papszExtraRastersLayerName));
4526
711
    int bUseExtraRasters = TRUE;
4527
4528
711
    const char *pszClippingProjectionRef = poSrcDS->GetProjectionRef();
4529
711
    if (CSLCount(papszExtraRasters) != 0)
4530
0
    {
4531
0
        GDALGeoTransform gt;
4532
0
        if (poSrcDS->GetGeoTransform(gt) == CE_None)
4533
0
        {
4534
0
            if (gt[2] != 0.0 || gt[4] != 0.0)
4535
0
            {
4536
0
                CPLError(CE_Warning, CPLE_AppDefined,
4537
0
                         "Cannot use EXTRA_RASTERS because main raster has a "
4538
0
                         "rotated geotransform");
4539
0
                bUseExtraRasters = FALSE;
4540
0
            }
4541
0
        }
4542
0
        else
4543
0
        {
4544
0
            CPLError(CE_Warning, CPLE_AppDefined,
4545
0
                     "Cannot use EXTRA_RASTERS because main raster has no "
4546
0
                     "geotransform");
4547
0
            bUseExtraRasters = FALSE;
4548
0
        }
4549
0
        if (bUseExtraRasters && (pszClippingProjectionRef == nullptr ||
4550
0
                                 pszClippingProjectionRef[0] == '\0'))
4551
0
        {
4552
0
            CPLError(CE_Warning, CPLE_AppDefined,
4553
0
                     "Cannot use EXTRA_RASTERS because main raster has no "
4554
0
                     "projection");
4555
0
            bUseExtraRasters = FALSE;
4556
0
        }
4557
0
    }
4558
4559
711
    for (int i = 0; bRet && bUseExtraRasters && papszExtraRasters[i] != nullptr;
4560
711
         i++)
4561
0
    {
4562
0
        auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
4563
0
            papszExtraRasters[i], GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
4564
0
            nullptr, nullptr, nullptr));
4565
0
        if (poDS != nullptr)
4566
0
        {
4567
0
            GDALGeoTransform gt;
4568
0
            int bUseRaster = TRUE;
4569
0
            if (poDS->GetGeoTransform(gt) == CE_None)
4570
0
            {
4571
0
                if (gt[2] != 0.0 || gt[4] != 0.0)
4572
0
                {
4573
0
                    CPLError(
4574
0
                        CE_Warning, CPLE_AppDefined,
4575
0
                        "Cannot use %s because it has a rotated geotransform",
4576
0
                        papszExtraRasters[i]);
4577
0
                    bUseRaster = FALSE;
4578
0
                }
4579
0
            }
4580
0
            else
4581
0
            {
4582
0
                CPLError(CE_Warning, CPLE_AppDefined,
4583
0
                         "Cannot use %s because it has no geotransform",
4584
0
                         papszExtraRasters[i]);
4585
0
                bUseRaster = FALSE;
4586
0
            }
4587
0
            const char *pszProjectionRef = poDS->GetProjectionRef();
4588
0
            if (bUseRaster &&
4589
0
                (pszProjectionRef == nullptr || pszProjectionRef[0] == '\0'))
4590
0
            {
4591
0
                CPLError(CE_Warning, CPLE_AppDefined,
4592
0
                         "Cannot use %s because it has no projection",
4593
0
                         papszExtraRasters[i]);
4594
0
                bUseRaster = FALSE;
4595
0
            }
4596
0
            if (bUseRaster)
4597
0
            {
4598
0
                if (pszClippingProjectionRef != nullptr &&
4599
0
                    pszProjectionRef != nullptr &&
4600
0
                    !EQUAL(pszClippingProjectionRef, pszProjectionRef))
4601
0
                {
4602
0
                    OGRSpatialReferenceH hClippingSRS =
4603
0
                        OSRNewSpatialReference(pszClippingProjectionRef);
4604
0
                    OGRSpatialReferenceH hSRS =
4605
0
                        OSRNewSpatialReference(pszProjectionRef);
4606
0
                    if (!OSRIsSame(hClippingSRS, hSRS))
4607
0
                    {
4608
0
                        CPLError(CE_Warning, CPLE_AppDefined,
4609
0
                                 "Cannot use %s because it has a different "
4610
0
                                 "projection than main dataset",
4611
0
                                 papszExtraRasters[i]);
4612
0
                        bUseRaster = FALSE;
4613
0
                    }
4614
0
                    OSRDestroySpatialReference(hClippingSRS);
4615
0
                    OSRDestroySpatialReference(hSRS);
4616
0
                }
4617
0
            }
4618
0
            if (bUseRaster)
4619
0
            {
4620
0
                bRet = oWriter.WriteClippedImagery(
4621
0
                    poDS.get(),
4622
0
                    bUseExtraRastersLayerName ? papszExtraRastersLayerName[i]
4623
0
                                              : nullptr,
4624
0
                    eCompressMethod, nPredictor, nJPEGQuality,
4625
0
                    pszJPEG2000_DRIVER, nBlockXSize, nBlockYSize, nullptr,
4626
0
                    nullptr);
4627
0
            }
4628
0
        }
4629
0
    }
4630
4631
711
    CSLDestroy(papszExtraRasters);
4632
711
    CSLDestroy(papszExtraRastersLayerName);
4633
4634
711
    if (bRet && pszOGRDataSource != nullptr)
4635
0
        oWriter.WriteOGRDataSource(pszOGRDataSource, pszOGRDisplayField,
4636
0
                                   pszOGRDisplayLayerNames, pszOGRLinkField,
4637
0
                                   bWriteOGRAttributes);
4638
4639
711
    if (bRet)
4640
494
        oWriter.EndPage(pszExtraImages, pszExtraStream, pszExtraLayerName,
4641
494
                        pszOffLayers, pszExclusiveLayers);
4642
4643
711
    if (pszJavascript)
4644
0
        oWriter.WriteJavascript(pszJavascript);
4645
711
    else if (pszJavascriptFile)
4646
0
        oWriter.WriteJavascriptFile(pszJavascriptFile);
4647
4648
711
    oWriter.Close();
4649
4650
711
    if (poClippingDS != poSrcDS)
4651
0
        delete poClippingDS;
4652
4653
711
    if (!bRet)
4654
217
    {
4655
217
        VSIUnlink(pszFilename);
4656
217
        return nullptr;
4657
217
    }
4658
494
    else
4659
494
    {
4660
494
#ifdef HAVE_PDF_READ_SUPPORT
4661
494
        GDALDataset *poDS = GDALPDFOpen(pszFilename, GA_ReadOnly);
4662
494
        if (poDS == nullptr)
4663
0
            return nullptr;
4664
494
        char **papszMD = CSLDuplicate(poSrcDS->GetMetadata());
4665
494
        papszMD = CSLMerge(papszMD, poDS->GetMetadata());
4666
494
        const char *pszAOP = CSLFetchNameValue(papszMD, GDALMD_AREA_OR_POINT);
4667
494
        if (pszAOP != nullptr && EQUAL(pszAOP, GDALMD_AOP_AREA))
4668
1
            papszMD = CSLSetNameValue(papszMD, GDALMD_AREA_OR_POINT, nullptr);
4669
494
        poDS->SetMetadata(papszMD);
4670
494
        if (EQUAL(pszGEO_ENCODING, "NONE"))
4671
0
        {
4672
0
            GDALGeoTransform gt;
4673
0
            if (poSrcDS->GetGeoTransform(gt) == CE_None)
4674
0
            {
4675
0
                poDS->SetGeoTransform(gt);
4676
0
            }
4677
0
            const char *pszProjectionRef = poSrcDS->GetProjectionRef();
4678
0
            if (pszProjectionRef != nullptr && pszProjectionRef[0] != '\0')
4679
0
            {
4680
0
                poDS->SetProjection(pszProjectionRef);
4681
0
            }
4682
0
        }
4683
494
        CSLDestroy(papszMD);
4684
494
        return poDS;
4685
#else
4686
        return new GDALFakePDFDataset();
4687
#endif
4688
494
    }
4689
711
}