View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://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  package org.htmlunit.javascript.host.css;
16  
17  import static org.htmlunit.javascript.host.css.CSSStyleDeclaration.isLength;
18  
19  import org.htmlunit.util.StringUtils;
20  
21  /**
22   * A helper class for handling font attributes of {@link ComputedCSSStyleDeclaration}.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   */
27  final class ComputedFont {
28  
29      // static final int FONT_STYLE_INDEX = 0;
30      // static final int FONT_WEIGHT_INDEX = 1;
31      // static final int FONT_STRETCH_INDEX = 2;
32  
33      static final int FONT_SIZE_INDEX = 3;
34      static final int LINE_HEIGHT_INDEX = 4;
35      static final int FONT_FAMILY_INDEX = 5;
36  
37      static String[] getDetails(final String font) {
38          String fontName = font;
39          while (fontName.contains("  ")) {
40              fontName = fontName.replace("  ", " ");
41          }
42          final String[] tokens = StringUtils.splitAtBlank(fontName);
43          if (tokens.length > 1) {
44              final String[] fontSizeDetails = getFontSizeDetails(tokens[tokens.length - 2]);
45              if (fontSizeDetails == null) {
46                  return null;
47              }
48              final String[] details = new String[6];
49              details[FONT_SIZE_INDEX] = fontSizeDetails[0];
50              details[LINE_HEIGHT_INDEX] = fontSizeDetails[1];
51              details[FONT_FAMILY_INDEX] = tokens[tokens.length - 1];
52              return details;
53          }
54          return null;
55      }
56  
57      /**
58       * @return an array of {@code fontSize} and {@code lineHeight}, or {@code null} if invalid
59       */
60      private static String[] getFontSizeDetails(final String fontSize) {
61          final int slash = fontSize.indexOf('/');
62          final String actualFontSize = slash == -1 ? fontSize : fontSize.substring(0, slash);
63          if (!isLength(actualFontSize)) {
64              return null;
65          }
66  
67          String actualLineHeight = slash == -1 ? "" : fontSize.substring(slash + 1);
68          if (actualLineHeight.isEmpty()) {
69              actualLineHeight = null;
70          }
71          else if (!isValidLineHeight(actualLineHeight)) {
72              return null;
73          }
74          return new String[] {actualFontSize, actualLineHeight};
75      }
76  
77      private static boolean isValidLineHeight(final String lineHeight) {
78          return isLength(lineHeight) || "normal".equals(lineHeight);
79      }
80  
81      private ComputedFont() {
82          // util class
83      }
84  
85  }