% engine=pdftex

% Example ConTeXt macros for using Adobe Reader's default Traditional Chinese
% font AdobeMingStd-Light without embedding it in the PDF file.
% © A. Reutenauer, December 2007.  Brest, Paris, Strasbourg, Opio.
% This file is distributed under the terms of the WTF Public License
% (http://sam.zoy.org/wtfpl/)

% It must be used with pdfTeX: the macros very strongly depends on some pdfTeX
% primitives.  They would probably work with LuaTeX which inherits them,
% although it's a complete waste of resources (LuaTeX doesn't need subfonts at
% all, which are used heavily here).  They would, on the other hand, most
% certainly *not* work with XeTeX (and would be just as pointless as with
% LuaTeX).

% The file is UTF-8-encoded!
\enableregime[utf8]

% We use the chi-00 module with slight modifications in one macro, at the very
% end of that file, before the text starts.
\usemodule[chi-00]

% This overrides the default ConTeXt settings and defines the “template” for
% TFM fonts: "uni-adobe-ming-XX" (where XX is the Unicode “row” number).
% We need to first produce the corresponding TFM files somehow.
\definefontsynonym [ChineseRegular]    [uni-adobe-ming-][encoding=cjk-uni]

% Counters for PDF object numbers (this is where the fun begins!)
\newcount\cidfontobj
\newcount\subfontobj
\newcount\fontdescobj
\newcount\cmapobj

% Three \if's: true/false whether the subfont has already been embedded or not.
% For the moment we only support 3 Unicode rows: U+51XX, U+66XX and U+67XX
% 
% Yes, this is completely ad-hoc code :-)  It's only meant as a proof of
% concept.  It doesn't even know about hex digits, by the way: in TeX's mind,
% 51, 66 and 67 are actually decimal number (which is why I used U+51E0 几 in
% the sample text, and not the—in my opinion more correct—character U+5E7E 幾).
%
% But of course, it can be improved an enhanced a lot.
\newif\ifli\lifalse
\newif\iflxvi\lxvifalse
\newif\iflxvii\lxviifalse

% --- The actual font data ---

% -- Global things --

% First we need a CIDFont dictionary for AdobeStdMing-Light.
% It only needs to be written once to the PDF file, and it consists of two PDF
% objects, so here goes:

% First the font descriptor for the CIDFont ...
\immediate\pdfobj {
<<
  /Type /FontDescriptor
  /FontName /AdobeMingStd-Light
  /Flags 12
  /FontBBox [ 0 -120 1000 880 ]
  /ItalicAngle 0
  /Ascent 880
  /Descent 120
  /AvgWidth 1000
  /MaxWidth 1000
  /MissingWidth 1000
>>
}
\fontdescobj\pdflastobj

% ... then the actual CIDFont dictionary.
% \fontdescobj has just been defined, and we set the counter \cidfontobj in
% passing; it will be used by every subfont in the PDF file.
\immediate\pdfobj {
<<
  /Type /Font
  /Subtype /CIDFontType0
  /BaseFont /AdobeMingStd-Light
  /CIDSystemInfo
  <<
    /Registry (Adobe)
    /Ordering (CNS1)
    /Supplement 5
  >>
  /FontDescriptor \the\fontdescobj\space 0 R
>>
}
\cidfontobj\pdflastobj

% -- “Local” things --

% Now, we prepare macros to write the subfont to the file; again, we need two
% PDF objects referring to one another.

% This write a CMap object, then a Font object for each subfont.  The actual
% data is split into three different macros for legibility: again, we only know
% about three different subfonts (for Unicode rows 0x51, 0x66 or 0x67 ... if
% the argument is anything else, the behaviour is “undefined” :-)
%
% Each of these three macros call \writesubfontdict which we define afterwards.
\def\writesubfontobj#1{%
  \ifnum#1=51
    \ifli\else\litrue\writesubfontli\fi
  \else\ifnum#1=66
    \iflxvi\else\lxvitrue\writesubfontlxvi\fi
  \else\ifnum#1=67
    \iflxvii\else\lxviitrue\writesubfontlxvii\fi
  \fi\fi\fi % To close 3 \if's
}

