Extracting Texts And Elements From SVG2

Have you ever wondered how SVG files render complex text layouts with different styles and directions so seamlessly? At the core of this magic lies text layout algorithms—an essential component of SVG rendering that ensures text appears exactly as intended.

Text layout algorithms are vital for rendering SVGs that include styled or bidirectional text. However, before layout comes text extraction—the process of collecting and organizing text content and properties from the XML tree to enable accurate rendering.

The Extraction Process

SVGs, being XML-based formats, resemble a tree-like structure similar to HTML. To extract information programmatically, you navigate through nodes in this structure.

Each node in the XML tree holds critical details for implementing the SVG2 text layout algorithm, including:

    • Text content
    • Bidi-control properties (manage text directionality)
    • Styling attributes like font and spacing
Understanding Bidi-Control

Bidi-control refers to managing text direction (e.g., Left-to-Right or Right-to-Left) using special Unicode characters. This is crucial for accurately displaying mixed-direction text, such as combining English and Arabic.

A Basic Example
<text>
  foo
  <tspan>bar</tspan>
  baz
</text>

The diagram and code sample shows the structure librsvg creates when it parses this XML tree.

Here, the <text> element has three children:

    1. A text node containing the characters “foo”.
    2. A <tspan> element with a single child text node containing “bar”.
    3. Another text node containing “baz”.

When traversed programmatically, the extracted text from this structure would be “foobarbaz”.

To extract text from the XML tree:

    1. Start traversing nodes from the <text> element.
    2. Continue through each child until the final closing tag.
    3. Concatenate character content into a single string.

While this example seems straightforward, real-world SVG2 files introduce additional complexities, such as bidi-control and styling, which must be handled during text extraction.

Handling Complex SVG Trees

Real-world examples often involve more than just plain text nodes. Let’s examine a more complex XML tree that includes styling and bidi-control:

Example:

<text>
  "Hello"
  <tspan font-style="bold;">bold</tspan>
  <tspan direction="rtl" unicode-bidi="bidi-override">مرحبا</tspan>
  <tspan font-style="italic;">world</tspan>
</text>
text extraction illustration credit: Federico (my mentor)
credit: Federico (my mentor)

In this example, the <text> element has four children:

    1. A text node containing “Hello”.
    2. A <tspan> element with font-style: bold, containing the text “bold”.
    3. A <tspan> element with bidi-control set to RTL (Right-To-Left), containing Arabic text “مرحبا”.
    4. Another <tspan> element with font-style: italic, containing “world”.

This structure introduces challenges, such as:

    • Styling: Managing diverse font styles (e.g., bold, italic).
    • Whitespace and Positioning: Handling spacing between nodes.
    • Bidirectional Control: Ensuring proper text flow for mixed-direction content.

Programmatically extracting text from such structures involves traversing nodes, identifying relevant attributes, and aggregating the text and bidi-control characters accurately.

Why Test-Driven Development Matters

One significant insight during development was the use of Test-Driven Development (TDD), thanks to my mentor Federico. Writing tests before implementation made it easier to visualize and address complex scenarios. This approach turned what initially seemed overwhelming into manageable steps, leading to robust and reliable solutions.

Conclusion

Text extraction is the foundational step in implementing the SVG2 text layout algorithm. By effectively handling complexities such as bidi-control and styling, we ensure that SVGs render text accurately and beautifully, regardless of direction or styling nuances.

If you’ve been following my articles and feel inspired to contribute to librsvg or open source projects, I’d love to hear from you! Drop a comment below to share your thoughts, ask questions, or offer insights. Your contributions—whether in the form of questions, ideas, or suggestions—are invaluable to both the development of librsvg and the ongoing discussion around SVG rendering. 😊

In my next article, we’ll explore how these extracted elements are processed and integrated into the text layout algorithm. Stay tuned—there’s so much more to uncover!

Demystifying SVG2 Text Layout: Understanding Librsvg

Prerequisite

Hi! I’m Anointing, your friendly neighborhood software engineer. I’m an Outreachy GNOME intern currently working on the project titled “Implement the SVG2 Text Layout Algorithm in Librsvg.”

In a previous blog post, I briefly introduced my project and tasks. If you missed it, don’t worry—this article dives deeper into the project and the specific task I’m working on.


What is Librsvg?

Librsvg is a lightweight library used to render Scalable Vector Graphics (SVGs), primarily within GNOME projects. It has been around since 2001, initially developed to handle SVG icons and graphical assets for GNOME desktops. Over the years, it has evolved into a versatile tool used for various SVG rendering needs.


What is SVG2?

Before understanding SVG2, let’s break down SVG (Scalable Vector Graphics):

  • SVG is an XML-based format for creating two-dimensional graphics.
  • Unlike raster images (e.g., JPEG or PNG), SVG images are scalable, meaning they retain quality regardless of size.
  • They are widely used for web graphics, illustrations, icons, and more because of their scalability and efficiency.

SVG2 (Scalable Vector Graphics, version 2) is the latest update to the SVG standard, developed by the World Wide Web Consortium (W3C). It builds upon SVG 1.1 with new features, bug fixes, and enhancements to make SVG more powerful and consistent across modern browsers.


Librsvg’s Current State

Librsvg supports some parts of the SVG 1.1 specifications for text, including bidirectional text. However, advanced features like per-glyph positioning or text-on-path are not yet implemented.

The SVG2 specification introduces significant improvements in text layout, such as:

  • Fine-grained glyph positioning
  • Support for right-to-left and bidirectional text
  • Vertical text layout
  • Text-on-path
  • Automatic text wrapping

