summaryrefslogtreecommitdiff
path: root/devices/vector/gdevdocxw.c
blob: f8ed2b2668e8bcd82ef45ee1f671ee640f6a95bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/

/* Device for creating docx files. */

#include "memory_.h"
#include "string_.h"
#include "gp.h"			/* for gp_file_name_sizeof */
#include "gx.h"
#include "gserrors.h"
#include "gsparam.h"
#include "gsutil.h"
#include "gxdevice.h"
#include "gsdevice.h"		/* requires gsmatrix.h */
#include "gxfont.h"
#include "gxfont0.h"
#include "gstext.h"
#include "gxfcid.h"
#include "gxgstate.h"
#include "gxpath.h"
#include "gsagl.h"
#include "gxdevsop.h"
#include "gzpath.h"
#include "gdevkrnlsclass.h" /* 'standard' built in subclasses, currently First/Last Page and obejct filter */
#include "gxchar.h"

#include "doc_common.h"

#include "extract/extract.h"
#include "extract/buffer.h"

#include <errno.h>

#define outf(format, ...)
#define outfx(format, ...)

#ifdef __unix__
#if 0

/* Enable logging using outf() macro/function. */
#include <unistd.h>

static void (outf)(const char* file, int line, const char* function, const char* format, ...)
{
    static char buffer[2048];
    size_t prefix;
    va_list va;
    snprintf(buffer, sizeof(buffer), "%s:%i:%s(): ", file, line, function);
    prefix = strlen(buffer);
    va_start(va, format);
    vsnprintf(buffer + prefix, sizeof(buffer) - 1 - prefix, format, va);
    va_end(va);
    size_t l = strlen(buffer);
    buffer[l] = '\n';
    buffer[l+1] = 0;
    write(2, buffer, l+1);
}

#undef outf
#define outf(format, ...)  outf(__FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)

#endif
#endif



extern single_glyph_list_t SingleGlyphList[];
extern double_glyph_list_t DoubleGlyphList[];
extern treble_glyph_list_t TrebleGlyphList[];
extern quad_glyph_list_t QuadGlyphList[];

/* Structure to record the Unicode characters, the total width of the text
 * recorded, and various useful attributes such as the font, size, colour
 * rendering mode, writing mode etc. These are stored as a series of x-ordered
 * entries in a list, using the starting x co-ordinate.
 */
typedef struct docx_list_entry_t {
    struct text_list_entry_s *previous;
    struct text_list_entry_s *next;
    gs_matrix matrix;		/* Tm et al */
    char *FontName;
    int wmode;			/* WMode of font */
    double size;
} docx_list_entry_t;

/* The custom sub-classed device structure */
typedef struct {
    gx_device_common;
    char fname[gp_file_name_sizeof];	/* OutputFile */
    gp_file *file;
    int TextFormat;
    extract_alloc_t *alloc;
    extract_t *extract;
    int file_per_page;
} gx_device_docxwrite_t;


/* Device procedures */
static dev_proc_open_device(docxwrite_open_device);
static dev_proc_close_device(docxwrite_close_device);
static dev_proc_output_page(docxwrite_output_page);
static dev_proc_fill_rectangle(docxwrite_fill_rectangle);
static dev_proc_get_params(docxwrite_get_params);
static dev_proc_put_params(docxwrite_put_params);
static dev_proc_fill_path(docxwrite_fill_path);
static dev_proc_stroke_path(docxwrite_stroke_path);
static dev_proc_text_begin(docxwrite_text_begin);
static dev_proc_strip_copy_rop2(docxwrite_strip_copy_rop2);
static dev_proc_dev_spec_op(docxwrite_dev_spec_op);


/* The device prototype */
#define X_DPI 72
#define Y_DPI 72

/* Define the text enumerator. */
typedef struct {
    gs_text_enum_common;
    gs_text_enum_t *pte_fallback;
    double d1_width;
    bool d1_width_set;
    bool charproc_accum;
    bool cdevproc_callout;
    double cdevproc_result[10];
    docx_list_entry_t *text_state;
} docxw_text_enum_t;
#define private_st_textw_text_enum()\
  extern_st(st_gs_text_enum);\
  gs_private_st_suffix_add0(st_textw_text_enum, docxw_text_enum_t,\
    "docxw_text_enum_t", textw_text_enum_enum_ptrs, textw_text_enum_reloc_ptrs,\
    st_gs_text_enum)

private_st_textw_text_enum();

static void
docxwrite_initialize_device_procs(gx_device *dev)
{
    set_dev_proc(dev, open_device, docxwrite_open_device);
    set_dev_proc(dev, output_page, docxwrite_output_page);
    set_dev_proc(dev, close_device, docxwrite_close_device);
    set_dev_proc(dev, fill_rectangle, docxwrite_fill_rectangle);
    set_dev_proc(dev, get_params, docxwrite_get_params);
    set_dev_proc(dev, put_params, docxwrite_put_params);
    set_dev_proc(dev, get_page_device, gx_page_device_get_page_device);
    set_dev_proc(dev, fill_path, docxwrite_fill_path);
    set_dev_proc(dev, stroke_path, docxwrite_stroke_path);
    set_dev_proc(dev, strip_copy_rop2, docxwrite_strip_copy_rop2);
    set_dev_proc(dev, composite, gx_null_composite);
    set_dev_proc(dev, text_begin, docxwrite_text_begin);
    set_dev_proc(dev, dev_spec_op, docxwrite_dev_spec_op);
}

const gx_device_docxwrite_t gs_docxwrite_device =
{
    /* Define the device as 8-bit gray scale to avoid computing halftones. */
    std_device_dci_body(gx_device_docxwrite_t,
                        docxwrite_initialize_device_procs,
                        "docxwrite",
                        DEFAULT_WIDTH_10THS * X_DPI / 10,
                        DEFAULT_HEIGHT_10THS * Y_DPI / 10,
                        X_DPI, Y_DPI,
                        1, 8, 255, 0, 256, 1),
    { 0 },                      /* procs */
    { 0 },			/* Page Data */
    0,				/* Output FILE * */
    0,				/* TextFormat */
    NULL,           /* alloc */
    NULL,			/* extract */
    0               /* file_per_page */
};


