207 Star 1.3K Fork 507

GVPofdrw / ofdrw

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DrawContextTest.java 48.06 KB
一键复制 编辑 原始数据 按行查看 历史
Cliven 提交于 2023-12-08 19:24 . 增加了canvas测量文字大小的方法
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
package org.ofdrw.layout.element.canvas;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.ofdrw.core.basicType.ST_Array;
import org.ofdrw.core.graph.pathObj.AbbreviatedData;
import org.ofdrw.core.pageDescription.drawParam.LineCapType;
import org.ofdrw.core.pageDescription.drawParam.LineJoinType;
import org.ofdrw.font.FontName;
import org.ofdrw.layout.OFDDoc;
import org.ofdrw.layout.PageLayout;
import org.ofdrw.layout.VirtualPage;
import org.ofdrw.layout.element.Position;
import org.ofdrw.pkg.tool.ElemCup;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Canvas 测试
*
* @author 权观宇
* @since 2020-05-02 18:25:25
*/
class DrawContextTest {
/**
* 外部字体测试
*/
@Test
void testExternalFont() throws IOException {
Path outP = Paths.get("target/Canvas-ExternalFont.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
// 添加外部字体
ctx.addFont("仿宋_裁剪", Paths.get("src/test/resources/仿宋_GB2312_subset.ttf"));
ctx.font = "italic 400 5mm 仿宋_裁剪";
ctx.fillStyle = "#0000FF";
String text = "你好 Hello World!";
double width = ctx.measureText(text).width;
System.out.println(">> 文字宽度: " + width + "mm");
ctx.fillText(text, 10, 50);
ctx.fillText(text, 10 + width, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
/**
* 字体样式设置 完整参数
*/
@Test
void testFontStyle1() throws IOException {
Path outP = Paths.get("target/Canvas-fontstyle-1.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
// 斜体 400粗 5mm字号 宋体
ctx.font = "italic 400 5mm 宋体";
ctx.fillStyle = "#0000FF";
String text = "你好 Hello World!";
double width = ctx.measureText(text).width;
System.out.println(">> 文字宽度: " + width + "mm");
ctx.fillText(text, 10, 50);
ctx.fillText(text, 10 + width, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
/**
* 字体样式设置 必要参数
*/
@Test
void testFontStyle2() throws IOException {
Path outP = Paths.get("target/Canvas-fontstyle-2.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.font = "5mm 黑体";
ctx.fillStyle = "#0000FF";
String text = "你好 Hello World!";
double width = ctx.measureText(text).width;
ctx.fillText(text, 10, 50);
ctx.fillText(text, 10 + width, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
public void testCreateRadialGradient() throws IOException {
ElemCup.ENABLE_DEBUG_PRINT = true;
Path outP = Paths.get("target/CreateRadialGradient.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(1000d, 1000d);
Canvas canvas = new Canvas(350, 50, 200, 150);
canvas.setDrawer(ctx -> {
CanvasRadialGradient gradient = ctx.createRadialGradient(40, 70, 10, 140, 70, 50);
gradient.addColorStop(0, "rgb(255,255,0)");
gradient.addColorStop(1, "rgb(0,0,255)");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 200, 150);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println(">> 生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testCreatePattern() throws IOException {
ElemCup.ENABLE_DEBUG_PRINT = true;
Path outP = Paths.get("target/CreatePattern.ofd");
Path img = Paths.get("src/test/resources/lamp.jpg");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
final PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setX(0D).setY(0D);
canvas.setDrawer(ctx -> {
CanvasPattern pattern = ctx.createPattern(img, "repeat");
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, 30, 30);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println(">> 生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testCreatePatternSize() throws IOException {
ElemCup.ENABLE_DEBUG_PRINT = true;
Path outP = Paths.get("target/CreatePatternSize.ofd");
Path img = Paths.get("src/test/resources/lamp.jpg");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
final PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setX(0D).setY(0D);
canvas.setDrawer(ctx -> {
CanvasPattern pattern = ctx.createPattern(img, "repeat");
pattern.setImageSize(10, 10);
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, 30, 30);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println(">> 生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testCreatePatternStyle2() throws IOException {
ElemCup.ENABLE_DEBUG_PRINT = true;
Path outP = Paths.get("target/CreatePattern2.ofd");
Path img = Paths.get("src/test/resources/lamp.jpg");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
final PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setX(0D).setY(0D);
canvas.setDrawer(ctx -> {
CanvasPattern pattern = ctx.createPattern(img, "row-column");
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, 30, 30);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println(">> 生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testCreatePatternCTM() throws IOException {
ElemCup.ENABLE_DEBUG_PRINT = true;
Path outP = Paths.get("target/CreatePatternCTM.ofd");
Path img = Paths.get("src/test/resources/lamp.jpg");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
final PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setX(0D).setY(0D);
canvas.setDrawer(ctx -> {
AffineTransform matrix = new AffineTransform();
CanvasPattern pattern = ctx.createPattern(img, "repeat");
matrix.rotate(-45);
matrix.scale(5d, 5d);
pattern.setTransform(matrix);
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, 30, 30);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println(">> 生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testCreateLinearGradient() throws IOException {
ElemCup.ENABLE_DEBUG_PRINT = true;
Path outP = Paths.get("target/CreateLinearGradient.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
final PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setX(0D).setY(0D);
canvas.setDrawer(ctx -> {
CanvasGradient my_gradient = ctx.createLinearGradient(0, 0, 170, 0);
my_gradient.addColorStop(0, "black");
my_gradient.addColorStop(0.5, "red");
my_gradient.addColorStop(1, "white");
ctx.fillStyle = my_gradient;
ctx.fillRect(20, 20, 150, 100);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println(">> 生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testFillStyle() throws IOException {
ElemCup.ENABLE_DEBUG_PRINT = true;
Path outP = Paths.get("target/FillStyle.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
final PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setX(0D).setY(0D);
canvas.setDrawer(ctx -> {
ctx.fillStyle = "#FF0000";
ctx.fillRect(20, 20, 150, 100);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
;
System.out.println(">> 生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testStrokeStyle() throws IOException {
ElemCup.ENABLE_DEBUG_PRINT = true;
Path outP = Paths.get("target/StrokeStyle.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
final PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setX(0D).setY(0D);
canvas.setDrawer(ctx -> {
ctx.strokeStyle = "#FF0000";
ctx.strokeRect(20, 20, 150, 100);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println(">> 生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testLineTo() throws IOException {
Path outP = Paths.get("target/LineTo.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
final PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setX(0D).setY(0D);
canvas.setDrawer(ctx -> {
// ctx.setFont(FontSetting.getInstance()).fillText("Hello world!", 0, 5);
ctx.beginPath()
.moveTo(0, 0)
.lineTo(100, 200)
.setStrokeColor(0, 0, 0)
.setLineWidth(1)
.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void stroke() throws IOException {
Path outP = Paths.get("target/CanvasLineCreate.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath()
.moveTo(20, 20)
.lineTo(20, 100)
.lineTo(70, 100)
.setStrokeColor(0, 255, 0)
.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void stroke2() throws IOException {
Path outP = Paths.get("target/stroke2.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.rect(20, 20, 40, 20);
ctx.setStrokeColor(255, 0, 0);
ctx.stroke();
ctx.rect(30, 30, 50, 30);
ctx.setStrokeColor(0, 0, 255);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void strokeRect2() throws IOException {
Path outP = Paths.get("target/strokeRect2.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.rect(20, 20, 40, 20);
ctx.setStrokeColor(255, 0, 0);
ctx.strokeRect(30, 30, 50, 30);
ctx.setStrokeColor(0, 255, 0);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void fillRect2() throws IOException {
Path outP = Paths.get("target/fillRect2.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.rect(20, 20, 40, 20);
ctx.setFillColor(255, 0, 0);
ctx.fillRect(30, 30, 50, 30);
ctx.setFillColor(0, 255, 0);
ctx.fill();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void fill() throws IOException {
Path outP = Paths.get("target/Canvas-fill.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath()
.moveTo(20, 20)
.lineTo(20, 100)
.lineTo(70, 100)
.closePath()
.setFillColor(255, 0, 0)
.stroke()
.fill();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void beginPath() throws IOException {
Path outP = Paths.get("target/Canvas-beginPath.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath();
ctx.setLineWidth(3d);
ctx.setStrokeColor(255, 0, 0);
ctx.moveTo(0, 75);
ctx.lineTo(150, 75);
ctx.stroke();
ctx.beginPath();
ctx.setStrokeColor(0, 0, 255);
ctx.moveTo(10, 0);
ctx.lineTo(150, 130);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void clip() throws IOException {
Path outP = Paths.get("target/Canvas-clip.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
// 剪切矩形区域
ctx.rect(20, 20, 40, 20);
ctx.setFillColor(255, 0, 0);
ctx.fill();
ctx.clip();
// 在 clip() 之后绘制绿色矩形
ctx.setFillColor(0, 255, 0);
ctx.fillRect(30, 30, 50, 30);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void clipCTM() throws IOException {
Path outP = Paths.get("target/Canvas-clip-ctm.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(new PageLayout(500d, 500d));
Canvas canvas = new Canvas(400d, 400d);
canvas.setPosition(Position.Absolute)
.setX(50d).setY(50d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
// 剪切矩形区域
ctx.translate(50, 50);
ctx.rect(0, 0, 100, 100);
ctx.clip();
ctx.setFillColor(255, 0, 0);
ctx.fill();
// 在 clip() 之后绘制绿色矩形
ctx.rotate(20);
ctx.setFillColor(0, 255, 0);
ctx.fillRect(0, 0, 100, 100);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void quadraticCurveTo() throws IOException {
Path outP = Paths.get("target/Canvas-quadraticCurveTo.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.quadraticCurveTo(20, 100, 200, 20);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void bezierCurveTo() throws IOException {
Path outP = Paths.get("target/Canvas-bezierCurveTo.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.bezierCurveTo(20, 100, 200, 100, 200, 20);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void arc() throws IOException {
Path outP = Paths.get("target/Canvas-arc.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath();
ctx.arc(100, 75, 50, 0, 360);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void rect() throws IOException {
Path outP = Paths.get("target/Canvas-rect.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.rect(20, 20, 150, 100);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void strokeRect() throws IOException {
Path outP = Paths.get("target/Canvas-strokeRect.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.strokeRect(20, 20, 150, 100);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void fillRect() throws IOException {
Path outP = Paths.get("target/Canvas-fillRect.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.fillRect(20, 20, 150, 100);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void drawImage() throws IOException {
Path outP = Paths.get("target/Canvas-drawImage.ofd");
Path imgPath = Paths.get("src/test/resources/eg_tulip.jpg");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.drawImage(imgPath, 10, 10, 120, 80);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void drawImagePart() throws IOException {
Path outP = Paths.get("target/drawImage_part.ofd");
Path imgPath = Paths.get("src/test/resources/rhino.jpg");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setXY(0d, 0d);
canvas.setDrawer(ctx -> {
ctx.PPM = 1d;
ctx.drawImage(imgPath,
ctx.mm(33), ctx.mm(71), ctx.mm(104), ctx.mm(124),
ctx.mm(21), ctx.mm(20), ctx.mm(87), ctx.mm(104)
);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath());
}
@Test
void drawImageLeftTop() throws IOException {
Path outP = Paths.get("target/drawImage_left_top.ofd");
Path imgPath = Paths.get("src/test/resources/rhino.jpg");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
PageLayout pageLayout = ofdDoc.getPageLayout();
VirtualPage vPage = new VirtualPage(pageLayout);
Canvas canvas = new Canvas(pageLayout.getWidth(), pageLayout.getHeight());
canvas.setPosition(Position.Absolute).setXY(0d, 0d);
canvas.setDrawer(ctx -> {
ctx.PPM = 3d;
ctx.drawImage(imgPath, 0, 0);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath());
}
@Test
void drawImage2() throws IOException {
Path outP = Paths.get("target/Canvas-drawImage2.ofd");
Path imgPath = Paths.get("src/test/resources/eg_tulip.jpg");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.rotate(20);
ctx.drawImage(imgPath, 10, 10, 60, 40);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void scale() throws IOException {
Path outP = Paths.get("target/Canvas-scale.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.strokeRect(5, 5, 10, 6);
ctx.scale(2, 2);
ctx.strokeRect(5, 5, 10, 6);
ctx.scale(2, 2);
ctx.strokeRect(5, 5, 10, 6);
ctx.scale(2, 2);
ctx.strokeRect(5, 5, 10, 6);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void rotate() throws IOException {
Path outP = Paths.get("target/Canvas-rotate.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.rotate(20);
ctx.fillRect(50, 20, 100, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void translate() throws IOException {
Path outP = Paths.get("target/Canvas-translate.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.fillRect(10, 10, 50, 25);
ctx.translate(30, 30);
ctx.fillRect(10, 10, 50, 25);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void transform() throws IOException {
Path outP = Paths.get("target/Canvas-transform.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.setFillColor(255, 255, 0);
ctx.fillRect(0, 0, 100, 50);
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.setFillColor(255, 0, 0);
ctx.fillRect(0, 0, 100, 50);
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.setFillColor(0, 0, 255);
ctx.fillRect(0, 0, 100, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void setTransform() throws IOException {
Path outP = Paths.get("target/Canvas-setTransform.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.setFillColor(255, 255, 0);
ctx.fillRect(0, 0, 100, 50);
ctx.setTransform(1, 0.5, -0.5, 1, 30, 10);
ctx.setFillColor(255, 0, 0);
ctx.fillRect(0, 0, 100, 50);
ctx.setTransform(1, 0.5, -0.5, 1, 30, 10);
ctx.setFillColor(0, 0, 255);
ctx.fillRect(0, 0, 100, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void getGlobalAlpha() throws IOException {
Path outP = Paths.get("target/Canvas-globalAlpha.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.setFillColor(255, 0, 0);
ctx.fillRect(20, 20, 75, 50);
// 调节透明度
ctx.setGlobalAlpha(0.2);
ctx.setFillColor(0, 0, 255);
ctx.fillRect(50, 50, 75, 50);
ctx.setFillColor(0, 255, 0);
ctx.fillRect(80, 80, 75, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void save() throws IOException {
Path outP = Paths.get("target/Canvas-save.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.save();
ctx.setFillColor(0, 255, 0);
ctx.fillRect(10, 10, 30, 30);
ctx.restore();
ctx.fillRect(40, 50, 30, 30);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void fillText() throws IOException {
Path outP = Paths.get("target/Canvas-fillText.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
FontSetting fontSetting = new FontSetting(5, FontName.SimSun.font())
.setCharDirection(180)
.setReadDirection(90);
ctx.setFont(fontSetting);
ctx.fillText("你好 Hello World!", 10, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void fillTextDefault() throws IOException {
Path outP = Paths.get("target/Canvas-fillText-Default-font.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.fillText("你好 Hello World!", 10, 50);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void measureText() throws IOException {
Path outP = Paths.get("target/Canvas-measureText.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
FontSetting fontSetting = new FontSetting(5, FontName.SimSun.font());
ctx.setFont(fontSetting);
String text = "你好 Hello World!";
double width = ctx.measureText(text).width;
System.out.println(">> 文字宽度: " + width + "mm");
ctx.fillText(text, 10, 50);
ctx.fillText(text, 10 + width, 50);
// Assertions.assertEquals();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
/**
* 测量字体所占空间
*/
@Test
void measureTextArea() throws IOException {
Path outP = Paths.get("target/Canvas-measureTextArea.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
FontSetting fontSetting = new FontSetting(5, FontName.SimSun.font());
ctx.setFont(fontSetting);
String text = "你好 Hello World!";
TextMetricsArea area = ctx.measureTextArea(text);
System.out.println(">> 文字宽度: " + area.width + "mm");
ctx.fillText(text, 10, 50);
ctx.fillText(text, 10, 50 + area.height);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
@Test
void fillTextAllDirection() throws IOException {
int[] readDirect = {0, 180, 90, 270};
int[] charDirect = {0, 180, 90, 270};
for (int r : readDirect) {
for (int c : charDirect) {
String fileName = String.format("Canvas-fillText-C%dR%d.ofd", c, r);
Path outP = Paths.get("target", fileName);
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
FontSetting fontSetting = new FontSetting(5, FontName.SimSun.font())
.setCharDirection(c)
.setReadDirection(r);
ctx.setFont(fontSetting);
ctx.fillText("li 你好 Hello World!", 100, 100);
ctx.beginPath();
if (r == 0 || r == 180) {
ctx.moveTo(100, 90);
ctx.lineTo(100, 110);
ctx.moveTo(40, 100);
ctx.lineTo(160, 100);
ctx.stroke();
} else {
ctx.moveTo(90, 100);
ctx.lineTo(110, 100);
ctx.moveTo(100, 40);
ctx.lineTo(100, 160);
ctx.stroke();
}
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
}
}
@Test
void textAlign() throws IOException {
Path outP = Paths.get("target/Canvas-textAlign.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
// 在位置 150 创建蓝线
ctx.setStrokeColor(0, 0, 255);
ctx.moveTo(150, 20);
ctx.lineTo(150, 170);
ctx.stroke();
ctx.setFont(new FontSetting(5, FontName.SimSun.font()));
// 显示不同的 textAlign 值
ctx.setTextAlign(TextAlign.start);
ctx.fillText("textAlign=start", 150, 60);
ctx.setTextAlign(TextAlign.end);
ctx.fillText("textAlign=end", 150, 80).setTextAlign(TextAlign.left);
ctx.fillText("textAlign=left", 150, 100);
ctx.setTextAlign(TextAlign.center);
ctx.fillText("textAlign=center", 150, 120);
ctx.setTextAlign(TextAlign.right);
ctx.fillText("textAlign=right", 150, 140);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
/**
* 设置端点样式
*
* @throws IOException
*/
@Test
void setLineCap() throws IOException {
Path outP = Paths.get("target/LineCap.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath();
ctx.setLineWidth(10d);
ctx.setLineCap(LineCapType.Butt);
ctx.moveTo(20, 20);
ctx.lineTo(150, 20);
ctx.stroke();
ctx.beginPath();
ctx.setLineCap(LineCapType.Round);
ctx.moveTo(20, 40);
ctx.lineTo(150, 40);
ctx.stroke();
ctx.beginPath();
ctx.setLineCap(LineCapType.Square);
ctx.moveTo(20, 60);
ctx.lineTo(150, 60);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
/**
* 两条线相交时,所创建的拐角类型
*
* @throws IOException
*/
@Test
void setLineJoin() throws IOException {
Path outP = Paths.get("target/LineJoin.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath();
ctx.setLineWidth(5d);
ctx.setLineJoin(LineJoinType.Round);
ctx.moveTo(20, 30);
ctx.lineTo(50, 50);
ctx.lineTo(20, 60);
ctx.stroke();
ctx.beginPath();
ctx.setLineJoin(LineJoinType.Bevel);
ctx.moveTo(80, 30);
ctx.lineTo(110, 50);
ctx.lineTo(80, 60);
ctx.stroke();
ctx.beginPath();
ctx.setLineJoin(LineJoinType.Miter);
ctx.moveTo(140, 30);
ctx.lineTo(170, 50);
ctx.lineTo(140, 60);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
/**
* 两条线相交时,所创建的拐角类型
*
* @throws IOException
*/
@Test
void setMiterLimit() throws IOException {
Path outP = Paths.get("target/MiterLimit.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath();
ctx.setLineWidth(5d);
ctx.setLineJoin(LineJoinType.Miter);
ctx.setMiterLimit(4d);
ctx.moveTo(20, 20);
ctx.lineTo(50, 27);
ctx.lineTo(20, 34);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
/**
* 设置线段虚线样式
*
* @throws IOException
*/
@Test
void setLineDash() throws IOException {
Path outP = Paths.get("target/LineDash.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(ofdDoc.getPageLayout());
Canvas canvas = new Canvas(200d, 200d);
canvas.setPosition(Position.Absolute)
.setX(5d).setY(45d)
.setBorder(1d);
canvas.setDrawer(ctx -> {
ctx.beginPath();
ctx.setLineWidth(10d);
ctx.moveTo(20, 30);
ctx.lineTo(150, 30);
ctx.stroke();
ctx.beginPath();
ctx.setLineDash(15d, 15d);
ctx.moveTo(20, 45);
ctx.lineTo(150, 45);
ctx.stroke();
ctx.beginPath();
ctx.setLineDash(5d, new Double[]{15d, 15d});
ctx.moveTo(20, 65);
ctx.lineTo(150, 65);
ctx.stroke();
ctx.beginPath();
ctx.setLineDash(15d, 7.5d);
ctx.moveTo(20, 80);
ctx.lineTo(150, 80);
ctx.stroke();
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}
/**
* 测试 绘制参数 缓存
*/
@Test
public void testMultiColorSet() throws Exception {
Path outP = Paths.get("target/testMultiColorSet.ofd");
try (OFDDoc ofdDoc = new OFDDoc(outP)) {
VirtualPage vPage = new VirtualPage(500d, 500d);
Canvas canvas = new Canvas(500d, 500d);
canvas.setPosition(Position.Absolute);
canvas.setXY(0d, 0d);
canvas.setDrawer(ctx -> {
ctx.setFillColor(255, 0, 0);
ctx.fillRect(100, 100, 100, 100);
ctx.setFillColor(0, 0, 255);
ctx.fillRect(200, 200, 100, 100);
ctx.setFillColor(255, 0, 0);
ctx.fillRect(300, 300, 100, 100);
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + outP.toAbsolutePath());
}
@Test
public void testTransform() {
// 平移
AbbreviatedData at = new AbbreviatedData();
at.moveTo(0, 0);
ST_Array ctm = new ST_Array(
1, 0,
0, 1,
30, 50
);
DrawContext.transform(at, ctm);
Assertions.assertEquals(30, at.getRawOptVal().get(0).values[0]);
Assertions.assertEquals(50, at.getRawOptVal().get(0).values[1]);
}
@Test
public void testCanvas() throws IOException {
Path out = Paths.get("target/first_canvas.ofd");
try (OFDDoc ofdDoc = new OFDDoc(out)) {
VirtualPage vPage = new VirtualPage(210d, 297d);
Canvas canvas = new Canvas(0, 0, 210d, 297d);
canvas.setDrawer(ctx -> {
ctx.fillStyle = "rgba(255, 0, 0, 0.8)";
ctx.font = "8mm 宋体";
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
ctx.save();
ctx.translate(22.4 * i, j * 50);
ctx.rotate(45);
ctx.fillText("保密资料", 10, 10);
ctx.restore();
}
}
});
vPage.add(canvas);
ofdDoc.addVPage(vPage);
}
System.out.println("生成文档位置:" + out.toAbsolutePath());
}
}
Java
1
https://gitee.com/ofdrw/ofdrw.git
git@gitee.com:ofdrw/ofdrw.git
ofdrw
ofdrw
ofdrw
master

搜索帮助