void MythUIText::SetArea(const MythRect &rect) { MythUIType::SetArea(rect); m_CutMessage.clear(); m_drawRect = m_Area; if (m_scrolling) { QFontMetrics fm(GetFontProperties()->face()); QSize stringSize = fm.size(Qt::TextSingleLine, m_Message); SetDrawRectSize(stringSize.width(), m_Area.height()); } FillCutMessage(); }
void MythUIText::SetText(const QString &text) { QString newtext = text; if (newtext == m_Message) return; if (newtext.isEmpty()) m_Message = m_DefaultMessage; m_Message = newtext; m_CutMessage.clear(); FillCutMessage(); if (m_scrolling) { QFontMetrics fm(GetFontProperties()->face()); QSize stringSize = fm.size(Qt::TextSingleLine, m_CutMessage); SetDrawRectSize(stringSize.width(), m_Area.height()); } SetRedraw(); }
void MythUIText::DrawSelf(MythPainter *p, int xoffset, int yoffset, int alphaMod, QRect clipRect) { if (m_Canvas.isNull()) return; FormatVector formats; QRect drawrect = m_drawRect.toQRect(); drawrect.translate(xoffset, yoffset); QRect canvas = m_Canvas.toQRect(); int alpha = CalcAlpha(alphaMod); if (m_Ascent) { drawrect.setY(drawrect.y() - m_Ascent); canvas.moveTop(canvas.y() + m_Ascent); canvas.setHeight(canvas.height() + m_Ascent); } if (m_Descent) { drawrect.setHeight(drawrect.height() + m_Descent); canvas.setHeight(canvas.height() + m_Descent); } if (m_leftBearing) { drawrect.setX(drawrect.x() + m_leftBearing); canvas.moveLeft(canvas.x() - m_leftBearing); canvas.setWidth(canvas.width() - m_leftBearing); } if (m_rightBearing) { drawrect.setWidth(drawrect.width() - m_rightBearing); canvas.setWidth(canvas.width() - m_rightBearing); } if (GetFontProperties()->hasOutline()) { QTextLayout::FormatRange range; QColor outlineColor; int outlineSize, outlineAlpha; GetFontProperties()->GetOutline(outlineColor, outlineSize, outlineAlpha); outlineColor.setAlpha(outlineAlpha); MythPoint outline(outlineSize, outlineSize); outline.NormPoint(); // scale it to screen resolution QPen pen; pen.setBrush(outlineColor); pen.setWidth(outline.x()); range.start = 0; range.length = m_CutMessage.size(); range.format.setTextOutline(pen); formats.push_back(range); drawrect.setX(drawrect.x() - outline.x()); drawrect.setWidth(drawrect.width() + outline.x()); drawrect.setY(drawrect.y() - outline.y()); drawrect.setHeight(drawrect.height() + outline.y()); /* Canvas pos is where the view port (drawrect) pulls from, so * it needs moved to the right for the left edge to be picked up*/ canvas.moveLeft(canvas.x() + outline.x()); canvas.setWidth(canvas.width() + outline.x()); canvas.moveTop(canvas.y() + outline.y()); canvas.setHeight(canvas.height() + outline.y()); } if (GetFontProperties()->hasShadow()) { QPoint shadowOffset; QColor shadowColor; int shadowAlpha; GetFontProperties()->GetShadow(shadowOffset, shadowColor, shadowAlpha); MythPoint shadow(shadowOffset); shadow.NormPoint(); // scale it to screen resolution drawrect.setWidth(drawrect.width() + shadow.x()); drawrect.setHeight(drawrect.height() + shadow.y()); canvas.setWidth(canvas.width() + shadow.x()); canvas.setHeight(canvas.height() + shadow.y()); } p->DrawTextLayout(canvas, m_Layouts, formats, *GetFontProperties(), alpha, drawrect); }
/* * If minsize and multiline are defined, minimize the width by using * as many lines as possible. */ bool MythUIText::MakeNarrow(QRect &min_rect) { QFontMetrics fm(GetFontProperties()->face()); if (m_scrolling || !m_MultiLine) { min_rect = fm.boundingRect(m_Area, m_Justification, m_CutMessage); return false; } /* * Shrinkage is desired, and multiline is allowed * If more than one line will fit, squeeze to the left */ if ((m_Area.height() + fm.leading()) / fm.lineSpacing() < 2) { min_rect = fm.boundingRect(m_Area, m_Justification, m_CutMessage); return false; } // Give plenty of vertical space, to prevent clipping from coloring results min_rect = m_Area; min_rect.setHeight(m_Area.height() * 2); QRect rect; int first = 1; int last = m_CutMessage.size() - 2; int min_width = INT_MAX; /* * Test from each end to find the best width to use. * An interior line may actually represent the best width, but it * is not worth the extra complexity to test interior lines. */ while (first < last && min_width > m_MinSize.x()) { if ((first = m_CutMessage.indexOf(' ', first)) < 1) break; min_rect.setWidth(fm.width(m_CutMessage.left(first))); if (min_rect.width() < m_Area.width()) { rect = fm.boundingRect(min_rect, m_Justification, m_CutMessage); if (rect.height() <= m_Area.height() && rect.width() < min_width) min_width = rect.width(); } if ((last = m_CutMessage.lastIndexOf(' ', last)) < 0) break; min_rect.setWidth(fm.width(m_CutMessage.mid(last))); if (min_rect.width() < m_Area.width()) { rect = fm.boundingRect(min_rect, m_Justification, m_CutMessage); if (rect.height() <= m_Area.height() && rect.width() < min_width) min_width = rect.width(); } ++first; --last; } if (min_width == INT_MAX) { // won't fit, even when the ara is the maxium size min_rect = fm.boundingRect(m_Area, m_Justification, m_CutMessage); return false; } else { if (min_width < m_MinSize.x() - m_Area.x()) min_width = m_MinSize.x() - m_Area.x(); // Found the minimal width which will accommodate the whole message. min_rect.setHeight(m_Area.height()); min_rect.setWidth(min_width); min_rect = fm.boundingRect(min_rect, m_Justification, m_CutMessage); return true; } }
bool FontConfigDirect::Match(std::string* result_family, unsigned* result_filefaceid, bool filefaceid_valid, unsigned filefaceid, const std::string& family, const void* data, size_t characters_bytes, bool* is_bold, bool* is_italic) { if (family.length() > kMaxFontFamilyLength) return false; SkAutoMutexAcquire ac(mutex_); // Given |family|, |is_bold| and |is_italic| but not |data|, the result will // be a function of these three parameters, and thus eligible for caching. // This is the fast path for |SkTypeface::CreateFromName()|. bool eligible_for_cache = !family.empty() && is_bold && is_italic && !data; if (eligible_for_cache) { int style = (*is_bold ? SkTypeface::kBold : 0 ) | (*is_italic ? SkTypeface::kItalic : 0); FontMatchKey key = FontMatchKey(family, style); const std::map<FontMatchKey, FontMatch>::const_iterator i = font_match_cache_.find(key); if (i != font_match_cache_.end()) { *is_bold = i->second.is_bold; *is_italic = i->second.is_italic; if (result_family) *result_family = i->second.family; if (result_filefaceid) *result_filefaceid = i->second.filefaceid; return true; } } FcPattern* pattern = FcPatternCreate(); if (filefaceid_valid) { const std::map<unsigned, std::string>::const_iterator i = fileid_to_filename_.find(FileFaceIdToFileId(filefaceid)); if (i == fileid_to_filename_.end()) { FcPatternDestroy(pattern); return false; } int face_index = filefaceid & 0xfu; FcPatternAddString(pattern, FC_FILE, reinterpret_cast<const FcChar8*>(i->second.c_str())); // face_index is added only when family is empty because it is not // necessary to uniquiely identify a font if both file and // family are given. if (family.empty()) FcPatternAddInteger(pattern, FC_INDEX, face_index); } if (!family.empty()) { FcPatternAddString(pattern, FC_FAMILY, (FcChar8*) family.c_str()); } FcCharSet* charset = NULL; if (data) { charset = FcCharSetCreate(); const uint16_t* chars = (const uint16_t*) data; size_t num_chars = characters_bytes / 2; for (size_t i = 0; i < num_chars; ++i) { if (U16_IS_SURROGATE(chars[i]) && U16_IS_SURROGATE_LEAD(chars[i]) && i != num_chars - 1 && U16_IS_TRAIL(chars[i + 1])) { FcCharSetAddChar(charset, U16_GET_SUPPLEMENTARY(chars[i], chars[i+1])); i++; } else { FcCharSetAddChar(charset, chars[i]); } } FcPatternAddCharSet(pattern, FC_CHARSET, charset); FcCharSetDestroy(charset); // pattern now owns it. } FcPatternAddInteger(pattern, FC_WEIGHT, is_bold && *is_bold ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL); FcPatternAddInteger(pattern, FC_SLANT, is_italic && *is_italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN); FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); FcConfigSubstitute(NULL, pattern, FcMatchPattern); FcDefaultSubstitute(pattern); // Font matching: // CSS often specifies a fallback list of families: // font-family: a, b, c, serif; // However, fontconfig will always do its best to find *a* font when asked // for something so we need a way to tell if the match which it has found is // "good enough" for us. Otherwise, we can return NULL which gets piped up // and lets WebKit know to try the next CSS family name. However, fontconfig // configs allow substitutions (mapping "Arial -> Helvetica" etc) and we // wish to support that. // // Thus, if a specific family is requested we set @family_requested. Then we // record two strings: the family name after config processing and the // family name after resolving. If the two are equal, it's a good match. // // So consider the case where a user has mapped Arial to Helvetica in their // config. // requested family: "Arial" // post_config_family: "Helvetica" // post_match_family: "Helvetica" // -> good match // // and for a missing font: // requested family: "Monaco" // post_config_family: "Monaco" // post_match_family: "Times New Roman" // -> BAD match // // However, we special-case fallback fonts; see IsFallbackFontAllowed(). FcChar8* post_config_family; FcPatternGetString(pattern, FC_FAMILY, 0, &post_config_family); FcResult result; FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result); if (!font_set) { FcPatternDestroy(pattern); return false; } FcPattern* match = MatchFont(font_set, post_config_family, family); if (!match) { FcPatternDestroy(pattern); FcFontSetDestroy(font_set); return false; } FcPatternDestroy(pattern); FcChar8* c_filename; if (FcPatternGetString(match, FC_FILE, 0, &c_filename) != FcResultMatch) { FcFontSetDestroy(font_set); return false; } int face_index; if (FcPatternGetInteger(match, FC_INDEX, 0, &face_index) != FcResultMatch) { FcFontSetDestroy(font_set); return false; } FontMatch font_match; if (filefaceid_valid) { font_match.filefaceid = filefaceid; } else { unsigned out_fileid; const std::string filename(reinterpret_cast<char*>(c_filename)); const std::map<std::string, unsigned>::const_iterator i = filename_to_fileid_.find(filename); if (i == filename_to_fileid_.end()) { out_fileid = next_file_id_++; filename_to_fileid_[filename] = out_fileid; fileid_to_filename_[out_fileid] = filename; } else { out_fileid = i->second; } // fileid stored in filename_to_fileid_ and fileid_to_filename_ is // unique only up to the font file. We have to encode face_index for // the out param. font_match.filefaceid = FileIdAndFaceIndexToFileFaceId(out_fileid, face_index); } bool success = GetFontProperties(match, &font_match.family, &font_match.is_bold, &font_match.is_italic); FcFontSetDestroy(font_set); if (success) { // If eligible, cache the result of the matching. if (eligible_for_cache) { int style = (*is_bold ? SkTypeface::kBold : 0 ) | (*is_italic ? SkTypeface::kItalic : 0); font_match_cache_[FontMatchKey(family, style)] = font_match; } if (result_family) *result_family = font_match.family; if (result_filefaceid) *result_filefaceid = font_match.filefaceid; if (is_bold) *is_bold = font_match.is_bold; if (is_italic) *is_italic = font_match.is_italic; } return success; }