static const gs_param_item_t docx_param_items[] = {
#define pi(key, type, memb) { key, type, offset_of(gx_device_docxwrite_t, memb) }
    pi("TextFormat", gs_param_type_int, TextFormat),
#undef pi
    gs_param_item_end
};

/* ---------------- Open/close/page ---------------- */


static void *s_extract_realloc_fn(void *state, void *ptr, size_t newsize)
{
    /* We have to go to some effort to behave like realloc() when we only have
    make gs_malloc() and gs_free() available. We store each allocated block's
    allocation size in the preceding size_t. */
    gs_memory_t *mem = state;
    size_t *oldbase = (ptr) ? (size_t*) ptr - 1 : NULL;
    size_t oldsize = (oldbase) ? *oldbase : 0;
    size_t *newbase = NULL;
    if (newsize) {
        /* malloc() or realloc() - allocate new buffer.  */
        newbase = gs_malloc(mem, sizeof(size_t) + newsize /*nelts*/, 1 /*esize*/, "extract");
        if (newbase) {
            *newbase = newsize;
            if (oldbase) {
                /* Realloc, so copy across. */
                size_t minsize = (newsize < oldsize) ? newsize : oldsize;
                memcpy(newbase+1, oldbase+1, minsize);
            }
        }
    }
    if (oldbase && (!newsize || newbase)) {
        /* free() or successful realloc() - free the old buffer. */
        gs_free(mem, oldbase, sizeof(size_t) + oldsize /*nelts*/, 1 /*esize*/, "extract");
    }
    return (newbase) ? newbase + 1 : NULL;
}

static int s_errno_to_gs(void)
{
    if (errno == EPERM) return gs_error_invalidaccess;
    if (errno == ENOENT) return gs_error_invalidfileaccess;
    if (errno == ESRCH) {}
    if (errno == EINTR) {}
    if (errno == EIO) return gs_error_ioerror;
    if (errno == ENXIO) {}

    return gs_error_unknownerror;
}

static int
docxwrite_open_device(gx_device * dev)
{
    gx_device_docxwrite_t * tdev = (gx_device_docxwrite_t *) dev;
    const char* fmt = NULL;
    gs_parsed_file_name_t parsed;
    int code = 0;

    if (tdev->extract) {
        /* We can be called multiple times; nothing to do after the first time.
        */
        return 0;
    }

    gx_device_fill_in_procs(dev);
    if (tdev->fname[0] == 0)
        return_error(gs_error_undefinedfilename);

    tdev->file = NULL;
    dev->color_info.separable_and_linear = GX_CINFO_SEP_LIN;
    set_linear_color_bits_mask_shift(dev);
    dev->interpolate_control = 0;
    dev->non_strict_bounds = 0;

    tdev->alloc = NULL;
    tdev->extract = NULL;

    code = gx_parse_output_file_name(&parsed, &fmt, tdev->fname, strlen(tdev->fname), tdev->memory);
    if (code < 0) goto end;
    tdev->file_per_page = (fmt) ? 1 : 0;

    if (extract_alloc_create(s_extract_realloc_fn, tdev->memory, &tdev->alloc)) {
        code = s_errno_to_gs();
        goto end;
    }
    extract_alloc_exp_min(tdev->alloc, 64);

    if (extract_begin(tdev->alloc, extract_format_DOCX, &tdev->extract)) {
        code = s_errno_to_gs();
        goto end;
    }
    /* Pass dummy page bbox for now; our simple use of extract ignores it. */
    if (extract_page_begin(tdev->extract, 0, 0, 0, 0)) {
        code = s_errno_to_gs();
        goto end;
    }
    code = install_internal_subclass_devices((gx_device **)&dev, NULL);

    end:

    if (code < 0) {
        extract_alloc_destroy(&tdev->alloc);
        extract_end(&tdev->extract);
    }
    return code;
}

/* For use with an extract_buffer_t - writes directly to tdev->file. */
static int docxwrite_extract_buffer_write(void* handle, const void* source, size_t numbytes, size_t* o_actual)
{
    int n;
    gx_device_docxwrite_t *tdev = handle;
    n = gp_fwrite(source, 1 /*size*/, numbytes /*count*/, tdev->file);
    if (n < 0) return s_errno_to_gs();
    *o_actual = n;
    return 0;
}

/* Finishes processing for page.

If <write_file> is true, we write to file; e.g. set if we are writing a file
per page, or if we are being called from docxwrite_close_device(). */
static int
s_end_page(gx_device_docxwrite_t *tdev, int write_file)
{
    int code = 0;
    extract_buffer_t *buffer = NULL;

    if (!tdev->extract) {
        /* Nothing to do. */
        return 0;
    }
    if (extract_page_end(tdev->extract)) {
        code = s_errno_to_gs();
        goto end;
    }
    if (extract_process(tdev->extract, 0 /*spacing*/, 1 /*rotation*/, 1 /*images*/)) {
        code = s_errno_to_gs();
        goto end;
    }
    if (write_file) {
        code = gx_device_open_output_file((gx_device*) tdev, tdev->fname, true, false, &tdev->file);
        if (code) goto end;
        /* extract_write() can only write to an extract_buffer_t, so we create one that
        writes to tdev->file. */
        if (extract_buffer_open(
                tdev->alloc,
                tdev,
                NULL /*fn_read*/,
                docxwrite_extract_buffer_write,
                NULL /*cache*/,
                NULL /*fn_close*/,
                &buffer
                )) {
            code = s_errno_to_gs();
            goto end;
        }
        if (extract_write(tdev->extract, buffer)) {
            code = s_errno_to_gs();
            goto end;
        }
    }

    end:
    extract_buffer_close(&buffer);
    if (tdev->file) {
        gx_device_close_output_file((gx_device*) tdev, tdev->fname, tdev->file);
        tdev->file = NULL;
    }
    return code;
}

