Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Graphics.doc
Скачиваний:
16
Добавлен:
16.11.2019
Размер:
3.1 Mб
Скачать

Получение метрик шрифтов

Класс FontFamily предоставляет следующие методы для получения различных метрик для некоторой комбинации "семейство шрифтов — начертание".

  • GetEmHeight(FontStyle)

  • GetCellAscent(FontStyle)

  • GetCellDescent(FontStyle)

  • GetLineSpacing(FontStyle)

Численные значения, возвращаемые этими методами, приводятся в специальных единицах, применяемых при разработке шрифта, поэтому они не зависят от размера и единиц измерения конкретного объекта Font.

Различные метрики шрифтов показаны на следующем рисунке.

Example

The following example displays the metrics for the regular style of the Arial font family. The code also creates a Font object (based on the Arial family) with size 16 pixels and displays the metrics (in pixels) for that particular Font object.

The following illustration shows the output of the example code.

Note the first two lines of output in the preceding illustration. The Font object returns a size of 16, and the FontFamily object returns an em height of 2,048. These two numbers (16 and 2,048) are the key to converting between font design units and the units (in this case pixels) of the Font object.

For example, you can convert the ascent from design units to pixels as follows:

The following code positions text vertically by setting the Y data member of a PointF object. The y-coordinate is increased by font.Height for each new line of text. The Height property of a Font object returns the line spacing (in pixels) for that particular Font object. In this example, the number returned by Height is 19. Note that this is the same as the number (rounded up to an integer) obtained by converting the line-spacing metric to pixels.

Note that the em height (also called size or em size) is not the sum of the ascent and the descent. The sum of the ascent and the descent is called the cell height. The cell height minus the internal leading is equal to the em height. The cell height plus the external leading is equal to the line spacing.

Пример

В следующем примере отображаются метрики обычного начертания шрифтов семейства Arial. Кодом также создается объект Font (на основе семейства шрифтов Arial) с размером в 16 точек и отображаются метрики (в точках) для этого объекта Font.

На следующем рисунке показан результат выполнения примера кода.

--------

Обратите внимание на две первые строки текста из приведенного выше рисунка. Объект Font возвращает в качестве размера 16, а объект FontFamily возвращает размер максимального пробела, равный 2048. Указанные два числа (16 и 2048) являются основой для преобразования между единицами измерения, применяемыми при проектировании шрифта, и единицами измерения объекта Font (в данном случае это точки).

Например, можно преобразовать проектные единицы измерения верхнего выносного элемента в точки следующим образом.

Приведенный выше код располагает текст по вертикали, устанавливая значение члена Y объекта PointF. Координата y увеличивается на значение font.Height для каждой новой строки текста. Свойство Height объекта Font возвращает межстрочный интервал (в точках) для рассматриваемого объекта Font. В данном примере возвращаемое значение Height равно 19. Обратите внимание, что оно равно численному значению (округленному до большего целого), получаемому при преобразовании метрики межстрочного интервала в точки.

Обратите также внимание, что размер максимального пробела (называемый также эм-размером) не равен сумме размеров верхнего и нижнего выносных элементов. Сумма размеров верхнего и нижнего выносных элементов называется высотой клетки. Размер максимального пробела вычисляется как разность между высотой клетки и внутренним интерлиньяжем. Сумма высоты клетки и внешнего интерлиньяжа равна междустрочному интервалу.

string infoString = ""; // enough space for one line of output

int ascent; // font family ascent in design units

float ascentPixel; // ascent converted to pixels

int descent; // font family descent in design units

float descentPixel; // descent converted to pixels

int lineSpacing; // font family line spacing in design units

float lineSpacingPixel; // line spacing converted to pixels

FontFamily fontFamily = new FontFamily("Arial");

Font font = new Font(

fontFamily,

16, FontStyle.Regular,

GraphicsUnit.Pixel);

PointF pointF = new PointF(10, 10);

SolidBrush solidBrush = new SolidBrush(Color.Black);

// Display the font size in pixels.

infoString = "font.Size returns " + font.Size + ".";

e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.

pointF.Y += font.Height;

// Display the font family em height in design units.

infoString = "fontFamily.GetEmHeight() returns " +

fontFamily.GetEmHeight(FontStyle.Regular) + ".";

e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down two lines.

pointF.Y += 2 * font.Height;

// Display the ascent in design units and pixels.

ascent = fontFamily.GetCellAscent(FontStyle.Regular);

// 14.484375 = 16.0 * 1854 / 2048

ascentPixel =

font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular);

infoString = "The ascent is " + ascent + " design units, " + ascentPixel +

" pixels.";

e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.

pointF.Y += font.Height;

// Display the descent in design units and pixels.

descent = fontFamily.GetCellDescent(FontStyle.Regular);

---------

// 3.390625 = 16.0 * 434 / 2048

descentPixel =

font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular);

infoString = "The descent is " + descent + " design units, " +

descentPixel + " pixels.";

e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.

pointF.Y += font.Height;

// Display the line spacing in design units and pixels.

lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);

// 18.398438 = 16.0 * 2355 / 2048

lineSpacingPixel =

font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular);

infoString = "The line spacing is " + lineSpacing + " design units, " +

lineSpacingPixel + " pixels.";

e.Graphics.DrawString(infoString, font, solidBrush, pointF);

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler.

--------------