<commit_before>/*
 * $Id$
 *
 * Copyright(C) 1998-2003 <NAME>
 * All rights reserved.
 *
 */

#include <qsconv.h>
#include <qsdevicecontext.h>
#include <qsstl.h>
#include <qsstring.h>

#include <vector>

#include <tchar.h>

using namespace qs;


/****************************************************************************
 *
 * DeviceContext
 *
 */

#ifndef UNICODE
bool qs::DeviceContext::getTextExtentEx(const WCHAR* pwszString,
	int nCount, int nMaxExtent, int* pnFit, int* pnDx, SIZE* pSize) const
{
	assert(hdc_);
	
	DECLARE_QSTATUS();
	
	string_ptr<STRING> str(wcs2mbs(pwszString, nCount));
	if (!str.get())
		return false;
	
	int nLen = strlen(str.get());
	
	std::vector<int> dx;
	if (pnDx) {
		status = STLWrapper<std::vector<int> >(dx).resize(nLen);
		CHECK_QSTATUS_VALUE(false);
	}
	
	if (!::GetTextExtentExPoint(hdc_, str.get(), nLen,
		nMaxExtent, pnFit, pnDx ? &dx[0] : 0, pSize))
		return false;
	
	if (pnDx) {
		int nDx = pnFit ? *pnFit : nLen;
		int* p = pnDx;
		for (int n = 0; n < nDx; ++n) {
			if (n == 0 || dx[n] != dx[n - 1])
				*p++ = dx[n];
		}
		if (pnFit)
			*pnFit = p - pnDx;
	}
	else if (pnFit) {
		if (*pnFit == nLen)
			*pnFit = nCount;
		else<commit_msg>Fix getTextExtentEx to avoid bug in Windows 9x. <BTS:214><commit_after>/*
 * $Id$
 *
 * Copyright(C) 1998-2003 <NAME>
 * All rights reserved.
 *
 */

#include <qsconv.h>
#include <qsdevicecontext.h>
#include <qsstl.h>
#include <qsstring.h>

#include <vector>

#include <tchar.h>

using namespace qs;


/****************************************************************************
 *
 * DeviceContext
 *
 */

#ifndef UNICODE
bool qs::DeviceContext::getTextExtentEx(const WCHAR* pwszString,
	int nCount, int nMaxExtent, int* pnFit, int* pnDx, SIZE* pSize) const
{
	assert(hdc_);
	
	DECLARE_QSTATUS();
	
	string_ptr<STRING> str(wcs2mbs(pwszString, nCount));
	if (!str.get())
		return false;
	
	int nLen = strlen(str.get());
	
	std::vector<int> dx;
	if (pnDx) {
		status = STLWrapper<std::vector<int> >(dx).resize(nLen);
		CHECK_QSTATUS_VALUE(false);
	}
	
	if (!::GetTextExtentExPoint(hdc_, str.get(), nLen,
		nMaxExtent, pnFit, pnDx ? &dx[0] : 0, pSize))
		return false;
	
	if (pnDx) {
		int nDx = pnFit ? *pnFit : nLen;
		int* pn = pnDx;
#if 1
		const CHAR* p = str.get();
		for (int n = 0; n < nDx; ++n, ++p) {
			if (::IsDBCSLeadByte(*p)) {
				*pn++ = dx[n + 1];
				++p;
				++n;
			}
			else {
				*pn++ = dx[n];
			}
		}
#else
		// This works well on NT but not on 9x but it should.
		for (int n = 0; n < nDx; ++n) {
			if (n == 0 || dx[n] != dx[n - 1])
				*pn++ = dx[n];
		}
#endif
		if (pnFit)
			*pnFit = pn - pnDx;
	}
	else if (pnFit) {
		if (*pnFit == nLen)
			*pnFit = nCount;
		else
{'fraction_non_alphanumeric': 0.22922102124487514, 'fraction_numerical': 0.013417815877748788, 'mean_word_length': 3.541455160744501, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2244271', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1126, 'n_words': 269}