Skip to content

Commit 44c2eb1

Browse files
authored
feat: support hatch gradient fill (#12)
1 parent b0ad10f commit 44c2eb1

7 files changed

Lines changed: 108 additions & 30 deletions

File tree

bindings/javascript/embind/binding_common.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,20 @@ emscripten::val dwg_ptr_to_hatch_path_array_wrapper(uintptr_t array_ptr, size_t
466466
return pathes;
467467
}
468468

469+
emscripten::val dwg_ptr_to_hatch_gradient_color_array_wrapper(uintptr_t array_ptr, size_t size) {
470+
Dwg_HATCH_Color* array = reinterpret_cast<Dwg_HATCH_Color*>(array_ptr);
471+
emscripten::val colors = emscripten::val::array();
472+
for (int i = 0; i < size; ++i) {
473+
auto gradient_color = array[i];
474+
emscripten::val gradient_color_obj = emscripten::val::object();
475+
gradient_color_obj.set("reservedData", gradient_color.shift_value);
476+
gradient_color_obj.set("rgb", gradient_color.color.rgb);
477+
gradient_color_obj.set("colorIndex", gradient_color.color.index);
478+
colors.call<void>("push", gradient_color_obj);
479+
}
480+
return colors;
481+
}
482+
469483
emscripten::val dwg_ptr_to_mline_vertex_array_wrapper(uintptr_t array_ptr, size_t size) {
470484
Dwg_MLINE_vertex* array = reinterpret_cast<Dwg_MLINE_vertex*>(array_ptr);
471485
emscripten::val vertices = emscripten::val::array();
@@ -532,6 +546,7 @@ EMSCRIPTEN_BINDINGS(libredwg_array) {
532546
DEFINE_FUNC(dwg_ptr_to_table_cell_array);
533547
DEFINE_FUNC(dwg_ptr_to_hatch_defline_array);
534548
DEFINE_FUNC(dwg_ptr_to_hatch_path_array);
549+
DEFINE_FUNC(dwg_ptr_to_hatch_gradient_color_array);
535550
DEFINE_FUNC(dwg_ptr_to_mline_vertex_array);
536551
}
537552

bindings/javascript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "A DWG/DXF JavaScript parser based on libredwg",
44
"license": "GPL-3.0",
55
"private": false,
6-
"version": "0.7.0",
6+
"version": "0.7.1",
77
"author": "MLight Lee <mlight.lee@outlook.com>",
88
"type": "module",
99
"repository": {

bindings/javascript/src/converter/entityConverter.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import {
2121
DwgEmbeddedMText,
2222
DwgEntity,
2323
DwgHatchAssociativity,
24-
DwgHatchEntityBase,
24+
DwgHatchEntity,
25+
DwgHatchGradientColorFlag,
26+
DwgHatchGradientFlag,
2527
DwgHatchPatternType,
2628
DwgHatchSolidFill,
2729
DwgHatchStyle,
@@ -676,7 +678,7 @@ export class LibreEntityConverter {
676678
private convertHatch(
677679
entity: Dwg_Object_Entity_Ptr,
678680
commonAttrs: DwgCommonAttributes
679-
): DwgHatchEntityBase {
681+
): DwgHatchEntity {
680682
const libredwg = this.libredwg
681683
const extrusionDirection = libredwg.dwg_dynapi_entity_value(
682684
entity,
@@ -737,8 +739,7 @@ export class LibreEntityConverter {
737739
numberOfSeedPoints
738740
)
739741

740-
return {
741-
type: 'HATCH',
742+
const result = {
742743
...commonAttrs,
743744
// elevationPoint: DwgPoint3D
744745
extrusionDirection: extrusionDirection,
@@ -772,6 +773,43 @@ export class LibreEntityConverter {
772773
seedPoints: seedPoints
773774
// gradientFlag?: DwgHatchGradientFlag
774775
}
776+
777+
const gradientFlag = libredwg.dwg_dynapi_entity_value(entity, 'is_gradient_fill')
778+
.data as number
779+
if (gradientFlag > 0) {
780+
const gradientName = libredwg.dwg_dynapi_entity_value(entity, 'gradient_name')
781+
.data as string
782+
const gradientRotation = libredwg.dwg_dynapi_entity_value(entity, 'gradient_angle')
783+
.data as number
784+
const gradientDefinition = libredwg.dwg_dynapi_entity_value(entity, 'gradient_shift')
785+
.data as number
786+
const colorTint = libredwg.dwg_dynapi_entity_value(entity, 'gradient_tint')
787+
.data as number
788+
const gradientColorFlag = libredwg.dwg_dynapi_entity_value(entity, 'single_color_gradient')
789+
.data as number
790+
// const numberOfColors = libredwg.dwg_dynapi_entity_value(entity, 'num_colors')
791+
// .data as number
792+
const gradientColors_ptr = libredwg.dwg_dynapi_entity_value(entity, 'colors')
793+
.data as number
794+
const gradientColors = libredwg.dwg_ptr_to_hatch_gradient_color_array(gradientColors_ptr, (gradientColorFlag == 1) ? 1 : 2)
795+
796+
return {
797+
type: 'HATCH',
798+
...result,
799+
gradientFlag: DwgHatchGradientFlag.Gradient,
800+
gradientColorFlag: gradientColorFlag == 1 ? DwgHatchGradientColorFlag.OneColor : DwgHatchGradientColorFlag.TwoColor,
801+
gradientName,
802+
gradientRotation,
803+
gradientDefinition,
804+
colorTint,
805+
gradientColors
806+
}
807+
} else {
808+
return {
809+
type: 'HATCH',
810+
...result
811+
}
812+
}
775813
}
776814

777815
private convertHatchBoundaryPaths(paths: Dwg_HATCH_Path[]) {

bindings/javascript/src/database/entities/hatch.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,21 @@ export interface DwgHatchEntityBase extends DwgEntity {
157157
gradientFlag?: DwgHatchGradientFlag
158158
}
159159

160+
export interface DwgGradientColor {
161+
reservedData: number
162+
rgb: number
163+
colorIndex?: number
164+
}
165+
160166
export interface DwgGradientHatchEntity extends DwgHatchEntityBase {
167+
gradientName: string
161168
gradientFlag: DwgHatchGradientFlag.Gradient
162169
gradientColorFlag: DwgHatchGradientColorFlag
163170
numberOfColors: 0 | 2
164171
gradientRotation?: number
165172
gradientDefinition: number
166173
colorTint?: number
174+
gradientColors?: Array<DwgGradientColor>
167175
}
168176

169177
export type DwgHatchEntity = DwgGradientHatchEntity | DwgHatchEntityBase

bindings/javascript/src/libredwg.ts

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
DwgCodePage,
66
dwgCodePageToEncoding,
77
DwgDatabase,
8+
DwgGradientColor,
89
DwgPoint2D,
910
DwgPoint3D,
1011
DwgPoint4D,
@@ -89,25 +90,25 @@ export class LibreDwg {
8990
})
9091
}
9192

92-
dwg_read_data(fileContent: string | ArrayBuffer, fileType: number) {
93-
if (fileType == Dwg_File_Type.DWG) {
94-
const fileName = 'tmp.dwg'
95-
try {
96-
this.wasmInstance.FS.writeFile(
97-
fileName,
98-
new Uint8Array(fileContent as ArrayBuffer)
99-
)
100-
const result = this.wasmInstance.dwg_read_file(fileName)
101-
if (result.error != 0) {
102-
console.log('Open dwg file with error code: ', result.error)
103-
}
104-
return result.data as Dwg_Data_Ptr
105-
} finally {
106-
if (this.wasmInstance.FS.analyzePath(fileName, false).exists) {
107-
this.wasmInstance.FS.unlink(fileName)
108-
}
109-
}
110-
}
93+
dwg_read_data(fileContent: string | ArrayBuffer, fileType: number) {
94+
if (fileType == Dwg_File_Type.DWG) {
95+
const fileName = 'tmp.dwg'
96+
try {
97+
this.wasmInstance.FS.writeFile(
98+
fileName,
99+
new Uint8Array(fileContent as ArrayBuffer)
100+
)
101+
const result = this.wasmInstance.dwg_read_file(fileName)
102+
if (result.error != 0) {
103+
console.log('Open dwg file with error code: ', result.error)
104+
}
105+
return result.data as Dwg_Data_Ptr
106+
} finally {
107+
if (this.wasmInstance.FS.analyzePath(fileName, false).exists) {
108+
this.wasmInstance.FS.unlink(fileName)
109+
}
110+
}
111+
}
111112
// else if (fileType == Dwg_File_Type.DXF) {
112113
// const fileName = "tmp.dxf";
113114
// this.wasmInstance.FS.writeFile(fileName, new Uint8Array(fileContent as ArrayBuffer));
@@ -144,12 +145,12 @@ export class LibreDwg {
144145
}
145146
return this.wasmInstance.FS.readFile(outputFileName) as Uint8Array
146147
} finally {
147-
if (this.wasmInstance.FS.analyzePath(inputFileName, false).exists) {
148-
this.wasmInstance.FS.unlink(inputFileName)
149-
}
150-
if (this.wasmInstance.FS.analyzePath(outputFileName, false).exists) {
151-
this.wasmInstance.FS.unlink(outputFileName)
152-
}
148+
if (this.wasmInstance.FS.analyzePath(inputFileName, false).exists) {
149+
this.wasmInstance.FS.unlink(inputFileName)
150+
}
151+
if (this.wasmInstance.FS.analyzePath(outputFileName, false).exists) {
152+
this.wasmInstance.FS.unlink(outputFileName)
153+
}
153154
}
154155
}
155156

@@ -896,6 +897,21 @@ export class LibreDwg {
896897
return this.wasmInstance.dwg_ptr_to_hatch_path_array(ptr, size)
897898
}
898899

900+
/**
901+
* Converts one C++ hatch gradient color array to one JavaScript hatch gradient color array.
902+
* @group Array Methods
903+
* @group Dwg_Entity_HATCH Methods
904+
* @param ptr Pointer to C++ hatch gradient color array.
905+
* @param size The size of C++ hatch gradient color array.
906+
* @returns Returns one JavaScript hatch gradient color array from the specified C++ hatch gradient color array.
907+
*/
908+
dwg_ptr_to_hatch_gradient_color_array(
909+
ptr: Dwg_Array_Ptr,
910+
size: number
911+
): DwgGradientColor[] {
912+
return this.wasmInstance.dwg_ptr_to_hatch_gradient_color_array(ptr, size)
913+
}
914+
899915
/**
900916
* Converts one C++ mline vertex array to one JavaScript mline vertex array.
901917
* @group Array Methods

bindings/javascript/wasm/libredwg-web.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ interface EmbindModule {
495495
dwg_ptr_to_table_cell_array(_0: number, _1: number): any;
496496
dwg_ptr_to_hatch_defline_array(_0: number, _1: number): any;
497497
dwg_ptr_to_hatch_path_array(_0: number, _1: number): any;
498+
dwg_ptr_to_hatch_gradient_color_array(_0: number, _1: number): any;
498499
dwg_ptr_to_mline_vertex_array(_0: number, _1: number): any;
499500
dwg_object_get_name(_0: number): any;
500501
dwg_object_get_handle_object(_0: number): any;
492 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)