11import { IGravity } from "../../qualifiers/gravity/GravityQualifier.js" ;
2- import { ICompassGravity } from "../../qualifiers/gravity/compassGravity/CompassGravity.js" ;
2+ import { CompassGravity , ICompassGravity } from "../../qualifiers/gravity/compassGravity/CompassGravity.js" ;
3+ import { AutoGravity } from "../../qualifiers/gravity/autoGravity/AutoGravity.js" ;
4+ import { AutoFocus } from "../../qualifiers/autoFocus.js" ;
35
4- export interface IGravityModel {
6+ export interface IGravityModel {
57 gravityType ?: string ;
68}
79
8- export interface ICompassGravityModel extends IGravityModel {
10+ export interface ICompassGravityModel extends IGravityModel {
911 compass : ICompassGravity
1012}
1113
12- export interface IOcrGravityModel extends IGravityModel { }
14+ export interface IOcrGravityModel extends IGravityModel {
15+ }
16+
17+ export interface IAutoGravityObjectModel {
18+ object : string , // 'cat' | 'dog' etc
19+ weight ?: number ,
20+ avoid ?: boolean
21+ }
22+
23+ export interface IAutoGravityModel extends IGravityModel {
24+ autoFocus : IAutoGravityObjectModel [ ]
25+ }
26+
27+ export interface IFocusOnGravityModel extends IGravityModel {
28+ focusOnObjects : string [ ] , // 'cat' | 'dog' etc
29+ fallbackGravity : IAutoGravityModel
30+ }
1331
1432/**
1533 * Validate that given val is an ICompassGravity
16- * @param val
34+ * @param gravity
35+ */
36+ function isCompassGravity ( gravity : unknown ) : gravity is CompassGravity {
37+ //const gravityString = `${(typeof gravity === "string" ? gravity : gravity.qualifierValue)}`;
38+ const gravityValue = getGravityValue ( gravity ) ;
39+ return [ 'north' , 'center' , 'east' , 'west' , 'south' , 'north_west' , 'south_east' , 'south_west' , 'north_east' ] . includes ( gravityValue ) ;
40+ }
41+
42+ /**
43+ * Get the value of given gravity
44+ * @param gravity
1745 */
18- function isICompassGravity ( val : unknown ) : val is ICompassGravity {
19- return [ 'north ', 'center' , 'east' , 'west' , 'south' , 'north_west' , 'south_east' , 'south_west' , 'north_east' ] . includes ( val as string ) ;
46+ function getGravityValue ( gravity : unknown ) : string {
47+ return ` ${ gravity } ` . replace ( 'g_ ', '' ) ;
2048}
2149
2250/**
2351 * Creates a compassGravity model
24- * @param compass
52+ * @param gravity
2553 */
26- function createCompassGravityModel ( compass : ICompassGravity ) : ICompassGravityModel {
54+ function createCompassGravityModel ( gravity : CompassGravity ) : ICompassGravityModel {
2755 return {
28- compass,
56+ compass : getGravityValue ( gravity ) as ICompassGravity ,
2957 gravityType : 'direction'
3058 } ;
3159}
3260
3361/**
34- * Validate that given string represents an ocr gravity
62+ * Validate that given gravity is an instance of ocr gravity
3563 * @param gravity
3664 */
37- function isIOcrGravity ( gravity : string ) : boolean {
38- return gravity === 'ocr_text' ;
65+ function isOcrGravity ( gravity : unknown ) : boolean {
66+ return getGravityValue ( gravity ) === 'ocr_text' ;
3967}
4068
4169/**
@@ -47,18 +75,63 @@ function createOcrGravityModel(): IOcrGravityModel {
4775 } ;
4876}
4977
78+ /**
79+ * Validate that given gravity is an instance of AutoGravity
80+ * @param gravity
81+ */
82+ function isAutoGravity ( gravity : unknown ) : gravity is AutoGravity {
83+ return `${ ( gravity as AutoGravity ) . qualifierValue } ` . split ( ':' ) [ 0 ] === 'auto' ;
84+ }
85+
86+ /**
87+ * Create an instance of IAutoGravityObjectModel
88+ * @param gravity
89+ */
90+ function createIAutoFocusObject ( gravity : AutoFocus ) : IAutoGravityObjectModel {
91+ const gravityString = gravity . toString ( ) ;
92+ const values = gravityString . split ( '_' ) ;
93+ const result : IAutoGravityObjectModel = {
94+ object : values [ 0 ]
95+ } ;
96+
97+ if ( values . length > 1 ) {
98+ if ( values [ 1 ] === 'avoid' ) {
99+ result . avoid = true ;
100+ } else {
101+ result . weight = + values [ 1 ] ;
102+ }
103+ }
104+
105+ return result ;
106+ }
107+
108+ /**
109+ * Creates an auto gravity model from given AutoGravity
110+ * @param gravity
111+ */
112+ function createAutoGravityModel ( gravity : AutoGravity ) : IAutoGravityModel {
113+ const values = gravity . qualifierValue . values . filter ( ( v ) => v !== 'auto' ) ;
114+ const autoFocus = values . map ( createIAutoFocusObject ) ;
115+
116+ return {
117+ autoFocus
118+ } ;
119+ }
120+
50121/**
51122 * Create a model of given gravity
52123 * @param gravity
53124 */
54125export function createGravityModel ( gravity : IGravity ) : IGravityModel {
55- const gravityString = `${ ( typeof gravity === "string" ? gravity : gravity . qualifierValue ) } ` ;
56-
57- if ( isICompassGravity ( gravityString ) ) {
58- return createCompassGravityModel ( gravityString ) ;
126+ if ( isCompassGravity ( gravity ) ) {
127+ return createCompassGravityModel ( gravity ) ;
59128 }
60129
61- if ( isIOcrGravity ( gravityString ) ) {
130+ if ( isOcrGravity ( gravity ) ) {
62131 return createOcrGravityModel ( ) ;
63132 }
133+
134+ if ( isAutoGravity ( gravity ) ) {
135+ return createAutoGravityModel ( gravity ) ;
136+ }
64137}
0 commit comments