% Now the actual CMap data for Unicode chars U+51XX ...
\def\writesubfontli{%
  \immediate\pdfobj stream
  attr {
    /Type /CMap /CMapName /TeXCNS-51-H
    /CIDSystemInfo << /Registry (Adobe) /Ordering (CNS1) /Supplement 5 >>
    /WMode 0
  }
  {
    /CIDInit /ProcSet findresource begin
    12 dict begin
    begincmap
    /CIDSystemInfo 3 dict dup begin
      /Registry (Adobe) def
      /Ordering (CNS1) def
      /Supplement 3 def
    end def
    /CMapName /TeXCNS-51-H def
    /CMapVersion 0.001 def
    /CMapType 1 def
    /WMode 0 def
    1 begincodespacerange
      <00> <FF>
    endcodespacerange
    10 begincidrange
    <00> <00> 4355
    <01> <01> 16434
    <02> <02> 4359
    <03> <03> 10366
    <04> <04> 4354
    <05> <05> 4362
    <06> <07> 10369
    <08> <09> 4360
    <0a> <0a> 10376
    <0b> <0c> 10373
    endcidrange
    100 begincidrange
    <0d> <0d> 14790
    <0e> <0e> 16436
    <10> <10> 4749
    <11> <11> 11012
    <12> <12> 4746
    <13> <13> 11009
    <14> <14> 4748
    <15> <15> 4750
    <17> <17> 11010
    <18> <18> 4747
    <1a> <1a> 11011
    <1c> <1c> 11008
    <1f> <1f> 5045
    <20> <20> 11582
    <21> <21> 5046
    <22> <22> 11580
    <24> <24> 11581
    <25> <25> 11579
    <26> <26> 11578
    <29> <29> 11583
    <2a> <2a> 5044
    <2b> <2b> 14766
    <2d> <2e> 12047
    <30> <30> 10367
    <31> <31> 12046
    <32> <32> 5047
    <33> <33> 5493
    <34> <35> 12410
    <37> <38> 5736
    <39> <39> 12998
    <3a> <3a> 12997
    <3b> <3b> 5820
    <3c> <3c> 5819
    <3d> <3d> 13326
    <3f> <3f> 604
    <40> <40> 628
    <41> <41> 682
    <43> <43> 681
    <44> <44> 771
    <45> <45> 770
    <46> <46> 906
    <47> <47> 905
    <48> <48> 907
    <49> <49> 904
    <4b> <4b> 1076
    <4c> <4c> 1075
    <4d> <4d> 1077
    <52> <52> 1349
    <54> <54> 1348
    <55> <55> 1350
    <56> <56> 17306
    <57> <57> 1724
    <59> <59> 281
    <5a> <5a> 7218
    <5b> <5b> 282
    <5c> <5c> 2575
    <5d> <5d> 284
    <5e> <5e> 283
    <5f> <5f> 8381
    <60> <60> 16442
    <61> <61> 285
    <62> <62> 3978
    <63> <63> 286
    <65> <65> 605
    <67> <67> 683
    <68> <68> 908
    <69> <69> 1351
    <6a> <6a> 16444
    <6b> <6b> 606
    <6c> <6c> 686
    <6d> <6e> 684
    <71> <71> 909
    <74> <74> 17727
    <75> <75> 1078
    <76> <76> 1353
    <77> <77> 1352
    <78> <78> 1354
    <79> <79> 15054
    <7c> <7c> 2130
    <80> <80> 4751
    <82> <82> 541
    <86> <86> 17311
    <87> <87> 6009
    <88> <88> 14006
    <89> <8a> 772
    <8d> <8d> 910
    <8f> <8f> 6195
    <91> <91> 1726
    <92> <92> 1725
    <93> <93> 7220
    <94> <94> 7219
    <95> <95> 2576
    <96> <96> 542
    <97> <97> 687
    <98> <98> 6016
    <9a> <9a> 16035
    <9c> <9c> 17728
    <9e> <9e> 6416
    <a0> <a0> 1727
    <a2> <a2> 2133
    endcidrange
    58 begincidrange
    <a4> <a5> 2131
    <a7> <a7> 16038
    <a8> <a8> 14810
    <aa> <aa> 4752
    <ab> <ab> 543
    <ac> <ac> 774
    <b0> <b0> 911
    <b1> <b1> 6086
    <b2> <b2> 16456
    <b3> <b3> 16465
    <b4> <b4> 15740
    <b5> <b5> 16402
    <b6> <b7> 1079
    <b8> <b8> 16457
    <b9> <b9> 6196
    <bc> <bc> 6417
    <bd> <bd> 1355
    <be> <be> 6418
    <c3> <c3> 14811
    <c4> <c5> 7222
    <c6> <c6> 2136
    <c7> <c7> 16938
    <c8> <c8> 7224
    <c9> <c9> 16459
    <ca> <ca> 7221
    <cb> <cb> 2137
    <cc> <cc> 2135
    <cd> <cd> 2134
    <ce> <ce> 7225
    <cf> <cf> 16460
    <d0> <d0> 7762
    <d1> <d1> 16461
    <d2> <d2> 15789
    <d3> <d3> 16463
    <d4> <d4> 8382
    <d7> <d7> 9077
    <d8> <d8> 9762
    <db> <db> 17818
    <dc> <dc> 4363
    <dd> <dd> 4753
    <de> <de> 11013
    <df> <df> 16975
    <e0> <e0> 607
    <e1> <e1> 621
    <e2> <e2> 16466
    <e4> <e4> 17729
    <ed> <ed> 16468
    <f0> <f0> 2577
    <f1> <f1> 3057
    <f3> <f3> 3979
    <f4> <f4> 16760
    <f5> <f5> 5998
    <f6> <f6> 688
    <f8> <f8> 777
    <f9> <fa> 775
    <fc> <fc> 17819
    <fd> <fd> 1356
    <fe> <fe> 16138
    endcidrange
    endcmap
    CMapName currentdict /CMap defineresource pop
    end
    end
  }
  \global\cmapobj\pdflastobj
  \writesubfontdict
}

