BLF: Utility to Wrap a String into Multiple Lines #118436
No reviewers
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#118436
Loading…
Reference in New Issue
No description provided.
Delete Branch "Harley/blender:WrapString"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Given a string, font id, and width, return a string vector with the string
wrapped to that width.
We currently only have string wrapping that occurs at the time of text output. But it can be useful to know where a string would break without printing it. For wrapping of uiBut labels, multi-line text input, etc.
@HooglyBoogly
Does this approach seem reasonable of taking a std::string and outputting a std::vectorstd::string ?
Having a utility like this seems nice. I have a few comments though:
Vector
should be used instead ofstd::vector
because of its inline buffer and methods that work well with other Blender data structuresstd::string
requires heap allocation, copying, etc. (unless the string is very short), so I would base the API aroundStringRef
instead.StringRef
can also reference a slice of another string for free, since it doesn't need a null terminator.font->wrap_width = width
seems a bit sketchy. Ideally this should be done without changing the font at all, so the font could be const, and the function could be threadsafe.The commit should probably be titled
BLF: Add utility to wrap string into lines
or something since this doesn't actually change user-visible behavior.@ -938,0 +939,4 @@
std::vector<std::string> BLF_string_wrap(int fontid, const std::string str, const int width)
{
FontBLF *font = blf_get(fontid);
if (font) {
Flip the check and
return {}
inside. It's nice to consistently put error handling at the start of the function@ -1247,0 +1255,4 @@
{
std::vector<std::string> *list = static_cast<std::vector<std::string> *>(str_list_ptr);
std::string line(str, str + str_len);
list->push_back(line);
Just as an example, these two lines do a fair amount of expensive things:
line
variable. This has to allocate space forstr_len
and copy the charactersline
. Withstd::vector
,emplace_back
is used to construct the new value in place rather than default constructing it then filling its value with copy assignment. C++ also hasstd::move
for this reason:line
is just temporary data, we can move it into the vector to avoid the copy@ -98,1 +101,4 @@
std::vector<std::string> blf_font_string_wrap(FontBLF *font,
const std::string str,
const int width);
const
is meaningless in declarations for arguments passed by valueUI: Generic String Wrappingto BLF: Utility to Wrap a String into Multiple Lines@HooglyBoogly
Good point, used
max_pixel_width
for thisYes done and makes sense. Sorry that you have mentioned that to me before.
It did seem very sketchy indeed, and gave me the heebie-jeebies while writing it. But yes, makes more sense that the wrap width get passed to
blf_font_wrap_apply
, using font->wrap_width for the other uses of it.You are so right. Just a (bad) habit of sticking "UI:" on almost everything.
@ -1245,0 +1254,4 @@
GlyphCacheBLF *gc,
const char *str,
const size_t str_len,
ft_pix pen_y,
Unused variable
pen_y
? andgc
?@ -1245,0 +1262,4 @@
list->append(line);
}
blender::Vector<std::string> blf_font_string_wrap(FontBLF *font,
To be clear, I meant this should return
Vector<StringRef>
. Then this function doesn't need to do any allocation or copying at all. The only requirement is that the lifetime of the input string is at least as long as the lifetime of the output. But that's a nice tradeoff.@ -98,1 +103,4 @@
blender::Vector<std::string> blf_font_string_wrap(FontBLF *font,
blender::StringRef str,
const int max_pixel_width);
const int
means the same thing asint
here in the declaration, so typicallyconst
isn't included here.@HooglyBoogly
Thanks for your patience. I really didn't appreciate what StringRef brings to this.
For the simplest case where the string is within the limits, there is barely a difference between just measuring it and calling this wrapping function and examining the vector size, and that's pretty cool.
Looking good now, thanks for the iterations!
@ -8,8 +8,12 @@
#pragma once
#include <string>
Unnecessary include now :)
@ -8,6 +8,11 @@
#pragma once
#include <string>
And here too
@blender-bot build
@blender-bot build
@blender-bot build