File: Font.sml

package info (click to toggle)
polyml 5.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 40,616 kB
  • sloc: cpp: 44,142; ansic: 26,963; sh: 22,002; asm: 13,486; makefile: 602; exp: 525; python: 253; awk: 91
file content (562 lines) | stat: -rw-r--r-- 23,630 bytes parent folder | download | duplicates (5)
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
(*
    Copyright (c) 2001, 2015
        David C.J. Matthews

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License version 2.1 as published by the Free Software Foundation.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*)

structure Font :
  sig
    type HDC and HFONT
    type COLORREF = Color.COLORREF
    type POINT = {x: int, y: int}
    and  SIZE = {cx: int, cy: int }
    and RECT = { top: int, left: int, bottom: int, right: int }

    datatype FontFamily =
          FF_DECORATIVE
        | FF_DONTCARE
        | FF_MODERN
        | FF_ROMAN
        | FF_SCRIPT
        | FF_SWISS
    and FontPitch = DEFAULT_PITCH | FIXED_PITCH | VARIABLE_PITCH
    and OutputPrecision =
          OUT_CHARACTER_PRECIS
        | OUT_DEFAULT_PRECIS
        | OUT_DEVICE_PRECIS
        | OUT_OUTLINE_PRECIS
        | OUT_RASTER_PRECIS
        | OUT_SCREEN_OUTLINE_PRECIS
        | OUT_STRING_PRECIS
        | OUT_STROKE_PRECIS
        | OUT_TT_ONLY_PRECIS
        | OUT_TT_PRECIS
    and OutputQuality =
            DEFAULT_QUALITY | DRAFT_QUALITY | PROOF_QUALITY | ANTIALIASED_QUALITY | CLEARTYPE_QUALITY | NONANTIALIASED_QUALITY
    and CharacterSet =
          ANSI_CHARSET | DEFAULT_CHARSET | SYMBOL_CHARSET | MAC_CHARSET |
                SHIFTJIS_CHARSET | HANGEUL_CHARSET | JOHAB_CHARSET | GB2312_CHARSET |
                CHINESEBIG5_CHARSET | GREEK_CHARSET | TURKISH_CHARSET | VIETNAMESE_CHARSET |
                HEBREW_CHARSET | ARABIC_CHARSET | BALTIC_CHARSET | RUSSIAN_CHARSET |
                THAI_CHARSET | EASTEUROPE_CHARSET | OEM_CHARSET

    type FontWeight =  int
    val FW_BLACK : FontWeight
    val FW_BOLD : FontWeight
    val FW_DEMIBOLD : FontWeight
    val FW_DONTCARE : FontWeight
    val FW_EXTRABOLD : FontWeight
    val FW_EXTRALIGHT : FontWeight
    val FW_HEAVY : FontWeight
    val FW_LIGHT : FontWeight
    val FW_MEDIUM : FontWeight
    val FW_NORMAL : FontWeight
    val FW_REGULAR : FontWeight
    val FW_SEMIBOLD : FontWeight
    val FW_THIN : FontWeight
    val FW_ULTRABOLD : FontWeight
    val FW_ULTRALIGHT : FontWeight

    datatype ClippingPrecision =
        CLIP_DEFAULT_PRECIS | CLIP_STROKE_PRECIS | CLIP_LH_ANGLES | CLIP_DFA_DISABLE | CLIP_EMBEDDED

    type LOGFONT =
    {
        height : int,
        width : int,
        escapement : int,
        orientation : int,
        weight : FontWeight,
        italic : bool,
        underline : bool,
        strikeOut : bool,
        charSet : CharacterSet,
        outputPrecision: OutputPrecision,
        clipPrecision : ClippingPrecision list,
        quality : OutputQuality,
        pitch: FontPitch,
        family: FontFamily,
        faceName : string
    }

    datatype DrawTextMode =
          DT_BOTTOM
        | DT_CALCRECT
        | DT_CENTER
        | DT_EXPANDTABS
        | DT_EXTERNALLEADING
        | DT_INTERNAL
        | DT_LEFT
        | DT_NOCLIP
        | DT_NOPREFIX
        | DT_RIGHT
        | DT_SINGLELINE
        | DT_TABSTOP of int
        | DT_TOP
        | DT_VCENTER
        | DT_WORDBREAK
    and TextAlign =
          TA_BASELINE
        | TA_BOTTOM
        | TA_CENTER
        | TA_LEFT
        | TA_NOUPDATECP
        | TA_RIGHT
        | TA_TOP
        | TA_UPDATECP
    and TextMetricPitch =
          TMPF_DEVICE
        | TMPF_FIXED_PITCH
        | TMPF_TRUETYPE
        | TMPF_VECTOR
    and ExtendedTextMode =
          ETO_CLIPPED
        | ETO_GLYPH_INDEX
        | ETO_IGNORELANGUAGE
        | ETO_OPAQUE
        | ETO_RTLREADING

    type TEXTMETRIC =
            { height: int, ascent: int, descent: int, internalLeading: int, externalLeading: int,
              aveCharWidth: int, maxCharWidth: int, weight: int, overhang: int,
              digitizedAspectX: int, digitizedAspectY: int, firstChar: char, lastChar: char,
              defaultChar: char, breakChar: char, italic: bool, underlined: bool, struckOut: bool,
              pitch: TextMetricPitch list, family: FontFamily, charSet : CharacterSet }

    val AddFontResource : string -> int
    val CreateFont : LOGFONT -> HFONT
    val CreateFontIndirect : LOGFONT -> HFONT
    val CreateScalableFontResource : int * string * string * string -> unit
    val DrawText : HDC * string * RECT * DrawTextMode list -> int
    val ExtTextOut : HDC * POINT * ExtendedTextMode list *
       RECT option * string * int list -> unit
    val GetAspectRatioFilterEx : HDC -> SIZE
    val GetCharABCWidths : HDC * char * char -> (int * int * int) vector
    val GetCharABCWidthsFloat : HDC * char * char -> (real * real * real) vector
    val GetCharWidth32 : HDC * char * char -> int vector
    val GetCharWidthFloat : HDC * char * char -> real vector
    val GetTabbedTextExtent : HDC * string * int list -> SIZE
    val GetTextAlign : HDC -> TextAlign list
    val GetTextCharacterExtra : HDC -> int
    val GetTextColor : HDC -> COLORREF
    val GetTextExtentExPoint : HDC * string * int option ->
       {fit: int option, size: SIZE, extents: int list}
    val GetTextExtentPoint32 : HDC * string -> SIZE
    val GetTextFace : HDC -> string
    val GetTextMetrics : HDC -> TEXTMETRIC
    val RemoveFontResource : string -> unit
    val SetMapperFlags : HDC * bool -> bool
    val SetTextAlign : HDC * TextAlign list -> TextAlign list
    val SetTextCharacterExtra : HDC * int -> int
    val SetTextColor : HDC * COLORREF -> COLORREF
    val SetTextJustification : HDC * int * int -> unit
    val TabbedTextOut: HDC * POINT * string * int list * int -> SIZE
    val TextOut : HDC * POINT * String.string -> unit
  end
 =
