Skip to content

Commit

Permalink
Added full support for UTF-8 font. With MIT license.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkFreedman committed Apr 19, 2019
1 parent 5262aa8 commit 0d6a38d
Show file tree
Hide file tree
Showing 15 changed files with 2,020 additions and 154 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ text, drawing and images.
* Templates
* Barcodes
* Charting facility
* UTF-8 support

gofpdf has no dependencies other than the Go standard library. All tests pass
on Linux, Mac and Windows platforms.

Like FPDF version 1.7, from which gofpdf is derived, this package does not yet
support UTF-8 fonts. In particular, languages that require more than one code
page such as Chinese, Japanese, and Arabic are not currently supported. This is
explained in [issue 109](https://github.com/jung-kurt/gofpdf/issues/109). However, support is provided to automatically translate
gofpdf supports UTF-8 fonts.

Also, support is provided to automatically translate
UTF-8 runes to code page encodings for languages that have fewer than 256
glyphs.

Expand Down Expand Up @@ -135,7 +135,9 @@ for all examples.
Nothing special is required to use the standard PDF fonts (courier, helvetica,
times, zapfdingbats) in your documents other than calling SetFont().

In order to use a different TrueType or Type1 font, you will need to generate a
You should use AddUTF8Font or AddUTF8FontFromBytes to add UTF-8 TTF font.

In order to use a different non-UTF-8 TrueType or Type1 font, you will need to generate a
font definition file and, if the font will be embedded into PDFs, a compressed
version of the font file. This is done by calling the MakeFont function or
using the included makefont command line utility. To create the utility, cd
Expand Down Expand Up @@ -225,12 +227,10 @@ which the internal catalogs were not sorted stably. Paul Montag added encoding
and decoding functionality for templates, including images that are embedded in
templates; this allows templates to be stored independently of gofpdf. Paul
also added support for page boxes used in printing PDF documents. Wojciech
Matusiak added supported for word spacing.
Matusiak added supported for word spacing. Artem Korotkiy added support of UTF-8 fonts.

## Roadmap

* Handle UTF-8 source text natively. Until then, automatic translation of
UTF-8 runes to code page bytes is provided.
* Improve test coverage as reported by the coverage tool.


34 changes: 19 additions & 15 deletions def.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ type fontFileType struct {
n int
embedded bool
content []byte
fontType string
}

type linkType struct {
Expand Down Expand Up @@ -495,6 +496,7 @@ type PageBox struct {

// Fpdf is the principal structure for creating a single PDF document
type Fpdf struct {
isCurrentUTF8 bool // is current font used in utf-8 mode
page int // current page number
n int // current object number
offsets []int // array of object offsets
Expand Down Expand Up @@ -682,20 +684,22 @@ type FontDescType struct {
}

type fontDefType struct {
Tp string // "Core", "TrueType", ...
Name string // "Courier-Bold", ...
Desc FontDescType // Font descriptor
Up int // Underline position
Ut int // Underline thickness
Cw [256]int // Character width by ordinal
Enc string // "cp1252", ...
Diff string // Differences from reference encoding
File string // "Redressed.z"
Size1, Size2 int // Type1 values
OriginalSize int // Size of uncompressed font file
N int // Set by font loader
DiffN int // Position of diff in app array, set by font loader
i string // 1-based position in font list, set by font loader, not this program
Tp string // "Core", "TrueType", ...
Name string // "Courier-Bold", ...
Desc FontDescType // Font descriptor
Up int // Underline position
Ut int // Underline thickness
Cw []int // Character width by ordinal
Enc string // "cp1252", ...
Diff string // Differences from reference encoding
File string // "Redressed.z"
Size1, Size2 int // Type1 values
OriginalSize int // Size of uncompressed font file
N int // Set by font loader
DiffN int // Position of diff in app array, set by font loader
i string // 1-based position in font list, set by font loader, not this program
utf8File *utf8FontFile // UTF-8 font
usedRunes map[int]int // Array of used runes
}

// generateFontID generates a font Id from the font definition
Expand All @@ -715,7 +719,7 @@ type fontInfoType struct {
IsFixedPitch bool
UnderlineThickness int
UnderlinePosition int
Widths [256]int
Widths []int
Size1, Size2 uint32
Desc FontDescType
}
2 changes: 2 additions & 0 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func loadMap(encodingFileStr string) (encList encListType, err error) {

// getInfoFromTrueType returns information from a TrueType font
func getInfoFromTrueType(fileStr string, msgWriter io.Writer, embed bool, encList encListType) (info fontInfoType, err error) {
info.Widths = make([]int, 256)
var ttf TtfType
ttf, err = TtfParse(fileStr)
if err != nil {
Expand Down Expand Up @@ -168,6 +169,7 @@ func segmentRead(r io.Reader) (s segmentType, err error) {

// getInfoFromType1 return information from a Type1 font
func getInfoFromType1(fileStr string, msgWriter io.Writer, embed bool, encList encListType) (info fontInfoType, err error) {
info.Widths = make([]int, 256)
if embed {
var f *os.File
f, err = os.Open(fileStr)
Expand Down
Binary file added font/DejaVuSansCondensed-Bold.ttf
Binary file not shown.
Binary file added font/DejaVuSansCondensed-BoldOblique.ttf
Binary file not shown.
Binary file added font/DejaVuSansCondensed-Oblique.ttf
Binary file not shown.
1 change: 1 addition & 0 deletions font/DejaVuSansCondensed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Tp":"TrueType","Name":"DejaVuSansCondensed","Desc":{"Ascent":760,"Descent":-240,"CapHeight":760,"Flags":32,"FontBBox":{"Xmin":-918,"Ymin":-463,"Xmax":1614,"Ymax":1232},"ItalicAngle":0,"StemV":70,"MissingWidth":540},"Up":-63,"Ut":44,"Cw":[540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,540,286,360,414,754,572,855,702,247,351,351,450,754,286,325,286,303,572,572,572,572,572,572,572,572,572,572,303,303,754,754,754,478,900,615,617,628,693,568,518,697,677,265,265,590,501,776,673,708,542,708,625,571,549,659,615,890,616,549,616,351,303,351,754,450,450,551,571,495,571,554,316,571,570,250,250,521,250,876,570,550,571,571,370,469,353,570,532,736,532,532,472,572,303,572,754,540,708,549,286,473,466,900,450,450,572,1208,984,360,940,639,708,677,563,286,286,466,466,531,450,900,540,900,812,360,809,543,586,588,286,548,532,265,572,549,303,450,568,900,628,550,754,325,900,265,450,754,265,250,473,572,572,286,554,936,494,550,250,571,469,250,615,617,617,549,703,568,969,577,673,673,639,677,776,677,708,677,542,628,549,548,774,616,699,617,962,984,749,794,617,628,971,625,551,555,530,473,622,554,811,479,584,584,543,575,679,588,550,588,571,495,524,532,769,532,612,532,823,848,636,710,530,494,757,541],"Enc":"cp1251","Diff":"128 /afii10051 /afii10052 131 /afii10100 136 /Euro 138 /afii10058 140 /afii10059 /afii10061 /afii10060 /afii10145 /afii10099 152 /.notdef 154 /afii10106 156 /afii10107 /afii10109 /afii10108 /afii10193 161 /afii10062 /afii10110 /afii10057 165 /afii10050 168 /afii10023 170 /afii10053 175 /afii10056 178 /afii10055 /afii10103 /afii10098 184 /afii10071 /afii61352 /afii10101 188 /afii10105 /afii10054 /afii10102 /afii10104 /afii10017 /afii10018 /afii10019 /afii10020 /afii10021 /afii10022 /afii10024 /afii10025 /afii10026 /afii10027 /afii10028 /afii10029 /afii10030 /afii10031 /afii10032 /afii10033 /afii10034 /afii10035 /afii10036 /afii10037 /afii10038 /afii10039 /afii10040 /afii10041 /afii10042 /afii10043 /afii10044 /afii10045 /afii10046 /afii10047 /afii10048 /afii10049 /afii10065 /afii10066 /afii10067 /afii10068 /afii10069 /afii10070 /afii10072 /afii10073 /afii10074 /afii10075 /afii10076 /afii10077 /afii10078 /afii10079 /afii10080 /afii10081 /afii10082 /afii10083 /afii10084 /afii10085 /afii10086 /afii10087 /afii10088 /afii10089 /afii10090 /afii10091 /afii10092 /afii10093 /afii10094 /afii10095 /afii10096 /afii10097","File":"","Size1":0,"Size2":0,"OriginalSize":0,"N":0,"DiffN":0}
Binary file added font/DejaVuSansCondensed.ttf
Binary file not shown.
Loading

0 comments on commit 0d6a38d

Please sign in to comment.