static int
docxwrite_close_device(gx_device * dev)
{
    int code = 0;
    gx_device_docxwrite_t *const tdev = (gx_device_docxwrite_t *) dev;
    if (!tdev->file_per_page)
    {
        s_end_page(tdev, true /*write_file*/);
    }
    extract_end(&tdev->extract);
    extract_alloc_destroy(&tdev->alloc);
    return code;
}

static int
docxwrite_output_page(gx_device * dev, int num_copies, int flush)
{
    int code;
    gx_device_docxwrite_t *tdev = (gx_device_docxwrite_t *) dev;

    s_end_page(tdev, tdev->file_per_page /*write_file*/);

    if (tdev->file_per_page) {
        /* Create a new extract_t for the next page. */
        extract_end(&tdev->extract);
        if (extract_begin(tdev->alloc, extract_format_DOCX, &tdev->extract)) {
            code = s_errno_to_gs();
            goto end;
        }
    }
    /* Pass dummy page bbox for now; our simple use of extract ignores it. */
    if (extract_page_begin(tdev->extract, 0, 0, 0, 0)) {
        code = s_errno_to_gs();
        goto end;
    }

    code =  gx_default_output_page(dev, num_copies, flush);
    if (code < 0)
        goto end;

    end:
    return code;
}

/* ---------------- Low-level drawing ---------------- */

static int
docxwrite_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
                    gx_color_index color)
{
    return 0;
}

/*static int
docxwrite_copy_alpha(gx_device * dev, const byte * data, int data_x,
                int raster, gx_bitmap_id id, int x, int y, int w, int h,
                gx_color_index color, int depth)
{
    return 0;
}

static int
docxwrite_copy_mono(gx_device * dev, const byte * data, int dx, int raster,
               gx_bitmap_id id, int x, int y, int w, int h,
               gx_color_index zero, gx_color_index one)
{
    return 0;
}
static int
docxwrite_copy_color(gx_device * dev, const byte * data,
                int data_x, int raster, gx_bitmap_id id,
                int x, int y, int width, int height)
{
    return 0;
}

static int
docxwrite_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
   int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
                          int px, int py)
{
    return 0;
}

static int
docxwrite_strip_copy_rop2(gx_device * dev,
                    const byte * sdata, int sourcex, uint sraster,
                    gx_bitmap_id id,
                    const gx_color_index * scolors,
                    const gx_strip_bitmap * textures,
                    const gx_color_index * tcolors,
                    int x, int y, int w, int h,
                    int phase_x, int phase_y, gs_logical_operation_t lop,
                    uint plane_height)
{
    return 0;
}*/

/* ---------------- Parameters ---------------- */

static int docxwrite_get_param(gx_device *dev, char *Param, void *list)
{
    gx_device_docxwrite_t *const tdev = (gx_device_docxwrite_t *) dev;
    gs_param_list * plist = (gs_param_list *)list;
    bool bool_T = true;

    if (strcmp(Param, "OutputFile") == 0) {
        gs_param_string ofns;

        ofns.data = (const byte *)tdev->fname,
        ofns.size = strlen(tdev->fname),
        ofns.persistent = false;
        return param_write_string(plist, "OutputFile", &ofns);
    }
    if (strcmp(Param, "WantsToUnicode") == 0) {
        return param_write_bool(plist, "WantsToUnicode", &bool_T);
    }
    if (strcmp(Param, "PreserveTrMode") == 0) {
        return param_write_bool(plist, "PreserveTrMode", &bool_T);
    }
    if (strcmp(Param, "HighLevelDevice") == 0) {
        return param_write_bool(plist, "HighLevelDevice", &bool_T);
    }
    return_error(gs_error_undefined);
}

static int
docxwrite_get_params(gx_device * dev, gs_param_list * plist)
{
    int code;
    bool bool_T = true;
    gs_param_string ofns;
    gx_device_docxwrite_t *const tdev = (gx_device_docxwrite_t *) dev;

    code = gx_default_get_params(dev, plist);
    if (code < 0)
        return code;

    ofns.data = (const byte *)tdev->fname,
    ofns.size = strlen(tdev->fname),
    ofns.persistent = false;
    code = param_write_string(plist, "OutputFile", &ofns);
    if (code < 0)
        return code;

    code = param_write_bool(plist, "WantsToUnicode", &bool_T);
    if (code < 0)
        return code;

    code = param_write_bool(plist, "PreserveTrMode", &bool_T);
    if (code < 0)
        return code;

    code = param_write_bool(plist, "HighLevelDevice", &bool_T);
    if (code < 0)
        return code;

   code = gs_param_write_items(plist, tdev, NULL, docx_param_items);
   return code;
}

