codeberg.org/go-pdf/fpdf@v0.11.1/fpdf_getter_test.go (about) 1 // Copyright ©2025 The go-pdf Authors. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package fpdf_test 6 7 import ( 8 "errors" 9 "io" 10 "math" 11 "strings" 12 "testing" 13 "time" 14 15 "codeberg.org/go-pdf/fpdf" 16 ) 17 18 var floatEpsilon = math.Nextafter(1.0, 2.0) - 1.0 19 20 func floatEqual(a, b float64) bool { 21 return math.Abs(a-b) <= floatEpsilon 22 } 23 24 func TestGetAlpha(t *testing.T) { 25 pdf := fpdf.New("P", "mm", "A4", "") 26 pdf.SetAlpha(0.17, "Luminosity") 27 28 alpha, blendModeStr := pdf.GetAlpha() 29 30 if got, want := alpha, 0.17; !floatEqual(got, want) { 31 t.Errorf("invalid alpha value: got=%v, want=%v", got, want) 32 } 33 if got, want := blendModeStr, "Luminosity"; got != want { 34 t.Errorf("invalid blend mode: got=%v, want=%v", got, want) 35 } 36 } 37 38 func TestGetAuthor(t *testing.T) { 39 pdf := fpdf.New("P", "mm", "A4", "") 40 pdf.SetAuthor("John Doe", false) 41 42 author := pdf.GetAuthor() 43 44 if got, want := author, "John Doe"; got != want { 45 t.Errorf("invalid author: got=%v, want=%v", got, want) 46 } 47 } 48 49 func TestGetAutoPageBreak(t *testing.T) { 50 pdf := fpdf.New("P", "mm", "A4", "") 51 pdf.SetAutoPageBreak(true, 10) 52 53 autoPageBreak, margin := pdf.GetAutoPageBreak() 54 55 if got, want := autoPageBreak, true; got != want { 56 t.Errorf("invalid autoPageBreak: got=%v, want=%v", got, want) 57 } 58 if got, want := margin, 10.0; !floatEqual(got, want) { 59 t.Errorf("invalid margin: got=%v, want=%v", got, want) 60 } 61 } 62 63 func TestGetCatalogSort(t *testing.T) { 64 pdf := fpdf.New("P", "mm", "A4", "") 65 pdf.SetCatalogSort(true) 66 67 catalogSort := pdf.GetCatalogSort() 68 69 if got, want := catalogSort, true; got != want { 70 t.Errorf("invalid catalogSort: got=%v, want=%v", got, want) 71 } 72 73 pdf.SetCatalogSort(false) 74 75 catalogSort = pdf.GetCatalogSort() 76 77 if got, want := catalogSort, false; got != want { 78 t.Errorf("invalid catalogSort: got=%v, want=%v", got, want) 79 } 80 } 81 82 func TestGetCellMargin(t *testing.T) { 83 pdf := fpdf.New("P", "mm", "A4", "") 84 pdf.SetCellMargin(6) 85 86 cellMargin := pdf.GetCellMargin() 87 88 if got, want := cellMargin, 6.0; !floatEqual(got, want) { 89 t.Errorf("invalid cellMargin: got=%v, want=%v", got, want) 90 } 91 } 92 93 func TestGetCompression(t *testing.T) { 94 pdf := fpdf.New("P", "mm", "A4", "") 95 pdf.SetCompression(true) 96 97 compression := pdf.GetCompression() 98 99 if got, want := compression, true; got != want { 100 t.Errorf("invalid compression: got=%v, want=%v", got, want) 101 } 102 103 pdf.SetCompression(false) 104 105 compression = pdf.GetCompression() 106 107 if got, want := compression, false; got != want { 108 t.Errorf("invalid compression: got=%v, want=%v", got, want) 109 } 110 } 111 112 func TestGetConversionRatio(t *testing.T) { 113 pdf := fpdf.New("P", "mm", "A4", "") 114 115 conversionRatio := pdf.GetConversionRatio() 116 117 if got, want := conversionRatio, 72.0/25.4; !floatEqual(got, want) { 118 t.Errorf("invalid conversionRatio: got=%v, want=%v", got, want) 119 } 120 121 pdf = fpdf.New("P", "pt", "A4", "") 122 123 conversionRatio = pdf.GetConversionRatio() 124 125 if got, want := conversionRatio, 1.0; !floatEqual(got, want) { 126 t.Errorf("invalid conversionRatio: got=%v, want=%v", got, want) 127 } 128 } 129 130 func TestGetCreationDate(t *testing.T) { 131 setDate, _ := time.Parse(time.RFC3339, "2003-06-17T01:23:45Z") 132 pdf := fpdf.New("P", "mm", "A4", "") 133 pdf.SetCreationDate(setDate) 134 135 creationDate := pdf.GetCreationDate() 136 137 if got, want := creationDate, setDate; !got.Equal(want) { 138 t.Errorf("invalid creationDate: got=%v, want=%v", got, want) 139 } 140 } 141 142 func TestGetCreator(t *testing.T) { 143 pdf := fpdf.New("P", "mm", "A4", "") 144 pdf.SetCreator("John Doe", false) 145 146 creator := pdf.GetCreator() 147 148 if got, want := creator, "John Doe"; got != want { 149 t.Errorf("invalid creator: got=%v, want=%v", got, want) 150 } 151 } 152 153 func TestGetDisplayMode(t *testing.T) { 154 pdf := fpdf.New("P", "mm", "A4", "") 155 pdf.SetDisplayMode("real", "OneColumn") 156 157 zoom, layout := pdf.GetDisplayMode() 158 159 if got, want := zoom, "real"; got != want { 160 t.Errorf("invalid zoom: got=%v, want=%v", got, want) 161 } 162 if got, want := layout, "OneColumn"; got != want { 163 t.Errorf("invalid layout: got=%v, want=%v", got, want) 164 } 165 } 166 167 func TestGetDrawColor(t *testing.T) { 168 pdf := fpdf.New("P", "mm", "A4", "") 169 pdf.SetDrawColor(134, 26, 34) 170 171 r, g, b := pdf.GetDrawColor() 172 173 if got, want := r, 134; got != want { 174 t.Errorf("invalid red component: got=%v, want=%v", got, want) 175 } 176 if got, want := g, 26; got != want { 177 t.Errorf("invalid green component: got=%v, want=%v", got, want) 178 } 179 if got, want := b, 34; got != want { 180 t.Errorf("invalid blue component: got=%v, want=%v", got, want) 181 } 182 } 183 184 func TestGetDrawSpotColor(t *testing.T) { 185 pdf := fpdf.New("P", "mm", "A4", "") 186 pdf.AddSpotColor("RAL 5018", 81, 0, 5, 48) 187 pdf.SetDrawSpotColor("RAL 5018", 100) 188 189 name, c, m, y, k := pdf.GetDrawSpotColor() 190 191 if got, want := name, "RAL 5018"; got != want { 192 t.Errorf("invalid spot color name: got=%v, want=%v", got, want) 193 } 194 if got, want := c, byte(81); got != want { 195 t.Errorf("invalid cyan component: got=%v, want=%v", got, want) 196 } 197 if got, want := m, byte(0); got != want { 198 t.Errorf("invalid magenta component: got=%v, want=%v", got, want) 199 } 200 if got, want := y, byte(5); got != want { 201 t.Errorf("invalid yellow component: got=%v, want=%v", got, want) 202 } 203 if got, want := k, byte(48); got != want { 204 t.Errorf("invalid black component: got=%v, want=%v", got, want) 205 } 206 } 207 208 func TestGetFillColor(t *testing.T) { 209 pdf := fpdf.New("P", "mm", "A4", "") 210 pdf.SetFillColor(255, 203, 0) 211 212 r, g, b := pdf.GetFillColor() 213 214 if got, want := r, 255; got != want { 215 t.Errorf("invalid red component: got=%v, want=%v", got, want) 216 } 217 if got, want := g, 203; got != want { 218 t.Errorf("invalid green component: got=%v, want=%v", got, want) 219 } 220 if got, want := b, 0; got != want { 221 t.Errorf("invalid blue component: got=%v, want=%v", got, want) 222 } 223 } 224 225 func TestGetFillSpotColor(t *testing.T) { 226 pdf := fpdf.New("P", "mm", "A4", "") 227 pdf.AddSpotColor("RAL 5018", 81, 0, 5, 48) 228 pdf.SetFillSpotColor("RAL 5018", 100) 229 230 name, c, m, y, k := pdf.GetFillSpotColor() 231 232 if got, want := name, "RAL 5018"; got != want { 233 t.Errorf("invalid spot color name: got=%v, want=%v", got, want) 234 } 235 if got, want := c, byte(81); got != want { 236 t.Errorf("invalid cyan component: got=%v, want=%v", got, want) 237 } 238 if got, want := m, byte(0); got != want { 239 t.Errorf("invalid magenta component: got=%v, want=%v", got, want) 240 } 241 if got, want := y, byte(5); got != want { 242 t.Errorf("invalid yellow component: got=%v, want=%v", got, want) 243 } 244 if got, want := k, byte(48); got != want { 245 t.Errorf("invalid black component: got=%v, want=%v", got, want) 246 } 247 } 248 249 func TestGetFontFamily(t *testing.T) { 250 pdf := fpdf.New("P", "mm", "A4", "") 251 pdf.SetFont("Times", "", 12) 252 253 fontFamily := pdf.GetFontFamily() 254 255 if got, want := fontFamily, "times"; got != want { 256 t.Errorf("invalid fontFamily: got=%v, want=%v", got, want) 257 } 258 } 259 260 type testFontLoader struct { 261 reader io.Reader 262 err error 263 } 264 265 func (tfl *testFontLoader) Open(name string) (io.Reader, error) { 266 return tfl.reader, tfl.err 267 } 268 269 func TestGetFontLoader(t *testing.T) { 270 testErr := errors.New("TestGetFontLoader error") 271 tfl := &testFontLoader{ 272 reader: strings.NewReader("TestGetFontLoader reader"), 273 err: testErr, 274 } 275 276 pdf := fpdf.New("P", "mm", "A4", "") 277 pdf.SetFontLoader(tfl) 278 279 fontLoader := pdf.GetFontLoader() 280 281 reader, err := fontLoader.Open("test") 282 283 if got := reader; got == nil { 284 t.Fatalf("invalid reader: got=nil, want non-nil") 285 } 286 if got, want := err, testErr; got != want { 287 t.Errorf("invalid error: got=%v, want=%v", got, want) 288 } 289 290 read, err := io.ReadAll(reader) 291 if err != nil { 292 t.Errorf("reading error: got=%v, want=nil", err) 293 } 294 if got, want := string(read[:]), "TestGetFontLoader reader"; got != want { 295 t.Errorf("invalid reader content: got=%v, want=%v", got, want) 296 } 297 } 298 299 func TestGetFontLocation(t *testing.T) { 300 pdf := fpdf.New("P", "mm", "A4", "") 301 pdf.SetFontLocation("test-font-location") 302 303 fontLocation := pdf.GetFontLocation() 304 305 if got, want := fontLocation, "test-font-location"; got != want { 306 t.Errorf("invalid fontLocation: got=%v, want=%v", got, want) 307 } 308 } 309 310 func TestGetFontSize(t *testing.T) { 311 pdf := fpdf.New("P", "mm", "A4", "") 312 pdf.SetFontSize(19) 313 314 ptSize, _ := pdf.GetFontSize() 315 316 if got, want := ptSize, 19.0; !floatEqual(got, want) { 317 t.Errorf("invalid ptSize: got=%v, want=%v", got, want) 318 } 319 320 pdf.SetFontUnitSize(246) 321 322 _, unitSize := pdf.GetFontSize() 323 324 if got, want := unitSize, 246.0; !floatEqual(got, want) { 325 t.Errorf("invalid unitSize: got=%v, want=%v", got, want) 326 } 327 } 328 329 func TestGetFontStyle(t *testing.T) { 330 pdf := fpdf.New("P", "mm", "A4", "") 331 pdf.SetFont("Arial", "BIUS", 12) 332 333 fontStyle := pdf.GetFontStyle() 334 335 if got, want := len(fontStyle), 4; got != want { 336 t.Errorf("invalid fontStyle length: got=%v, want=%v", got, want) 337 } 338 if got, want := strings.Contains(fontStyle, "B"), true; got != want { 339 t.Errorf("missing bold style: got=%v, want=%v", got, want) 340 } 341 if got, want := strings.Contains(fontStyle, "I"), true; got != want { 342 t.Errorf("missing italic style: got=%v, want=%v", got, want) 343 } 344 if got, want := strings.Contains(fontStyle, "U"), true; got != want { 345 t.Errorf("missing underline style: got=%v, want=%v", got, want) 346 } 347 if got, want := strings.Contains(fontStyle, "S"), true; got != want { 348 t.Errorf("missing strikeout style: got=%v, want=%v", got, want) 349 } 350 } 351 352 func TestGetJavascript(t *testing.T) { 353 const want = `<script>console.log('fpdf is awesome')</script>` 354 pdf := fpdf.New("P", "mm", "A4", "") 355 356 if got, want := pdf.GetJavascript(), ""; got != want { 357 t.Errorf("invalid javascript: got=%v, want=%v", got, want) 358 } 359 360 { 361 want := "" 362 pdf.SetJavascript(want) 363 if got := pdf.GetJavascript(); got != want { 364 t.Errorf("invalid javascript: got=%v, want=%v", got, want) 365 } 366 } 367 368 pdf.SetJavascript(want) 369 370 got := pdf.GetJavascript() 371 if got != want { 372 t.Errorf("invalid javascript: got=%v, want=%v", got, want) 373 } 374 } 375 376 func TestGetKeywords(t *testing.T) { 377 pdf := fpdf.New("P", "mm", "A4", "") 378 pdf.SetKeywords("test keywords", false) 379 380 keywords := pdf.GetKeywords() 381 382 if got, want := keywords, "test keywords"; got != want { 383 t.Errorf("invalid keywords: got=%v, want=%v", got, want) 384 } 385 } 386 387 func TestGetLang(t *testing.T) { 388 pdf := fpdf.New("P", "mm", "A4", "") 389 pdf.SetLang("de-CH") 390 391 lang := pdf.GetLang() 392 393 if got, want := lang, "de-CH"; got != want { 394 t.Errorf("invalid lang: got=%v, want=%v", got, want) 395 } 396 } 397 398 func TestGetLineCapStyle(t *testing.T) { 399 pdf := fpdf.New("P", "mm", "A4", "") 400 pdf.SetLineCapStyle("round") 401 402 lineCapStyle := pdf.GetLineCapStyle() 403 404 if got, want := lineCapStyle, "round"; got != want { 405 t.Errorf("invalid lineCapStyle: got=%v, want=%v", got, want) 406 } 407 } 408 409 func TestGetLineJoinStyle(t *testing.T) { 410 pdf := fpdf.New("P", "mm", "A4", "") 411 pdf.SetLineJoinStyle("bevel") 412 413 lineJoinStyle := pdf.GetLineJoinStyle() 414 415 if got, want := lineJoinStyle, "bevel"; got != want { 416 t.Errorf("invalid lineJoinStyle: got=%v, want=%v", got, want) 417 } 418 } 419 420 func TestGetLineWidth(t *testing.T) { 421 pdf := fpdf.New("P", "mm", "A4", "") 422 pdf.SetLineWidth(42) 423 424 lineWidth := pdf.GetLineWidth() 425 426 if got, want := lineWidth, 42.0; !floatEqual(got, want) { 427 t.Errorf("invalid lineWidth: got=%v, want=%v", got, want) 428 } 429 } 430 431 func TestGetMargins(t *testing.T) { 432 pdf := fpdf.New("P", "mm", "A4", "") 433 pdf.SetMargins(17, 6, 3) 434 pdf.SetAutoPageBreak(true, 3.14) 435 436 left, top, right, bottom := pdf.GetMargins() 437 438 if got, want := left, 17.0; !floatEqual(got, want) { 439 t.Errorf("invalid left margin: got=%v, want=%v", got, want) 440 } 441 if got, want := top, 6.0; !floatEqual(got, want) { 442 t.Errorf("invalid top margin: got=%v, want=%v", got, want) 443 } 444 if got, want := right, 3.0; !floatEqual(got, want) { 445 t.Errorf("invalid right margin: got=%v, want=%v", got, want) 446 } 447 if got, want := bottom, 3.14; !floatEqual(got, want) { 448 t.Errorf("invalid bottom margin: got=%v, want=%v", got, want) 449 } 450 } 451 452 func TestGetModificationDate(t *testing.T) { 453 setDate, _ := time.Parse(time.RFC3339, "9-08-02T09:54:32Z") 454 pdf := fpdf.New("P", "mm", "A4", "") 455 pdf.SetModificationDate(setDate) 456 457 modificationDate := pdf.GetModificationDate() 458 459 if got, want := modificationDate, setDate; !got.Equal(want) { 460 t.Errorf("invalid modificationDate: got=%v, want=%v", got, want) 461 } 462 } 463 464 func TestGetPageSize(t *testing.T) { 465 pdf := fpdf.New("P", "pt", "A4", "") 466 467 pageWidth, pageHeight := pdf.GetPageSize() 468 469 if got, want := pageWidth, 595.28; !floatEqual(got, want) { 470 t.Errorf("invalid pageWidth: got=%v, want=%v", got, want) 471 } 472 if got, want := pageHeight, 841.89; !floatEqual(got, want) { 473 t.Errorf("invalid pageHeight: got=%v, want=%v", got, want) 474 } 475 } 476 477 func TestGetProducer(t *testing.T) { 478 pdf := fpdf.New("P", "mm", "A4", "") 479 pdf.SetProducer("John Doe", false) 480 481 producer := pdf.GetProducer() 482 483 if got, want := producer, "John Doe"; got != want { 484 t.Errorf("invalid producer: got=%v, want=%v", got, want) 485 } 486 } 487 488 func TestGetSubject(t *testing.T) { 489 pdf := fpdf.New("P", "mm", "A4", "") 490 pdf.SetSubject("test subject", false) 491 492 subject := pdf.GetSubject() 493 494 if got, want := subject, "test subject"; got != want { 495 t.Errorf("invalid subject: got=%v, want=%v", got, want) 496 } 497 } 498 499 func TestGetTextColor(t *testing.T) { 500 pdf := fpdf.New("P", "mm", "A4", "") 501 pdf.SetTextColor(255, 203, 0) 502 503 r, g, b := pdf.GetTextColor() 504 505 if got, want := r, 255; got != want { 506 t.Errorf("invalid red component: got=%v, want=%v", got, want) 507 } 508 if got, want := g, 203; got != want { 509 t.Errorf("invalid green component: got=%v, want=%v", got, want) 510 } 511 if got, want := b, 0; got != want { 512 t.Errorf("invalid blue component: got=%v, want=%v", got, want) 513 } 514 } 515 516 func TestGetTextSpotColor(t *testing.T) { 517 pdf := fpdf.New("P", "mm", "A4", "") 518 pdf.AddSpotColor("RAL 5018", 81, 0, 5, 48) 519 pdf.SetTextSpotColor("RAL 5018", 100) 520 521 name, c, m, y, k := pdf.GetTextSpotColor() 522 523 if got, want := name, "RAL 5018"; got != want { 524 t.Errorf("invalid spot color name: got=%v, want=%v", got, want) 525 } 526 if got, want := c, byte(81); got != want { 527 t.Errorf("invalid cyan component: got=%v, want=%v", got, want) 528 } 529 if got, want := m, byte(0); got != want { 530 t.Errorf("invalid magenta component: got=%v, want=%v", got, want) 531 } 532 if got, want := y, byte(5); got != want { 533 t.Errorf("invalid yellow component: got=%v, want=%v", got, want) 534 } 535 if got, want := k, byte(48); got != want { 536 t.Errorf("invalid black component: got=%v, want=%v", got, want) 537 } 538 } 539 540 func TestGetTitle(t *testing.T) { 541 pdf := fpdf.New("P", "mm", "A4", "") 542 pdf.SetTitle("test title", false) 543 544 title := pdf.GetTitle() 545 546 if got, want := title, "test title"; got != want { 547 t.Errorf("invalid title: got=%v, want=%v", got, want) 548 } 549 } 550 551 func TestGetUnderlineThickness(t *testing.T) { 552 pdf := fpdf.New("P", "mm", "A4", "") 553 pdf.SetUnderlineThickness(17) 554 555 underlineThickness := pdf.GetUnderlineThickness() 556 557 if got, want := underlineThickness, 17.0; !floatEqual(got, want) { 558 t.Errorf("invalid underlineThickness: got=%v, want=%v", got, want) 559 } 560 } 561 562 func TestGetWordSpacing(t *testing.T) { 563 pdf := fpdf.New("P", "mm", "A4", "") 564 pdf.SetWordSpacing(6) 565 566 wordSpacing := pdf.GetWordSpacing() 567 568 if got, want := wordSpacing, 6.0; !floatEqual(got, want) { 569 t.Errorf("invalid wordSpacing: got=%v, want=%v", got, want) 570 } 571 } 572 573 func TestGetX(t *testing.T) { 574 pdf := fpdf.New("P", "mm", "A4", "") 575 pdf.SetX(17) 576 577 x := pdf.GetX() 578 579 if got, want := x, 17.0; !floatEqual(got, want) { 580 t.Errorf("invalid x coordinate: got=%v, want=%v", got, want) 581 } 582 } 583 584 func TestGetXmpMetadata(t *testing.T) { 585 pdf := fpdf.New("P", "mm", "A4", "") 586 pdf.SetXmpMetadata([]byte("test xmp metadata")) 587 588 xmpMetadata := pdf.GetXmpMetadata() 589 590 if got, want := string(xmpMetadata[:]), "test xmp metadata"; got != want { 591 t.Errorf("invalid xmpMetadata: got=%v, want=%v", got, want) 592 } 593 } 594 595 func TestGetXY(t *testing.T) { 596 pdf := fpdf.New("P", "mm", "A4", "") 597 pdf.SetXY(42, 4.13) 598 599 x, y := pdf.GetXY() 600 601 if got, want := x, 42.0; !floatEqual(got, want) { 602 t.Errorf("invalid x coordinate: got=%v, want=%v", got, want) 603 } 604 if got, want := y, 4.13; !floatEqual(got, want) { 605 t.Errorf("invalid y coordinate: got=%v, want=%v", got, want) 606 } 607 } 608 609 func TestGetY(t *testing.T) { 610 pdf := fpdf.New("P", "mm", "A4", "") 611 pdf.SetY(4.13) 612 613 y := pdf.GetY() 614 615 if got, want := y, 4.13; !floatEqual(got, want) { 616 t.Errorf("invalid y coordinate: got=%v, want=%v", got, want) 617 } 618 }