summaryrefslogtreecommitdiff
path: root/src/lib/preprocess/windows/WindowsSvgDC.cpp
blob: 05322399d26ee6f6e1a023bdd93b9d45e6298270 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifdef WIN32 // Windows API-dependent unit


#include "SvgUtils.h"
#include "WindowsSvgDC.h"
#include "WindowsSvgFont.h"

using namespace std;


SvgDC &GetSystemDC()
{
  static WindowsSvgDC dc;
  return dc;
}


WindowsSvgDC::WindowsSvgDC(HDC hDC)
  : m_pixelPerInchX(-1),
    m_pixelPerInchY(-1)
{
  if (hDC == NULL)
  {
    m_ownDC = true;
    m_hDC = GetWindowDC(NULL);
  }
  else
  {
    m_ownDC = false;
  }
}

WindowsSvgDC::~WindowsSvgDC()
{
  if (m_ownDC)
  {
    ReleaseDC(NULL, m_hDC);
  }

  for (FontsItTp it = m_fonts.begin(); it != m_fonts.end(); it++)
  {
    delete *it;
  }
}

double WindowsSvgDC::GetConversionPrecisionFactor() const
{
  return 1000.0;
}

double WindowsSvgDC::PixelsToInches(unsigned int pixels, bool horizontal) const
{
  return static_cast<double>(pixels)
         / (horizontal ? GetPixelsPerInchX() : GetPixelsPerInchY());
}

unsigned int WindowsSvgDC::InchesToPixels(double inches, bool horizontal) const
{
  return static_cast<unsigned int>(SvgUtilsC::Round(inches
                                                    * (horizontal ? GetPixelsPerInchX() : GetPixelsPerInchY())));
}

int WindowsSvgDC::GetPixelsPerInchX() const
{
  if (m_pixelPerInchX < 0)
  {
    m_pixelPerInchX =
      SvgUtilsC::Round(GetConversionPrecisionFactor() * GetDeviceCaps(m_hDC, LOGPIXELSX));
  }

  return m_pixelPerInchX;
}

int WindowsSvgDC::GetPixelsPerInchY() const
{
  if (m_pixelPerInchY < 0)
  {
    m_pixelPerInchY =
      SvgUtilsC::Round(GetConversionPrecisionFactor() * GetDeviceCaps(m_hDC, LOGPIXELSY));
  }

  return m_pixelPerInchY;
}

const SvgFontC *WindowsSvgDC::GetFont(
  double heightInch, unsigned int weight, bool isItalic, const string &name) const
{
  WindowsSvgFontC font(0, heightInch, weight, isItalic, name);
  FontsItTp it = m_fonts.find(&font);
  WindowsSvgFontC *pFont;

  if (it == m_fonts.end())
  {
    pFont = new WindowsSvgFontC(GetNextFontId(), heightInch, weight, isItalic, name);
    pFont->Create(GetPixelsPerInchX());
    m_fonts.insert(pFont);
    m_fontIds[pFont->GetId()] = pFont;
  }
  else
  {
    pFont = *it;
  }

  return pFont;
}

double WindowsSvgDC::GetFontBaseLineHeightRatio(unsigned int fontId) const
{
  TEXTMETRIC tm;
  GetFontMetric(fontId, tm);
  return static_cast<double>(tm.tmAscent) / tm.tmHeight;
}

bool WindowsSvgDC::GetFontMetric(unsigned int fontId, TEXTMETRIC &metric) const
{
  FontIdsItTp it = m_fontIds.find(fontId);

  if (it == m_fontIds.end())
  {
    return false;
  }

  HGDIOBJ oldObj = SelectObject(m_hDC, *it->second);
  BOOL result = GetTextMetrics(m_hDC, &metric);
  SelectObject(m_hDC, oldObj);

  return result == TRUE;
}

double WindowsSvgDC::GetTextPartialExtents(
  const wstring &text, unsigned int fontId, double offsetInch, vector<double> &extentsInch) const
{
  unsigned int textLen = text.length();

  if (textLen > 0)
  {
    FontIdsItTp it = m_fontIds.find(fontId);

    if (it == m_fontIds.end())
    {
      return -1.0;
    }

    HGDIOBJ oldObj = SelectObject(m_hDC, *it->second);

    vector<int> extentsPx;
    extentsPx.resize(textLen);

    SIZE strSize;
    GetTextExtentExPointW(m_hDC, text.c_str(), textLen, 0, NULL, &extentsPx.front(), &strSize);

    extentsInch.resize(textLen);

    for (unsigned int i = 0; i < textLen; i++)
    {
      extentsInch[i] = PixelsToInches(extentsPx[i], true) + offsetInch;
    }

    SelectObject(m_hDC, oldObj);
    return offsetInch + PixelsToInches(strSize.cx, true);
  }

  return offsetInch;
}

double WindowsSvgDC::GetTabCharExtent(unsigned int fontId, double offsetInch) const
{
  FontIdsItTp it = m_fontIds.find(fontId);

  if (it == m_fontIds.end())
  {
    return -1.0;
  }

  HGDIOBJ oldObj = SelectObject(m_hDC, *it->second);
  DWORD extent = GetTabbedTextExtentW(m_hDC, L"\t", 1, 0, NULL);
  SelectObject(m_hDC, oldObj);
  return PixelsToInches(extent & 0xFFFF, true) + offsetInch;
}

unsigned int WindowsSvgDC::GetNextFontId() const
{
  static unsigned int id = 0;
  return ++id;
}


#endif // Windows API-dependent unit