struct
    local
        open Foreign Base GdiBase
        (*val HEIGHT = INT: int conversion*)
    in
        type COLORREF = Color.COLORREF
        type SIZE = SIZE and POINT = POINT and RECT = RECT
        type HDC = HDC and HFONT = HFONT

        open FontBase (* Contains the types used in LOGFONT. *)

        datatype TextAlign = 
            TA_NOUPDATECP | TA_UPDATECP | TA_LEFT | TA_RIGHT | TA_CENTER | TA_TOP | TA_BOTTOM | TA_BASELINE
        local
            val tab = [
                (TA_NOUPDATECP,     0w0),
                (TA_UPDATECP,       0w1),
                (TA_LEFT,           0w0),
                (TA_RIGHT,          0w2),
                (TA_CENTER,         0w6),
                (TA_TOP,            0w0),
                (TA_BOTTOM,         0w8),
                (TA_BASELINE,       0w24)
            ]
            val TEXTALIGN = tableSetConversion(tab, NONE)
        in
            val GetTextAlign               = winCall1(gdi "GetTextAlign") (cHDC) TEXTALIGN
            val SetTextAlign               = winCall2(gdi "SetTextAlign") (cHDC,TEXTALIGN) TEXTALIGN
        end
        
        (*TYPE: DrawTextMode *)
        datatype DrawTextMode = DT_TOP | DT_LEFT | DT_CENTER | DT_RIGHT | DT_VCENTER | DT_BOTTOM |
            DT_WORDBREAK | DT_SINGLELINE | DT_EXPANDTABS | DT_NOCLIP | DT_EXTERNALLEADING |
            DT_CALCRECT  | DT_NOPREFIX | DT_INTERNAL | DT_TABSTOP of int
        local
            val tab = [
                (DT_TOP,                0wx0000),
                (DT_LEFT,               0wx0000),
                (DT_CENTER,             0wx0001),
                (DT_RIGHT,              0wx0002),
                (DT_VCENTER,            0wx0004),
                (DT_BOTTOM,             0wx0008),
                (DT_WORDBREAK,          0wx0010),
                (DT_SINGLELINE,         0wx0020),
                (DT_EXPANDTABS,         0wx0040),
                (DT_NOCLIP,             0wx0100),
                (DT_EXTERNALLEADING,    0wx0200),
                (DT_CALCRECT,           0wx0400),
                (DT_NOPREFIX,           0wx0800),
                (DT_INTERNAL,           0wx1000)
            ]
            val tabStop = 0wx0080
            fun toInt (DT_TABSTOP i) = Word32.orb(tabStop, Word32.fromInt i*0w256) | toInt _ = raise Match
            fun fromInt i =
                if Word32.andb(i, tabStop) = tabStop
                then DT_TABSTOP(Word32.toInt(Word32.andb((Word32.>>(i, 0w8)), 0wxff)))
                else raise Match;
            val DRAWTEXTMODE = tableSetConversion(tab, SOME(fromInt, toInt))
        in
            val DrawText                   =
                winCall4(user "DrawTextA") (cHDC,cString,cConstStar cRect,DRAWTEXTMODE) cInt
        end

        val AddFontResource            = winCall1(gdi "AddFontResourceA") (cString) cInt
        val CreateScalableFontResource =
            winCall4(gdi "CreateScalableFontResourceA") (cDWORD,cString,cString,cString) (successState "CreateScalableFontResource")
                                         

        val GetTextCharacterExtra      = winCall1(gdi "GetTextCharacterExtra") (cHDC) cInt
        val RemoveFontResource         = winCall1(gdi "RemoveFontResourceA") (cString) (successState "RemoveFontResource")

        local
            val cFONTMAPPERFLAG: bool conversion =
                absConversion{rep=fn true => 0w1 | false => 0w0, abs=fn n => n <> 0w0} cDWORDw
        in
            val SetMapperFlags             = winCall2(gdi "SetMapperFlags") (cHDC, cFONTMAPPERFLAG) cFONTMAPPERFLAG
        end

        val SetTextCharacterExtra      = winCall2(gdi "SetTextCharacterExtra") (cHDC,cInt) cInt
        val SetTextJustification       = winCall3(gdi "SetTextJustification") (cHDC,cInt,cInt) (successState "SetTextJustification")
        val GetTextColor = winCall1 (gdi "GetTextColor") (cHDC) cCOLORREF
        and SetTextColor = winCall2 (gdi "SetTextColor") (cHDC, cCOLORREF) cCOLORREF
        
        local
            val getAspectRatioFilterEx =
                winCall2(gdi "GetAspectRatioFilterEx") (cHDC, cStar cSize)(successState "GetAspectRatioFilterEx")
        in
            fun GetAspectRatioFilterEx hdc =
            let
                val s = ref{cx=0, cy= 0}
            in
                getAspectRatioFilterEx(hdc, s);
                !s
            end
        end

        local
            val createFont =
                winCall14 (gdi "CreateFontA") (cInt, cInt, cInt, cInt, cInt (* FONTWEIGHT *), cDWORDw, cDWORDw, cDWORDw,
                        cDWORDw (*CHARACTERSET *), cDWORDw (* OUTPUTPRECISION *), cDWORDw (* CLIPPINGPRECISION *),
                        cDWORDw (* OUTPUTQUALITY *), cDWORDw (* FONTPITCHANDFAMILY *), cString) cHFONT
            fun bToch false = 0w0 | bToch true = 0w1
            val w8ToW32 = Word32.fromLargeWord o Word8.toLargeWord
        in
            fun CreateFont({height: int, width: int, escapement: int, orientation: int,
                           weight: FontWeight, italic: bool, underline: bool, strikeOut: bool,
                           charSet: CharacterSet, outputPrecision: OutputPrecision,
                           clipPrecision: ClippingPrecision list, quality: OutputQuality,
                           pitch: FontPitch, family: FontFamily, faceName: string}: LOGFONT) =
                    createFont(height, width, escapement, orientation, weight, bToch italic, bToch underline,
                     bToch strikeOut, w8ToW32(charsetToW8 charSet), w8ToW32(outPrecToW8 outputPrecision),
                     clipPrecSetToW32 clipPrecision, w8ToW32(outQualToW8 quality),
                     w8ToW32(pitchAndFamilyToW8 (pitch, family)),
                     if size faceName > 31 then String.substring(faceName, 0, 31) else faceName)
        end

        (* CreateFont and CreateFontIndirect take the same arguments in ML. *)
        val CreateFontIndirect =
            winCall1 (gdi "CreateFontIndirectA") (cConstStar cLOGFONT) cHFONT

        datatype ExtendedTextMode = ETO_OPAQUE | ETO_CLIPPED | ETO_GLYPH_INDEX |
                    ETO_RTLREADING | ETO_IGNORELANGUAGE
        local
            val tab = [
                (ETO_OPAQUE,                   0wx0002),
                (ETO_CLIPPED,                  0wx0004),
                (ETO_GLYPH_INDEX,              0wx0010),
                (ETO_RTLREADING,               0wx0080),
                (ETO_IGNORELANGUAGE,           0wx1000)
            ]
        in
            val EXTENDEDTEXTOUT = tableSetConversion(tab, NONE)
        end

        local
            val extTextOut =
                winCall8 (gdi "ExtTextOutA")
                 (cHDC,cInt,cInt, EXTENDEDTEXTOUT, cOptionPtr (cConstStar cRect), cString, cUint, cPointer)
                    (successState "ExtTextOut")
            val l2Vec = list2Vector cInt
        in
            fun ExtTextOut (h,({x,y}:POINT), option, rect, text, gapl) =
            let         
                val slen = String.size text
                val (gaps, _) =
                    case gapl of
                        [] => (Memory.null, 0)
                    |   _ => l2Vec gapl
                (* The Rect is optional but really depends on the ETO_OPAQUE or ETO_CLIPPED
                   options. *)
            in
                extTextOut(h, x, y, option, rect, text, slen, gaps)
                    handle ex => (Memory.free gaps; raise ex);
                Memory.free gaps
            end
        end

        local
            val ABC = cStruct3(cInt, cUint, cInt)
            val getCharABCWidths =
                winCall4 (gdi "GetCharABCWidthsA")
                            (cHDC, cUint, cUint, cPointer) (successState "GetCharABCWidths")
            val getVec = getVectorResult ABC
        in
            fun GetCharABCWidths (h, c1: char, c2: char) = 
            let
                fun getCharABC(abcarr, count) =
                    (getCharABCWidths(h, ord c1, ord c2, abcarr); count)
            in
                getVec getCharABC (ord c2 - ord c1 + 1) 
            end
        end

        local
            val ABC = cStruct3(cFloat, cFloat, cFloat)
            val getCharABCWidthsFloat =
                winCall4 (gdi "GetCharABCWidthsFloatA")
                            (cHDC, cUint, cUint, cPointer) (successState "GetCharABCWidthsFloat")
            val getVec = getVectorResult ABC
        in
            fun GetCharABCWidthsFloat (h,c1,c2) = 
            let
                fun getCharABC(abcarr, count) =
                    (getCharABCWidthsFloat(h, ord c1, ord c2, abcarr); count)
            in
                getVec getCharABC (ord c2 - ord c1 + 1)
            end
        end

        local
            val getCharWidth32 =
                winCall4 (gdi "GetCharWidth32A")
                                (cHDC, cUint, cUint, cPointer) (successState "GetCharWidth32")
            val getVec = getVectorResult cInt
        in
            fun GetCharWidth32 (h,c1,c2) = 
            let
                fun getCharW(vec, count) =
                    (getCharWidth32(h, ord c1, ord c2, vec); count)
            in
                getVec getCharW (ord c2 - ord c1 + 1)
            end
        end

        local
            val getCharWidthFloat =
                winCall4 (gdi "GetCharWidthFloatA")
                            (cHDC,cUint, cUint, cPointer) (successState "GetCharWidthFloat")
            val getVec = getVectorResult cFloat
        in
            fun GetCharWidthFloat (h,c1,c2) = 
            let
                fun getCharW(vec, count) =
                    (getCharWidthFloat(h, ord c1, ord c2, vec); count)
            in
                getVec getCharW (ord c2 - ord c1 + 1)
            end
        end
 
        local
            val getTextExtentPoint32 =
                winCall4 (gdi "GetTextExtentPoint32A")
                            (cHDC, cString, cInt, cStar cSize) (successState "GetTextExtentPoint32")
        in
            fun GetTextExtentPoint32 (h, s) = 
            let
                val r = ref {cx=0, cy=0}
                val () = getTextExtentPoint32(h, s, size s, r)
            in
               !r
            end
        end

        local
            val textOut =
                winCall5 (gdi "TextOutA")
                 (cHDC,cInt,cInt,cString,cInt) (successState "TextOut")
        in
            fun TextOut (h,({x,y}:POINT),s) = textOut(h, x, y, s, size s)
        end


        datatype TextMetricPitch = TMPF_FIXED_PITCH | TMPF_VECTOR | TMPF_TRUETYPE | TMPF_DEVICE
        
        (* N.B. TMPF_FIXED_PITCH is included if the font is NOT fixed pitch!! *)
        type TEXTMETRIC =
            { height: int, ascent: int, descent: int, internalLeading: int, externalLeading: int,
              aveCharWidth: int, maxCharWidth: int, weight: int, overhang: int,
              digitizedAspectX: int, digitizedAspectY: int, firstChar: char, lastChar: char,
              defaultChar: char, breakChar: char, italic: bool, underlined: bool, struckOut: bool,
              pitch: TextMetricPitch list, family: FontFamily, charSet : CharacterSet }
        local
            val TEXTMETRIC =
                cStruct20(cLong, cLong, cLong, cLong, cLong, cLong, cLong, cLong, cLong, cLong,
                          cLong, cChar, cChar, cChar, cChar, cUint8w, cUint8w, cUint8w, cUint8w, cUint8w)
            val getTextMetrics =
                winCall2 (gdi "GetTextMetricsA") (cHDC, cStar TEXTMETRIC) (successState "GetTextMetrics")
            val tmpfTab = [
                (TMPF_FIXED_PITCH,  0wx1), (* N.B.  This is the opposite *)
                (TMPF_VECTOR,       0wx2),
                (TMPF_TRUETYPE,     0wx4),
                (TMPF_DEVICE,       0wx8)
                ]
            val (_, tmpfFromW32) = tableSetLookup(tmpfTab, NONE)
        in

            fun GetTextMetrics hdc : TEXTMETRIC =
            let
                val r = ref (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, #" ", #" ", #" ", #" ", 0w0, 0w0, 0w0, 0w0, 0w0)
                val () = getTextMetrics (hdc, r)
                val (height, ascent, descent, internalLeading, externalLeading,
                      aveCharWidth, maxCharWidth, weight, overhang,
                      digitizedAspectX, digitizedAspectY, firstChar, lastChar,
                      defaultChar, breakChar, italic, underlined, struckOut,
                      pitchAndFamily, charSet) = !r
                (*val (fromChs, _, _) = breakConversion CHARACTERSET*)
                val family = toFamily(Word8.andb(pitchAndFamily, 0wxf0))
                val pitch = tmpfFromW32(Word32.fromLargeWord(Word8.toLargeWord(Word8.andb(pitchAndFamily, 0wxf))))
            in
                {
                height = height, ascent = ascent, descent = descent, internalLeading = internalLeading,
                externalLeading = externalLeading, aveCharWidth = aveCharWidth, maxCharWidth = maxCharWidth,
                weight = weight, overhang = overhang, digitizedAspectX = digitizedAspectX,
                digitizedAspectY = digitizedAspectY, firstChar = firstChar, lastChar = lastChar,
                defaultChar = defaultChar, breakChar = breakChar, italic = italic <> 0w0,
                underlined = underlined <> 0w0, struckOut = struckOut <> 0w0,
                family = family, pitch = pitch, charSet = charsetFromW8 charSet
                }
            end
        end

        local
            val getFaceCall = winCall3(gdi "GetTextFaceA") (cHDC, cInt, cPointer) cInt
        in
            fun GetTextFace hdc : string =
                getStringWithNullIsLength(fn(vec, len) => getFaceCall(hdc, len, vec))
        end

        local
            val getTextExtentExPoint =
                winCall7(gdi "GetTextExtentExPointA")
                    (cHDC, cString, cInt, cInt, cPointer, cPointer, cStar cSize) (successState "GetTextExtentExPoint")
            val {load=loadInt, ctype={size=sizeInt, ...}, ...} = breakConversion cInt
        in
            fun GetTextExtentExPoint(hdc: HDC, s: string, maxWidth: int option) :
                {fit: int option, extents: int list, size: SIZE} =
            let
                val count = size s
                open Memory
                infix 6 ++
                val vec = malloc(Word.fromInt count * sizeInt)
                (* The lpnFit argument controls whether we get the maximum no. of chars. *)
                val lpnFit =
                    case maxWidth of
                        NONE => null
                    |   SOME _ => malloc sizeInt
                val sizeVec = ref {cx=0, cy=0}
                val () =
                    getTextExtentExPoint
                        (hdc, s, count, getOpt(maxWidth, 0), lpnFit, vec, sizeVec)
                            handle ex => (free vec; free lpnFit; raise ex)
                val fit = case maxWidth of NONE => NONE | _ => SOME(loadInt lpnFit)
                fun loadExt i = loadInt(vec ++ Word.fromInt i * sizeInt)
                val extents = List.tabulate(getOpt(fit, count), loadExt)
                val () = free vec
                val () = free lpnFit
            in
                {fit = fit, extents = extents, size = ! sizeVec}
            end
        end

        local
            val tabbedTextOut =
                winCall8 (user "TabbedTextOutA") (cHDC, cInt, cInt, cString, cInt, cInt, cPointer, cInt) cDWORDw
            val list2vec = list2Vector cInt
        in
            fun TabbedTextOut(hdc, {x, y}: POINT, str, tabs, origin): SIZE =
            let
                val (tabVec, nTabs) =
                    case tabs of
                        [] => (Memory.null, 0) (* Make the vector null. *)
                    | _ => list2vec tabs
                val res =
                    tabbedTextOut(hdc, x, y, str, size str, nTabs, tabVec, origin)
                        handle ex => (Memory.free tabVec; raise ex)
                val () = Memory.free tabVec
                val () = checkResult(res <> 0w0)
            in
                (* Zero represents an error.  But it's also possible to return zero if
                   the string is empty. *)
                {cx = Word.toInt(LOWORD res), cy = Word.toInt(HIWORD res)}
            end
        end

        local
            val tabbedTextExtent =
                winCall5 (user "GetTabbedTextExtentA") (cHDC, cString, cInt, cInt, cPointer) cDWORDw
                    (*(POSINT "GetTabbedTextExtent")*)
        in
            fun GetTabbedTextExtent(hdc, str, tabs): SIZE =
            let
                val (tabVec, nTabs) =
                    case tabs of
                        [] => (Memory.null, 0) (* Make the vector null. *)
                    | _ => list2Vector cInt tabs
                val res =
                    tabbedTextExtent(hdc, str, size str, nTabs, tabVec)
                        handle ex => (Memory.free tabVec; raise ex)
                val () = Memory.free tabVec
                val () = checkResult(res <> 0w0)
            in
                {cx = Word.toInt(LOWORD res), cy = Word.toInt(HIWORD res)}
            end
        end

        (*
        Other Font and Text functions:
            DrawTextEx
            EnumFontFamiliesEx  
            EnumFontFamExProc
            GetCharacterPlacement  
            GetFontData  
            GetFontLanguageInfo  
            GetFontUnicodeRanges  
            GetGlyphIndices  
            GetGlyphOutline  
            GetKerningPairs  
            GetOutlineTextMetrics  
            GetRasterizerCaps  
            GetTabbedTextExtent  
            PolyTextOut  
        *)
            end
end;