diff options
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | doc.go | 12 | ||||
-rw-r--r-- | fpdf_test.go | 252 | ||||
-rw-r--r-- | internal/example/example.go | 104 | ||||
-rw-r--r-- | internal/example/example_test.go | 31 | ||||
-rw-r--r-- | ttfparser_test.go | 9 |
6 files changed, 265 insertions, 155 deletions
@@ -93,8 +93,9 @@ rather than PHP. ##Example PDFs -A side effect of running "go test" is the production of a number of example -PDFs. These can be found in the gofpdf/pdf directory after the tests complete. +A side effect of running "go test ./..." is the production of a number of +example PDFs. These can be found in the gofpdf/pdf directory after the tests +complete. Please note that these examples run in the context of a test. In order run an example as a standalone application, you'll need to examine [fpdf_test.go](https://github.com/jung-kurt/gofpdf/blob/master/fpdf_test.go) for @@ -135,8 +136,11 @@ gofpdf for its document production mode. gofpdf is a global community effort and you are invited to make it even better. If you have implemented a new feature or corrected a problem, please consider -contributing your change to the project. Here are guidelines for making -submissions. Your change should +contributing your change to the project. Contributions that do not directly +pertain to the core functionality of gofpdf should be placed in their own +directory directly beneath the `contrib` directory. + +Here are guidelines for making submissions. Your change should * be compatible with the MIT License * be properly documented @@ -112,8 +112,9 @@ rather than PHP. Example PDFs -A side effect of running "go test" is the production of a number of example -PDFs. These can be found in the gofpdf/pdf directory after the tests complete. +A side effect of running "go test ./..." is the production of a number of +example PDFs. These can be found in the gofpdf/pdf directory after the tests +complete. Please note that these examples run in the context of a test. In order run an example as a standalone application, you'll need to examine fpdf_test.go for @@ -149,8 +150,11 @@ Contributing Changes gofpdf is a global community effort and you are invited to make it even better. If you have implemented a new feature or corrected a problem, please consider -contributing your change to the project. Here are guidelines for making -submissions. Your change should +contributing your change to the project. Contributions that do not directly +pertain to the core functionality of gofpdf should be placed in their own +directory directly beneath the `contrib` directory. + +Here are guidelines for making submissions. Your change should • be compatible with the MIT License diff --git a/fpdf_test.go b/fpdf_test.go index 71aa0d2..0af3556 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2014 Kurt Jung (Gmail: kurt.w.jung) + * Copyright (c) 2013-2015 Kurt Jung (Gmail: kurt.w.jung) * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,15 +30,7 @@ import ( "strings" "github.com/jung-kurt/gofpdf" -) - -// Absolute path needed for gocov tool; relative OK for test -const ( - cnGofpdfDir = "." - cnFontDir = cnGofpdfDir + "/font" - cnImgDir = cnGofpdfDir + "/image" - cnTextDir = cnGofpdfDir + "/text" - cnExampleDir = cnGofpdfDir + "/pdf" + "github.com/jung-kurt/gofpdf/internal/example" ) func init() { @@ -46,37 +38,23 @@ func init() { } func cleanup() { - filepath.Walk(cnExampleDir, + filepath.Walk(example.PdfDir(), func(path string, info os.FileInfo, err error) (reterr error) { - if path[len(path)-4:] == ".pdf" { - os.Remove(path) + if len(path) > 3 { + if path[len(path)-4:] == ".pdf" { + os.Remove(path) + } } return }) } -func imageFile(fileStr string) string { - return filepath.Join(cnImgDir, fileStr) -} - -func fontFile(fileStr string) string { - return filepath.Join(cnFontDir, fileStr) -} - -func textFile(fileStr string) string { - return filepath.Join(cnTextDir, fileStr) -} - -func exampleFile(fileStr string) string { - return filepath.Join(cnExampleDir, fileStr) -} - type fontResourceType struct { } func (f fontResourceType) Open(name string) (rdr io.Reader, err error) { var buf []byte - buf, err = ioutil.ReadFile(fontFile(name)) + buf, err = ioutil.ReadFile(example.FontFile(name)) if err == nil { rdr = bytes.NewReader(buf) fmt.Printf("Generalized font loader reading %s\n", name) @@ -103,35 +81,23 @@ func lorem() string { "officia deserunt mollit anim id est laborum." } -func exampleFilename(baseStr string) string { - return filepath.Join(cnExampleDir, baseStr+".pdf") -} - -func summary(err error, fileStr string) { - if err == nil { - fileStr = filepath.ToSlash(fileStr) - fmt.Printf("Successfully generated %s\n", fileStr) - } else { - fmt.Println(err) - } -} - // This example demonstrates the generation of a simple PDF document. Note that // since only core fonts are used (in this case Arial, a synonym for // Helvetica), an empty string can be specified for the font directory in the -// call to New(). Note also that the exampleFilename and summary functions are -// local to the test file and are not part of the gofpdf library. If an error -// occurs at some point during the construction of the document, subsequent -// method calls exit immediately and the error is finally retreived with the -// output call where it can be handled by the application. +// call to New(). Note also that the example.Filename() and example.Summary() +// functions belong to a separate, internal package and are not part of the +// gofpdf library. If an error occurs at some point during the construction of +// the document, subsequent method calls exit immediately and the error is +// finally retreived with the output call where it can be handled by the +// application. func Example() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "B", 16) pdf.Cell(40, 10, "Hello World!") - fileStr := exampleFilename("basic") + fileStr := example.Filename("basic") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/basic.pdf } @@ -140,7 +106,7 @@ func Example() { func ExampleFpdf_AddPage() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.SetHeaderFunc(func() { - pdf.Image(imageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "") + pdf.Image(example.ImageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "") pdf.SetY(5) pdf.SetFont("Arial", "B", 15) pdf.Cell(80, 0, "") @@ -160,9 +126,9 @@ func ExampleFpdf_AddPage() { pdf.CellFormat(0, 10, fmt.Sprintf("Printing line number %d", j), "", 1, "", false, 0, "") } - fileStr := exampleFilename("Fpdf_AddPage") + fileStr := example.Filename("Fpdf_AddPage") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_AddPage.pdf } @@ -234,11 +200,11 @@ func ExampleFpdf_MultiCell() { chapterTitle(chapNum, titleStr) chapterBody(fileStr) } - printChapter(1, "A RUNAWAY REEF", textFile("20k_c1.txt")) - printChapter(2, "THE PROS AND CONS", textFile("20k_c2.txt")) - fileStr := exampleFilename("Fpdf_MultiCell") + printChapter(1, "A RUNAWAY REEF", example.TextFile("20k_c1.txt")) + printChapter(2, "THE PROS AND CONS", example.TextFile("20k_c2.txt")) + fileStr := example.Filename("Fpdf_MultiCell") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_MultiCell.pdf } @@ -340,11 +306,11 @@ func ExampleFpdf_SetLeftMargin() { pdf.CellFormat(0, 10, fmt.Sprintf("Page %d", pdf.PageNo()), "", 0, "C", false, 0, "") }) - printChapter(1, "A RUNAWAY REEF", textFile("20k_c1.txt")) - printChapter(2, "THE PROS AND CONS", textFile("20k_c2.txt")) - fileStr := exampleFilename("Fpdf_SetLeftMargin_multicolumn") + printChapter(1, "A RUNAWAY REEF", example.TextFile("20k_c1.txt")) + printChapter(2, "THE PROS AND CONS", example.TextFile("20k_c2.txt")) + fileStr := example.Filename("Fpdf_SetLeftMargin_multicolumn") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetLeftMargin_multicolumn.pdf } @@ -460,7 +426,7 @@ func ExampleFpdf_CellFormat_1() { } pdf.CellFormat(wSum, 0, "", "T", 0, "", false, 0, "") } - loadData(textFile("countries.txt")) + loadData(example.TextFile("countries.txt")) pdf.SetFont("Arial", "", 14) pdf.AddPage() basicTable() @@ -468,9 +434,9 @@ func ExampleFpdf_CellFormat_1() { improvedTable() pdf.AddPage() fancyTable() - fileStr := exampleFilename("Fpdf_CellFormat_1_tables") + fileStr := example.Filename("Fpdf_CellFormat_1_tables") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_CellFormat_1_tables.pdf } @@ -491,7 +457,7 @@ func ExampleFpdf_HTMLBasicNew() { // Second page: image link and basic HTML with link pdf.AddPage() pdf.SetLink(link, 0, -1) - pdf.Image(imageFile("logo.png"), 10, 12, 30, 0, false, "", 0, "http://www.fpdf.org") + pdf.Image(example.ImageFile("logo.png"), 10, 12, 30, 0, false, "", 0, "http://www.fpdf.org") pdf.SetLeftMargin(45) pdf.SetFontSize(14) _, lineHt = pdf.GetFontSize() @@ -501,23 +467,23 @@ func ExampleFpdf_HTMLBasicNew() { `<a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.` html := pdf.HTMLBasicNew() html.Write(lineHt, htmlStr) - fileStr := exampleFilename("Fpdf_HTMLBasicNew") + fileStr := example.Filename("Fpdf_HTMLBasicNew") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_HTMLBasicNew.pdf } // This example demonstrates the use of a non-standard font. func ExampleFpdf_AddFont() { - pdf := gofpdf.New("P", "mm", "A4", cnFontDir) + pdf := gofpdf.New("P", "mm", "A4", example.FontDir()) pdf.AddFont("Calligrapher", "", "calligra.json") pdf.AddPage() pdf.SetFont("Calligrapher", "", 35) pdf.Cell(0, 10, "Enjoy new fonts with FPDF!") - fileStr := exampleFilename("Fpdf_AddFont") + fileStr := example.Filename("Fpdf_AddFont") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_AddFont.pdf } @@ -527,19 +493,19 @@ func ExampleFpdf_Image() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "", 11) - pdf.Image(imageFile("logo.png"), 10, 10, 30, 0, false, "", 0, "") + pdf.Image(example.ImageFile("logo.png"), 10, 10, 30, 0, false, "", 0, "") pdf.Text(50, 20, "logo.png") - pdf.Image(imageFile("logo.gif"), 10, 40, 30, 0, false, "", 0, "") + pdf.Image(example.ImageFile("logo.gif"), 10, 40, 30, 0, false, "", 0, "") pdf.Text(50, 50, "logo.gif") - pdf.Image(imageFile("logo-gray.png"), 10, 70, 30, 0, false, "", 0, "") + pdf.Image(example.ImageFile("logo-gray.png"), 10, 70, 30, 0, false, "", 0, "") pdf.Text(50, 80, "logo-gray.png") - pdf.Image(imageFile("logo-rgb.png"), 10, 100, 30, 0, false, "", 0, "") + pdf.Image(example.ImageFile("logo-rgb.png"), 10, 100, 30, 0, false, "", 0, "") pdf.Text(50, 110, "logo-rgb.png") - pdf.Image(imageFile("logo.jpg"), 10, 130, 30, 0, false, "", 0, "") + pdf.Image(example.ImageFile("logo.jpg"), 10, 130, 30, 0, false, "", 0, "") pdf.Text(50, 140, "logo.jpg") - fileStr := exampleFilename("Fpdf_Image") + fileStr := example.Filename("Fpdf_Image") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_Image.pdf } @@ -589,17 +555,17 @@ func ExampleFpdf_SetAcceptPageBreakFunc() { pdf.SetFont("Times", "", 12) for j := 0; j < 20; j++ { if j == 1 { - pdf.Image(imageFile("fpdf.png"), -1, 0, colWd, 0, true, "", 0, "") + pdf.Image(example.ImageFile("fpdf.png"), -1, 0, colWd, 0, true, "", 0, "") } else if j == 5 { - pdf.Image(imageFile("golang-gopher.png"), + pdf.Image(example.ImageFile("golang-gopher.png"), -1, 0, colWd, 0, true, "", 0, "") } pdf.MultiCell(colWd, 5, loremStr, "", "", false) pdf.Ln(-1) } - fileStr := exampleFilename("Fpdf_SetAcceptPageBreakFunc_landscape") + fileStr := example.Filename("Fpdf_SetAcceptPageBreakFunc_landscape") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetAcceptPageBreakFunc_landscape.pdf } @@ -607,15 +573,15 @@ func ExampleFpdf_SetAcceptPageBreakFunc() { // This examples tests corner cases as reported by the gocov tool. func ExampleFpdf_SetKeywords() { var err error - fileStr := exampleFilename("Fpdf_SetKeywords") - err = gofpdf.MakeFont(fontFile("CalligrapherRegular.pfb"), - fontFile("cp1252.map"), cnFontDir, nil, true) + fileStr := example.Filename("Fpdf_SetKeywords") + err = gofpdf.MakeFont(example.FontFile("CalligrapherRegular.pfb"), + example.FontFile("cp1252.map"), example.FontDir(), nil, true) if err == nil { - err = gofpdf.MakeFont(fontFile("calligra.ttf"), - fontFile("cp1252.map"), cnFontDir, nil, true) + err = gofpdf.MakeFont(example.FontFile("calligra.ttf"), + example.FontFile("cp1252.map"), example.FontDir(), nil, true) if err == nil { pdf := gofpdf.New("", "", "", "") - pdf.SetFontLocation(cnFontDir) + pdf.SetFontLocation(example.FontDir()) pdf.SetTitle("世界", true) pdf.SetAuthor("世界", true) pdf.SetSubject("世界", true) @@ -628,7 +594,7 @@ func ExampleFpdf_SetKeywords() { err = pdf.OutputFileAndClose(fileStr) } } - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetKeywords.pdf } @@ -709,9 +675,9 @@ func ExampleFpdf_Circle() { pdf.SetLineWidth(thin) pdf.SetLineCapStyle("butt") - fileStr := exampleFilename("Fpdf_Circle_figures") + fileStr := example.Filename("Fpdf_Circle_figures") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_Circle_figures.pdf } @@ -753,7 +719,7 @@ func ExampleFpdf_SetAlpha() { pdf.SetXY(x, y+2) pdf.CellFormat(rectW, rectH, "A", "", 0, "C", false, 0, "") pdf.SetAlpha(0.5, modeList[j]) - pdf.Image(imageFile("golang-gopher.png"), + pdf.Image(example.ImageFile("golang-gopher.png"), x-gapX, y, rectW+2*gapX, 0, false, "", 0, "") pdf.SetAlpha(1.0, "Normal") x += rectW + gapX @@ -761,9 +727,9 @@ func ExampleFpdf_SetAlpha() { } y += rectH + gapY } - fileStr := exampleFilename("Fpdf_SetAlpha_transparency") + fileStr := example.Filename("Fpdf_SetAlpha_transparency") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetAlpha_transparency.pdf } @@ -784,9 +750,9 @@ func ExampleFpdf_LinearGradient() { pdf.RadialGradient(115, 120, 75, 75, 220, 220, 250, 80, 80, 220, 0.25, 0.75, 0.75, 0.75, 0.75) pdf.Rect(115, 120, 75, 75, "D") - fileStr := exampleFilename("Fpdf_LinearGradient_gradient") + fileStr := example.Filename("Fpdf_LinearGradient_gradient") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_LinearGradient_gradient.pdf } @@ -829,7 +795,7 @@ func ExampleFpdf_ClipText() { y += 28 pdf.ClipEllipse(26, y+10, 16, 10, true) - pdf.Image(imageFile("logo.jpg"), 10, y, 32, 0, false, "JPG", 0, "") + pdf.Image(example.ImageFile("logo.jpg"), 10, y, 32, 0, false, "JPG", 0, "") pdf.ClipEnd() pdf.ClipCircle(60, y+10, 10, true) @@ -854,9 +820,9 @@ func ExampleFpdf_ClipText() { pdf.MultiCell(130, 5, lorem(), "", "", false) pdf.ClipEnd() - fileStr := exampleFilename("Fpdf_ClipText") + fileStr := example.Filename("Fpdf_ClipText") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_ClipText.pdf } @@ -866,7 +832,7 @@ func ExampleFpdf_PageSize() { pdf := gofpdf.NewCustom(&gofpdf.InitType{ UnitStr: "in", Size: gofpdf.SizeType{Wd: 6, Ht: 6}, - FontDirStr: cnFontDir, + FontDirStr: example.FontDir(), }) pdf.SetMargins(0.5, 1, 0.5) pdf.SetFont("Times", "", 14) @@ -883,9 +849,9 @@ func ExampleFpdf_PageSize() { wd, ht, u := pdf.PageSize(j) fmt.Printf("%d: %6.2f %s, %6.2f %s\n", j, wd, u, ht, u) } - fileStr := exampleFilename("Fpdf_PageSize") + fileStr := example.Filename("Fpdf_PageSize") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // 0: 6.00 in, 6.00 in // 1: 12.00 in, 3.00 in @@ -909,9 +875,9 @@ func ExampleFpdf_Bookmark() { pdf.Bookmark("Page 2", 0, 0) pdf.Bookmark("Paragraph 3", 1, -1) pdf.Cell(0, 6, "Paragraph 3") - fileStr := exampleFilename("Fpdf_Bookmark") + fileStr := example.Filename("Fpdf_Bookmark") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_Bookmark.pdf } @@ -1031,9 +997,9 @@ func ExampleFpdf_TransformBegin() { refDupe() pdf.TransformEnd() - fileStr := exampleFilename("Fpdf_TransformBegin") + fileStr := example.Filename("Fpdf_TransformBegin") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_TransformBegin.pdf } @@ -1060,7 +1026,7 @@ func ExampleFpdf_RegisterImage() { pdf.SetMargins(10, 10, 10) pdf.SetFont("Helvetica", "", 15) for j, str := range fileList { - imageFileStr = imageFile(str) + imageFileStr = example.ImageFile(str) infoPtr = pdf.RegisterImage(imageFileStr, "") imgWd, imgHt = infoPtr.Extent() switch j { @@ -1082,10 +1048,10 @@ func ExampleFpdf_RegisterImage() { } pdf.Image(imageFileStr, lf, tp, imgWd, imgHt, false, "", 0, "") } - fileStr := exampleFilename("Fpdf_RegisterImage") + fileStr := example.Filename("Fpdf_RegisterImage") // Test the image information retrieval method infoShow := func(imageStr string) { - imageStr = imageFile(imageStr) + imageStr = example.ImageFile(imageStr) info := pdf.GetImageInfo(imageStr) if info != nil { if info.Width() > 0.0 { @@ -1100,7 +1066,7 @@ func ExampleFpdf_RegisterImage() { infoShow(fileList[0]) infoShow("foo.png") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Image image/logo-gray.png is registered // Image image/foo.png is not registered @@ -1129,9 +1095,9 @@ func ExampleFpdf_SplitLines() { for _, line := range lines { pdf.CellFormat(190.0, lineHt, string(line), "", 1, "C", false, 0, "") } - fileStr := exampleFilename("Fpdf_Splitlines") + fileStr := example.Filename("Fpdf_Splitlines") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_Splitlines.pdf } @@ -1162,7 +1128,7 @@ func ExampleFpdf_SVGBasicWrite() { `web control is supported and is used in this example.` html := pdf.HTMLBasicNew() html.Write(lineHt, htmlStr) - sig, err = gofpdf.SVGBasicFileParse(imageFile(sigFileStr)) + sig, err = gofpdf.SVGBasicFileParse(example.ImageFile(sigFileStr)) if err == nil { scale := 100 / sig.Wd scaleY := 30 / sig.Ht @@ -1177,9 +1143,9 @@ func ExampleFpdf_SVGBasicWrite() { } else { pdf.SetError(err) } - fileStr := exampleFilename("Fpdf_SVGBasicWrite") + fileStr := example.Filename("Fpdf_SVGBasicWrite") err = pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SVGBasicWrite.pdf } @@ -1230,9 +1196,9 @@ func ExampleFpdf_CellFormat_2() { pdf.AddFont("Calligrapher", "", "calligra.json") pdf.SetFont("Calligrapher", "", 16) formatRect(pdf, recListBaseline) - fileStr := exampleFilename("Fpdf_CellFormat_2_align") + fileStr := example.Filename("Fpdf_CellFormat_2_align") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Generalized font loader reading calligra.json // Generalized font loader reading calligra.z @@ -1264,9 +1230,9 @@ func ExampleFpdf_CellFormat_3() { write("Falsches \xdcben von Xylophonmusik qu\xe4lt jeden gr\xf6\xdferen Zwerg.") write("Heiz\xf6lr\xfccksto\xdfabd\xe4mpfung") write("For\xe5rsj\xe6vnd\xf8gn / Efter\xe5rsj\xe6vnd\xf8gn") - fileStr := exampleFilename("Fpdf_CellFormat_3_codepageescape") + fileStr := example.Filename("Fpdf_CellFormat_3_codepageescape") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_CellFormat_3_codepageescape.pdf } @@ -1274,7 +1240,7 @@ func ExampleFpdf_CellFormat_3() { // This example demonstrates the automatic conversion of UTF-8 strings to an // 8-bit font encoding. func ExampleFpdf_CellFormat_4() { - pdf := gofpdf.New("P", "mm", "A4", cnFontDir) // A4 210.0 x 297.0 + pdf := gofpdf.New("P", "mm", "A4", example.FontDir()) // A4 210.0 x 297.0 // See documentation for details on how to generate fonts pdf.AddFont("Helvetica-1251", "", "helvetica_1251.json") pdf.AddFont("Helvetica-1253", "", "helvetica_1253.json") @@ -1304,9 +1270,9 @@ func ExampleFpdf_CellFormat_4() { tr = pdf.UnicodeTranslatorFromDescriptor("cp1253") write("Θέλει αρετή και τόλμη η ελευθερία. (Ανδρέας Κάλβος)") - fileStr := exampleFilename("Fpdf_CellFormat_4_codepage") + fileStr := example.Filename("Fpdf_CellFormat_4_codepage") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_CellFormat_4_codepage.pdf } @@ -1318,9 +1284,9 @@ func ExampleFpdf_SetProtection() { pdf.AddPage() pdf.SetFont("Arial", "", 12) pdf.Write(10, "Password-protected.") - fileStr := exampleFilename("Fpdf_SetProtection") + fileStr := example.Filename("Fpdf_SetProtection") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetProtection.pdf } @@ -1368,9 +1334,9 @@ func ExampleFpdf_Polygon() { } y += advance } - fileStr := exampleFilename("Fpdf_Polygon") + fileStr := example.Filename("Fpdf_Polygon") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_Polygon.pdf } @@ -1408,9 +1374,9 @@ func ExampleFpdf_AddLayer() { pdf.Write(8, "This line belongs to layer 1 again.\n") pdf.EndLayer() - fileStr := exampleFilename("Fpdf_AddLayer") + fileStr := example.Filename("Fpdf_AddLayer") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_AddLayer.pdf } @@ -1451,9 +1417,9 @@ func ExampleFpdf_RegisterImageReader() { } else { pdf.SetError(err) } - fileStr := exampleFilename("Fpdf_RegisterImageReader_url") + fileStr := example.Filename("Fpdf_RegisterImageReader_url") err = pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_RegisterImageReader_url.pdf @@ -1540,9 +1506,9 @@ func ExampleFpdf_Beziergon() { pdf.SetDrawColor(64, 64, 128) pdf.SetLineWidth(pdf.GetLineWidth() * 3) pdf.Beziergon(curveList, "D") - fileStr := exampleFilename("Fpdf_Beziergon") + fileStr := example.Filename("Fpdf_Beziergon") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_Beziergon.pdf @@ -1559,9 +1525,9 @@ func ExampleFpdf_SetFontLoader() { pdf.AddPage() pdf.SetFont("Calligrapher", "", 35) pdf.Cell(0, 10, "Load fonts from any source") - fileStr := exampleFilename("Fpdf_SetFontLoader") + fileStr := example.Filename("Fpdf_SetFontLoader") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Generalized font loader reading calligra.json // Generalized font loader reading calligra.z @@ -1582,9 +1548,9 @@ func ExampleFpdf_MoveTo() { pdf.SetFillColor(200, 200, 200) pdf.SetLineWidth(3) pdf.DrawPath("DF") - fileStr := exampleFilename("Fpdf_MoveTo_path") + fileStr := example.Filename("Fpdf_MoveTo_path") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_MoveTo_path.pdf } @@ -1623,9 +1589,9 @@ func ExampleFpdf_SetLineJoinStyle() { draw(caps[i], joins[i], x, 50, x, 160) x += offset } - fileStr := exampleFilename("Fpdf_SetLineJoinStyle_caps") + fileStr := example.Filename("Fpdf_SetLineJoinStyle_caps") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SetLineJoinStyle_caps.pdf } @@ -1699,9 +1665,9 @@ func ExampleFpdf_DrawPath() { pdf.DrawPath("B*") pdf.Text(115, 290, "B* (even odd)") - fileStr := exampleFilename("Fpdf_DrawPath_fill") + fileStr := example.Filename("Fpdf_DrawPath_fill") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_DrawPath_fill.pdf } @@ -1712,7 +1678,7 @@ func ExampleFpdf_CreateTemplate() { pdf.SetCompression(false) // pdf.SetFont("Times", "", 12) template := pdf.CreateTemplate(func(tpl *gofpdf.Tpl) { - tpl.Image(imageFile("logo.png"), 6, 6, 30, 0, false, "", 0, "") + tpl.Image(example.ImageFile("logo.png"), 6, 6, 30, 0, false, "", 0, "") tpl.SetFont("Arial", "B", 16) tpl.Text(40, 20, "Template says hello") tpl.SetDrawColor(0, 100, 200) @@ -1726,7 +1692,7 @@ func ExampleFpdf_CreateTemplate() { template2 := pdf.CreateTemplate(func(tpl *gofpdf.Tpl) { tpl.UseTemplate(template) subtemplate := tpl.CreateTemplate(func(tpl2 *gofpdf.Tpl) { - tpl2.Image(imageFile("logo.png"), 6, 86, 30, 0, false, "", 0, "") + tpl2.Image(example.ImageFile("logo.png"), 6, 86, 30, 0, false, "", 0, "") tpl2.SetFont("Arial", "B", 16) tpl2.Text(40, 100, "Subtemplate says hello") tpl2.SetDrawColor(0, 200, 100) @@ -1752,9 +1718,9 @@ func ExampleFpdf_CreateTemplate() { pdf.Line(60, 210, 80, 210) pdf.Text(40, 200, "Template example page 2") - fileStr := exampleFilename("Fpdf_CreateTemplate") + fileStr := example.Filename("Fpdf_CreateTemplate") err := pdf.OutputFileAndClose(fileStr) - summary(err, fileStr) + example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_CreateTemplate.pdf } diff --git a/internal/example/example.go b/internal/example/example.go new file mode 100644 index 0000000..8486994 --- /dev/null +++ b/internal/example/example.go @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2015 Kurt Jung (Gmail: kurt.w.jung) + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* +Package example provides some helper routines for the test packages of +gofpdf and its various contributed packages located beneath the contrib +directory. +*/ +package example + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +var gofpdfDir string + +func init() { + setRoot() +} + +// Assign the relative path to the gofpdfDir directory based on current working +// directory +func setRoot() { + wdStr, err := os.Getwd() + if err == nil { + gofpdfDir = "" + sepStr := string(os.PathSeparator) + list := strings.Split(wdStr, sepStr) + for j := len(list) - 1; j >= 0 && list[j] != "gofpdf"; j-- { + gofpdfDir = filepath.Join(gofpdfDir, "..") + } + } else { + panic(err) + } +} + +// ImageFile returns a qualified filename in which the path to the image +// directory is prepended to the specified filename. +func ImageFile(fileStr string) string { + return filepath.Join(gofpdfDir, "image", fileStr) +} + +// FontDir returns the path to the font directory. +func FontDir() string { + return filepath.Join(gofpdfDir, "font") +} + +// FontFile returns a qualified filename in which the path to the font +// directory is prepended to the specified filename. +func FontFile(fileStr string) string { + return filepath.Join(FontDir(), fileStr) +} + +// TextFile returns a qualified filename in which the path to the text +// directory is prepended to the specified filename. +func TextFile(fileStr string) string { + return filepath.Join(gofpdfDir, "text", fileStr) +} + +// PdfDir returns the path to the PDF output directory. +func PdfDir() string { + return filepath.Join(gofpdfDir, "pdf") +} + +// PdfFile returns a qualified filename in which the path to the PDF output +// directory is prepended to the specified filename. +func PdfFile(fileStr string) string { + return filepath.Join(PdfDir(), fileStr) +} + +// Filename returns a qualified filename in which the example PDF directory +// path is prepended and the suffix ".pdf" is appended to the specified +// filename. +func Filename(baseStr string) string { + return PdfFile(baseStr + ".pdf") +} + +// Summary generates a predictable report for use by test examples. If the +// specified error is nil, the filename delimiters are normalized and the +// filename printed to standard output with a success message. If the specified +// error is not nil, its String() value is printed to standard output. +func Summary(err error, fileStr string) { + if err == nil { + fileStr = filepath.ToSlash(fileStr) + fmt.Printf("Successfully generated %s\n", fileStr) + } else { + fmt.Println(err) + } +} diff --git a/internal/example/example_test.go b/internal/example/example_test.go new file mode 100644 index 0000000..54f4689 --- /dev/null +++ b/internal/example/example_test.go @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015 Kurt Jung (Gmail: kurt.w.jung) + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +package example_test + +import ( + "fmt" + "path/filepath" + + "github.com/jung-kurt/gofpdf/internal/example" +) + +// Test the Filename() function. +func ExampleExample_Filename() { + fmt.Println(filepath.ToSlash(example.Filename("foo"))) + // Output: + // ../../pdf/foo.pdf +} diff --git a/ttfparser_test.go b/ttfparser_test.go index 9f494a6..3286db2 100644 --- a/ttfparser_test.go +++ b/ttfparser_test.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Kurt Jung (Gmail: kurt.w.jung) + * Copyright (c) 2013-2015 Kurt Jung (Gmail: kurt.w.jung) * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,12 +19,13 @@ package gofpdf_test import ( "bytes" "fmt" + "github.com/jung-kurt/gofpdf" - // "testing" + "github.com/jung-kurt/gofpdf/internal/example" ) func ExampleTtfParse() { - ttf, err := gofpdf.TtfParse(cnFontDir + "/calligra.ttf") + ttf, err := gofpdf.TtfParse(example.FontDir() + "/calligra.ttf") if err == nil { fmt.Printf("Postscript name: %s\n", ttf.PostScriptName) fmt.Printf("unitsPerEm: %8d\n", ttf.UnitsPerEm) @@ -55,7 +56,7 @@ func hexStr(s string) string { } func ExampleFpdf_GetStringWidth() { - pdf := gofpdf.New("", "", "", cnFontDir) + pdf := gofpdf.New("", "", "", example.FontDir()) pdf.SetFont("Helvetica", "", 12) pdf.AddPage() for _, s := range []string{"Hello", "世界", "\xe7a va?"} { |