1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6#include "chart/StringChartLegend.h"
7
8#include <Font.h>
9#include <View.h>
10
11
12// #pragma mark - StringChartLegend
13
14
15StringChartLegend::StringChartLegend(const char* string, int32 level)
16	:
17	ChartLegend(level),
18	fString(string)
19{
20}
21
22
23// #pragma mark - StringChartLegendRenderer
24
25
26StringChartLegendRenderer::StringChartLegendRenderer()
27	:
28	fFont(*be_plain_font)
29{
30	_Init();
31}
32
33
34StringChartLegendRenderer::StringChartLegendRenderer(const BFont& font)
35	:
36	fFont(font)
37{
38	_Init();
39}
40
41
42void
43StringChartLegendRenderer::GetMinimumLegendSpacing(BView* view,
44	float* horizontal, float* vertical)
45{
46	*horizontal = 2 * fEmWidth;
47	*vertical = fFontHeight / 2;
48}
49
50
51BSize
52StringChartLegendRenderer::LegendSize(ChartLegend* _legend, BView* view)
53{
54	StringChartLegend* legend = dynamic_cast<StringChartLegend*>(_legend);
55	if (legend == NULL)
56		return BSize();
57
58	float width = ceilf(fFont.StringWidth(legend->String()));
59	return BSize(width, fFontHeight);
60}
61
62
63void
64StringChartLegendRenderer::RenderLegend(ChartLegend* _legend, BView* view,
65	BPoint point)
66{
67	StringChartLegend* legend = dynamic_cast<StringChartLegend*>(_legend);
68	if (legend == NULL)
69		return;
70
71	point.y += ceil(fFontAscent);
72
73	view->SetFont(&fFont);
74	view->SetHighColor(rgb_color().set_to(0, 0, 0, 255));
75	view->DrawString(legend->String(), point);
76}
77
78
79void
80StringChartLegendRenderer::_Init()
81{
82	font_height fh;
83	fFont.GetHeight(&fh);
84	fFontAscent = ceilf(fh.ascent);
85	fFontHeight = fFontAscent + ceilf(fh.descent);
86	fEmWidth = ceilf(fFont.StringWidth("m", 1));
87}
88