Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 23 additions & 41 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,35 @@ jobs:
fail-fast: false
max-parallel: 20
matrix:
branch: [master]
branch: [devel, version-2-2]
TEST_LANG: [c, cpp]
target:
- os: linux
cpu: amd64
TEST_LANG: c
- os: linux
cpu: amd64
TEST_LANG: cpp
- os: linux
cpu: i386
TEST_LANG: c
- os: linux
cpu: i386
TEST_LANG: cpp
- os: macos
cpu: amd64
TEST_LANG: c
- os: macos
cpu: amd64
TEST_LANG: cpp
- os: windows
cpu: amd64
TEST_LANG: c
- os: windows
cpu: amd64
TEST_LANG: cpp
- os: windows
cpu: i386
TEST_LANG: c
- os: windows
cpu: i386
TEST_LANG: cpp
include:
- target:
os: linux
builder: ubuntu-18.04
builder: ubuntu-22.04
- target:
os: macos
builder: macos-10.15
builder: macos-13
- target:
os: windows
builder: windows-2019
builder: windows-latest

name: '${{ matrix.target.os }}-${{ matrix.target.cpu }}-${{ matrix.target.TEST_LANG }} (${{ matrix.branch }})'
name: '${{ matrix.target.os }}-${{ matrix.target.cpu }}-${{ matrix.TEST_LANG }} (${{ matrix.branch }})'
runs-on: ${{ matrix.builder }}
steps:
- name: Checkout nimPDF
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
path: nimPDF
submodules: false
Expand Down Expand Up @@ -82,15 +63,15 @@ jobs:
- name: Restore MinGW-W64 (Windows) from cache
if: runner.os == 'Windows'
id: windows-mingw-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: external/mingw-${{ matrix.target.cpu }}
key: 'mingw-${{ matrix.target.cpu }}'

- name: Restore Nim DLLs dependencies (Windows) from cache
if: runner.os == 'Windows'
id: windows-dlls-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: external/dlls-${{ matrix.target.cpu }}
key: 'dlls-${{ matrix.target.cpu }}'
Expand All @@ -102,11 +83,12 @@ jobs:
shell: bash
run: |
mkdir -p external
MINGW_BASE="https://github.com/brechtsanders/winlibs_mingw/releases/download/11.2.0-12.0.1-9.0.0-r1"
if [[ '${{ matrix.target.cpu }}' == 'amd64' ]]; then
MINGW_URL="https://sourceforge.net/projects/mingw-w64/files/Toolchains targetting Win64/Personal Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z"
MINGW_URL="$MINGW_BASE/winlibs-x86_64-posix-seh-gcc-11.2.0-mingw-w64-9.0.0-r1.7z"
ARCH=64
else
MINGW_URL="https://sourceforge.net/projects/mingw-w64/files/Toolchains targetting Win32/Personal Builds/mingw-builds/8.1.0/threads-posix/dwarf/i686-8.1.0-release-posix-dwarf-rt_v6-rev0.7z"
MINGW_URL="$MINGW_BASE/winlibs-i686-posix-dwarf-gcc-11.2.0-mingw-w64-9.0.0-r1.7z"
ARCH=32
fi
curl -L "$MINGW_URL" -o "external/mingw-${{ matrix.target.cpu }}.7z"
Expand Down Expand Up @@ -143,38 +125,38 @@ jobs:
git ls-remote "https://github.com/$1" "${2:-HEAD}" | cut -f 1
}
nimHash=$(getHash nim-lang/Nim '${{ matrix.branch }}')
csourcesHash=$(getHash nim-lang/csources)
echo "::set-output name=nim::$nimHash"
echo "::set-output name=csources::$csourcesHash"
csourcesHash=$(getHash nim-lang/csources_v2)
echo "nimHash=$nimHash" >> $GITHUB_OUTPUT
echo "csourcesHash=$csourcesHash" >> $GITHUB_OUTPUT

- name: Restore prebuilt Nim from cache
id: nim-cache
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: nim
key: 'nim-${{ matrix.target.os }}-${{ matrix.target.cpu }}-${{ steps.versions.outputs.nim }}'
key: 'nim-${{ matrix.target.os }}-${{ matrix.target.cpu }}-${{ steps.versions.outputs.nimHash }}'

- name: Restore prebuilt csources from cache
if: steps.nim-cache.outputs.cache-hit != 'true'
id: csources-cache
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: csources/bin
key: 'csources-${{ matrix.target.os }}-${{ matrix.target.cpu }}-${{ steps.versions.outputs.csources }}'
key: 'csources-${{ matrix.target.os }}-${{ matrix.target.cpu }}-${{ steps.versions.outputs.csourcesHash }}'