/* We implement put_params to ensure that we keep the important */
/* device parameters up to date, and to prevent an /undefined error */
static int
docxwrite_put_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_docxwrite_t *tdev = (gx_device_docxwrite_t *) dev;
    int ecode = 0;
    int code, old_TextFormat = tdev->TextFormat;
    const char *param_name;
    gs_param_string ofs;
    bool dummy, open = dev->is_open;

    switch (code = param_read_string(plist, (param_name = "OutputFile"), &ofs)) {
        case 0:
            if (dev->LockSafetyParams &&
                    bytes_compare(ofs.data, ofs.size,
                        (const byte *)tdev->fname, strlen(tdev->fname))) {
                ecode = gs_note_error(gs_error_invalidaccess);
                goto ofe;
            }
            if (ofs.size >= gp_file_name_sizeof)
                ecode = gs_error_limitcheck;
            else
                break;
            goto ofe;
        default:
            ecode = code;
          ofe:param_signal_error(plist, param_name, ecode);
        /* fall through */
        case 1:
            ofs.data = 0;
            break;
    }

    if (ecode < 0)
        return ecode;

    code = param_read_int(plist, "TextFormat", &tdev->TextFormat);
    if (code < 0)
        return code;

    code = param_read_bool(plist, "WantsToUnicode", &dummy);
    if (code < 0)
        return code;

    code = param_read_bool(plist, "HighLevelDevice", &dummy);
    if (code < 0)
        return code;

    code = param_read_bool(plist, "PreserveTrMode", &dummy);
    if (code < 0)
        return code;

    if (ofs.data != 0) {	/* Close the file if it's open. */
        if (0 && tdev->file != 0) {
            gp_fclose(tdev->file);
            tdev->file = 0;
            outf("have closed tdef->file. tdev=%p", tdev);
        }
        memcpy(tdev->fname, ofs.data, ofs.size);
        tdev->fname[ofs.size] = 0;
    }

    /* If we change media size then gs_default_put_params will close
     * the device if it is open. We don't want it to do that, so set
     * the device's 'is_open' flag to false, and reset it after we've
     * processed the params.
     */
    if (old_TextFormat == tdev->TextFormat && open)
        dev->is_open = false;

    code = gx_default_put_params(dev, plist);
    if (code < 0)
        return code;

    dev->is_open = open;

    dev->interpolate_control = 0;
    dev->non_strict_bounds = 0;

    return 0;
}

/* ---------------- Polygon drawing ---------------- */

/*static int
docxwrite_fill_trapezoid(gx_device * dev,
                    const gs_fixed_edge * left, const gs_fixed_edge * right,
                    fixed ybot, fixed ytop, bool swap_axes,
                    const gx_device_color * pdevc, gs_logical_operation_t lop)
{
    return 0;
}

static int
docxwrite_fill_parallelogram(gx_device * dev,
                        fixed px, fixed py, fixed ax, fixed ay,
                        fixed bx, fixed by, const gx_device_color * pdevc,
                        gs_logical_operation_t lop)
{
    return 0;
}

static int
docxwrite_fill_triangle(gx_device * dev,
                   fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
                   const gx_device_color * pdevc, gs_logical_operation_t lop)
{
    return 0;
}

static int
docxwrite_draw_thin_line(gx_device * dev,
                    fixed fx0, fixed fy0, fixed fx1, fixed fy1,
                    const gx_device_color * pdevc, gs_logical_operation_t lop,
                    fixed adjustx, fixed adjusty)
{
    return 0;
}*/

/* ---------------- High-level drawing ---------------- */

static int
docxwrite_fill_path(gx_device * dev, const gs_gstate * pgs, gx_path * ppath,
               const gx_fill_params * params, const gx_device_color * pdevc,
               const gx_clip_path * pcpath)
{
        return 0;
}

static int
docxwrite_stroke_path(gx_device * dev, const gs_gstate * pgs, gx_path * ppath,
                 const gx_stroke_params * params,
                 const gx_drawing_color * pdevc, const gx_clip_path * pcpath)
{
    return 0;
}


/*
 * Compute the cached values in the text processing state from the text
 * parameters, current_font, and pgs->ctm.  Return either an error code (<
 * 0) or a mask of operation attributes that the caller must emulate.
 * Currently the only such attributes are TEXT_ADD_TO_ALL_WIDTHS and
 * TEXT_ADD_TO_SPACE_WIDTH.  Note that this procedure fills in all the
 * values in ppts->values, not just the ones that need to be set now.
 */
static int
transform_delta_inverse(const gs_point *pdelta, const gs_matrix *pmat,
                        gs_point *ppt)
{
    int code = gs_distance_transform_inverse(pdelta->x, pdelta->y, pmat, ppt);
    gs_point delta;

    if (code < 0)
        return code;
    if (ppt->y == 0)
        return 0;
    /* Check for numerical fuzz. */
    code = gs_distance_transform(ppt->x, 0.0, pmat, &delta);
    if (code < 0)
        return 0;                /* punt */
    if (fabs(delta.x - pdelta->x) < 0.01 && fabs(delta.y - pdelta->y) < 0.01) {
        /* Close enough to y == 0: device space error < 0.01 pixel. */
        ppt->y = 0;
    }
    return 0;
}

static int
docx_update_text_state(docx_list_entry_t *ppts,
                      const docxw_text_enum_t *penum,
                      gs_font *ofont, const gs_matrix *pfmat)
{
    gx_device *const pdev = penum->dev;
    gs_font *font = penum->current_font;
    gs_fixed_point cpt;
    gs_matrix smat, tmat;
    float size;
    int mask = 0;
    int code = gx_path_current_point(gs_text_enum_path(penum), &cpt);

    if (code < 0)
        return code;

    size = txt_calculate_text_size(penum->pgs, ofont, pfmat, &smat, &tmat, penum->current_font, pdev);
    /* Check for spacing parameters we can handle, and transform them. */

    if (penum->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
        if (penum->current_font->WMode == 0) {
            gs_point pt;

            code = transform_delta_inverse(&penum->text.delta_all, &smat, &pt);
            if (code < 0 || pt.y != 0)
                mask |= TEXT_ADD_TO_ALL_WIDTHS;
        }
        else
            mask |= TEXT_ADD_TO_ALL_WIDTHS;
    }

    if (penum->text.operation & TEXT_ADD_TO_SPACE_WIDTH) {
        gs_point pt;

        code = transform_delta_inverse(&penum->text.delta_space, &smat, &pt);
        if (code < 0 || pt.y != 0 || penum->text.space.s_char != 32)
            mask |= TEXT_ADD_TO_SPACE_WIDTH;
    }
    /* Store the updated values. */

    tmat.xx /= size;
    tmat.xy /= size;
    tmat.yx /= size;
    tmat.yy /= size;
    tmat.tx += fixed2float(cpt.x);
    tmat.ty += fixed2float(cpt.y);

    ppts->size = size;
    ppts->matrix = tmat;
    gs_free_object(pdev->memory->non_gc_memory, ppts->FontName, "txtwrite alloc font name");
    ppts->FontName = (char *)gs_malloc(pdev->memory, 1,
        font->font_name.size + 1, "txtwrite alloc font name");
    if (!ppts->FontName)
        return gs_note_error(gs_error_VMerror);
    memcpy(ppts->FontName, font->font_name.chars, font->font_name.size);
    ppts->FontName[font->font_name.size] = 0;

    if (font->PaintType == 2 && penum->pgs->text_rendering_mode == 0)
    {
        gs_gstate *pgs = penum->pgs;
        gs_font *font = penum->current_font;
        double scaled_width = font->StrokeWidth != 0 ? font->StrokeWidth : 0.001;
        double saved_width = pgs->line_params.half_width;
        /*
         * See stream_to_text in gdevpdfu.c re the computation of
         * the scaling value.
         */
        double scale = 72.0 / pdev->HWResolution[1];

        if (font->FontMatrix.yy != 0)
            scaled_width *= fabs(font->orig_FontMatrix.yy) * size * tmat.yy * scale;
        else
            scaled_width *= fabs(font->orig_FontMatrix.xy) * size * tmat.xy * scale;

        pgs->line_params.half_width = scaled_width / 2;
        if (code < 0)
            return code;

        pgs->line_params.half_width = saved_width;
    }
    return (code < 0 ? code : mask);
}