Currently, librsvg does not fully implement SVG2’s comprehensive text layout algorithm. My role is to help improve this functionality.


My Role: Implementing the SVG2 Text Layout Algorithm

If the above sounds technical, don’t worry—I’ll explain the key tasks in simpler terms.

1. Support for Basic Text Layout

This involves ensuring that text in SVG images appears correctly. Imagine a digital poster: every word and letter must be precisely positioned. My task is to make sure librsvg can handle this properly.

2. Whitespace Handling

Whitespace refers to the blank space between words and lines. In SVG, whitespace is standardized—extra spaces should not disrupt the layout. I’m implementing an algorithm to handle whitespace effectively.

3. Left-to-Right (LTR) and Right-to-Left (RTL) Languages

Languages like English are read from left to right, while Arabic or Hebrew are read from right to left. Librsvg must handle both correctly using a process called the Bidi (Bidirectional) algorithm.

4. Inter-Glyph Spacing

In SVG, attributes like x, y, dx, and dy allow precise control over letter spacing. This ensures text looks balanced and beautiful. Additionally, this task involves handling ligatures (e.g., combining characters in Arabic) to ensure the output is correct.

5. Text-on-Path Handling (If Time Permits)

This feature allows text to follow a specific shape, like a circle or wave. It’s a fancy but useful way to add artistic effects to SVGs.


Why Does This Matter?

Improving librsvg’s text layout makes it more powerful and accessible for designers, developers, and artists. Whether creating infographics, digital posters, or interactive charts, these enhancements ensure that text renders beautifully in every language and style.


Tips for Newbies

If you’re new to SVG, text layout algorithms, or even Rust (the programming language used in librsvg), here’s what you need to know:

  • Skills Needed: Communication, basic Rust programming, and familiarity with terms like shaping, bidi algorithm, glyphs, ligatures, and baselines.
  • Start Small: Focus on one concept at a time—there’s no need to know everything at once.
  • Resources: The GNOME librsvg project is beginner-friendly and a great way to dive into open-source contributions.

Resources for Learning and Contributing


Wrapping Up

These tasks may seem technical, but they boil down to making librsvg a better tool for rendering SVGs. Whether it’s neat text placement, handling multiple languages, or adding artistic text effects, we’re improving SVG rendering for everyone.

So far, this project has been a journey of immense learning for me—both in technical skills like Rust programming and soft skills like clear communication.

In future posts, I’ll explore SVG2, librsvg features, and text layout terminologies in greater detail. Stay tuned!

Feel free to ask questions or share your thoughts. I’d love to hear from you and see you in the next chapter—cheers!! 😊🎉

Introducing Your Favorite Neighborhood Software Engineer (GNOME Outreachy Intern)…Anointing

My Journey to Becoming a GNOME Outreachy Intern

Hello everyone! You might be curious about how my personal blog found its way onto the renowned GNOME blog site. Before I share that story, let me introduce myself and my journey.

About myself

My name is Adetoye Anointing, and I’m a Computer Science graduate with a passion for technology, history, and creativity. I’m a former Google Developer Student Club Lead  now called GDG On Campus, hobbyist photographer, anime enthusiast, and lover of superhero movies.

I’ve been a software engineer for over three years. Like many, my journey started with the ever-popular “Hello, World!” in HTML. Initially, I aspired to become a security engineer, but without proper guidance or reliable internet, it was challenging. However, I discovered Python during this time, which opened the door to backend engineering and eventually software engineering

Later, After gaining foundational skills in Python, I realized I wanted to explore more efficient and modern languages. That’s when I discovered Go (Golang) through Google and roadmap.sh. I created a plan, stuck to it, and that marked the start of a fulfilling tech career. Along the way, I’ve contributed to various open-source projects and built meaningful connections in tech communities and subsequent picked up Rust.

My long-term aspirations include becoming a Site Reliability Engineer at Google, owning an Aston Martin Vantage, and contributing to complex infrastructure and blockchain projects.

Core Value

As much as I will like to give in-depth views on my core value, I will keep it simple, my core values are:

  • COMMUNITY : I owe much of my growth to tech communities. Joining the Google Developer Community was a turning point in my career, providing support, guidance, and opportunities. I also discovered Outreachy through Open Source Community Africa (OSCAFEST). I always encourage newcomers to join a community—it’s a powerful catalyst for growth.
  • OPENNESS: I believe in lifelong learning and the power of feedback.
    This mindset drives me to learn from my mistakes, seek feedback, and absorb knowledge from those around me.
  • DETERMINATION: Setting clear, concrete goals is vital. Determination fuels progress, keeps you going through tough times, and ensures results. It’s this value that motivated me to persist with my Outreachy applications until I was finally accepted.

If not for these core values, I wouldn’t have persisted with my Outreachy applications, let alone been accepted into this incredible program.

So, how did I end up with my name on the GNOME blog site? It started with applying to Outreachy multiple times, finally locking in on the project “Implement the SVG2 Text Layout Algorithm in librsvg.” With the guidance of my amazing mentor, Federico Mena Quintero, and support from Felipe Borges, I was accepted as an Outreachy Intern!

Now, here I am, on day one, with my first blog post published on the GNOME blog site.

I’m thrilled to begin this journey as an Outreachy Intern for GNOME! Over the next few months, I’ll be blogging about my progress, challenges, and achievements. I hope you’ll join me for the ride.

Looking forward to sharing more with you soon!

See you in the next chapter—cheers!