- name: Checkout Nim csources
if: >
steps.csources-cache.outputs.cache-hit != 'true' &&
steps.nim-cache.outputs.cache-hit != 'true'
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
repository: nim-lang/csources
repository: nim-lang/csources_v2
path: csources
ref: ${{ steps.versions.outputs.csources }}

- name: Checkout Nim
if: steps.nim-cache.outputs.cache-hit != 'true'
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
repository: nim-lang/Nim
path: nim
Expand Down Expand Up @@ -239,4 +221,4 @@ jobs:
shell: cmd
working-directory: nimPDF
run: |
env TEST_LANG="${{ matrix.target.TEST_LANG }}" nimble testvcc
env TEST_LANG="${{ matrix.TEST_LANG }}" nimble testvcc
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.exe
nimble.paths
4 changes: 4 additions & 0 deletions config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# begin Nimble config (version 1)
when fileExists("nimble.paths"):
include "nimble.paths"
# end Nimble config
31 changes: 17 additions & 14 deletions demo/test.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import streams, nimPDF, unittest
import
std/[streams, strutils],
nimPDF,
unittest

proc draw_title(doc: PDF, text:string) =
let size = getSizeFromName("A4")
Expand Down Expand Up @@ -43,48 +46,48 @@ proc test(doc: PDF) =
var tw = doc.getTextWidth(text)
var th = doc.getTextHeight(text)
check:
$tw == "11.39"
$th == "3.59"
tw.formatFloat(ffDecimal, 2) == "11.39"
th.formatFloat(ffDecimal, 2) == "3.59"

test "getTextWidth and GetTextHeight TTF":
doc.setFont("FreeMono", {FS_REGULAR}, 5)
var tw = doc.getTextWidth(text)
var th = doc.getTextHeight(text)
check:
$tw == "15.0"
$th == "3.02"
tw.formatFloat(ffDecimal, 2) == "15.00"
th.formatFloat(ffDecimal, 2) == "3.02"

test "getVTextWidth and GetVTextHeight base14":
doc.setFont("Helvetica", {FS_REGULAR}, 5)
var tw = doc.getVTextWidth(text)
var th = doc.getVTextHeight(text)
check:
$tw == "3.61"
$th == "14.05"
tw.formatFloat(ffDecimal, 2) == "3.61"
th.formatFloat(ffDecimal, 2) == "14.05"

test "getTextWidth and GetTextHeight TTF":
doc.setFont("FreeMono", {FS_REGULAR}, 5)
var tw = doc.getVTextWidth(text)
var th = doc.getVTextHeight(text)
check:
$tw == "3.0"
$th == "13.325"
tw.formatFloat(ffDecimal, 2) == "3.00"
th.formatFloat(ffDecimal, 3) == "13.325"

proc main(): bool {.discardable.} =
#echo currentSourcePath()
proc main() =
var fileName = "test.pdf"
var file = newFileStream(fileName, fmWrite)

if file != nil:
var doc = newPDF()
var opts = newPDFOptions()
opts.addFontsPath("fonts")
var doc = newPDF(opts)
doc.createPDF()
doc.writePDF(file)
doc.test()
file.close()
echo "OK"
return true
return

echo "cannot open: ", fileName
result = false

main()
43 changes: 21 additions & 22 deletions nimPDF/basic2d.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,22 @@ const
## used internally by DegToRad and RadToDeg

type
Matrix2d* = object
## Implements a row major 2d matrix, which means
## transformations are applied the order they are concatenated.
## The rightmost column of the 3x3 matrix is left out since normally
## not used for geometric transformations in 2d.
ax*,ay*,bx*,by*,tx*,ty*:float
Point2d* = object
## Implements a non-homogeneous 2d point stored as
## an `x` coordinate and an `y` coordinate.
x*,y*:float
Vector2d* = object
## Implements a 2d **direction vector** stored as
## an `x` coordinate and an `y` coordinate. Direction vector means,
## that when transforming a vector with a matrix, the translational
## part of the matrix is ignored.
x*,y*:float
{.deprecated: [TMatrix2d: Matrix2d, TPoint2d: Point2d, TVector2d: Vector2d].}
Matrix2d* = object
## Implements a row major 2d matrix, which means
## transformations are applied the order they are concatenated.
## The rightmost column of the 3x3 matrix is left out since normally
## not used for geometric transformations in 2d.
ax*,ay*,bx*,by*,tx*,ty*:float
Point2d* = object
## Implements a non-homogeneous 2d point stored as
## an `x` coordinate and an `y` coordinate.
x*,y*:float
Vector2d* = object
## Implements a 2d **direction vector** stored as
## an `x` coordinate and an `y` coordinate. Direction vector means,
## that when transforming a vector with a matrix, the translational
## part of the matrix is ignored.
x*,y*:float