% U+66XX ...
\def\writesubfontlxvi{%
  \immediate\pdfobj stream
  attr {
    /Type /CMap /CMapName /TeXCNS-66-H
    /CIDSystemInfo << /Registry (Adobe) /Ordering (CNS1) /Supplement 5 >>
    /WMode 0
  }
  {
    /CIDInit /ProcSet findresource begin
    12 dict begin
    begincmap
    /CIDSystemInfo 3 dict dup begin
      /Registry (Adobe) def
      /Ordering (CNS1) def
      /Supplement 3 def
    end def
    /CMapName /TeXCNS-66-H def
    /CMapVersion 0.001 def
    /CMapType 1 def
    /WMode 0 def
    1 begincodespacerange
      <00> <FF>
    endcodespacerange
    69 begincidrange
    <00> <00> 1533
    <02> <02> 1531
    <03> <03> 6570
    <04> <04> 6566
    <05> <05> 6573
    <06> <06> 1530
    <07> <07> 1537
    <08> <08> 6568
    <09> <09> 6564
    <0a> <0a> 1536
    <0b> <0b> 6571
    <0c> <0c> 1529
    <0d> <0d> 6572
    <0e> <0e> 1532
    <0f> <0f> 1534
    <10> <10> 6576
    <11> <11> 6575
    <12> <12> 6567
    <13> <13> 1528
    <14> <14> 1527
    <15> <15> 1535
    <18> <18> 16817
    <1c> <1c> 6921
    <1d> <1d> 6927
    <1e> <1e> 14995
    <1f> <1f> 1867
    <20> <20> 1864
    <21> <21> 6918
    <22> <22> 6923
    <23> <23> 17045
    <24> <24> 1870
    <25> <25> 1862
    <26> <26> 6922
    <27> <27> 1865
    <28> <28> 1868
    <2b> <2b> 6925
    <2d> <2d> 1863
    <2e> <2e> 6930
    <2f> <2f> 1866
    <30> <30> 14989
    <31> <31> 1869
    <32> <32> 6919
    <33> <33> 6924
    <34> <34> 6928
    <35> <35> 6920
    <36> <36> 6917
    <39> <39> 6929
    <3a> <3a> 6926
    <41> <41> 2274
    <42> <42> 2267
    <43> <43> 2270
    <44> <44> 16820
    <45> <45> 2273
    <47> <47> 7366
    <48> <48> 15769
    <49> <49> 2268
    <4a> <4a> 7364
    <4b> <4b> 16823
    <4c> <4c> 2272
    <4f> <4f> 2269
    <51> <51> 7367
    <52> <52> 2271
    <53> <53> 17995
    <57> <57> 16539
    <59> <59> 7953
    <5a> <5a> 2761
    <5b> <5b> 7952
    <5c> <5c> 7954
    <5d> <5d> 2760
    endcidrange
    100 begincidrange
    <5e> <5e> 2765
    <5f> <5f> 7365
    <61> <61> 7951
    <62> <62> 7955
    <63> <63> 14992
    <64> <64> 2762
    <65> <65> 7950
    <66> <66> 2764
    <67> <67> 16825
    <68> <68> 2763
    <6a> <6a> 8575
    <6b> <6b> 14987
    <6c> <6c> 8570
    <6e> <6e> 3191
    <6f> <6f> 3195
    <70> <70> 3192
    <71> <71> 8573
    <72> <72> 8576
    <73> <73> 16827
    <74> <74> 3193
    <76> <76> 3194
    <77> <77> 3199
    <78> <78> 9236
    <79> <79> 8574
    <7a> <7a> 3197
    <7b> <7b> 8571
    <7c> <7c> 8569
    <7d> <7d> 18075
    <7e> <7e> 3198
    <80> <80> 8572
    <84> <84> 3635
    <85> <85> 16738
    <86> <86> 9228
    <87> <88> 3632
    <89> <89> 3631
    <8a> <8a> 9233
    <8b> <8b> 9232
    <8c> <8c> 9229
    <8d> <8d> 3637
    <8e> <8e> 14985
    <90> <90> 9231
    <91> <91> 3196
    <92> <92> 17997
    <94> <94> 9235
    <95> <95> 9230
    <96> <96> 3634
    <97> <97> 3630
    <98> <98> 3636
    <99> <99> 9234
    <9a> <9a> 15677
    <9d> <9d> 4077
    <9f> <9f> 9912
    <a0> <a0> 9911
    <a1> <a1> 9910
    <a2> <a2> 4075
    <a4> <a4> 16570
    <a8> <a8> 4076
    <a9> <a9> 10489
    <aa> <aa> 10492
    <ab> <ab> 4466
    <ad> <ad> 18760
    <ae> <ae> 4465
    <af> <af> 10493
    <b0> <b0> 10488
    <b1> <b1> 4468
    <b2> <b2> 10490
    <b3> <b3> 16565
    <b4> <b4> 4467
    <b5> <b5> 10487
    <b6> <b6> 15891
    <b7> <b7> 10491
    <b8> <b8> 4812
    <b9> <b9> 4809
    <ba> <ba> 11109
    <bb> <bb> 11108
    <bd> <bd> 11107
    <be> <be> 11102
    <bf> <bf> 15770
    <c0> <c0> 11103
    <c4> <c4> 4810
    <c6> <c6> 4807
    <c7> <c7> 4811
    <c8> <c8> 11101
    <c9> <c9> 4808
    <ca> <cb> 11104
    <cc> <cc> 11110
    <cd> <cd> 16568
    <ce> <ce> 14967
    <cf> <cf> 11106
    <d2> <d2> 11634
    <d6> <d6> 5092
    <d8> <d8> 12083
    <d9> <d9> 5091
    <da> <db> 12081
    <dc> <dc> 5338
    <dd> <dd> 5509
    <de> <de> 12441
    <e0> <e0> 5508
    <e3> <e4> 12762
    <e6> <e6> 5657
    endcidrange
    15 begincidrange
    <e8> <e8> 12761
    <e9> <e9> 5749
    <eb> <eb> 13341
    <ec> <ec> 5884
    <ed> <ee> 13445
    <f0> <f0> 731
    <f1> <f1> 16004
    <f2> <f3> 988
    <f4> <f4> 1211
    <f6> <f6> 6577
    <f7> <f7> 1871
    <f8> <f8> 2275
    <f9> <f9> 2766
    <fc> <fc> 2591
    <fe> <ff> 3200
    endcidrange
    endcmap
    CMapName currentdict /CMap defineresource pop
    end
    end
  }
  \global\cmapobj\pdflastobj
  \writesubfontdict
}

