write_shrink_to_fit.json

The JSON file of the “Badges of one size, names of every length” example. Eight badges cut to the same size, carrying names that run from two short words to a double-barrelled surname. The library finds, for each name, the largest size that fits its rectangle, and that size is printed underneath.

JSON 333 lines

This file is a request: an application sends it over the network to the server, which creates the PDF and sends it back. It makes the same page as the Rust and Python versions of this example. A request names every file it uses, never its content; the call carries the request and the files together. A font the server already holds, such as DejaVuSans, is named with nothing attached. Send the request to the address the API page gives.

What this example is for

A conference orders its badges weeks before it knows who is coming. The card is one size, and the guest list is not: two short words for one visitor, a double-barrelled surname and a middle name for the next. Pick a size that suits the longest name and the short ones sit lost in the middle of their cards; pick one that suits the short names and the long ones walk off the edge. The evening before the doors open, somebody is shortening names by hand.

What this example shows

  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
{
  "items": [
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 768.4, "urx": 523.0, "ury": 790.0 },
      "content": "Badges of one size, names of every length",
      "font": "DejaVuSans",
      "font_size": 18.0
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 696.4, "urx": 523.0, "ury": 756.4 },
      "content": "The eight boxes below are the same box, cut before anyone knew who would wear it. Each name is handed the box it has to fit and asked what size it fits at: the sizes step down by a quarter of a point, each one broken to the width of the box and weighed against its height, and the name is set at the first size that holds. Nothing here is measured by hand, and no name is shortened.",
      "font": "DejaVuSans",
      "font_size": 10.0
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 660.0, "urx": 523.0, "ury": 674.4 },
      "content": "One box, eight names",
      "font": "DejaVuSans",
      "font_size": 12.0
    },
    {
      "type": "rect",
      "rect": { "llx": 72.0, "lly": 588.0, "urx": 288.0, "ury": 646.0 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 82.0, "lly": 609.0, "urx": 278.0, "ury": 636.0 },
      "content": "Ana Ruiz",
      "font": "DejaVuSans",
      "font_size": 22.0,
      "align": "center",
      "fit_size": { "floor": 8.0 }
    },
    {
      "type": "flow",
      "rect": { "llx": 82.0, "lly": 598.2, "urx": 278.0, "ury": 609.0 },
      "content": "Speaker",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 574.4, "urx": 288.0, "ury": 584.0 },
      "content": "Set at 22.00 points",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "rect",
      "rect": { "llx": 307.0, "lly": 588.0, "urx": 523.0, "ury": 646.0 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 317.0, "lly": 609.0, "urx": 513.0, "ury": 636.0 },
      "content": "Wei Chen",
      "font": "DejaVuSans",
      "font_size": 22.0,
      "align": "center",
      "fit_size": { "floor": 8.0 }
    },
    {
      "type": "flow",
      "rect": { "llx": 317.0, "lly": 598.2, "urx": 513.0, "ury": 609.0 },
      "content": "Attendee",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 307.0, "lly": 574.4, "urx": 523.0, "ury": 584.0 },
      "content": "Set at 22.00 points",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "rect",
      "rect": { "llx": 72.0, "lly": 504.4, "urx": 288.0, "ury": 562.4 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 82.0, "lly": 525.4, "urx": 278.0, "ury": 552.4 },
      "content": "Marguerite Vandenbossche",
      "font": "DejaVuSans",
      "font_size": 22.0,
      "align": "center",
      "fit_size": { "floor": 8.0 }
    },
    {
      "type": "flow",
      "rect": { "llx": 82.0, "lly": 514.6, "urx": 278.0, "ury": 525.4 },
      "content": "Session chair",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 490.8, "urx": 288.0, "ury": 500.4 },
      "content": "Set at 14.25 points",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "rect",
      "rect": { "llx": 307.0, "lly": 504.4, "urx": 523.0, "ury": 562.4 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 317.0, "lly": 525.4, "urx": 513.0, "ury": 552.4 },
      "content": "Jean-Baptiste Delaunay",
      "font": "DejaVuSans",
      "font_size": 22.0,
      "align": "center",
      "fit_size": { "floor": 8.0 }
    },
    {
      "type": "flow",
      "rect": { "llx": 317.0, "lly": 514.6, "urx": 513.0, "ury": 525.4 },
      "content": "Press",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 307.0, "lly": 490.8, "urx": 523.0, "ury": 500.4 },
      "content": "Set at 16.50 points",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "rect",
      "rect": { "llx": 72.0, "lly": 420.8, "urx": 288.0, "ury": 478.8 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 82.0, "lly": 441.8, "urx": 278.0, "ury": 468.8 },
      "content": "Konstantinos Papadopoulos",
      "font": "DejaVuSans",
      "font_size": 22.0,
      "align": "center",
      "fit_size": { "floor": 8.0 }
    },
    {
      "type": "flow",
      "rect": { "llx": 82.0, "lly": 431.0, "urx": 278.0, "ury": 441.8 },
      "content": "Workshop host",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 407.2, "urx": 288.0, "ury": 416.8 },
      "content": "Set at 14.00 points",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "rect",
      "rect": { "llx": 307.0, "lly": 420.8, "urx": 523.0, "ury": 478.8 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 317.0, "lly": 441.8, "urx": 513.0, "ury": 468.8 },
      "content": "Aleksandra Wiśniewska-Kowalczyk",
      "font": "DejaVuSans",
      "font_size": 22.0,
      "align": "center",
      "fit_size": { "floor": 8.0 }
    },
    {
      "type": "flow",
      "rect": { "llx": 317.0, "lly": 431.0, "urx": 513.0, "ury": 441.8 },
      "content": "Organiser",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 307.0, "lly": 407.2, "urx": 523.0, "ury": 416.8 },
      "content": "Set at 11.25 points",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "rect",
      "rect": { "llx": 72.0, "lly": 337.2, "urx": 288.0, "ury": 395.2 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 82.0, "lly": 358.2, "urx": 278.0, "ury": 385.2 },
      "content": "Åsa Lindqvist",
      "font": "DejaVuSans",
      "font_size": 22.0,
      "align": "center",
      "fit_size": { "floor": 8.0 }
    },
    {
      "type": "flow",
      "rect": { "llx": 82.0, "lly": 347.4, "urx": 278.0, "ury": 358.2 },
      "content": "Volunteer",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 323.6, "urx": 288.0, "ury": 333.2 },
      "content": "Set at 22.00 points",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "rect",
      "rect": { "llx": 307.0, "lly": 337.2, "urx": 523.0, "ury": 395.2 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 317.0, "lly": 358.2, "urx": 513.0, "ury": 385.2 },
      "content": "Mohammed Al-Rashid ibn Saleh",
      "font": "DejaVuSans",
      "font_size": 22.0,
      "align": "center",
      "fit_size": { "floor": 8.0 }
    },
    {
      "type": "flow",
      "rect": { "llx": 317.0, "lly": 347.4, "urx": 513.0, "ury": 358.2 },
      "content": "Exhibitor",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 307.0, "lly": 323.6, "urx": 523.0, "ury": 333.2 },
      "content": "Set at 12.25 points",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 287.2, "urx": 523.0, "ury": 301.6 },
      "content": "Where the shrinking stops",
      "font": "DejaVuSans",
      "font_size": 12.0
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 203.2, "urx": 523.0, "ury": 275.2 },
      "content": "A floor is given along with the box, and the size never goes under it: a name shrunk until it disappears is worse than a name that runs over. The luggage tag below is far too small for the name it carries, so the floor is what comes back, and the tag is drawn at the floor and clipped to its own edges. What to do about a box no size fills is the caller's to decide: draw it clipped, as here, print the name on a second line, or hand the register back a name it has to shorten.",
      "font": "DejaVuSans",
      "font_size": 10.0
    },
    {
      "type": "text",
      "rect": { "llx": 73.0, "lly": 154.4, "urx": 171.0, "ury": 183.2 },
      "content": ["Bartholomew Christopher Fitzwilliam-Harrington"],
      "font": "DejaVuSans",
      "font_size": 8.0,
      "align": "center",
      "clip": {
        "segments": [
          { "move_to": { "x": 72.0, "y": 163.2 } },
          { "line_to": { "x": 172.0, "y": 163.2 } },
          { "line_to": { "x": 172.0, "y": 183.2 } },
          { "line_to": { "x": 72.0, "y": 183.2 } },
          "close"
        ]
      }
    },
    {
      "type": "rect",
      "rect": { "llx": 72.0, "lly": 163.2, "urx": 172.0, "ury": 183.2 },
      "stroke": { "width": 0.5, "color": [0.55, 0.55, 0.55] }
    },
    {
      "type": "flow",
      "rect": { "llx": 184.0, "lly": 169.6, "urx": 523.0, "ury": 179.2 },
      "content": "At the floor, clipped to the tag",
      "font": "DejaVuSans",
      "font_size": 8.0,
      "color": [0.42, 0.42, 0.42]
    },
    {
      "type": "flow",
      "rect": { "llx": 72.0, "lly": 115.2, "urx": 523.0, "ury": 147.6 },
      "content": "The size that comes back is never larger than the one asked for, so a short name is set at the size the design chose and only a long one comes out smaller. Every badge on this sheet went through the same call, and the size printed under each is the one it handed back.",
      "font": "DejaVuSans",
      "font_size": 9.0,
      "color": [0.42, 0.42, 0.42]
    }
  ]
}