Example #1
0
bool nuiTextLayout::LayoutParagraph(int32 start, int32 length)
{
  //printf("new paragraph: %d + %d\n", start, length);

  float spacewidth = 0;
  float tabwidth = 0;
  {
    nuiFontBase* pFont = mStyle.GetFont();
    nuiGlyphInfo glyphinfo;
    uint32 space = pFont->GetGlyphIndex(32);
    pFont->GetGlyphInfo(glyphinfo, space, nuiFontBase::eGlyphNative);
    spacewidth = glyphinfo.AdvanceX;
    tabwidth = 4 * spacewidth;
  }
  
  mpParagraphs.push_back(new Paragraph());

  nuiTextLine* pLine = new nuiTextLine(*this, 0, 0);
  mpParagraphs.back()->push_back(pLine);
  
  // Split the paragraph into ranges:
  nuiTextRangeList ranges;
  nuiSplitText(mUnicode, ranges, nuiST_ScriptChange, start, length, &Split);

  {
    nuiTextRangeList::iterator it = ranges.begin();
    nuiTextRangeList::iterator end = ranges.end();
    int32 origin = start;
    int32 i = 0;
    while (it != end)
    {
      const nuiTextRange& range(*it);
      int32 len = range.mLength;
      int32 pos = origin;
      //printf("\trange %d (%d - %d) (%s - %s)\n", i, pos, len, nuiGetUnicodeScriptName(range.mScript).GetChars(), nuiGetUnicodeRangeName(range.mRange).GetChars());
      
      std::set<nglUChar>& charset(mCharsets[range.mScript]);
      {
        while (pos < origin + len)
        {
          nglUChar ch = mUnicode[pos++];
          if (ucisprint(ch) && ch > 32)
            charset.insert(ch);
        }
      }

      origin += len;
      ++it;
      i++;
    }
  }
  

  {
    auto styleit = mStyleChanges.begin();
    auto styleend = mStyleChanges.end();
    nuiTextStyle style = mStyle;

    nuiTextRangeList::iterator it = ranges.begin();
    nuiTextRangeList::iterator end = ranges.end();
    uint32 i = 0;
    uint32 pos = start;
    int32 oldp = -1;
    while (it != end)
    {
      const nuiTextRange& range(*it);
      uint32 len = range.mLength;
      //printf("\trange %d (%d - %d) (%s - %s)\n", i, pos, len, nuiGetUnicodeScriptName(range.mScript).GetChars(), nuiGetUnicodeRangeName(range.mRange).GetChars());
      nglUChar ch = mUnicode[pos];
      
      nuiTextRun* pRun = NULL;

      while (len > 0 && styleit != styleend)
      {
        if (ch < 32)
        {
          int32 tabs = 0;
          int32 spaces = 0;
          for (uint32 i = pos; i < pos + len && mUnicode[i] < 32; i++)
          {
            if (mUnicode[i] == 9)
              tabs++;
            else
              spaces++;
          }
          
          pRun = new nuiTextRun(*this, pos, len, spacewidth * (float)spaces + tabwidth * (float)tabs, 0.0f);

          if (styleit != styleend)
          {
            int32 p = styleit->first;
            NGL_ASSERT(oldp <= p); oldp = p;

            if ((p >= pos) && (p < pos + len))
            {
              style = styleit->second;
              ++styleit;
            }
          }

          pos += len;
          len = 0;
        }
        else
        {
          if (styleit != styleend)
          {
            int32 p = styleit->first;
            NGL_ASSERT(oldp <= p); oldp = p;

            if ((p >= pos) && (p < pos + len))
            {
              // Change the style now:
              int32 newpos = styleit->first;
              int32 s = newpos - pos;

              if (s > 0)
              {
                //printf ("Cutting run to change style (%d - %d)\n", pos, s);
                pRun = new nuiTextRun(*this, range.mScript, pos, s, style);
                pLine->AddRun(pRun);
                pRun = NULL;
              }

              style = styleit->second;
              pos += s;
              len -= s;
              ++styleit;
            }
            else
              break;
          }

        }
        
      }

      if (len > 0)
      {
        pRun = new nuiTextRun(*this, range.mScript, pos, len, style);
        pos += len;
        len = 0;
      }
      if (pRun)
        pLine->AddRun(pRun);

      ++i;
      ++it;
    }
  }
  
  return true;
}
Example #2
0
bool nuiTextLayout::LayoutParagraph(int32 start, int32 length)
{
  //printf("new paragraph: %d + %d\n", start, length);

  float spacewidth = 0;
  {
    nuiFontBase* pFont = mStyle.GetFont();
    nuiGlyphInfo glyphinfo;
    uint32 space = pFont->GetGlyphIndex(32);
    pFont->GetGlyphInfo(glyphinfo, space, nuiFontBase::eGlyphNative);
    spacewidth = glyphinfo.AdvanceX;
  }
  
  mpParagraphs.push_back(new Paragraph());

  nuiTextLine* pLine = new nuiTextLine(*this, 0, 0);
  mpParagraphs.back()->push_back(pLine);
  
  // Split the paragraph into ranges:
  nuiTextRangeList ranges;
  nuiSplitText(mUnicode, ranges, nuiST_ScriptChange, start, length, &Split);

  {
    nuiTextRangeList::iterator it = ranges.begin();
    nuiTextRangeList::iterator end = ranges.end();
    int32 origin = start;
    int32 i = 0;
    while (it != end)
    {
      const nuiTextRange& range(*it);
      int32 len = range.mLength;
      int32 pos = origin;
      //printf("\trange %d (%d - %d) (%s - %s)\n", i, pos, len, nuiGetUnicodeScriptName(range.mScript).GetChars(), nuiGetUnicodeRangeName(range.mRange).GetChars());
      
      std::set<nglUChar>& charset(mCharsets[range.mScript]);
      {
        while (pos < origin + len)
        {
          nglUChar ch = mUnicode[pos++];
          if (ucisprint(ch) && ch > 32)
            charset.insert(ch);
        }
      }

      origin += len;
      ++it;
      i++;
    }
  }
  

  {
    nuiTextRangeList::iterator it = ranges.begin();
    nuiTextRangeList::iterator end = ranges.end();
    uint32 i = 0;
    uint32 pos = start;
    while (it != end)
    {
      const nuiTextRange& range(*it);
      uint32 len = range.mLength;
      //printf("\trange %d (%d - %d) (%s - %s)\n", i, pos, len, nuiGetUnicodeScriptName(range.mScript).GetChars(), nuiGetUnicodeRangeName(range.mRange).GetChars());
      nglUChar ch = mUnicode[pos];
      
      nuiTextRun* pRun = NULL;
      
      if (ch < 32)
      {
        int32 tabs = 0;
        for (uint32 i = pos; i < pos + range.mLength; i++)
        {
          if (mUnicode[i] == 9)
            tabs++;
        }
        
        if (tabs > 0)
        {
          pRun = new nuiTextRun(*this, pos, len, spacewidth * (float)tabs, 0.0f);
        }
      }
      else
      {
        pRun = new nuiTextRun(*this, range.mScript, pos, len, mStyle);
      }
      
      if (pRun)
        pLine->AddRun(pRun);
      
      
      pos += len;
      ++i;
      ++it;
    }
  }
  
  return true;
}