% And U+67XX !
\def\writesubfontlxvii{%
  \immediate\pdfobj stream
  attr {
    /Type /CMap /CMapName /TeXCNS-67-H
    /CIDSystemInfo << /Registry (Adobe) /Ordering (CNS1) /Supplement 5 >>
    /WMode 0
  }
  {
    /CIDInit /ProcSet findresource begin
    12 dict begin
    begincmap
    /CIDSystemInfo 3 dict dup begin
      /Registry (Adobe) def
      /Ordering (CNS1) def
      /Supplement 3 def
    end def
    /CMapName /TeXCNS-67-H def
    /CMapVersion 0.001 def
    /CMapType 1 def
    /WMode 0 def
    1 begincodespacerange
      <00> <FF>
    endcodespacerange
    85 begincidrange
    <00> <00> 3056
    <01> <01> 8577
    <02> <02> 16836
    <03> <03> 3638
    <04> <04> 9914
    <05> <05> 9913
    <08> <08> 732
    <09> <09> 990
    <0a> <0a> 6578
    <0b> <0b> 1539
    <0c> <0c> 16354
    <0d> <0d> 1538
    <0e> <0e> 15777
    <0f> <10> 6931
    <12> <13> 7368
    <14> <15> 2276
    <16> <16> 17999
    <17> <17> 2278
    <18> <18> 7956
    <1b> <1b> 2768
    <1d> <1d> 3203
    <1e> <1e> 15755
    <1f> <1f> 3202
    <20> <20> 9237
    <21> <21> 9506
    <22> <22> 9915
    <23> <23> 11111
    <25> <25> 14239
    <26> <26> 5339
    <27> <27> 5658
    <28> <28> 733
    <2a> <2b> 842
    <2c> <2c> 841
    <2d> <2d> 844
    <2e> <2e> 840
    <31> <31> 993
    <33> <33> 6144
    <34> <34> 992
    <35> <35> 994
    <36> <36> 15937
    <38> <38> 6139
    <39> <39> 6138
    <3a> <3a> 6141
    <3b> <3b> 6140
    <3c> <3c> 6143
    <3d> <3d> 991
    <3e> <3e> 6137
    <3f> <3f> 6142
    <44> <44> 16411
    <45> <45> 6307
    <46> <46> 1221
    <47> <47> 6308
    <48> <48> 6312
    <49> <49> 1220
    <4b> <4b> 6316
    <4c> <4c> 6311
    <4d> <4d> 6314
    <4e> <51> 1213
    <53> <53> 1223
    <55> <55> 6310
    <56> <56> 1218
    <57> <57> 1224
    <59> <59> 6309
    <5a> <5a> 6315
    <5c> <5c> 1217
    <5d> <5d> 6313
    <5e> <5e> 1219
    <5f> <5f> 1212
    <60> <60> 1222
    <61> <61> 15718
    <62> <62> 16843
    <67> <67> 16842
    <6a> <6a> 1560
    <6c> <6c> 6580
    <6d> <6d> 1540
    <6f> <70> 1550
    <71> <71> 1543
    <72> <72> 1561
    <73> <73> 1545
    <74> <74> 6588
    <75> <75> 1556
    <76> <76> 6583
    <77> <77> 1546
    <78> <79> 6598
    <7a> <7a> 6591
    endcidrange
    100 begincidrange
    <7b> <7b> 6584
    <7c> <7c> 1559
    <7d> <7d> 6596
    <7e> <7e> 1554
    <7f> <7f> 1552
    <81> <81> 6597
    <83> <83> 6595
    <84> <84> 6587
    <85> <85> 6579
    <86> <86> 6586
    <87> <87> 1547
    <89> <89> 1553
    <8b> <8b> 1541
    <8c> <8c> 6590
    <8d> <8d> 6589
    <8e> <8e> 6581
    <8f> <8f> 18113
    <90> <90> 1555
    <91> <91> 6593
    <92> <92> 6582
    <93> <93> 1558
    <94> <94> 6600
    <95> <95> 1542
    <97> <97> 1549
    <98> <98> 6585
    <99> <99> 6594
    <9a> <9a> 1557
    <9c> <9c> 1544
    <9d> <9d> 1548
    <9f> <9f> 6592
    <a0> <a0> 15738
    <a4> <a4> 18001
    <ac> <ac> 16867
    <ae> <ae> 6964
    <af> <af> 1879
    <b0> <b0> 1892
    <b1> <b1> 16884
    <b2> <b2> 6959
    <b3> <b3> 6949
    <b4> <b4> 1885
    <b5> <b5> 6947
    <b6> <b6> 1878
    <b7> <b7> 6942
    <b8> <b8> 1888
    <b9> <b9> 6955
    <ba> <ba> 6936
    <bb> <bb> 6938
    <bf> <bf> 14330
    <c0> <c0> 6941
    <c1> <c1> 6933
    <c2> <c2> 6954
    <c3> <c3> 6970
    <c4> <c4> 1883
    <c5> <c5> 6943
    <c6> <c6> 6961
    <c8> <c8> 6935
    <c9> <ca> 6968
    <cb> <cb> 6972
    <cc> <cc> 6963
    <cd> <cd> 6948
    <ce> <ce> 6956
    <cf> <cf> 1889
    <d0> <d0> 1876
    <d1> <d1> 1884
    <d2> <d2> 1896
    <d3> <d3> 1873
    <d4> <d4> 1875
    <d6> <d6> 15192
    <d7> <d7> 16846
    <d8> <d8> 6940
    <d9> <d9> 1893
    <da> <da> 1886
    <db> <db> 6966
    <dc> <dc> 6937
    <dd> <dd> 1895
    <de> <de> 1890
    <df> <df> 6946
    <e2> <e2> 1894
    <e3> <e3> 6953
    <e4> <e4> 6945
    <e5> <e5> 1887
    <e6> <e6> 6965
    <e7> <e7> 6957
    <e9> <e9> 1881
    <ea> <ea> 6971
    <eb> <eb> 6944
    <ec> <ec> 1877
    <ed> <ed> 6962
    <ee> <ee> 6952
    <ef> <ef> 1882
    <f0> <f0> 6958
    <f1> <f1> 1874
    <f2> <f2> 6934
    <f3> <f3> 1891
    <f4> <f4> 2293
    <f5> <f5> 1880
    <f6> <f6> 6951
    <f7> <f7> 6950
    <f8> <f8> 6939
    <f9> <f9> 14742
    endcidrange
    4 begincidrange
    <fa> <fa> 6967
    <fc> <fc> 6960
    <fe> <fe> 17749
    <ff> <ff> 1872
    endcidrange
    endcmap
    CMapName currentdict /CMap defineresource pop
    end
    end
  }
  \global\cmapobj\pdflastobj
  \writesubfontdict
}