/* Simple routine to update the current point by the accumulated width of the
 * text.
 */
static int
docx_shift_text_currentpoint(docxw_text_enum_t *penum, gs_point *wpt)
{
    return gs_moveto_aux(penum->pgs, gx_current_path(penum->pgs),
                              fixed2float(penum->origin.x) + wpt->x,
                              fixed2float(penum->origin.y) + wpt->y);
}

static int font_final(gx_device_docxwrite_t *tdev, docxw_text_enum_t *penum)
{
    gs_text_enum_t* pte = (void*) penum;
    gs_font_base *font_base = (gs_font_base *)pte->current_font;
    int code;

    penum->d1_width = 0;
    penum->d1_width_set = false;
    if (font_base->FontBBox.p.x != font_base->FontBBox.q.x ||
        font_base->FontBBox.p.y != font_base->FontBBox.q.y) {
        gs_point p0, p1, p2, p3;
        gs_matrix m;

        m = ctm_only(pte->pgs);
        m.tx = m.ty = fixed2float(0);
        gs_matrix_multiply(&font_base->FontMatrix, &m, &m);
        gs_point_transform(font_base->FontBBox.p.x, font_base->FontBBox.p.y, &m, &p0);
        gs_point_transform(font_base->FontBBox.p.x, font_base->FontBBox.q.y, &m, &p1);
        gs_point_transform(font_base->FontBBox.q.x, font_base->FontBBox.p.y, &m, &p2);
        gs_point_transform(font_base->FontBBox.q.x, font_base->FontBBox.q.y, &m, &p3);
    }
    code = docx_shift_text_currentpoint(penum, &penum->returned.total_width);
    return code;
}


/* Routines to enumerate each glyph/character code in turn and sent to
 * extract_add_char().
 */

