Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add primitive use-image to use image by natural size #297

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ jobs:
run: |
(cd demo; opam exec -- make)
(cd doc; opam exec -- make)
(cd tests; opam exec -- make)
6 changes: 3 additions & 3 deletions doc/doc-primitives.saty
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,9 @@ document (|
現在の実装では実行時エラーとなり処理を中止する.
\subject-to-change;
}
+command (`use-image-by-width`) (tIMG --> (tL --> tIB)) {
\code{use-image-by-width ${img} ${w}}で
画像\code{${img}}を幅\code{${w}}の大きさで描画したものをインラインボックス列として返す
+command (`use-image`) (tIMG --> (tL --> tIB)) {
\code{use-image ${img}}で
画像\code{${img}}を自然な大きさで描画したものをインラインボックス列として返す
}
>
+subsection {グラフィックスに関する処理} <
Expand Down
17 changes: 17 additions & 0 deletions lib-satysfi/dist/packages/pervasives.satyh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ let-inline ctx \TeX =
script-guard Latin (no-break ib)


let use-image-by-width img wid =
let ib = use-image img in
let g = ib |> draw-text (0pt, 0pt) in
let ((_, mh), (w, d)) = get-graphics-bbox g in
let r = wid /' w in
let hgh = (0pt -' (mh *' r)) in
let dpt = d *' r in
inline-graphics wid hgh dpt (fun (x, y) -> (
[
g |> linear-transform-graphics r 0. 0. r
|> shift-graphics (x, y -' dpt)
]
))




let length-max len1 len2 =
if len1 <' len2 then len2 else len1

Expand Down
8 changes: 8 additions & 0 deletions src/backend/imageInfo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ let get_height_from_width key wid =
wid *% ((ymax -. ymin) /. (xmax -. xmin))


let get_size key =
let (_,(_, _, pw, ph), _) = ImageHashTable.find key in
(* points to inches *)
let wid = pw /. 72. in
let hgt = ph /. 72. in
(Length.of_inch wid, Length.of_inch hgt)


let get_ratio key wid hgt =
let (_,bbox, valuemain) = ImageHashTable.find key in
match valuemain with
Expand Down
2 changes: 2 additions & 0 deletions src/backend/imageInfo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ val add_image : abs_path -> key

val get_height_from_width : key -> length -> length

val get_size : key -> Length.t * Length.t

val get_ratio : key -> length -> length -> float * float

val get_xobject_dictionary : Pdf.t -> Pdf.pdfobject
Expand Down
10 changes: 1 addition & 9 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
DEPS=list.satyh math.satyh head.satyh

all:
satysfi $(DEPS) first.saty -o output.pdf

math1:
satysfi $(DEPS) math1.saty -o output-math1.pdf

math2:
satysfi $(DEPS) math2.saty -o output-math2.pdf
satysfi -C ../lib-satysfi clip.saty
2 changes: 2 additions & 0 deletions tests/clip.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/home/yasuo/ghq/github.com/yasuo-ozu/SATySFi/tests/clip.pdf: /home/yasuo/ghq/github.com/yasuo-ozu/SATySFi/tests/clip.saty
/home/yasuo/ghq/github.com/yasuo-ozu/SATySFi/tests/clip.pdf: /home/yasuo/ghq/github.com/yasuo-ozu/SATySFi/tests/images/peppers-rgb.jpg
16 changes: 8 additions & 8 deletions tests/clip.saty
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
@import: ../lib-satysfi/dist/packages/color


let-inline ctx \do-fill wid f =
let ib = use-image-by-width (load-image `images/peppers-rgb.jpg`) wid
let-inline ctx \do-fill f =
let ib = use-image (load-image `images/peppers-rgb.jpg`)
in
let (w, h, d) = get-natural-metrics ib in
inline-graphics w h d (fun (x, y) -> [
fill (Color.blue) (f x y w (h +' d))
])

let-inline ctx \do-clip wid f =
let ib = use-image-by-width (load-image `images/peppers-rgb.jpg`) wid
let-inline ctx \do-clip f =
let ib = use-image (load-image `images/peppers-rgb.jpg`)
in
let (w, h, d) = get-natural-metrics ib in
inline-graphics w h d (fun (x, y) -> [
Expand Down Expand Up @@ -42,11 +42,11 @@ document (|
author = {\SATySFi; Contributors};
|) '<
+p {
\do-fill (5cm) (path-circle);
\do-clip (5cm) (path-circle);
\do-fill (path-circle);
\do-clip (path-circle);
}
+p {
\do-fill (5cm) (path-donut);
\do-clip (5cm) (path-donut);
\do-fill (path-donut);
\do-clip (path-donut);
}
>
11 changes: 5 additions & 6 deletions tools/gencode/vminst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -548,24 +548,23 @@ let abspath = MyUtil.make_abs_path (Filename.concat (OptionState.job_directory (
let imgkey = ImageInfo.add_image abspath in
make_image_key imgkey
|}
; inst "BackendUseImageByWidth"
~name:"use-image-by-width"
~type_:Type.(tIMG @-> tLN @-> tIB)
; inst "BackendUseImageByNaturalSize"
~name:"use-image"
~type_:Type.(tIMG @-> tIB)
~fields:[
]
~params:[
param "valueimg";
param "wid" ~type_:"length";
]
~is_pdf_mode_primitive:true
~code:{|
match valueimg with
| BaseConstant(BCImageKey(imgkey)) ->
let hgt = ImageInfo.get_height_from_width imgkey wid in
let (wid, hgt) = ImageInfo.get_size imgkey in
make_horz (HorzBox.([HorzPure(PHGFixedImage(wid, hgt, imgkey))]))

| _ ->
report_bug_vm "BackendUseImage"
report_bug_vm "BackendUseImageByNaturalSize"
|}
; inst "BackendHookPageBreak"
~name:"hook-page-break"
Expand Down