File: font.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 4,669 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_FONT_H_
#define UI_GFX_FONT_H_

#include <string>

#include "base/component_export.h"
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "ui/gfx/native_widget_types.h"

#if BUILDFLAG(IS_APPLE)
#include <CoreText/CoreText.h>
#endif

namespace gfx {

struct FontRenderParams;
class PlatformFont;

// Font provides a wrapper around an underlying font. Copy and assignment
// operators are explicitly allowed, and cheap.
//
// Figure of font metrics:
//   +--------+-------------------+------------------+
//   |        |                   | internal leading |
//   |        | ascent (baseline) +------------------+
//   | height |                   | cap height       |
//   |        |-------------------+------------------+
//   |        | descent (height - baseline)          |
//   +--------+--------------------------------------+
class COMPONENT_EXPORT(GFX) Font {
 public:
  // The following constants indicate the font style.
  // These are treated as bitwise operators.
  enum FontStyle {
    NORMAL = 0b0,
    ITALIC = 0b1,
    STRIKE_THROUGH = 0b10,
    UNDERLINE = 0b100,
  };

  // Standard font weights as used in Pango and Windows. The values must match
  // https://msdn.microsoft.com/en-us/library/system.windows.fontweights(v=vs.110).aspx
  enum class Weight {
    INVALID = -1,
    THIN = 100,
    EXTRA_LIGHT = 200,
    LIGHT = 300,
    NORMAL = 400,
    MEDIUM = 500,
    SEMIBOLD = 600,
    BOLD = 700,
    EXTRA_BOLD = 800,
    BLACK = 900,
  };

  // Creates a font with the default name and style.
  Font();

  // Creates a font that is a clone of another font object.
  Font(const Font& other);
  Font& operator=(const Font& other);

#if BUILDFLAG(IS_APPLE)
  // Creates a font from the specified CTFontRef.
  explicit Font(CTFontRef ct_font);
#endif

  // Constructs a Font object with the specified PlatformFont object. The Font
  // object takes ownership of the PlatformFont object.
  explicit Font(PlatformFont* platform_font);

  // Creates a font with the specified name in UTF-8 and size in pixels.
  Font(const std::string& font_name, int font_size);

  ~Font();

  // Returns a new Font derived from the existing font.
  // |size_delta| is the size in pixels to add to the current font. For example,
  // a value of 5 results in a font 5 pixels bigger than this font.
  // The style parameter specifies the new style for the font, and is a
  // bitmask of the values: ITALIC and UNDERLINE.
  Font Derive(int size_delta, int style, Font::Weight weight) const;

  // Returns the number of vertical pixels needed to display characters from
  // the specified font.  This may include some leading, i.e. height may be
  // greater than just ascent + descent.  Specifically, the Windows and Mac
  // implementations include leading and the Linux one does not.  This may
  // need to be revisited in the future.
  int GetHeight() const;

  // Returns the font weight.
  Font::Weight GetWeight() const;

  // Returns the baseline, or ascent, of the font.
  int GetBaseline() const;

  // Returns the cap height of the font.
  int GetCapHeight() const;

  // Returns the expected number of horizontal pixels needed to display the
  // specified length of characters. Call gfx::GetStringWidth() to retrieve the
  // actual number.
  int GetExpectedTextWidth(int length) const;

  // Returns the style of the font.
  int GetStyle() const;

  // Returns the specified font name in UTF-8, without font mapping.
  const std::string& GetFontName() const;

  // Returns the actually used font name in UTF-8 after font mapping.
  std::string GetActualFontName() const;

  // Returns the font size in pixels.
  int GetFontSize() const;

  // Returns an object describing how the font should be rendered.
  const FontRenderParams& GetFontRenderParams() const;

#if BUILDFLAG(IS_APPLE)
  // Returns the CTFontRef. This is owned by the gfx::Font as per the standard
  // "get" idiom.
  CTFontRef GetCTFont() const;
#endif

  // Raw access to the underlying platform font implementation.
  PlatformFont* platform_font() const { return platform_font_.get(); }

 private:
  // Wrapped platform font implementation.
  scoped_refptr<PlatformFont> platform_font_;
};

#ifndef NDEBUG
COMPONENT_EXPORT(GFX)
std::ostream& operator<<(std::ostream& stream, const Font::Weight weight);
#endif

// Returns the Font::Weight that matches |weight| or the next bigger one.
COMPONENT_EXPORT(GFX) Font::Weight FontWeightFromInt(int weight);

}  // namespace gfx

#endif  // UI_GFX_FONT_H_