static int
docxwrite_process_cmap_text(gx_device_docxwrite_t *tdev, gs_text_enum_t *pte)
{
    docxw_text_enum_t *const penum = (docxw_text_enum_t *)pte;
    unsigned int rcode = 0;
    gs_text_enum_t scan = *(gs_text_enum_t *)pte;
    int i;
    gs_point at = { 0, 0 };

    /* Composite font using a CMap */

    for (i=0; ; ++i) {
        const char* prevFontName;

        gs_glyph glyph;
        int font_code, code;
        gs_font *subfont;
        gs_char chr;
        txt_glyph_widths_t widths;
        gs_matrix m3;
        gs_point wanted;	/* user space */
        gs_point dpt = {0,0};

        font_code = scan.orig_font->procs.next_char_glyph
                (&scan, &chr, &glyph);

        subfont = scan.fstack.items[scan.fstack.depth].font;

        if (font_code == 2) {
            /* end of string */
            return 0;
        }
        if (font_code != 0 && font_code != 1) {
            /* error */
            return font_code;
        }

        if (font_code == 1) {
            /* font change */
            if (i) abort();
        }

        code = txt_glyph_widths(subfont, scan.orig_font->WMode, glyph, (gs_font *)subfont, &widths,
                penum->cdevproc_callout ? penum->cdevproc_result : NULL);
        if (code == TEXT_PROCESS_CDEVPROC) {
            penum->cdevproc_callout = true;
            pte->returned.current_glyph = glyph;
            scan.returned.current_glyph = glyph;
            pte->current_font = subfont;
            scan.current_font = subfont;
            rcode = TEXT_PROCESS_CDEVPROC;
            break;
        }
        else {
            penum->cdevproc_callout = false;
            pte->index = scan.index;
        }
        code = gs_matrix_multiply(&subfont->FontMatrix, &pte->orig_font->FontMatrix, &m3);
        if (code < 0) {
            outf("returning code=%i", code);
            return code;
        }
        prevFontName = penum->text_state->FontName;
        code = docx_update_text_state(penum->text_state, (docxw_text_enum_t *)pte, subfont, &m3);
        if (code < 0) {
            outf("returning code=%i", code);
            return code;
        }
        if (!prevFontName && penum->text_state->FontName) {
            gs_rect *fbbox = &((gs_font_base *)subfont)->FontBBox;
            gs_matrix fm = *&((gs_font_base *)subfont)->FontMatrix;
            gs_rect bbox;

            code = gs_bbox_transform(fbbox, &fm, &bbox);
            if (code < 0)
                return code;

            if (extract_span_begin(
                    tdev->extract,
                    penum->text_state->FontName,
                    0 /*font_bold*/,
                    0 /*font_italic*/,
                    penum->text_state->wmode,
                    penum->pgs->ctm.xx,
                    penum->pgs->ctm.xy,
                    penum->pgs->ctm.yx,
                    penum->pgs->ctm.yy,
                    bbox.p.x,
                    bbox.p.y,
                    bbox.q.x,
                    bbox.q.y))
            {
                return s_errno_to_gs();
            }
        }
        txt_char_widths_to_uts(pte->orig_font, &widths); /* convert design->text space */
        gs_distance_transform(widths.real_width.xy.x * penum->text_state->size,
                  widths.real_width.xy.y * penum->text_state->size,
                  &penum->text_state->matrix, &wanted);
        pte->returned.total_width.x += wanted.x;
        pte->returned.total_width.y += wanted.y;

        {
            unsigned short buffer[4];
            float glyph_width = widths.real_width.xy.x * penum->text_state->size;

            if (pte->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
                gs_point tpt;

                gs_distance_transform(pte->text.delta_all.x, pte->text.delta_all.y,
                          &ctm_only(pte->pgs), &tpt);
                dpt.x += tpt.x;
                dpt.y += tpt.y;
            }
            if (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH && chr == pte->text.space.s_char) {
                gs_point tpt;

                gs_distance_transform(pte->text.delta_space.x, pte->text.delta_space.y,
                          &ctm_only(pte->pgs), &tpt);
                dpt.x += tpt.x;
                dpt.y += tpt.y;
            }
            pte->returned.total_width.x += dpt.x;
            pte->returned.total_width.y += dpt.y;

            glyph_width += dpt.x;

            /* We can get up to 4 Unicode points per glyph, and a glyph can be
             * be represented by as little as one byte. So we make a very large
             * temporary buffer to hold the Unicode string as we assemble it. When
             * we copy it to the text fragment we will allocate only as many bytes
             * as are required to hold the actual nukmber of Unicode values we
             * decoded, and this temporary buffer will be discarded.
             */

            txt_get_unicode(penum->dev, (gs_font *)pte->orig_font, glyph, chr, &buffer[0]);

            /* Pass dummy glyph bbox because our use of extract does not
            currently cause it to be used. */
            if (extract_add_char(
                    tdev->extract,
                    penum->text_state->matrix.tx + at.x, penum->text_state->matrix.ty + at.y,
                    buffer[0] /*ucs*/,
                    glyph_width / penum->text_state->size /*adv*/,
                    0, 0, 0, 0 /* bbox*/
                    )) {
                return s_errno_to_gs();
            }
        }

        gs_distance_transform(widths.real_width.xy.x,
                              widths.real_width.xy.y,
                              &ctm_only(pte->pgs), &wanted);
        at.x += dpt.x + wanted.x;
        at.y += dpt.y + wanted.y;

        if (rcode || pte->index >= pte->text.size)
            break;
    }
    if (!rcode) rcode = font_final(tdev, penum);
    return rcode;
}

static int
docxwrite_process_plain_text(gx_device_docxwrite_t *tdev, gs_text_enum_t *pte)
{
    /* one byte regular font */
    docxw_text_enum_t *const penum = (docxw_text_enum_t *)pte;
    gs_font *font = pte->orig_font;
    const gs_glyph *gdata = NULL;
    gs_glyph glyph;
    gs_char ch = 0;
    int i, code;
    uint operation = pte->text.operation;
    txt_glyph_widths_t widths;
    gs_point wanted;	/* user space */
    gs_point at = { 0, 0 };

    for (i=pte->index;i<pte->text.size;i++) {
        const char* prevFontName;
        float glyph_width;
        unsigned short chr2[4];

        gs_point dpt = {0,0};
        if (operation & (TEXT_FROM_STRING | TEXT_FROM_BYTES)) {
            ch = pte->text.data.bytes[pte->index];
        } else if (operation & (TEXT_FROM_CHARS | TEXT_FROM_SINGLE_CHAR)) {
            ch = pte->text.data.chars[pte->index];
        } else if (operation & (TEXT_FROM_GLYPHS | TEXT_FROM_SINGLE_GLYPH)) {
            if (operation & TEXT_FROM_GLYPHS) {
                gdata = pte->text.data.glyphs + pte->index++;
            } else {
                gdata = &pte->text.data.d_glyph;
            }
        }
        glyph = (gdata == NULL ? pte->orig_font->procs.encode_char(pte->orig_font, ch, GLYPH_SPACE_NAME)
                           : *gdata);

        code = txt_glyph_widths(font, font->WMode, glyph, (gs_font *)font, &widths, NULL);
        if (code < 0) {
            if (penum->d1_width_set) {
                widths.Width.w = widths.Width.xy.x = widths.real_width.w = widths.real_width.xy.x = penum->d1_width;
                penum->d1_width = 0;
                penum->d1_width_set = 0;
            }
            else
                return code;
        }

        prevFontName = penum->text_state->FontName;
        penum->cdevproc_callout = false;
        code = docx_update_text_state(penum->text_state, (docxw_text_enum_t *)pte, pte->orig_font, &font->FontMatrix);
        if (code < 0)
            return code;

        if (!prevFontName && penum->text_state->FontName) {
            gs_rect *fbbox = &((gs_font_base *)font)->FontBBox;
            gs_matrix fm = *&((gs_font_base *)font)->FontMatrix;
            gs_rect bbox;

            code = gs_bbox_transform(fbbox, &fm, &bbox);
            if (code < 0)
                return code;

            if (extract_span_begin(
                    tdev->extract,
                    penum->text_state->FontName,
                    0 /*font_bold*/,
                    0 /*font_italic*/,
                    penum->text_state->wmode,
                    penum->pgs->ctm.xx,
                    penum->pgs->ctm.xy,
                    penum->pgs->ctm.yx,
                    penum->pgs->ctm.yy,
                    bbox.p.x,
                    bbox.p.y,
                    bbox.q.x,
                    bbox.q.y))
            {
                return s_errno_to_gs();
            }
        }

        /* Calculate glyph_width from the **original** glyph metrics, not the overriding
         * advance width (if TEXT_REPLACE_WIDTHS is set below)
         */
        txt_char_widths_to_uts(pte->orig_font, &widths); /* convert design->text space */
        glyph_width = widths.real_width.xy.x * penum->text_state->size;

        if (pte->text.operation & TEXT_REPLACE_WIDTHS)
        {
            gs_point tpt;

            /* We are applying a width override, from x/y/xyshow. This could be from
             * a PostScript file, or it could be from a PDF file where we have a font
             * with a FontMatrix which is neither horizontal nor vertical.
             */
            code = gs_text_replaced_width(&pte->text, pte->xy_index++, &tpt);
            if (code < 0)
                return_error(gs_error_unregistered);

            widths.Width.w = widths.real_width.w = tpt.x;
            widths.Width.xy.x = widths.real_width.xy.x = tpt.x;
            widths.Width.xy.y = widths.real_width.xy.y = tpt.y;
        }

        gs_distance_transform(widths.real_width.xy.x * penum->text_state->size,
                          widths.real_width.xy.y * penum->text_state->size,
                          &penum->text_state->matrix, &wanted);
        pte->returned.total_width.x += wanted.x;
        pte->returned.total_width.y += wanted.y;

        if (pte->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
            gs_point tpt;

            gs_distance_transform(pte->text.delta_all.x, pte->text.delta_all.y,
                              &ctm_only(pte->pgs), &tpt);
            dpt.x += tpt.x;
            dpt.y += tpt.y;
        }
        if (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH && ch == pte->text.space.s_char) {
            gs_point tpt;

            gs_distance_transform(pte->text.delta_space.x, pte->text.delta_space.y,
                              &ctm_only(pte->pgs), &tpt);
            dpt.x += tpt.x;
            dpt.y += tpt.y;
        }
        pte->returned.total_width.x += dpt.x;
        pte->returned.total_width.y += dpt.y;

        (void) txt_get_unicode(penum->dev, (gs_font *)pte->orig_font, glyph, ch, &chr2[0]);
        /* If a single text code returned multiple Unicode values, then we need to set the
         * 'extra' code points' widths to 0.
         */

        /* Pass dummy glyph bbox because our use of extract does not currently
        cause it to be used. */
        if (extract_add_char(
                tdev->extract,
                penum->text_state->matrix.tx + at.x, penum->text_state->matrix.ty + at.y,
                chr2[0] /*ucs*/,
                glyph_width / penum->text_state->size /*adv*/,
                0, 0, 0, 0 /*bbox*/
                )) {
            return s_errno_to_gs();
        }

        gs_distance_transform(widths.real_width.xy.x,
                              widths.real_width.xy.y,
                              &ctm_only(pte->pgs), &wanted);
        at.x += dpt.x + wanted.x;
        at.y += dpt.y + wanted.y;

        pte->index++;
    }
    code = 0;
    if (!code) code = font_final(tdev, penum);

    return code;
}