% Finally, this writes a subfont dictionary to the PDF file.  It is a template
% that refers to both the global CIDFont object (\cidfontobj) and the local CMap
% object just defined (\cmapobj).
\def\writesubfontdict{%
  \immediate\pdfobj useobjnum \subfontobj {
    <<
      /Type /Font
      /Subtype /Type0
      /BaseFont /AdobeMingStd-Light
      /Encoding \the\cmapobj\space 0 R
      /DescendantFonts [ \the\cidfontobj\space 0 R ]
    >>
  }
}

% --- End of font data ---

% And now, the macro that actually writes a character to the file.
%
% It's a shameless plug from font-chi.tex, and therefore (very) strongly
% depends on all of the other macros in font-chi.tex.  Only the fore-to-last
% two lines are different: they call \writesubfontobj with an appropriate
% setting of \subfontobj; in that context, the subfont is well defined, and we
% need to output the PDF object corresponding to that subfont if it hasn't
% already been written to the file.
\unexpanded\def\unicodeglyph#1#2%
  {\begingroup
   \getvalue{@@\currentucharmapping\strippedcsname\uchar}{#1}{#2}
   \bodyfontsize\unicodescale\bodyfontsize
   \font\unicodefont=\truefontname{\doifelsefontsynonym{\unicodestyle
     \unicodeone}\empty\truefontname\unicodestyle\unicodeone}
      at \currentfontscale\bodyfontsize
   \unicodestrut
   \unicodefont\unicodecharcommand{\char\unicodetwo\relax}%
   \subfontobj=\pdffontobjnum\unicodefont
   \writesubfontobj\unicodeone
   \endgroup}

% That's all, folks!  Now we can write text from the three Unicode rows we know
% about.  That's not much, admittedly ;-)

\starttext

明月几時有

\stoptext
