Exemplo n.º 1
0
LineArray CLineScanner::scan( char* buffer, unsigned long bufferSize )
{
	m_pBuffer = buffer;
	m_nBufferSize = bufferSize;
	reset();

	LineArray lines;
	char strLine[512] = {0};
	while ( readLine( strLine ) != -1 )
	{
		std::string singleLine = strLine;
		CC3String::trim( singleLine );
		if ( !singleLine.empty() )
		{
			lines.push_back( strLine );

			if ( m_pDelegate )
			{
				m_pDelegate->onScanLine( strLine );
			}
		}
	}

	return lines;
}
Exemplo n.º 2
0
   /// <summary>Opens a document template.</summary>
   /// <param name="docPath">document path.</param>
   /// <param name="t">template.</param>
   /// <returns></returns>
   BOOL ScriptDocument::OnOpenTemplate(Path docPath, const TemplateFile& t)
   {
      WorkerData data(Operation::LoadSaveDocument);
      
      try
      {
         // Feedback
         Console << Cons::UserAction << "Creating script: " << docPath << " from template: " << Path(t.SubPath) << ENDL;
         data.SendFeedback(ProgressType::Operation, 0, VString(L"Creating %s '%s'", t.Name.c_str(), docPath.c_str()));

         // Read/Parse template script
         AppPath path(t.SubPath);
         Script = ScriptFileReader(XFileInfo(path).OpenRead()).ReadFile(path, false);

         // Set properties
         Script.Name = docPath.RemoveExtension().FileName;
         Script.Game = PrefsLib.GameDataVersion;
         Script.Version = 1;
         Script.Description = L"no description";

         // Populate template
         LineArray lines;
         wstring date = COleDateTime(time(nullptr)).Format(L"%d/%m/%Y");
         for (auto& cmd : Script.Commands.Input)
         {
            cmd.Text = cmd.Text.ReplaceAll(L"$NAME$", Script.Name);
            cmd.Text = cmd.Text.ReplaceAll(L"$DATE$", date);
            cmd.Text = cmd.Text.ReplaceAll(L"$AUTHOR$", L"");
            lines.push_back(cmd.Text);
         }

         // Create temp scriptFile
         ScriptFile tmp(Script);
         tmp.FullPath = docPath;

         // Compile changes into temporary script
         ScriptParser parser(tmp, lines, Script.Game);
         parser.Compile();
         
         // Write to disc
         ScriptFileWriter w(XFileInfo(docPath).OpenWrite());
         w.Write(tmp);
         w.Close();

         // Feedback
         data.SendFeedback(Cons::Success, ProgressType::Succcess, 0, L"Script created successfully");
         return TRUE;
      }
      catch (ExceptionBase&  e)
      {
         // Feedback/Display error
         data.SendFeedback(Cons::Error, ProgressType::Failure, 0, VString(L"Failed to create %s '%s'", t.Name.c_str(), docPath.c_str()));
         theApp.ShowError(HERE, e, VString(L"Failed to create %s '%s'", t.Name.c_str(), docPath.c_str()));
         return FALSE;
      }
   }