# Some forward declarations...
Expand Down Expand Up @@ -287,7 +286,7 @@ proc inverse*(m:Matrix2d):Matrix2d {.noInit.} =
## will be raised.
let d=m.determinant
if d==0.0:
raise newException(DivByZeroError,"Cannot invert a zero determinant matrix")
raise newException(ValueError,"Cannot invert a zero determinant matrix")

result.setElements(
m.by/d,-m.ay/d,
Expand Down Expand Up @@ -433,7 +432,7 @@ proc normalize*(v:var Vector2d) {.inline.}=
## Modifies `v` to have a length of 1.0, keeping its angle.
## If `v` has zero length, an EDivByZero will be raised.
if not tryNormalize(v):
raise newException(DivByZeroError,"Cannot normalize zero length vector")
raise newException(ValueError,"Cannot normalize zero length vector")

proc transformNorm*(v:var Vector2d,t:Matrix2d)=
## Applies a normal direction transformation `t` onto `v` in place.
Expand All @@ -449,7 +448,7 @@ proc transformNorm*(v:var Vector2d,t:Matrix2d)=
# | | 0 0 1 | |
let d=t.determinant
if(d==0.0):
raise newException(DivByZeroError,"Matrix is not invertible")
raise newException(ValueError,"Matrix is not invertible")
let newx = (t.by*v.x-t.ay*v.y)/d
v.y = (t.ax*v.y-t.bx*v.x)/d
v.x = newx
Expand All @@ -463,7 +462,7 @@ proc transformInv*(v:var Vector2d,t:Matrix2d)=
let d=t.determinant

if(d==0.0):
raise newException(DivByZeroError,"Matrix is not invertible")
raise newException(ValueError,"Matrix is not invertible")

let newx=(t.by*v.x-t.bx*v.y)/d
v.y = (t.ax*v.y-t.ay*v.x)/d
Expand Down Expand Up @@ -705,7 +704,7 @@ proc transformInv*(p:var Point2d,t:Matrix2d){.inline.}=
# | TX TY 1 |
let d=t.determinant
if d==0.0:
raise newException(DivByZeroError,"Cannot invert a zero determinant matrix")
raise newException(ValueError,"Cannot invert a zero determinant matrix")
let
newx= (t.bx*t.ty-t.by*t.tx+p.x*t.by-p.y*t.bx)/d
p.y = -(t.ax*t.ty-t.ay*t.tx+p.x*t.ay-p.y*t.ax)/d
Expand Down
4 changes: 2 additions & 2 deletions nimPDF/fontmanager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ type

Base14* = ref object of Font
baseFont* : string
getWidth : proc(cp: int): int {.locks:0.}
getWidth : proc(cp: int): int
isFontSpecific : bool
ascent, descent, xHeight, capHeight : int
bbox : BBox
missingWidth: int
encoding*: EncodingType
encode: proc(val: int): int {.locks:0.}
encode: proc(val: int): int

TextWidth* = object
numchars*, width*, numspace*, numwords*: int
Expand Down
2 changes: 1 addition & 1 deletion nimPDF/gstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#-----------------------------------------
# graphic state

import math, basic2d, image, "subsetter/Font", tables
import basic2d, image, "subsetter/Font", tables

const
PGU_K_MM = 72/25.4
Expand Down
4 changes: 2 additions & 2 deletions nimPDF/page.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import strutils, gstate, objects, fontmanager, image, path
import tables, encryptdict, os, resources, times, "subsetter/Font"
import streams, encryptdict, encrypt, options, wtf8, unicode
import streams, encrypt, options, wtf8, unicode
import basic2d, math

const
Expand Down Expand Up @@ -472,7 +472,7 @@ proc loadImage*(doc: DocState, fileName: string): Image =
var imagePath = doc.opts.getImagesPath()
for p in imagePath:
let fName = p & DirSep & fileName
if existsFile(fName):
if fileExists(fName):
let image = loadImage(fName)
if image != nil: return image
result = nil
Expand Down
4 changes: 2 additions & 2 deletions nimPDF/subsetter/ByteArray.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ proc size*(ba: ByteArray): int = ba.storageLength

method put*(ba: ByteArray, index: int, b: char) {.base.} =
if index < 0 or index >= ba.size():
raise newIndexError("Attempt to write outside the bounds of the data.")
raise newException(ValueError, "Attempt to write outside the bounds of the data.")
ba.internalPut(index, b)
ba.filledLength = max(ba.filledLength, index + 1)

method put*(ba: ByteArray, index: int, b:string, offset, length: int): int {.base.} =
if index < 0 or index >= ba.size():
raise newIndexError("Attempt to write outside the bounds of the data.")
raise newException(ValueError, "Attempt to write outside the bounds of the data.")
let actualLength = min(length, ba.size() - index)

let bytesWritten = ba.internalPut(index, b, offset, actualLength)
Expand Down
Loading