/* This routine selects whether the text needs to be handled as regular glyphs
 * and character codes, or as CIDs, depending on the font type. This is required
 * because there are ways that regular text can be handled that aren't possible
 * with CIDFonts.
 */
static int
textw_text_process(gs_text_enum_t *pte)
{
    docxw_text_enum_t *const penum = (docxw_text_enum_t *)pte;
    gx_device_docxwrite_t *const tdev = (gx_device_docxwrite_t *) pte->dev;
    gs_font *font = pte->orig_font;
    int code = 0;
    gs_text_enum_t *pte_fallback;

    if (pte->text.size == 0)
        return 0;

    pte_fallback = penum->pte_fallback;
    if (pte_fallback) {
        code = gx_default_text_restore_state(pte_fallback);
        if (code < 0)
            return code;
        gs_text_release(NULL, pte_fallback, "docxwrite_text_process");
    }
    pte_fallback = penum->pte_fallback = NULL;

    {
        switch (font->FontType) {
        case ft_CID_encrypted:
        case ft_CID_TrueType:
        case ft_composite:
            code = docxwrite_process_cmap_text(tdev, pte);
            break;
        case ft_encrypted:
        case ft_encrypted2:
        case ft_TrueType:
        case ft_user_defined:
        case ft_PCL_user_defined:
        case ft_GL2_stick_user_defined:
        case ft_GL2_531:
            code = docxwrite_process_plain_text(tdev, pte);
            break;
        default:
            return_error(gs_error_rangecheck);
            break;
        }
        if (code != 0) {
            if (code == gs_error_unregistered) /* Debug purpose only. */
                return code;
            if (code == gs_error_VMerror)
                return code;
            if (code == gs_error_invalidfont) /* Bug 688370. */
                return code;
            /* Fall back to the default implementation. */
            code = gx_default_text_begin(pte->dev, pte->pgs, &pte->text, pte->current_font,
                                         pte->pcpath, &pte_fallback);
            if (code < 0)
                return code;
            penum->pte_fallback = pte_fallback;
            gs_text_enum_copy_dynamic(pte_fallback, pte, false);

            code = gs_text_process(pte_fallback);
            if (code != 0) {
                penum->returned.current_char = pte_fallback->returned.current_char;
                penum->returned.current_glyph = pte_fallback->returned.current_glyph;
                return code;
            }
            gs_text_release(NULL, pte_fallback, "docxwrite_text_process");
            penum->pte_fallback = 0;
        }
    }
    return code;
}

/* Begin processing text. */

