1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { Log } from '../utils/Log'
17import deviceInfo from '@ohos.deviceInfo';
18
19export default class DisplayCalculator {
20  private static TAG = '[DisplayCalculator]:'
21
22  public static calcSurfaceDisplaySize(screenWidth: number, screenHeight: number, previewWidth: number, previewHeight: number) {
23    const displaySize = {
24      width: 1920, height: 1080
25    }
26    const ratio = previewWidth / previewHeight
27    if (deviceInfo.deviceType == 'tablet' || screenWidth > screenHeight) {
28      if (screenWidth / screenHeight > ratio) {
29        displaySize.width = Math.floor(screenHeight * ratio)
30        displaySize.height = Math.floor(screenHeight)
31      } else {
32        displaySize.width = Math.floor(screenWidth)
33        displaySize.height = Math.floor(screenWidth / ratio)
34      }
35    } else {
36      if (screenWidth / screenHeight > ratio) {
37        displaySize.width = Math.floor(screenHeight / ratio)
38        displaySize.height = Math.floor(screenHeight)
39      } else {
40        displaySize.width = Math.floor(screenWidth)
41        displaySize.height = Math.floor(screenWidth * ratio)
42      }
43    }
44    Log.info(`${this.TAG} calcSurfaceDisplaySize screenWidth=${screenWidth} screenHeight=${screenHeight} `)
45    Log.info(`${this.TAG} calcSurfaceDisplaySize previewWidth=${previewWidth} previewHeight=${previewHeight} displaySize= ${JSON.stringify(displaySize)}`)
46    return displaySize
47  }
48}