float
pcl::registration::CorrespondenceRejectorVarTrimmed::optimizeInlierRatio (std::vector <double>&  dists)
{
  unsigned int points_nbr = static_cast<unsigned int> (dists.size ());
  std::sort (dists.begin (), dists.end ());

  const int min_el = int (floor (min_ratio_ * points_nbr));
  const int max_el = int (floor (max_ratio_ * points_nbr));

  typedef Eigen::Array <double, Eigen::Dynamic, 1> LineArray;
  Eigen::Map<LineArray> sorted_dist (&dists[0], points_nbr);

  const LineArray trunk_sorted_dist = sorted_dist.segment (min_el, max_el-min_el);
  const double lower_sum = sorted_dist.head (min_el).sum ();
  const LineArray ids = LineArray::LinSpaced (trunk_sorted_dist.rows (), min_el+1, max_el);
  const LineArray ratio = ids / points_nbr;
  const LineArray deno = ratio.pow (lambda_);
  const LineArray FRMS = deno.inverse ().square () * ids.inverse () * (lower_sum + trunk_sorted_dist);
  int min_index (0);
  FRMS.minCoeff (&min_index);

  const float opt_ratio = float (min_index + min_el) / float (points_nbr);
  return (opt_ratio);
}
Exemplo n.º 4
0
void TextFormatter::FormatPlainText(CFDC& dc,
    int& width, int& height,
    int fontsize,
    const wchar_t *text, int len,
    LineArray& lines)
{
    lines.RemoveAll();

    int save_width = m_width;
    bool save_justified = m_justified;
    m_width = width;
    m_justified = false;

    const wchar_t *top = text + len;

    Attr attr;
    attr.wa = 0;
    attr.fsize = fontsize;

    int curh = 0;

    while (text < top && curh < height)
    {
        const wchar_t *p_end = text;
        while (p_end < top && *p_end != '\r' && *p_end != '\n')
            ++p_end;
        Paragraph p(p_end - text);
        memcpy(p.str, text, (p_end - text)*sizeof(wchar_t));
        for (int i = 0; i < p.cflags.size(); ++i)
            p.cflags[i].wa = attr.wa;
        p.findent = 3; // XXX
        int last = lines.GetSize();
        FilePos fp = FilePos();
        int lh = WrapLine(dc, p, fp, lines, curh, height - curh);
        if (lh < 0)
        {
            // it still might add something
            while (last < lines.GetSize())
                curh += lines[last++].height;
            break;
        }
        curh += lh;
        while (p_end < top && (*p_end == '\r' || *p_end == '\n'))
            ++p_end;
        text = p_end;
    }

    m_width = save_width;
    m_justified = save_justified;

    // deduct ispace
    int min_ispace = -1, max_width = 0;
    for (int i = 0; i < lines.GetSize(); ++i)
    {
        const Line& ll = lines[i];
        int w = 0;
        for (int j = 0; j < ll.dx.size(); ++j)
            w += ll.dx[j];
        w += ll.ispace;
        if (max_width < w)
            max_width = w;
        if (min_ispace<0 || min_ispace>ll.ispace)
            min_ispace = ll.ispace;
    }

    if (min_ispace > 0)
    {
        for (int i = 0; i < lines.GetSize(); ++i)
            lines[i].ispace -= min_ispace;
        max_width -= min_ispace;
    }

    height = curh;
    width = max_width;
}
Exemplo n.º 5
0
bool TextFormatter::FormatBack(CFDC& dc, FilePos start, FilePos prev_top)
{
    AdjustPos(start, true);
    if (start.para == 0 && start.off == 0)
        return false; // at the top
    m_lines.RemoveAll();
    m_bot = start;
    for (int page = m_pages - 1; page >= 0; --page)
    {
        LineArray tmp;
        FilePos pos = start;
        int h = 0;
        // while there are still paragrahs before
        while (h < m_height && (pos.para>0 || pos.off > 0))
        {
            // format entire paragraph
            LineArray cp;
            Paragraph para(m_tf->GetParagraph(pos.docid, pos.para));
            if (pos.off < para.len) // double check args
                para.len = pos.off;
            else
                pos.off = para.len;
            FilePos fp = FilePos(pos.para, 0, pos.docid);
            WrapLine(dc, para, fp, cp, 0, 32768);
            // insert the formatted paragraph at start of list
            tmp.InsertAt(0, &cp);
            for (int i = 0; i < cp.GetSize(); ++i)
                h += cp[i].height;
            pos.off = 0;
            AdjustPos(pos, true);
        }
        // delete extra lines
        int j;
        // remove top lines
        for (h = 0, j = tmp.GetUpperBound(); j >= 0 && h + tmp[j].height <= m_height; --j)
            h += tmp[j].height;
        if (j < tmp.GetUpperBound())
        {
            if (j >= 0 && prev_top != 0 && tmp[j + 1].pos >= prev_top)
            {
                --j;
                tmp.RemoveAt(0, j + 1);
                // now remove bottom lines
                for (h = j = 0; j < tmp.GetSize() && h + tmp[j].height <= m_height; ++j)
                    h += tmp[j].height;
                if (j < tmp.GetSize())
                    tmp.RemoveAt(j, tmp.GetSize() - j);
            }
            else
            {
                tmp.RemoveAt(0, j + 1);
            }
        }
        // save lines
        m_lines.InsertAt(0, &tmp);
        m_pagelen.SetAtGrow(page, tmp.GetSize());
        start = m_lines[0].pos;
        if (start.para == 0 && start.off == 0) // we reached the top of file
            return FormatFwd(dc, FilePos(0, 0, start.docid));
    }
    // save positions
    m_top = m_lines[0].pos;
    Highlight();
    // add one dummy line always
    Line l;
    l.pos = m_bot;
    m_lines.Add(l);
    return true;
}
Exemplo n.º 6
0
// the formatter's core, wraps the line and justifies it if needed
int TextFormatter::WrapLine(CFDC& dc, Paragraph& pp,
    FilePos& pos, LineArray& la,
    int top, int maxh)
{
    if (maxh <= 0)
        return 0;
    // process images separately
    if (pp.flags&Paragraph::image)
        return WrapImage(dc, pp, pos, la, top, maxh);
    if (pp.len == 0 || (pp.len == 1 && pp.str[0] == L' '))
    {
        dc.SelectFont(0, 0);
        int fh, fa;
        dc.GetFontSize(fh, fa);
        if (fh > maxh)
            return -1;
        Line l;
        l.pos = pos;
        l.flags = Line::first | Line::last | Line::defstyle;
        l.height = fh;
        l.base = fa;
        la.Add(l);
        pos.off = pp.len;
        return l.height;
    }
    if (m_hyphenate)
        pp.Hyphenate();
    const wchar_t *str = pp.str;
    int len = pp.len;
    Buffer<int> dx(len + 1);
    int toth = 0;
    while (toth < maxh && pos.off < len)
    {
        // 1. get text size
        int nch = len;
        int curwidth = m_width;
        int ispace = 0;
        if (pos.off == 0 && (pp.flags&(Paragraph::center | Paragraph::right)) == 0)
            AdjustIndent(curwidth, ispace, pp.lindent, pp.rindent, pp.findent, dc.GetLPX());
        else
            AdjustIndent(curwidth, ispace, pp.lindent, pp.rindent, 0, dc.GetLPX());
        dx[0] = 0;
        int lh = 1, lbase = 1;
        GetTextExtent(dc, pp, pos.off, curwidth, nch, dx + 1, lh, lbase);
        if (toth + lh > maxh)
            return -1;
        if (nch == 0)
            nch = 1;
        // 2. do word wrap
        bool addhyp = false;
        if (nch + pos.off < pp.str.size())
        {
            int i;
            for (i = nch; i > 0 && str[pos.off + i] != L' '; --i)
            {
                // wrap at existing dashes
                if (i < nch && (str[pos.off + i] == L'-' || str[pos.off + i] == 0x2013 ||
                    str[pos.off + i] == 0x2014) && i < len - 1 && (str[pos.off + i + 1] == L' ' ||
                    iswalpha(str[pos.off + i + 1])))
                {
                    ++i;
                    break;
                }
                // or at possible hyphenation points
                if (m_hyphenate && pp.cflags[pos.off + i].hyphen &&
                    dx[i] + dc.GetHypWidth() <= curwidth)
                {
                    addhyp = true;
                    break;
                }
            }
            if (i > 0)
                nch = i;
            else
                addhyp = false;
        }
        // insert it into line list
        if (pos.off == 0 && nch == pp.str.size())
        {
            // got full line
            Line l(str, len, false);
            l.pos = pos;
            l.flags = Line::first | Line::last;
            l.ispace = ispace;
            l.height = lh;
            l.base = lbase;
            if (dx[nch] < curwidth)
            {
                if (pp.flags&Paragraph::center)
                    l.ispace += (curwidth - dx[nch]) / 2;
                else if (pp.flags&Paragraph::right)
                    l.ispace += curwidth - dx[nch];
            }
            CopyAttr(l.attr, pp.cflags, len);
            for (int j = 0; j < len; ++j)
                l.dx[j] = dx[j + 1] - dx[j];
            la.Add(l);
            pos.off = len;
        }
        else
        {
            Line l(str + pos.off, nch, addhyp);
            if (addhyp)
                l.str[nch] = L'-';
            l.pos = pos;
            l.ispace = ispace;
            l.height = lh;
            l.base = lbase;
            l.flags = 0;
            if (pos.off == 0)
                l.flags |= Line::first;
            if (pos.off + nch == pp.str.size())
                l.flags |= Line::last;
            for (int j = 0; j < nch; ++j)
                l.dx[j] = dx[j + 1] - dx[j];
            int extra_width = 0;
            if (addhyp)
                l.dx[nch] = extra_width = dc.GetHypWidth();
            // 3. justify/center text if needed
            if (dx[nch] < curwidth)
            {
                if (addhyp)
                    curwidth -= extra_width;
                if (pp.flags&Paragraph::center)
                {
                    l.ispace += (curwidth - dx[nch]) / 2;
                }
                else if (pp.flags&Paragraph::right)
                {
                    l.ispace += curwidth - dx[nch];
                }
                else if ((m_justified || pp.flags&Paragraph::justify) &&
                    !(l.flags&Line::last))
                {
                    // count spaces in string
                    int nspc = 0, i;
                    for (i = 0; i < nch; ++i)
                        if (L' ' == str[pos.off + i])
                            ++nspc;
                    // and distribute extra width to them
                    if (nspc > 0)
                    {
                        int addw = (curwidth - dx[nch]) / nspc;
                        int extraddw = curwidth - dx[nch] - addw*nspc;
                        for (i = 0; i < nch; ++i)
                        {
                            if (str[pos.off + i] == L' ')
                            {
                                l.dx[i] += addw;
                                if (extraddw)
                                {
                                    ++l.dx[i];
                                    --extraddw;
                                }
                            }
                        }
                    }
                }
            }
            CopyAttr(l.attr, pp.cflags + pos.off, nch);
            if (addhyp)
                l.attr[nch] = l.attr[nch - 1];
            la.Add(l);
            pos.off += nch;
            while (pos.off < len && str[pos.off] == L' ')
                ++pos.off;
        }
        toth += lh;
    }
    return toth;
}
Exemplo n.º 7
0
// split image into strips
int TextFormatter::WrapImage(CFDC& dc, Paragraph& pp,
    FilePos& pos, LineArray& la,
    int top, int maxh)
{
    Image img;
#if 0
    int curwidth = m_width;
    int ispace = 0;
    AdjustIndent(curwidth, ispace, pp.lindent, pp.rindent, 0, dc.GetLPX());
#else
    int curwidth = m_total_width;
    int ispace = -m_margin;
#endif
    if (pp.links.size() <= 0 ||
        !m_tf->GetImage(pp.links[0].target, dc.DC(), curwidth, m_height, m_angle, img))
    {
        // image fetch failed, just skip the paragraph
        pos.off += pp.len;
        return 0;
    }
#if 0
    if (top && img.height > maxh)
        return -1;
#endif
    // calc strips, min strip height is 16
    int striph = (img.height + pp.str.size() - 1) / pp.str.size();
    if (striph < 16)
        striph = 16;
    if (striph > img.height)
        striph = img.height;
    // number of visible strips
    int vstrips = (img.height + striph - 1) / striph;
    int topstrip = vstrips;
    if (topstrip > pp.len)
        topstrip = pp.len;
    // all lines are the same
    Line line(L" ", 1, false);
    line.attr[0].wa = 0;
    line.dx[0] = 0;
    line.ispace = ispace + (curwidth - img.width) / 2;
    line.href = pp.links[0].target;
    line.pos = pos;
    line.flags = Line::image;
    line.height = striph;
    line.base = curwidth;
    line.imageheight = m_height;
    // take care of the strip offset
    int stripnum = pos.off;
    int yoffset = stripnum*striph;
    // add visible strips as lines
    int toth = 0;
    // add visible strips
    while (stripnum < topstrip)
    {
        line.yoffset = yoffset;
        line.pos = pos;
        if (stripnum == vstrips - 1)
        {
            // last line
            // assign all unsused spaces here
            int spcount = pp.len - pos.off;
            line.attr = Buffer<Attr>(spcount);
            line.str = Buffer<wchar_t>(spcount);
            line.dx = Buffer<int>(spcount);
            for (int i = 0; i < spcount; ++i)
            {
                line.attr[i].wa = 0;
                line.str[i] = L' ';
                line.dx[i] = 0;
            }
            line.real_len = spcount;
            // adjust line height
            line.height = img.height - striph*stripnum;
        }
        if (toth + line.height > maxh)
            break;
        la.Add(line);
        pos.off += line.real_len;
        ++stripnum;
        yoffset += striph;
        toth += line.height;
    }
    // ok, return processed height
    // if we didn't process anything, return a failure
    return toth ? toth : -1;
}