/* Define the auxiliary procedures for text processing. */
static int
textw_text_resync(gs_text_enum_t *pte, const gs_text_enum_t *pfrom)
{
    return gs_text_resync(pte, pfrom);
}
static bool
textw_text_is_width_only(const gs_text_enum_t *pte)
{
    return false;
}
static int
textw_text_current_width(const gs_text_enum_t *pte, gs_point *pwidth)
{
    return gs_text_current_width(pte, pwidth);
}
static int
textw_text_set_cache(gs_text_enum_t *pte, const double *pw,
                   gs_text_cache_control_t control)
{
    docxw_text_enum_t *const penum = (docxw_text_enum_t *)pte;

    switch (control) {
        case TEXT_SET_CHAR_WIDTH:
        case TEXT_SET_CACHE_DEVICE:
            if (penum->pte_fallback != NULL) {
                penum->d1_width = *pw;
                penum->d1_width_set = true;
                return 0;
            }
            return gs_text_set_cache(pte, pw, control);
        case TEXT_SET_CACHE_DEVICE2:
            if (penum->cdevproc_callout) {
                memcpy(penum->cdevproc_result, pw, sizeof(penum->cdevproc_result));
                return 0;
            }
            return gs_text_set_cache(pte, pw, control);
        default:
            return_error(gs_error_rangecheck);
    }
    return 0;
}

static int
textw_text_retry(gs_text_enum_t *pte)
{
    return gs_text_retry(pte);
}
static void
textw_text_release(gs_text_enum_t *pte, client_name_t cname)
{
    docxw_text_enum_t *const penum = (docxw_text_enum_t *)pte;
    gx_device_docxwrite_t *const tdev = (gx_device_docxwrite_t *) pte->dev;

    /* If this is copied away when we complete the text enumeration succesfully, then
     * we set the pointer to NULL, if we get here with it non-NULL , then there was
     * an error.
     */
    if (penum->text_state)
    {
        gs_free_object(tdev->memory->non_gc_memory, penum->text_state->FontName, "txtwrite free text state");
        gs_free(tdev->memory, penum->text_state, 1, sizeof(penum->text_state), "txtwrite free text state");
    }

    gs_text_release(NULL, pte, cname);
}

/* This is the list of methods for the text enumerator */
static const gs_text_enum_procs_t textw_text_procs = {
    textw_text_resync, textw_text_process,
    textw_text_is_width_only, textw_text_current_width,
    textw_text_set_cache, textw_text_retry,
    textw_text_release
};

/* This device method gets called by the interpreter to deal with text. It
 * must create a text enumerator, which contains the methods for dealing with
 * the text itself. The interpreter will use the enumerator methods to deal with
 * the text, it won't refer to the device methods again for this text.
 */
static int
docxwrite_text_begin(gx_device * dev, gs_gstate * pgs,
                const gs_text_params_t * text, gs_font * font,
                const gx_clip_path * pcpath,
                gs_text_enum_t ** ppenum)
{
    gx_device_docxwrite_t *const tdev = (gx_device_docxwrite_t *) dev;
    docxw_text_enum_t *penum;
    int code;
    gx_path *path = pgs->path;
    gs_memory_t * mem = pgs->memory;

    /* If this is a stringwidth, we must let the default graphics library code handle it
     * in case there is no current point (this can happen if this is the first operation
     * we get, the current font is a CIDFont, and its descendant is a substitute type 1
     * font). If there is no current point we throw an error in text_process and that
     * messes up all the font handling. The test below is copied from pdfwrite
     * (gdev_pdf_text_begin) and seems to do the job.
     */
    if ((!(text->operation & TEXT_DO_DRAW) && pgs->text_rendering_mode != 3)
                    || path == 0 || !path_position_valid(path))
            return gx_default_text_begin(dev, pgs, text, font,
                                         pcpath, ppenum);
    /* Allocate and initialize one of our text enumerators. */
    rc_alloc_struct_1(penum, docxw_text_enum_t, &st_textw_text_enum, mem,
                      return_error(gs_error_VMerror), "gdev_textw_text_begin");
    penum->rc.free = rc_free_text_enum;
    penum->charproc_accum = false;
    penum->cdevproc_callout = false;
    penum->returned.total_width.x = penum->returned.total_width.y = 0;
    penum->pte_fallback = NULL;
    penum->d1_width = 0;
    penum->d1_width_set = false;
    /* The enumerator's text_release method frees this memory */
    penum->text_state = (docx_list_entry_t *)gs_malloc(tdev->memory->stable_memory, 1,
            sizeof(docx_list_entry_t), "txtwrite alloc text state");
    if (!penum->text_state)
        return gs_note_error(gs_error_VMerror);
    memset(penum->text_state, 0x00, sizeof(docx_list_entry_t));

    code = gs_text_enum_init((gs_text_enum_t *)penum, &textw_text_procs,
                             dev, pgs, text, font, pcpath, mem);
    if (code < 0) {
        /* Belt and braces; I'm not certain this is required, but its safe */
        gs_free(tdev->memory, penum->text_state, 1, sizeof(docx_list_entry_t), "txtwrite free text state");
        penum->text_state = NULL;
        gs_free_object(mem, penum, "textwrite_text_begin");
        return code;
    }

    code = gx_path_current_point(gs_text_enum_path(penum), &penum->origin);
    if (code != 0)
       return code;

    *ppenum = (gs_text_enum_t *)penum;

    return 0;
}

static int
docxwrite_strip_copy_rop2(gx_device * dev,
                    const byte * sdata, int sourcex, uint sraster,
                    gx_bitmap_id id,
                    const gx_color_index * scolors,
                    const gx_strip_bitmap * textures,
                    const gx_color_index * tcolors,
                    int x, int y, int w, int h,
                    int phase_x, int phase_y, gs_logical_operation_t lop,
                    uint plane_height)
{
    return 0;
}

int
docxwrite_dev_spec_op(gx_device *pdev, int dev_spec_op, void *data, int size)
{
    switch (dev_spec_op) {
        case gxdso_get_dev_param:
            {
                int code;
                dev_param_req_t *request = (dev_param_req_t *)data;
                code = docxwrite_get_param(pdev, request->Param, request->list);
                if (code != gs_error_undefined)
                    return code;
            }
    }
    return gx_default_dev_spec_op(pdev, dev_spec_op, data, size);
}